高效開發(fā) Dubbo?用 Spring Boot 可得勁!
不僅簡化了 Dubbo 基于 xml 配置的方式,也提高了日常開發(fā)效率,甚至提升了工作幸福感。
為了節(jié)省親愛的讀者您的時間,請根據(jù)以下2點提示來閱讀本文,以提高您的閱讀收獲效率哦。
-
如果您只有簡單的 Java 基礎(chǔ)和 Maven 經(jīng)驗,而不熟悉 Dubbo,本文檔將幫助您從零開始使用 Spring Boot 開發(fā) Dubbo 服務(wù),并使用 EDAS 服務(wù)注冊中心實現(xiàn)服務(wù)注冊與發(fā)現(xiàn)。
-
如果您熟悉 Dubbo,可以選擇性地閱讀相關(guān)章節(jié)。
為什么使用 Spring Boot 開發(fā) Dubbo 應(yīng)用
Spring Boot 使用極簡的一些配置,就能快速搭建一個基于 Spring 的應(yīng)用,提高的日常的開發(fā)效率。因此,如果您使用 Spring Boot 來開發(fā)基于 Dubbo 的應(yīng)用,簡化了 Bubbo 基于 xml 配置的方式,提高了日常開發(fā)效率,提升了工作幸福感。
為什么使用 EDAS 服務(wù)注冊中心
EDAS 服務(wù)注冊中心實現(xiàn)了 Dubbo 所提供的 SPI 標(biāo)準(zhǔn)的 注冊中心擴展 ,能夠完整地支持 Dubbo 服務(wù)注冊、 路由規(guī)則 、 配置規(guī)則功能 。
EDAS 服務(wù)注冊中心能夠完全代替 ZooKeeper 和 Redis,作為您 Dubbo 服務(wù)的注冊中心。同時,與 ZooKeeper 和 Redis 相比,還具有以下優(yōu)勢:
- EDAS 服務(wù)注冊中心為共享組件,節(jié)省了您運維、部署 ZooKeeper 等組件的機器成本。
- EDAS 服務(wù)注冊中心在通信過程中增加了鑒權(quán)加密功能,為您的服務(wù)注冊鏈路進(jìn)行了安全加固。
- EDAS 服務(wù)注冊中心與 EDAS 其他組件緊密結(jié)合,為您提供一整套的微服務(wù)解決方案。
本地開發(fā)
準(zhǔn)備工作
- 下載、啟動及配置輕量級配置中心。
為了便于本地開發(fā),EDAS 提供了一個包含了 EDAS 服務(wù)注冊中心基本功能的輕量級配置中心?;谳p量級配置中心開發(fā)的應(yīng)用無需修改任何代碼和配置就可以部署到云端的 EDAS 中。
請您參考 配置輕量級配置中心 進(jìn)行下載、啟動及配置。推薦使用***版本。
- 下載 Maven 并設(shè)置環(huán)境變量(本地已安裝的可略過)。
創(chuàng)建服務(wù)提供者
1. 創(chuàng)建一個 Spring Boot 工程,命名為 spring-boot-dubbo-provider
這里我們以 Spring Boot 2.0.6.RELEASE 為例,在 pom.xml 文件中加入如下內(nèi)容。
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-dependencies</artifactId>
- <version>2.0.6.RELEASE</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
- </dependencies>
- </dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-actuator</artifactId>
- </dependency>
- <dependency>
- <groupId>com.alibaba.boot</groupId>
- <artifactId>dubbo-spring-boot-starter</artifactId>
- <version>0.2.0</version>
- </dependency>
- <dependency>
- <groupId>com.alibaba.edas</groupId>
- <artifactId>edas-dubbo-extension</artifactId>
- <version>1.0.0-SNAPSHOT</version>
- </dependency>
- </dependencies>
如果您需要選擇使用 Spring Boot 1.x 的版本,請使用 Spring Boot 1.5.x 版本,對應(yīng)的 com.alibaba.boot:dubbo-spring-boot-starter 版本為 0.1.0。
說明:Spring Boot 1.x 版本的生命周期即將在 2019 年 8 月 結(jié)束,推薦使用新版本開發(fā)您的應(yīng)用。
2.開發(fā) Dubbo 服務(wù)提供者
2.1 Dubbo 中服務(wù)都是以接口的形式提供的。因此需要開發(fā)一個接口,例如這里的 IHelloService ,接口里有若干個可被調(diào)用的方法,例如這里的 SayHello 方法。
- package com.alibaba.edas.boot;
- public interface IHelloService {
- String sayHello(String str);
- }
2.2 在服務(wù)提供方,需要實現(xiàn)所有以接口形式暴露的服務(wù)接口。例如這里實現(xiàn) IHelloService 接口的類為 HelloServiceImpl 。
- package com.alibaba.edas.boot;
- import com.alibaba.dubbo.config.annotation.Service;
- @Service
- public class HelloServiceImpl implements IHelloService {
- public String sayHello(String name) {
- return "Hello, " + name + " (from Dubbo with Spring Boot)";
- }
- }
- ```
- *說明:** 這里的 Service 注解式 Dubbo 提供的一個注解類,類的全名稱為:**com.alibaba.dubbo.config.annotation.Service** 。
2.3 配置 Dubbo 服務(wù)。在 application.properties/application.yaml 配置文件中新增以下配置:
- ```properties
- # Base packages to scan Dubbo Components (e.g @Service , @Reference)
- dubbo.scan.basePackages=com.alibaba.edas.boot
- dubbo.application.name=dubbo-provider-demo
- dubbo.registry.address=edas://127.0.0.1:8080
- ```
- **說明:**
- * 以上三個配置沒有默認(rèn)值,必須要給出具體的配置。
- * dubbo.scan.basePackages 的值是開發(fā)的代碼中含有 com.alibaba.dubbo.config.annotation.Service 和 com.alibaba.dubbo.config.annotation.Reference 注解所在的包。多個包之間用逗號隔開。
- * dubbo.registry.address 的值前綴必須是一個 **edas://** 開頭,后面的ip地址和端口指的是輕量版配置中心
3. 開發(fā)并啟動 Spring Boot 入口類
- ```java
- package com.alibaba.edas.boot;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- @SpringBootApplication
- public class DubboProvider {
- public static void main(String[] args) {
- SpringApplication.run(DubboProvider.class, args);
- }
- }
4.登錄輕量版配置中心控制臺 http://127.0.0.1:8080,在左側(cè)導(dǎo)航欄中單擊服務(wù)列表 ,查看提供者列表??梢钥吹椒?wù)提供者里已經(jīng)包含了 com.alibaba.edas.IHelloService,且可以查詢該服務(wù)的服務(wù)分組和提供者 IP。
創(chuàng)建服務(wù)消費者
1. 創(chuàng)建一個 Spring Boot 工程,命名為 spring-boot-dubbo-consumer。
這里我們以 Spring Boot 2.0.6.RELEASE 為例,在 pom.xml 文件中加入如下內(nèi)容。
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-dependencies</artifactId>
- <version>2.0.6.RELEASE</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
- </dependencies>
- </dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-actuator</artifactId>
- </dependency>
- <dependency>
- <groupId>com.alibaba.boot</groupId>
- <artifactId>dubbo-spring-boot-starter</artifactId>
- <version>0.2.0</version>
- </dependency>
- <dependency>
- <groupId>com.alibaba.edas</groupId>
- <artifactId>edas-dubbo-extension</artifactId>
- <version>1.0.0-SNAPSHOT</version>
- </dependency>
- </dependencies>
如果您需要選擇使用 Spring Boot 1.x 的版本,請使用 Spring Boot 1.5.x 版本,對應(yīng)的 com.alibaba.boot:dubbo-spring-boot-starter 版本為 0.1.0。
說明:Spring Boot 1.x 版本的生命周期即將在 2019 年 8 月 結(jié)束,推薦使用新版本開發(fā)您的應(yīng)用。
2.開發(fā) Dubbo 消費者
2.1 在服務(wù)消費方,需要引入所有以接口形式暴露的服務(wù)接口。例如這里 IHelloService 接口。
- package com.alibaba.edas.boot;
- public interface IHelloService {
- String sayHello(String str);
- }
- ```
2.2 Dubbo 服務(wù)調(diào)用。例如需要在 Controller 中調(diào)用一次遠(yuǎn)程 Dubbo 服務(wù),開發(fā)的代碼如下所示:
- ```java
- package com.alibaba.edas.boot;
- import com.alibaba.dubbo.config.annotation.Reference;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- @RestController
- public class DemoConsumerController {
- @Reference
- private IHelloService demoService;
- @RequestMapping("/sayHello/{name}") public String sayHello(@PathVariable String name) { return demoService.sayHello(name);
- }
- }
說明:這里的 Reference 注解是 com.alibaba.dubbo.config.annotation.Reference 。
2.3 配置 Dubbo 服務(wù)。在 application.properties/application.yaml 配置文件中新增以下配置:
- dubbo.application.name=dubbo-consumer-demo
- dubbo.registry.address=edas://127.0.0.1:8080
- ```
- **說明:**
- * 以上兩個配置沒有默認(rèn)值,必須要給出具體的配置。
- * dubbo.registry.address 的值前綴必須是一個 **edas://** 開頭,后面的ip地址和端口指的是輕量版配置中心
3.開發(fā)并啟動 Spring Boot 入口類
- ```java
- package com.alibaba.edas.boot;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- @SpringBootApplication
- public class DubboConsumer {
- public static void main(String[] args) {
- SpringApplication.run(DubboConsumer.class, args);
- }
- }
- 登錄
4. 輕量版配置中心控制臺 http://127.0.0.1:8080,在左側(cè)導(dǎo)航欄中單擊 服務(wù)列表 ,再在服務(wù)列表頁面選擇 調(diào)用者列表 ,可以看到包含了 com.alibaba.edas.IHelloService,且可以查看該服務(wù)的服務(wù)分組和調(diào)用者 IP。
結(jié)果驗證
- 本地結(jié)果驗證
curl http://localhost:17080/sayHello/EDAS
- Hello, EDAS (from Dubbo with Spring Boot)
- EDAS 部署結(jié)果驗證
curl http://localhost:8080/sayHello/EDAS
- Hello, EDAS (from Dubbo with Spring Boot)