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,

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM