最新springboot-整合dubbo研究
最近研究springboot整合dubbo,之前學習到的是說springboot不太支援javabean配置的方式,所以之前的專案都是採用xml的方式(這種方式就不再這裡介紹了。)
Dubbo現在2018年已經進入apache的開源孵化器。就Springboot整合這塊,發現不同的人有不同的處理方式。
1.先說說依賴:
第一種(未找到出處,個人覺得是誰寫的一個整合依賴而已:spring-boot-starter+zkclient+dubbo)未研究不推薦:
<dependency> <groupId>io.dubbo.springboot</groupId><artifactId>spring-boot-starter-dubbo</artifactId> </dependency>
第二種(apache下的整合依賴,版本問題參考官方資料分0.2.x和0.1.x,分別支援springboot2.x和springboot1.x):
具體參考:https://github.com/apache/incubator-dubbo-spring-boot-project
官方推薦使用0.2.x
<dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.2.0</version> </dependency>
第三種(alibaba出的整合包,雖然進入了apache,自身也在維護dubbo。目前該專案已經停止維護了)
具體參考:https://github.com/alibaba/dubbo-spring-boot-starter
<dependency> <groupId>com.alibaba.spring.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>2.0.0</version> </dependency>
結論:使用第二種整合包即可以,不需要每樣弄精通。
2.說說dubbo的使用;
基本的一些原始碼和概念參考如下資料:
https://blog.csdn.net/flashflight/article/details/43939275
https://blog.csdn.net/flashflight/article/details/44318447
https://blog.csdn.net/flashflight/article/details/44473617
https://blog.csdn.net/flashflight/article/details/44529805
3.整合步驟:
A.provider的pom檔案
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>springboot</groupId> <artifactId>springboot-dubbo-server</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot-dubbo 服務端:: 整合 Dubbo/ZooKeeper 詳解 SOA 案例</name> <!-- Spring Boot 啟動父依賴 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.2.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <!-- Spring Boot Dubbo 依賴 --> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.2.0</version> </dependency> <!-- Spring Boot Web 依賴 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Spring Boot Test 依賴 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- Junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
B.實體類
public class City implements Serializable { private static final long serialVersionUID = -1L; /** * 城市編號 */ private Long id; /** * 省份編號 */ private Long provinceId; /** * 城市名稱 */ private String cityName; /** * 描述 */ private String description; public City() { } public City(Long id, Long provinceId, String cityName, String description) { this.id = id; this.provinceId = provinceId; this.cityName = cityName; this.description = description; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public Long getProvinceId() { return provinceId; } public void setProvinceId(Long provinceId) { this.provinceId = provinceId; } public String getCityName() { return cityName; } public void setCityName(String cityName) { this.cityName = cityName; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } }
C.服務的介面,需要暴露的API
public interface CityDubboService { /** * 根據城市名稱,查詢城市資訊 * @param cityName */ City findCityByName(String cityName); }
D.服務的提供者的實現類。注意@Service註解是dubbo下面的註解。使用該註解即可以達到向zookeeper註冊的目的。
package org.spring.springboot.dubbo.impl; // 註冊為 Dubbo 服務 @Service(version = "1.0.0") public class CityDubboServiceImpl implements CityDubboService { public City findCityByName(String cityName) { return new City(1L,2L,"溫嶺","是我的故鄉"); } }
E.application.properties
# Spring boot application spring.application.name = city-server #management.port = 9091 # Base packages to scan Dubbo Components (e.g., @Service, @Reference) # 需要掃描的包 dubbo.scan.basePackages = org.spring.springboot.dubbo # Dubbo Config properties ## ApplicationConfig Bean dubbo.application.id = city-server dubbo.application.name = city-provider ## ProtocolConfig Bean dubbo.protocol.id = dubbo dubbo.protocol.name = dubbo dubbo.protocol.port = 20880 ## RegistryConfig Bean dubbo.registry.id = my-registry # 這裡是zk的連線配置 dubbo.registry.address = zookeeper://192.168.25.128:2181
F.consumer的pom檔案和provider一致,application.properties也沒多大區別。
## 避免和 server 工程埠衝突 server.port=8088 ## Dubbo 服務消費者配置 # Spring boot application spring.application.name = city-consumer #management.port = 9091 # Base packages to scan Dubbo Components (e.g., @Service, @Reference) # 需要掃描的包 dubbo.scan.basePackages = org.spring.springboot.dubbo # Dubbo Config properties ## ApplicationConfig Bean dubbo.application.id = city-consumer dubbo.application.name = city-consumer ## ProtocolConfig Bean dubbo.protocol.id = dubbo dubbo.protocol.name = dubbo dubbo.protocol.port = 20880 ## RegistryConfig Bean dubbo.registry.id = my-registry # 這裡是zk的連線配置 dubbo.registry.address = zookeeper://192.168.25.128:2181
G.介面類一致,避免耦合,所以各自寫了一份,事實上可以抽取出來單獨寫一個依賴API,讓consumer和provider直接去依賴這個API更好,這裡暫時未實現。
H.消費者類。
package org.spring.springboot.dubbo; import com.alibaba.dubbo.config.annotation.Reference; import org.spring.springboot.domain.City; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * Created By 夜羽 on 2018/7/8 */ @RestController public class CityController { @Reference(version = "1.0.0") CityDubboService cityDubboService; @RequestMapping("/hello") public City showCity() { String cityName="溫嶺"; City city = cityDubboService.findCityByName(cityName); return city; } }
I.測試頁面省略
最後總結下:
1》配置檔案application.properties參考官方,具體有些引數是否可以不配置,暫時沒去研究。
2》官方Service註解裡面配置的application.protocol.registry不配置也是可以(未深入研究)。
@Service( version = "1.0.0", application = "${dubbo.application.id}", protocol = "${dubbo.protocol.id}", registry = "${dubbo.registry.id}" )
3》有的文章說需要加入zkclient依賴,個人覺得不是必須的
<dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.2</version> </dependency>
ZkClient是由Datameer的工程師開發的開源客戶端,對Zookeeper的原生API進行了包裝,實現了超時重連、Watcher反覆註冊等功能。ZkClient的運用本人未研究,有知道的同學可以告訴下!
PS:例子是參考的別人的例子。資料並未去查詢DAO層。