不只簡化了 Dubbo 基於 xml 配置的方式,也提升了平常開發效率,甚至提高了工做幸福感。html
爲了節省親愛的讀者您的時間,請根據如下2點提示來閱讀本文,以提升您的閱讀收穫效率哦。java
若是您只有簡單的 Java 基礎和 Maven 經驗,而不熟悉 Dubbo,本文檔將幫助您從零開始使用 Spring Boot 開發 Dubbo 服務,並使用 EDAS 服務註冊中心實現服務註冊與發現。web
若是您熟悉 Dubbo,能夠選擇性地閱讀相關章節。spring
Spring Boot 使用極簡的一些配置,就能快速搭建一個基於 Spring 的應用,提升的平常的開發效率。所以,若是您使用 Spring Boot 來開發基於 Dubbo 的應用,簡化了 Bubbo 基於 xml 配置的方式,提升了平常開發效率,提高了工做幸福感。apache
EDAS 服務註冊中心實現了 Dubbo 所提供的 SPI 標準的註冊中心擴展,可以完整地支持 Dubbo 服務註冊、路由規則、配置規則功能。安全
EDAS 服務註冊中心可以徹底代替 ZooKeeper 和 Redis,做爲您 Dubbo 服務的註冊中心。同時,與 ZooKeeper 和 Redis 相比,還具備如下優點:app
爲了便於本地開發,EDAS 提供了一個包含了 EDAS 服務註冊中心基本功能的輕量級配置中心。基於輕量級配置中心開發的應用無需修改任何代碼和配置就能夠部署到雲端的 EDAS 中。運維
請您參考 配置輕量級配置中心 進行下載、啓動及配置。推薦使用最新版本。curl
這裏咱們以 Spring Boot 2.0.6.RELEASE 爲例,在 pom.xml 文件中加入以下內容。maven
```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。
```java
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 配置文件中新增如下配置:
```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
```
複製代碼
說明:
3.開發並啓動 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,在左側導航欄中單擊服務列表 ,查看提供者列表。能夠看到服務提供者裏已經包含了 com.alibaba.edas.IHelloService,且能夠查詢該服務的服務分組和提供者 IP。
這裏咱們以 Spring Boot 2.0.6.RELEASE 爲例,在 pom.xml 文件中加入以下內容。
```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 接口。
```java
package com.alibaba.edas.boot;
public interface IHelloService {
String sayHello(String str);
}
```
複製代碼
2.2 Dubbo 服務調用。例如須要在 Controller 中調用一次遠程 Dubbo 服務,開發的代碼以下所示:
```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 服務。在 application.properties/application.yaml 配置文件中新增如下配置:
```properties
dubbo.application.name=dubbo-consumer-demo
dubbo.registry.address=edas://127.0.0.1:8080
```
複製代碼
說明:
3.開發並啓動 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);
}
}
```
複製代碼
Hello, EDAS (from Dubbo with Spring Boot)
Hello, EDAS (from Dubbo with Spring Boot)