Spring Cloud Alibaba为分布式应用程序开发提供了一站式解决方案。 它包含开发分布式应用程序所需的所有组件,使您可以轻松地使用Spring Cloud开发应用程序。Dubbo是Alibaba 推出的一款高性能 RPC 框架,通过 Spring Cloud Alibaba 可以方便的与 Spring Cloud 继承提供更加强大的服务治理能力。
1. 下载 nacos 并启动
Nacos作为服务注册中心,提供注册发现能力。下载完成之后解压并启动。
sh startup.sh -m standalone # 单机模式启动
启动完成之后打开控制台页面,默认账号密码:nacos/nacos
2. 创建父工程
通过 IDEA 创建父工程,maven 项目类型选择 pom,并添加一下依赖
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR3</version>
<scope>import</scope>
<type>pom</type>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.1.RELEASE</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
3. 创建 sample-api
添加接口
public interface EchoService {
String echo(String message);
}
4. 创建 server 工程
添加依赖
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>sample-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
创建启动类
@SpringBootApplication
@EnableDiscoveryClient
@EnableDubbo(scanBasePackages = {"com.example.server.service"})
public class ServerApp {
public static void main(String[] args) {
SpringApplication.run(ServerApp.class, args);
}
}
配置文件
dubbo:
protocol:
# dubbo 协议
name: dubbo
# dubbo 协议端口( -1 表示自增端口,从 20880 开始)
port: -1
registry:
# 挂载到 Spring Cloud 注册中心
address: spring-cloud://localhost
application:
name: ${spring.application.name}
id: ${spring.application.name}
provider:
scope: remote
spring:
application:
# Dubbo 应用名称
name: server
main:
# Spring Boot 2.1 需要设定
allow-bean-definition-overriding: true
cloud:
nacos:
# Nacos 服务发现与注册配置
discovery:
server-addr: 127.0.0.1:8848
启动服务端,观察 Nacos 页面是否注册成功,接下来创建消费端
5. 创建 client 工程
添加依赖
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>sample-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<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>
<!-- Dubbo Spring Cloud Starter -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
</dependency>
<!-- Spring Cloud Nacos Service Discovery -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
创建启动类
@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class ClientApp {
@Reference
private EchoService echoService;
@GetMapping("/echo")
public String echo(String message) {
return echoService.echo(message);
}
public static void main(String[] args) {
SpringApplication.run(ClientApp.class, args);
}
}
配置文件
dubbo:
registry:
# 挂载到 Spring Cloud 注册中心
address: spring-cloud://localhost
cloud:
subscribed-services: server
application:
name: ${spring.application.name}
id: ${spring.application.name}
spring:
application:
# Dubbo 应用名称
name: client
main:
# Spring Boot 2.1 需要设定
allow-bean-definition-overriding: true
cloud:
nacos:
# Nacos 服务发现与注册配置
discovery:
server-addr: 127.0.0.1:8848
启动消费端,观察 Nacos 页面是否注册成功。
6. 测试
测试接口响应
curl http://localhost:8080/echo?message=bro
返回内容如下
[echo] Hello, bro
基于 Dubbo 的 RPC 调用成功。