javascript
高效开发 Dubbo?用 Spring Boot 可得劲!
不僅簡化了 Dubbo 基于 xml 配置的方式,也提高了日常開發效率,甚至提升了工作幸福感。
為了節省親愛的讀者您的時間,請根據以下2點提示來閱讀本文,以提高您的閱讀收獲效率哦。
如果您只有簡單的 Java 基礎和 Maven 經驗,而不熟悉 Dubbo,本文檔將幫助您從零開始使用 Spring Boot 開發 Dubbo 服務,并使用 EDAS 服務注冊中心實現服務注冊與發現。
如果您熟悉 Dubbo,可以選擇性地閱讀相關章節。
為什么使用 Spring Boot 開發 Dubbo 應用
Spring Boot 使用極簡的一些配置,就能快速搭建一個基于 Spring 的應用,提高的日常的開發效率。因此,如果您使用 Spring Boot 來開發基于 Dubbo 的應用,簡化了 Bubbo 基于 xml 配置的方式,提高了日常開發效率,提升了工作幸福感。
為什么使用 EDAS 服務注冊中心
EDAS 服務注冊中心實現了 Dubbo 所提供的 SPI 標準的注冊中心擴展,能夠完整地支持 Dubbo 服務注冊、路由規則、配置規則功能。
EDAS 服務注冊中心能夠完全代替 ZooKeeper 和 Redis,作為您 Dubbo 服務的注冊中心。同時,與 ZooKeeper 和 Redis 相比,還具有以下優勢:
- EDAS 服務注冊中心為共享組件,節省了您運維、部署 ZooKeeper 等組件的機器成本。
- EDAS 服務注冊中心在通信過程中增加了鑒權加密功能,為您的服務注冊鏈路進行了安全加固。
- EDAS 服務注冊中心與 EDAS 其他組件緊密結合,為您提供一整套的微服務解決方案。
本地開發
準備工作
- 下載、啟動及配置輕量級配置中心。
為了便于本地開發,EDAS 提供了一個包含了 EDAS 服務注冊中心基本功能的輕量級配置中心。基于輕量級配置中心開發的應用無需修改任何代碼和配置就可以部署到云端的 EDAS 中。
請您參考 配置輕量級配置中心 進行下載、啟動及配置。推薦使用最新版本。
- 下載 Maven 并設置環境變量(本地已安裝的可略過)。
創建服務提供者
創建一個 Spring Boot 工程,命名為 spring-boot-dubbo-provider。
這里我們以 Spring Boot 2.0.6.RELEASE 為例,在 pom.xml 文件中加入如下內容。
<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 版本,對應的 com.alibaba.boot:dubbo-spring-boot-starter 版本為 0.1.0。
說明: Spring Boot 1.x 版本的生命周期即將在 2019 年 8 月 結束,推薦使用新版本開發您的應用。
開發 Dubbo 服務提供者
2.1 Dubbo 中服務都是以接口的形式提供的。因此需要開發一個接口,例如這里的 IHelloService,接口里有若干個可被調用的方法,例如這里的 SayHello 方法。
package com.alibaba.edas.boot;public interface IHelloService {String sayHello(String str); }2.2 在服務提供方,需要實現所有以接口形式暴露的服務接口。例如這里實現 IHelloService 接口的類為 HelloServiceImpl。
package com.alibaba.edas.boot;import com.alibaba.dubbo.config.annotation.Service;@Servicepublic 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 服務。在 application.properties/application.yaml 配置文件中新增以下配置:
# Base packages to scan Dubbo Components (e.g @Service , @Reference)dubbo.scan.basePackages=com.alibaba.edas.bootdubbo.application.name=dubbo-provider-demodubbo.registry.address=edas://127.0.0.1:8080 **說明:** * 以上三個配置沒有默認值,必須要給出具體的配置。 * dubbo.scan.basePackages 的值是開發的代碼中含有 com.alibaba.dubbo.config.annotation.Service 和 com.alibaba.dubbo.config.annotation.Reference 注解所在的包。多個包之間用逗號隔開。 * dubbo.registry.address 的值前綴必須是一個 **edas://** 開頭,后面的ip地址和端口指的是輕量版配置中心開發并啟動 Spring Boot 入口類
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);}}登錄輕量版配置中心控制臺 http://127.0.0.1:8080,在左側導航欄中單擊服務列表 ,查看提供者列表。可以看到服務提供者里已經包含了 com.alibaba.edas.IHelloService,且可以查詢該服務的服務分組和提供者 IP。
創建服務消費者
創建一個 Spring Boot 工程,命名為 spring-boot-dubbo-consumer。
這里我們以 Spring Boot 2.0.6.RELEASE 為例,在 pom.xml 文件中加入如下內容。
<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 版本,對應的 com.alibaba.boot:dubbo-spring-boot-starter 版本為 0.1.0。
說明: Spring Boot 1.x 版本的生命周期即將在 2019 年 8 月 結束,推薦使用新版本開發您的應用。
開發 Dubbo 消費者
2.1 在服務消費方,需要引入所有以接口形式暴露的服務接口。例如這里 IHelloService 接口。
package com.alibaba.edas.boot;public interface IHelloService {String sayHello(String str); }2.2 Dubbo 服務調用。例如需要在 Controller 中調用一次遠程 Dubbo 服務,開發的代碼如下所示:
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;@RestControllerpublic class DemoConsumerController {@Referenceprivate 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 服務。在 application.properties/application.yaml 配置文件中新增以下配置:
dubbo.application.name=dubbo-consumer-demo dubbo.registry.address=edas://127.0.0.1:8080 **說明:** * 以上兩個配置沒有默認值,必須要給出具體的配置。 * dubbo.registry.address 的值前綴必須是一個 **edas://** 開頭,后面的ip地址和端口指的是輕量版配置中心開發并啟動 Spring Boot 入口類
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);}}結果驗證
- 本地結果驗證
curl http://localhost:17080/sayHello/EDAS
Hello, EDAS (from Dubbo with Spring Boot)
- EDAS 部署結果驗證
curl http://localhost:8080/sayHello/EDAS
Hello, EDAS (from Dubbo with Spring Boot)
總結
以上是生活随笔為你收集整理的高效开发 Dubbo?用 Spring Boot 可得劲!的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: callable函数 stride的意义
- 下一篇: C# 异常处理