Dubbo

视频学习:动力节点Dubbo教程(2020最新版)Dubbo项目实战_哔哩哔哩_bilibili

简介

Apache Dubbo 是一款易用、高性能的 WEB 和 RPC 框架,同时为构建企业级微服务提供服务发现、流量治理、可观测、认证鉴权等能力、工具与最佳实践。

Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案、服务治理方案。

官网Apache Dubbo

dubboArchitecture

  • 服务提供者(Provider)暴露服务的服务提供方;服务提供者在启动时向注册中心注册自己的服务。

  • 消费者(Consumer)调用远程服务的服务消费方;服务消费者在启动时,向注册中心订阅自己所需的服务;服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。

  • 注册中心(Registry)服务注册与发现的注册中心;注册中心返回服务提供者地址列表给消费者;如果有变更,注册中心将基于长连接推送变更数据给消费者。

  • 监控中心(Monitor):服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。

  • 容器(Container):服务运行的容器

使用SpringBoot进行连接

使用的Dubbo版本是3.1.7

使用Zookeeper3.7.1作为注册中心

例子

分别创建三个SpringBoot项目或模块:

1
2
3
springboot-interface	--提供接口
springboot-provider --提供者
springboot-consumer --消费者

springboot-interface存在的意义是防止消费者通过new的方式来获取提供者的服务。

编写接口

  • springboot-interface使用普通的Maven工程即可。

  • pom.xml引入相应的依赖,打包方式为jar。

  • 创建服务接口top.wshape1.interface.service.HelloService

  • HelloService.java内容如下:

    1
    2
    3
    public interface HelloService {
    String hello(String msg);
    }

编写完成之后编译安装到本地Maven仓库。

编写提供者

添加依赖
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
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

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

<dependency>
<groupId>top.wshape1</groupId>
<artifactId>springboot-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-x-discovery</artifactId>
<version>5.4.0</version>
</dependency>

<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>3.1.7</version>
</dependency>
</dependencies>
创建接口实现类

top.wshape1.provider.service.impl.HelloServiceImpl

HelloServiceImpl.java内容如下:

1
2
3
4
5
6
7
@DubboService
public class HelloServiceImpl implements HelloService {
@Override
public String hello(String msg) {
return "hello, " + msg;
}
}

3.x版本后为了防止与视频spring的@Service混淆而变更为@DubboService,标注为暴露的服务

启用Dubbo

在配置类上注解@EnableDubbo

1
2
3
4
5
6
7
8
9
@EnableDubbo
@SpringBootApplication
public class SpringbootProviderApplication {

public static void main(String[] args) {
SpringApplication.run(SpringbootProviderApplication.class, args);
}

}
配置application.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
dubbo:
application:
# dubbo 应用唯一标识
name: dubbo-provider
registry:
# 注册中心地址,这里使用zookeeper
address: zookeeper://127.0.0.1:2181
protocol:
# 通信协议和端口
# 通信协议有dubbo、gRPC、Thrift、REST、JsonRPC、Hessian2等,官方推荐dubbo
name: dubbo
# 端口默认为20880
port: 20880
server:
# web端口,修改以防止端口冲突
port: 8081

编写消费者

添加依赖
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
<dependencies>
<dependency>
<groupId>top.wshape1</groupId>
<artifactId>springboot-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>

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

<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-x-discovery</artifactId>
<version>5.4.0</version>
</dependency>

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

<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>3.1.7</version>
</dependency>
</dependencies>
创建controller

包路径top.wshape1.consumer.controller.HelloController

HelloController.java如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
@Controller
public class HelloController {

@DubboReference
private HelloService helloService;

@GetMapping("/hello")
@ResponseBody
public String hello(String msg) {
return helloService.hello(msg);
}

}

注解@DubboReference引用远程服务注入

配置application.yml
1
2
3
4
5
6
7
8
9
10
11
dubbo:
application:
# dubbo 应用唯一标识
name: dubbo-consumer
registry:
# 注册中心地址,与提供者使用同一个
address: zookeeper://127.0.0.1:2181

server:
# web端口,修改以防止端口冲突
port: 8080

运行测试

  1. 启动Zookeeper
  2. 启动提供者
  3. 启动消费者
  4. 打开http://localhost:8080/hello?msg=world测试

出现问题

1.端口冲突

出现端口冲突,如下qos端口冲突

1
2
3
2023-03-10 22:43:32.780 ERROR 11144 --- [           main] org.apache.dubbo.qos.server.Server       :  [DUBBO] qos-server can not bind localhost:22222, dubbo version: 3.1.7, current host: 10.77.97.83, error code: 7-4. This may be caused by , go to https://dubbo.apache.org/faq/7/4 to find instructions. 

java.net.BindException: Address already in use: bind

可尝试修改端口

application.yml修改qos端口

1
2
3
4
dubbo:
application:
# 默认为22222
qos-host: 22223

常见配置属性

1
2
3
4
5
6
7
8
9
10
11
12
# application.yml
dubbo:
consumer:
# 是否在启动时检查提供者是否存在,不存在将停止
check: true

# 消费者将引用的远程服务版本
# version: ""

provider:
# 提供者将提供的远程服务版本
# version: ""

@DubboService注解

  • String version() default ""暴露服务的版本号,若有版本号则消费者必须指定版本号。

@DubboReference

  • String version() default ""指定版本号,若有版本号则必填。
  • boolean check() default true在启动期间检查提供者是否可用。

监控中心

apache/dubbo-admin: The ops and reference implementation for Apache Dubbo (github.com)