SpringBoot 整合 Dubbo 2.7

1. 概述

  • Dubbo 是阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的 RPC 实现服务的输出和输入功能,可以和 Spring 框架无缝集成

节点 角色说明
Provider 暴露服务的服务提供方
Consumer 调用远程服务的服务消费方
Registry 服务注册与发现的注册中心
Monitor 统计服务的调用次数和调用时间的监控中心
Container 服务运行容器

2. Api

  • DemoService.java
1
2
3
public interface DemoService {
String sayHello(String name);
}

3. Provider

  • pom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<dependencies>
<!-- api -->
<dependency>
<groupId>com.lb</groupId>
<artifactId>dubbo-api</artifactId>
<version>${project.parent.version}</version>
</dependency>

<!-- starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<!-- dubbo -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.8</version>
</dependency>

<!-- zookeeper -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.12.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.12.0</version>
</dependency>
</dependencies>
  • ProviderApplication.java
1
2
3
4
5
6
7
@EnableDubbo
@SpringBootApplication
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}
  • DemoServiceImpl.java
1
2
3
4
5
6
7
@DubboService
public class DemoServiceImpl implements DemoService {
@Override
public String sayHello(String name) {
return "Hello " + name;
}
}
  • application.yml
1
2
3
4
5
6
7
8
9
10
dubbo:
application:
name: dubbo-provider
config-center:
timeout: 10000
protocol:
name: dubbo
port: -1
registry:
address: zookeeper://10.4.7.101:2181

4. Consumer

  • pom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<dependencies>
<!-- api -->
<dependency>
<groupId>com.lb</groupId>
<artifactId>dubbo-api</artifactId>
<version>${project.parent.version}</version>
</dependency>

<!-- starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<!-- dubbo -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.8</version>
</dependency>

<!-- zookeeper -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.12.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.12.0</version>
</dependency>
</dependencies>
  • ConsumerApplication.java
1
2
3
4
5
6
7
@EnableDubbo
@SpringBootApplication
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
  • Task.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@Component
public class Task implements CommandLineRunner {

@DubboReference
private DemoService demoService;

@Override
public void run(String... args) throws Exception {
System.out.println("Receive result ======> " + demoService.sayHello("world"));

new Thread(() -> {
while (true) {
try {
Thread.sleep(1000);
System.out.println(new Date() + " Receive result ======> " + demoService.sayHello("world"));
} catch (InterruptedException e) {
e.printStackTrace();
Thread.currentThread().interrupt();
}
}
}).start();
}
}
  • application.yml
1
2
3
4
5
6
7
8
9
10
dubbo:
application:
name: dubbo-consumer
config-center:
timeout: 10000
protocol:
name: dubbo
port: -1
registry:
address: zookeeper://10.4.7.101:2181

参考