• dubbo springboot,踩到的坑,主要是版本问题,,与熔断的版本有冲突,springboot的版本为2.2.5.RELEASE,可以用,否则,报类找不到的错误


    (文章最后,有打包发布的问题解决方案)

     两台机器,必须内网是相连的,如果不是一个内网,则在dubbo时,注册的地址不能互通,则不能访问。

    如题,废了很大的劲,从2.6.1开始,往下找,直到springboot为<version>2.2.5.RELEASE</version>,dubbo的消费者,才连上。(后期做没有熔断的测试时,用2.6.1也能正常。只要加载过 熔断(netflix-hystrix),2.6.1就有问题)

    Error creating bean with name 'configurationPropertiesBeans' defined in class path resource [org/springframework/cloud/autoconfigure/ConfigurationPropertiesRebinderAutoConfiguration.class]: Post-processing of merged bean definition failed; nested exception is java.lang.IllegalStateException: Failed to introspect Class [org.springframework.cloud.context.properties.ConfigurationPropertiesBeans] from ClassLoader [sun.misc.Launcher$AppClassLoader@18b4aac2]

    主要步骤,

    1. 建立bean和接口类,并声明。在后面的应用中,在pom里引用此。

    2. 建立提供者,实现一个接口,并且在实现的时候,用dubbo的@service方法,暴露服务。

    3. 建立消费者,实现一个接口,并在用dubbo的@Reference方法引用远程接口,并用set方法,将其注入。

    4. 消费者和提供者,在应用启动时,都要@EnableDubbo,声明为dubbo应用。

       <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.2.5.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>

    首先 是建立接口,一般是建立bean的domain,然后在里面提供至少2个接口,分别被消费者consumer和提供者provider使用

    然后建立提供者,在pom里引用domain,然后

    <!-- add by xuyong -->
            <dependency>
                <groupId>cn.taotao</groupId>
                <artifactId>User</artifactId>
                <version>0.0.1-SNAPSHOT</version>
            </dependency>
    
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-spring-boot-starter</artifactId>
                <version>2.7.5</version>
            </dependency>
            <!-- 使用zk 做注册中心,Dubbo 需要的依赖 -->
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-dependencies-zookeeper</artifactId>
                <version>2.7.5</version>
                <type>pom</type>
                <exclusions>
                    <exclusion>
                        <groupId>org.slf4j</groupId>
                        <artifactId>slf4j-log4j12</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>

    然后建立application.yml 文件

    dubbo:
      #声明注册到zk的名字 应该程序的名称
      application:
        name: taotao-provider
      #声明注册中心的地址和方式
      registry:
        address: zookeeper://www.yiwiki.com:2181
      #使用dubbo协议,将服务暴露在20880端口
      protocol:
        name: dubbo
        port: 20881

    并实现上面的一个接口,

    注意这里的service注解,使用的是 dubbo里的,不能用原生的spring的,否则发现不了服务!

    import org.apache.dubbo.config.annotation.Service;

    package cn.taotao.provider.service.impl;
    
    import cn.taotao.domain.User;
    import cn.taotao.service.UserService;
    import org.apache.dubbo.config.annotation.Service;
    import java.util.ArrayList;
    import java.util.List;
    
    @Service
    public class UserServiceImpl implements UserService {
    
    
        public static List<User> users = new ArrayList<>();
    
    
        static {
            users.add(new User(1,"gaotang","xu"));
            users.add(new User(2,"qingping","guo"));
        }
    
        @Override
        public List<User> getAll() {
            return users;
        }
    
        @Override
        public User getOne(Integer id) {
            return new User(3,"gaxxx","xu");
        }
    }

    然后建立consumer,消费者

    pom文件引用,引用超时熔断机制 netflix-hystrix

      <!--引入公共接口项目-->
            <dependency>
                <groupId>cn.taotao</groupId>
                <artifactId>User</artifactId>
                <version>0.0.1-SNAPSHOT</version>
            </dependency>
    
            <!-- Dubbo Spring Boot Starter -->
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-spring-boot-starter</artifactId>
                <version>2.7.5</version>
            </dependency>
            <!-- 使用zk 做注册中心,Dubbo 需要的依赖 -->
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-dependencies-zookeeper</artifactId>
                <version>2.7.5</version>
                <type>pom</type>
                <exclusions>
                    <exclusion>
                        <groupId>org.slf4j</groupId>
                        <artifactId>slf4j-log4j12</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
                <version>2.2.1.RELEASE</version>
            </dependency>

    yml的引用,这里不需要提供端口号

    dubbo:
      #声明注册到zk的名字 应该程序的名称
      application:
        name: taotao-consumer
      registry:
        address: zookeeper://www.yiwiki.com:2181
    #  consumer:
    #    check: false
    #    retries: 2

    建立consumer的java实现其中的另一个接口,如OrderService 在这个里面远程调用 UserService,这里主要的坑,是好多springboot的版本有问题,我实验了几个,发现2.2.5release版本可用,

    他报类无法找到,配置文件有问题,等等。好像是反射没有做好。

    package cn.taotao.consumer.service.impl;
    
    import cn.taotao.service.OrderService;
    import cn.taotao.service.UserService;
    import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
    import org.apache.dubbo.config.annotation.Reference;
    import org.springframework.stereotype.Service;
    
    
    @Service
    public class OrderServiceImpl implements OrderService {
        @Reference
        private  UserService userService;
    
        public void setUserService(UserService userService){
            this.userService=userService;
        }
    
    
        @Override
        @HystrixCommand(fallbackMethod = "error")
        public void orderinit() {
            System.out.println( this.userService.getOne(1));
        }
    }

     然后在 dubbo admin里即可看到 consumer和provider了。

    程序打包发布的时候,需要把接口,在maven里 install一下,然后再打provider和consumer的包,在打consumer的包的时候,因为需要运行test,所以这时候的provider应该正常能链接上,才能打包成功

    启动的时候,只要启动provider和consumer的jar即可。不需要启动接口。在部署到阿里云后,启动时,先启动provider,后启动 consumer,不用启动domain的接口类的jar,

  • 相关阅读:
    验证SMTP工作过程
    FileZilla FTP服务器的安装和配置
    最后一块石头的重量
    不用加号的加法
    同构字符串
    最长公共子序列
    Telnet 验证HTTP工作过程
    矩阵的最小路径和
    子数组的最大累加和问题
    海思开发板——YOLOv3模型移植(4)
  • 原文地址:https://www.cnblogs.com/sdgtxuyong/p/15703917.html
Copyright © 2020-2023  润新知