高效開發 Dubbo?用 Spring Boot 可得勁!

zhaowei121發表於2019-01-15

不僅簡化了 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 並設定環境變數(本地已安裝的可略過)。

建立服務提供者

  1. 建立一個 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 月 結束,推薦使用新版本開發您的應用。

  2. 開發 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;
 
 @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 服務。在 application.properties/application.yaml 配置檔案中新增以下配置:

 # 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複製程式碼
    
**說明:** 

* 以上三個配置沒有預設值,必須要給出具體的配置。
* dubbo.scan.basePackages 的值是開發的程式碼中含有 com.alibaba.dubbo.config.annotation.Service 和  com.alibaba.dubbo.config.annotation.Reference 註解所在的包。多個包之間用逗號隔開。
* dubbo.registry.address 的值字首必須是一個 **edas://** 開頭,後面的ip地址和埠指的是輕量版配置中心
複製程式碼
  1. 開發並啟動 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);
        }
    
    }複製程式碼
  2. 登入輕量版配置中心控制檯 http://127.0.0.1:8080,在左側導航欄中單擊服務列表 ,檢視提供者列表。可以看到服務提供者裡已經包含了 com.alibaba.edas.IHelloService,且可以查詢該服務的服務分組和提供者 IP。

建立服務消費者

  1. 建立一個 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 月 結束,推薦使用新版本開發您的應用。

  2. 開發 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;

@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 服務。在 application.properties/application.yaml 配置檔案中新增以下配置:

dubbo.application.name=dubbo-consumer-demo
dubbo.registry.address=edas://127.0.0.1:8080複製程式碼
    
**說明:** 

* 以上兩個配置沒有預設值,必須要給出具體的配置。
* dubbo.registry.address 的值字首必須是一個 **edas://** 開頭,後面的ip地址和埠指的是輕量版配置中心
複製程式碼
  1. 開發並啟動 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);
        }
    
    }複製程式碼
  2. 登入輕量版配置中心控制檯 http://127.0.0.1:8080,在左側導航欄中單擊 服務列表 ,再在服務列表頁面選擇 呼叫者列表 ,可以看到包含了 com.alibaba.edas.IHelloService,且可以檢視該服務的服務分組和呼叫者 IP。

結果驗證

  • 本地結果驗證

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)


相關文章