springboot中呼叫dubbo介面
阿新 • • 發佈:2021-06-11
工作中需要呼叫dubbo介面,網上資料很多,但胡亂不堪,特別總結一下
由於介面已經寫好,所有我們的角色是消費者
一、依賴包配置
<!--dubbo依賴--> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>2.7.0</version> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo</artifactId> <version>2.7.0</version> </dependency> <!--依賴服務的jar包:這個需要生產端提供-->
二、生產者的地址
配置檔案很簡單,application.propterties
spring.profiles.active=dev spring.main.allow-bean-definition-overriding=true #消費端的name,協議和埠一般都是寫死的 dubbo.application.name=bigscreen-wuwei dubbo.protocol.name=dubbo dubbo.protocol.port=20880 #生產端的地址,這個需要從zk獲取,zk算是註冊中心 dubbo.registry.address=zookeeper://ip:port
三、service中的呼叫
@Service public class ServiceImpl implementsXXXService{ //本地service一般要用@AutoWired,dubbo的要用@Reference,就這點差別 @Reference private GisOuterService gisOuterService; }
四、多註冊中心
考慮一種特殊的情況,需要對接多個數據註冊中心
配置檔案就要改一下application.properties
dubbo.registries.gis.address=zookeeper://ip1:2181 dubbo.registries.patrol.address=zookeeper://ip2:2182
在具體呼叫的時候就需要指定一下
@Reference(registry = "gis") private GisOuterService gisOuterService;