Spring Boot 简化了微服务应用的配置和部署,您可以自行选择注册中心和配置管理。本文介绍如何使用 Spring Boot 注解的方式基于 Nacos 开发一个
Dubbo 微服务示例应用。如果您已有 Spring Boot 开发的Dubbo 应用,可以跳过本文内容,直接部署到 EDAS。
前提条件
在使用 Spring Boot 开发 Dubbo 微服务应用前,请先完成以下工作:
- 下载 Maven并设置环境变量。
- 下载最新版本的 Nacos Server。
-
启动 Nacos Server。
- 解压下载的 Nacos Server 压缩包
- 进入
nacos/bin
目录,启动 Nacos Server。
- Linux/Unix/Mac 系统:执行命令
sh startup.sh -m standalone
。
- Windows 系统:双击执行
startup.cmd
文件。
-
示例工程
您可以按照本文的逐步搭建工程,也可以选择直接下载本文对应的示例工程,或者使用 Git 来 clone: git clone https://github.com/aliyun/alibabacloud-microservice-demo.git
该项目包含了众多了示例工程,本文对应的示例工程位于 alibabacloud-microservice-demo/microservice-doc-demo/dubbo-samples-spring-boot
。
创建服务提供者
- 创建命名为
spring-boot-dubbo-provider
的 Maven 工程。
- 在
pom.xml
文件中添加所需的依赖。
这里以 Spring Boot 2.0.6.RELEASE 为例。
<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</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
- 开发 Dubbo 服务提供者。
Dubbo 中服务都是以接口的形式提供的。
- 在
src/main/java
路径下创建一个 package com.alibaba.edas.boot
。
- 在
com.alibaba.edas.boot
下创建一个接口(interface) IHelloService
,里面包含一个 SayHello
方法。
package com.alibaba.edas.boot;
public interface IHelloService {
String sayHello(String str);
}
- 在
com.alibaba.edas.boot
下创建一个类IHelloServiceImpl
,实现此接口。
package com.alibaba.edas.boot;
import com.alibaba.dubbo.config.annotation.Service;
@Service
public class IHelloServiceImpl implements IHelloService {
public String sayHello(String name) {
return "Hello, " + name + " (from Dubbo with Spring Boot)";
}
}
说明 这里的 Service 注解是 Dubbo 提供的一个注解类,类的全名称为:com.alibaba.dubbo.config.annotation.Service 。
- 配置 Dubbo 服务。
- 在
src/main/resources
路径下创建application.properties
或application.yaml
文件并打开。
- 在
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=nacos://127.0.0.1:8848
说明
- 以上三个配置没有默认值,必须要给出具体的配置。
dubbo.scan.basePackages
的值是开发的代码中含有com.alibaba.dubbo.config.annotation.Service
和com.alibaba.dubbo.config.annotation.Reference
注解所在的包。多个包之间用逗号隔开。
dubbo.registry.address
的值前缀必须以 nacos:// 开头,后面的 IP 地址和端口指的是 Nacos Server 的地址。代码示例中为本地地址,如果您将 Nacos Server 部署在其它机器上,请修改为实际的
IP 地址。
- 开发并启动 Spring Boot 入口类
DubboProvider
。
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);
}
}
- 登录 Nacos 控制台
http://127.0.0.1:8848
,在左侧导航栏中单击服务列表 ,查看提供者列表。
可以看到服务提供者里已经包含了com.alibaba.edas.boot.IHelloService
,且可以查询该服务的服务分组和提供者 IP。
创建服务消费者
- 创建一个 Maven 工程,命名为
spring-boot-dubbo-consumer
。
- 在
pom.xml
文件中添加相关依赖。
这里以 Spring Boot 2.0.6.RELEASE 为例。
<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>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>1.1.1</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 消费者。
- 在
src/main/java
路径下创建 package com.alibaba.edas.boot
。
- 在
com.alibaba.edas.boot
下创建一个接口(interface) IHelloService
,里面包含一个 SayHello
方法。
package com.alibaba.edas.boot;
public interface IHelloService {
String sayHello(String str);
}
- 开发 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 。
- 在
application.properties/application.yaml
配置文件中新增以下配置。
dubbo.application.name=dubbo-consumer-demo
dubbo.registry.address=nacos://127.0.0.1:8848
说明
- 以上两个配置没有默认值,必须要给出具体的配置。
dubbo.registry.address
的值前缀必须以 nacos://
开头,后面的 IP 地址和端口为 Nacos Server 的地址。代码示例中为本地地址,如果您将 Nacos Server 部署在其它机器上,请修改为实际的 IP
地址。
- 开发并启动 Spring Boot 入口类
DubboConsumer
。
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);
}
}
- 登录 Nacos 控制台
http://127.0.0.1:8848
,在左侧导航栏中单击服务列表,再在服务列表页面单击调用者列表页签,查看调用者列表。
可以看到包含了com.alibaba.edas.boot.IHelloService
,且可以查看该服务的服务分组和调用者 IP。
结果验证
`curl http://localhost:8080/sayHello/EDAS`
`Hello, EDAS (from Dubbo with Spring Boot)`
部署到 EDAS
说明 EDAS 集成的 ACM 即 Nacos 的正式商用版本。当您将应用部署到 EDAS 的时候,EDAS 会自动替换您本地 Nacos Server 的地址和服务端口(127.0.0.1:8848
),您无需修改。
您可以根据实际需求选择部署的集群类型(主要为 ECS 集群或容器服务 Kubernetes 集群)和部署途径(控制台或工具),详情请参见如下文档。
如果您使用控制台部署,在部署前,需要在本地应用程序中完成以下操作:
- 在
pom.xml
文件中添加以下打包插件的配置。
- Provider
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<classifier>spring-boot</classifier>
<mainClass>com.alibaba.edas.boot.DubboProvider</mainClass>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
- Consumer
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<classifier>spring-boot</classifier>
<mainClass>com.alibaba.edas.boot.DubboConsumer</mainClass>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
- 执行 mvn clean package 将本地的程序打成 JAR 包。