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

不只簡化了 Dubbo 基於 xml 配置的方式,也提升了平常開發效率,甚至提高了工做幸福感。html

爲了節省親愛的讀者您的時間,請根據如下2點提示來閱讀本文,以提升您的閱讀收穫效率哦。web

若是您只有簡單的 Java 基礎和 Maven 經驗,而不熟悉 Dubbo,本文檔將幫助您從零開始使用 Spring Boot 開發 Dubbo 服務,並使用 EDAS 服務註冊中心實現服務註冊與發現。spring

若是您熟悉 Dubbo,能夠選擇性地閱讀相關章節。apache

爲何使用 Spring Boot 開發 Dubbo 應用

Spring Boot 使用極簡的一些配置,就能快速搭建一個基於 Spring 的應用,提升的平常的開發效率。所以,若是您使用 Spring Boot 來開發基於 Dubbo 的應用,簡化了 Bubbo 基於 xml 配置的方式,提升了平常開發效率,提高了工做幸福感。安全

爲何使用 EDAS 服務註冊中心

EDAS 服務註冊中心實現了 Dubbo 所提供的 SPI 標準的註冊中心擴展,可以完整地支持 Dubbo 服務註冊、路由規則配置規則功能app

EDAS 服務註冊中心可以徹底代替 ZooKeeper 和 Redis,做爲您 Dubbo 服務的註冊中心。同時,與 ZooKeeper 和 Redis 相比,還具備如下優點:運維

  • EDAS 服務註冊中心爲共享組件,節省了您運維、部署 ZooKeeper 等組件的機器成本。
  • EDAS 服務註冊中心在通訊過程當中增長了鑑權加密功能,爲您的服務註冊鏈路進行了安全加固。
  • EDAS 服務註冊中心與 EDAS 其餘組件緊密結合,爲您提供一整套的微服務解決方案。

本地開發

準備工做

  • 下載、啓動及配置輕量級配置中心。

爲了便於本地開發,EDAS 提供了一個包含了 EDAS 服務註冊中心基本功能的輕量級配置中心。基於輕量級配置中心開發的應用無需修改任何代碼和配置就能夠部署到雲端的 EDAS 中。curl

請您參考 配置輕量級配置中心 進行下載、啓動及配置。推薦使用最新版本。maven

  • 下載 Maven 並設置環境變量(本地已安裝的可略過)。

建立服務提供者

  1. 建立一個 Spring Boot 工程,命名爲 spring-boot-dubbo-provider。ide

    這裏咱們以 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)

相關文章
相關標籤/搜索