Dubbo3简单使用
👉 使用Spring Boot实现Dubbo3,请参见以下地址。
# Dubbo3官网地址
https://cn.dubbo.apache.org/zh/
# 使用SpringBoot实现Dubbo3的地址
https://cn.dubbo.apache.org/zh/docs3-v2/java-sdk/quick-start/spring-boot/
# 该项目的git地址
https://github.com/MasonYyp/mydubbo
1 同一个项目中创建多个应用
1.1 创建空项目
(1)创建空项目
(2)提示错误
⚠️ 注意:会提示下面错误,点击“OK”即可。
(3)空项目创建完成
1.2 创建应用模块
(1)创建模块
(2)创建Maven
选择Maven模块,点击“Next”。
(3)设置应用信息
设置应用信息后,点击“Finish”即可。
(4)创建完成
1.3 创建其他
创建其他应用的方法和上面的方法类似,分别创建Dubbo的服务提供者(duprovider)和服务消费者(duconsumer)。
- GroupId: com.mason
- Version: 1.0
- 创建应用的目录:mydubbo目录下
分别在应用下创建java包(选择Module时创建Maven默认不会创建java包)。
应用名称(ch) | 应用名称(en) | 包名 | 备注 |
---|---|---|---|
公共模块 | ducommon | com.mason.ducommon | 为其他模块提供公共接口 |
服务提供者 | duprovider | com.mason.duprovider | 提供服务 |
服务消费者 | duconsumer | com.mason.duconsumer | 消费服务 |
应用创建完成后如下
2 软件版本选择
(1)Spring Cloud Alibaba版本说明
https://github.com/alibaba/spring-cloud-alibaba/wiki/版本说明
(2)Spring Cloud版本说明
# 介绍了相应的版本
https://spring.io/projects/spring-cloud#overview
# 介绍了稳定版(含有GA标识)
https://spring.io/projects/spring-cloud#learn
(3)版本选择
⚠️ 组件版本很重要,需要慎重选择,Dubbo3需要配合Nacos2使用。
软件 | 版本号 |
---|---|
Spring Cloud | Hoxton.SR12 |
Spring Boot | 2.3.12.RELEASE |
Spring Cloud Alibaba | 2.2.9.RELEASE |
Sentinel | 1.8.5 |
Nacos | 2.1.0 |
RocketMQ | 4.9.4 |
Seata | 1.5.2 |
Dubbo | 3.1.4 |
3 安装nacos
3.1 Nacos安装参考地址
https://blog.csdn.net/make_progress/article/details/128855472
3.2 安装MySQL
# 创建网桥
docker network create --driver bridge du_net
# 下载MySQL
docker pull mysql:5.7.34
# 创建数据库
docker run -itd --name du_mysql \
--net du_net \
--restart=always \
-p 3306:3306 \
-v /home/dubbo/mysql/conf:/etc/mysql/conf.d \
-v /home/dubbo/mysql/data:/var/lib/mysql \
-v /home/dubbo/mysql/log:/var/log/mysql \
-e MYSQL_ROOT_PASSWORD=123456 \
mysql:5.7.34
3.3 初始化Nacos数据库
# 解压“nacos-server-2.1.0.zip”文件,将“nacos-server-2.1.0/nacos/conf”目录中的“nacos-mysql.sql”复制到容器中
docker cp nacos-mysql.sql du_mysql:/home/
# 进入容器
docker exec -it du_mysql /bin/bash
# 进入MySQL数据库,输入密码后进入MySQL
mysql -u root -p
# 创建数据库
create database nacos_config;
# 切换数据库
use nacos_config
# 导入nacos配置信息到数据库
source /home/nacos-mysql.sql
3.4 安装Nacos
# 下载Nacos镜像
docker pull nacos/nacos-server:v2.1.0
# 安装nacos
# 注意:Nacos2比Nacos1多开9848、9849两个端口需求全部开放,否则应用无法注册到Nacos
docker run -itd --name du_nacos \
--net du_net \
--restart=always \
-p 8848:8848 \
-p 9848:9848 \
-p 9849:9849 \
--env MODE=standalone \
--env SPRING_DATASOURCE_PLATFORM=mysql \
--env MYSQL_SERVICE_HOST=du_mysql \
--env MYSQL_SERVICE_PORT=3306 \
--env MYSQL_SERVICE_USER=root \
--env MYSQL_SERVICE_PASSWORD=123456 \
--env MYSQL_SERVICE_DB_NAME=nacos_config \
nacos/nacos-server:v2.1.0
访问Nacos
# 地址
http://192.168.108.200:8848/nacos
账号:nacos
密码:nacos
4 公共模块
4.1 项目截图
4.2 pom.xml
<?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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.12.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.mason</groupId>
<artifactId>ducommon</artifactId>
<version>1.0</version>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<!-- Parse json -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.58</version>
</dependency>
<!-- Init the entity -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
4.3 服务层service下接口文件
service包下文件
package com.mason.ducommon.service;
public interface TestService {
public String getInfo(String message);
}
5 服务提供者
5.1 项目截图
5.2 pom.xml
<?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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.12.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.mason</groupId>
<artifactId>duprovider</artifactId>
<version>1.0</version>
<properties>
<java.version>11</java.version>
<spring-cloud.version>Hoxton.SR12</spring-cloud.version>
<spring.cloud.alibaba.version>2.2.9.RELEASE</spring.cloud.alibaba.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>3.1.4</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>3.1.4</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
<version>3.1.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 引入自定义的公共模块 -->
<dependency>
<groupId>com.mason</groupId>
<artifactId>ducommon</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring.cloud.alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
5.3 yml文件
服务提供者做了3个节点,yml文件有“application-node1.yml,application-node2.yml,application-node3.yml”
application-node1.yml
server:
port: 8081
spring:
# 配置注册在Nacos中的应用名称
application:
name: app-duprovider
cloud:
# 设置Nacos服务注册地址
nacos:
discovery:
server-addr: 192.168.108.200:8848
dubbo:
# 设置Dubbo服务的应用名,如果不设置此应用名默认为应用名,此外默认为“app-duprovider”
application:
name: dubbo-duprovider
# 设置Dubbo协议信息
protocol:
name: dubbo
# 端口可以设置为“-1”,表示端口自增长
port: 9081
# 设置注册地址
registry:
address: nacos://192.168.108.200:8848
# 设置服务超时
provider:
timeout: 3000
management:
endpoints:
web:
exposure:
include: '*'
application-node2.yml
server:
port: 8082
spring:
application:
name: app-duprovider
cloud:
nacos:
discovery:
server-addr: 192.168.108.200:8848
dubbo:
application:
name: dubbo-duprovider
protocol:
name: dubbo
port: 9082
registry:
address: nacos://192.168.108.200:8848
provider:
timeout: 3000
management:
endpoints:
web:
exposure:
include: '*'
application-node3.yml
server:
port: 8083
spring:
application:
name: app-duprovider
cloud:
nacos:
discovery:
server-addr: 192.168.108.200:8848
dubbo:
application:
name: dubbo-duprovider
protocol:
name: dubbo
port: 9083
registry:
address: nacos://192.168.108.200:8848
provider:
timeout: 3000
management:
endpoints:
web:
exposure:
include: '*'
5.4 controller
此处的controller的作用是查看服务是否启动,与Dubbo提供服务无关,可以不要controller。
DubboController.java
package com.mason.duprovider.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/provider")
public class DubboController {
@Value("${server.port}")
private String port;
@RequestMapping("/getdata")
public String getData(){
return "Dubbo " + this.port;
}
}
5.5 service
提供Dubbo服务,实现公共模块(ducommon)中的服务接口。
Dubbo在新版本中使用@DubboService,旧版本中使用@Service,将Dubbo服务注册到注册中心。
TestServiceImpl.java
package com.mason.duprovider.service;
import com.mason.ducommon.service.TestService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Value;
@DubboService
public class TestServiceImpl implements TestService {
@Value("${server.port}")
private String port;
@Override
public String getInfo(String message) {
return "Service " + this.port + message;
}
}
5.6 启动类
DuproviderApplication.java
⚠️ 注意:需要在启动类上,添加“@EnableDubbo"注解
package com.mason.duprovider;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo
public class DuproviderApplication {
public static void main(String[] args) {
SpringApplication.run(DuproviderApplication.class, args);
}
}
5.7 访问controller的服务
# 访问地址
http://127.0.0.1:8081/provider/getdata
浏览器结果
Nacos中结果
6 服务消费者
6.1 项目截图
6.2 pom.xml
<?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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.12.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.mason</groupId>
<artifactId>duconsumer</artifactId>
<version>1.0</version>
<properties>
<java.version>11</java.version>
<spring-cloud.version>Hoxton.SR12</spring-cloud.version>
<spring.cloud.alibaba.version>2.2.9.RELEASE</spring.cloud.alibaba.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>3.1.4</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>3.1.4</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
<version>3.1.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 引入自定义的公共模块 -->
<dependency>
<groupId>com.mason</groupId>
<artifactId>ducommon</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring.cloud.alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
6.3 yml文件
服务消费者也做了3个节点,yml文件有“application-node1.yml,application-node2.yml,application-node3.yml”
application-node1.yml
server:
port: 8091
spring:
# 配置注册在Nacos中的应用名称
application:
name: app-duconsumer
cloud:
# 设置Nacos服务注册地址
nacos:
discovery:
server-addr: 192.168.108.200:8848
dubbo:
# 此处可以不设置Dubbo服务的应用名,因为没有注册Dubbo服务
application:
name: dubbo-duprovider
# 设置Dubbo协议信息
protocol:
name: dubbo
# 端口可以设置为“-1”,表示端口自增长
port: 9091
# 设置注册地址
registry:
address: nacos://192.168.108.200:8848
# 设置服务超时
provider:
timeout: 3000
management:
endpoints:
web:
exposure:
include: '*'
application-node2.yml
server:
port: 8092
spring:
application:
name: app-duconsumer
cloud:
nacos:
discovery:
server-addr: 192.168.108.200:8848
dubbo:
application:
name: dubbo-duprovider
protocol:
name: dubbo
port: 9092
registry:
address: nacos://192.168.108.200:8848
provider:
timeout: 3000
management:
endpoints:
web:
exposure:
include: '*'
application-node3.yml
server:
port: 8093
spring:
application:
name: app-duconsumer
cloud:
nacos:
discovery:
server-addr: 192.168.108.200:8848
dubbo:
application:
name: dubbo-duprovider
protocol:
name: dubbo
port: 9093
registry:
address: nacos://192.168.108.200:8848
provider:
timeout: 3000
management:
endpoints:
web:
exposure:
include: '*'
6.4 controller
控制层,为用户提供服务。
DubboController.java
package com.mason.duconsumer.controller;
import com.mason.duconsumer.service.TestServiceRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/consumer")
public class DubboController {
@Autowired
private TestServiceRequest testServiceRequest;
@RequestMapping("/getdata")
public String getData(){
return this.testServiceRequest.getInfo();
}
}
6.5 service
服务层,消费Dubbo服务提供者提供的服务。
Dubbo在新版本中使用@DubboReference,旧版本中使用@Reference,消费注册中心的Dubbo服务。
TestServiceRequest.java
package com.mason.duconsumer.service;
import com.mason.ducommon.service.TestService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;
@Service
public class TestServiceRequest {
@DubboReference
private TestService testService;
public String getInfo() {
return this.testService.getInfo(" -- Hello Dubbo");
}
}
6.6 启动类
DuconsumerApplication.java
package com.mason.duconsumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DuconsumerApplication {
public static void main(String[] args) {
SpringApplication.run(DuconsumerApplication.class, args);
}
}
6.7 访问controller的服务
# 访问地址
http://127.0.0.1:8081/consumer/getdata
浏览器中的结果
Nacos中结果