上一篇:Spring Cloud Alibaba学习(八):配置中心
Dubbo作为SpringCloud生态中服务治理的一部分,同样需要有服务提供者和服务消费者。
由于服务提供者和服务消费者都要实现共同的接口,所以可以将接口单独作为一个模块,因此建立三个子模块:dubbo-api,dubbo-provider,dubbo-consumer。右键父模块,创建maven模块,再将子模块加入父模块pom.xml的<module>即可
dubbo-api
只做接口的定义,不需要引入依赖
接口
public interface DubboApiService {
String helloDubboService();
}
目录结构
dubbo-provider
pom.xml
引入两个依赖:
- spring-cloud-starter-dubbo:dubbo的依赖
- dubbo-api:我们自己建立的公共接口模块
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-cloud-alibaba-icydate</artifactId>
<groupId>com.icydate</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dubbo-provider</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!-- Spring Boot Begin -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Boot End -->
<!-- Spring Cloud Begin -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- Dubbo Spring Cloud Starter -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
</dependency>
<!-- Spring Cloud End -->
<!-- 引入公共接口的模块 -->
<dependency>
<groupId>com.icydate</groupId>
<artifactId>dubbo-api</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.DubboConsumerApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
application.yml
spring:
cloud:
nacos:
discovery:
server-addr: localhost:8848
application:
name: icydate-dubbo-provider
server:
port: 8084
dubbo:
protocol:
# 协议名称
name: dubbo
# dubbo协议缺省端口为20880,rmi协议缺省端口为1099,http和hessian协议缺省端口为80;如果没有配置port,则自动采用默认端口,如果配置为-1,则会分配一个没有被占用的端口
# Dubbo 2.4.0+,分配的端口在协议缺省端口的基础上增长,确保端口段可控
port: -1
registry:
# 前缀spring-cloud说明:挂载到 Spring Cloud注册中心
address: spring-cloud://localhost:8848
SpringBoot启动类
@DubboComponentScan:扫描指定包下作为dubbo服务的类
@SpringBootApplication
@EnableDiscoveryClient
@DubboComponentScan(value = {"com.icydate.service"})
public class DubboProviderApplication {
public static void main(String[] args) {
SpringApplication.run(DubboProviderApplication.class, args);
}
}
服务类
@DubboService:标注此类为dubbo服务
@DubboService
public class DubboProviderService implements DubboApiService {
// 实现公共接口,内容为具体的服务业务逻辑
@Override
public String helloDubboService() {
return "provider dubbo";
}
}
目录结构
dubbo-consumer
pom.xml
引入与dubbo-provider相同的依赖
application.yml
spring:
cloud:
nacos:
discovery:
server-addr: localhost:8848
application:
name: icydate-dubbo-consumer
server:
port: 8085
dubbo:
protocol:
name: dubbo
port: -1
registry:
address: spring-cloud://localhost:8848
cloud:
# 要订阅的服务
subscribed-services: icydate-dubbo-provider
SpringBoot启动类
@SpringBootApplication
@EnableDiscoveryClient
public class DubboConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(DubboConsumerApplication.class, args);
}
}
Controller接口
@DubboReference:指定要消费的服务接口
@RestController
public class DubboConsumerController {
@DubboReference
DubboApiService dubboApiService;
@GetMapping("/call/dubbo")
public String callDubboProvider() {
return dubboApiService.helloDubboService();
}
}
目录结构
验证
分别启动nacos服务,dubbo-provider,dubbo-consumer模块
可以看到nacos中已有服务列表
调用dubbo-consumer提供的接口,发现能够成功调用到dubbo-provider的返回