dubbo springboot,踩到的坑,主要是版本問題,springboot的版本為2.2.5release,可以用,否則,報類找不到的錯誤
阿新 • • 發佈:2021-12-17
如題,廢了很大的勁,從2.6.1開始,往下找,直到springboot為2.2.5release,dubbo的消費者,才連上。
首先 是建立介面,一般是建立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
並實現上面的一個介面
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了。