B站学习视频
基于Dubbo3.2.9、Nacos2.3.0、SpringBoot 2.7.17、Dubbo-Admin 0.6.0、Jdk1.8 搭建的Dubbo学习Demo
一、前置安装
1-1、Nacos 安装
我本地是通过docker-compose来安装nacos的,如果需要其它方式安装可以去百度找下教程,版本是2.3.0的
docker-compose 安装Nacos
1-1-1、创建 namespase
1-1-2、新增配置文件
dubbo3-consumer
dubbo:application:id: xdx-dubbo3-consumername: xdx-dubbo3-consumerserialize-check-status: WARNregistry:address: nacos://${nacos.config.server-addr}parameters.namespace: ${nacos.config.namespace}
dubbo3-provider
server:port: 5656dubbo:application:id: xdx-dubbo3-providername: xdx-dubbo3-providerserialize-check-status: WARNprotocol:id: dubboname: dubbohost: 127.0.0.1port: 7788serialization: hessian2registry:address: nacos://${nacos.config.server-addr}parameters.namespace: ${nacos.config.namespace}dubboParams: xdx971
dubbo3-provider2
server:port: 5657dubbo:application:id: xdx-dubbo3-providername: xdx-dubbo3-providerserialize-check-status: WARNprotocol:id: dubboname: dubbohost: 127.0.0.1port: 7789serialization: hessian2registry:address: nacos://${nacos.config.server-addr}parameters.namespace: ${nacos.config.namespace}dubboParams: xdx
1-2、Dubbo-Admin 安装
### 1-2-1、下载代码
https://github.com/apache/dubbo-admin/releases
1-2-2、环境安装
Dubbo-Admin 是基于 Vue和 Java开发的,下载源码后想要运行就需要依赖以下环境
- Node v16.20.2 【18会报错】
- Jdk 1.8
- Maven
1-2-3、服务启动
1-2-3-1、前端启动
进入下面的路径执行npm 或 yarn 命令 /dubbo-admin-0.6.0/dubbo-admin-ui
- npm install / yarn install
- npm run dev / yarn run dev
1-2-3-2、后端启动
配置文件修改为如下(主要是指定 nacos地址),然后用 idea 打开 dubbo-admin-server 项目,下载maven依赖后启动
server.port=38080
dubbo.protocol.port=30880
dubbo.application.qos-port=32222# centers in dubbo, if you want to add parameters, please add them to the url
admin.registry.address=nacos://127.0.0.1:8848?namespace=70b158a6-7e64-478d-8c5b-699089aa81d1
admin.config-center=nacos://127.0.0.1:8848?namespace=70b158a6-7e64-478d-8c5b-699089aa81d1
admin.metadata-report.address=nacos://127.0.0.1:8848?namespace=70b158a6-7e64-478d-8c5b-699089aa81d1# dubbo-admin 登录账号密码
admin.root.user.name=root
admin.root.user.password=root#session timeout, default is one hour
admin.check.sessionTimeoutMilli=3600000#compress
server.compression.enabled=true
server.compression.mime-types=text/css,text/javascript,application/javascript
server.compression.min-response-size=10240#token timeout, default is one hour
admin.check.tokenTimeoutMilli=3600000
#Jwt signingKey
admin.check.signSecret=86295dd0c4ef69a1036b0b0c15158d77#dubbo config
dubbo.application.name=dubbo-admin
dubbo.registry.address=${admin.registry.address}
#dubbo.registry.parameters.namespace=70b158a6-7e64-478d-8c5b-699089aa81d1# id generate type
mybatis-plus.global-config.db-config.id-type=nonedubbo.application.logger=slf4j
二、搭建Dubbo-Demo
项目整体结构图
2-1、父工程项目依赖
<?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><groupId>com.xdx97</groupId><artifactId>dubbo3-demo</artifactId><packaging>pom</packaging><version>1.0-SNAPSHOT</version><modules><module>dubbo3-provider</module><module>dubbo3-consumer</module><module>dubbo3-api</module><module>dubbo3-provider2</module></modules><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><java.version>1.8</java.version><spring.boot.version>2.7.17</spring.boot.version><nacos.boot.version>0.2.12</nacos.boot.version><dubbo.version>3.2.9</dubbo.version></properties></project>
2-2、api
dubbo提供和调用本地方法一样的体验,所以需要提供api包
2-2-1、pom
<?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>dubbo3-demo</artifactId><groupId>com.xdx97</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>dubbo3-api</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties></project>2-2-2、DemoService —接口
package com.xdx97.dubbo3.api;public interface DemoService {String getString();
}
2-3、provider —服务提供者
2-3-1、pom
<?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>dubbo3-demo</artifactId><groupId>com.xdx97</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>dubbo3-provider</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>${spring.boot.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><dependencies><!-- spring boot starter --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--nacos配置中心依赖--><dependency><groupId>com.alibaba.boot</groupId><artifactId>nacos-config-spring-boot-starter</artifactId><version>${nacos.boot.version}</version></dependency><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-spring-boot-starter</artifactId><version>${dubbo.version}</version><exclusions><exclusion><artifactId>javassist</artifactId><groupId>org.javassist</groupId></exclusion></exclusions></dependency><!-- dubbo整合nacos --><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-registry-nacos</artifactId><version>${dubbo.version}</version></dependency><!-- 自定义api --><dependency><groupId>com.xdx97</groupId><artifactId>dubbo3-api</artifactId><version>1.0-SNAPSHOT</version><scope>compile</scope></dependency></dependencies>
</project>
2-3-2、启动类
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
@EnableDubbo
public class App {public static void main(String[] args) {SpringApplication.run(App.class, args);}
}
2-3-3、DemoServiceImpl —服务实现类
这里的代码做了小小的改动
- 实时读取nacos的配置文件数据
- 验证dubbo超时设置和重试机制
import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.xdx97.dubbo3.api.DemoService;
import org.apache.dubbo.config.annotation.DubboService;
import java.util.concurrent.atomic.AtomicLong;@DubboService(version = "1.0.0")
public class DemoServiceImpl implements DemoService {// nacos.config.autoRefresh = true 这个配置要打才可以@NacosValue(value = "${dubboParams}", autoRefreshed = true)private String dubboParams;private AtomicLong atomicLong = new AtomicLong(0);@Overridepublic String getString() {long l = atomicLong.incrementAndGet();try {Thread.sleep(1000 - (100 * l));}catch (Exception e) {e.printStackTrace();}return dubboParams;}
}
2-3-4、application —配置文件
nacos:config:# 指定命名空间namespace: 70b158a6-7e64-478d-8c5b-699089aa81d1#配置服务地址server-addr: 127.0.0.1:8848accessKey:secretKey:#data-ids 为新增加的data-iddata-ids: dubbo3-provider#配置类型type: yaml#是否启动刷新配置autoRefresh: true#运行时启用bootstrap:enable: true
注:
- nacos的 namespace 在nacos上面已经配置好了
- dubbo3-provider2 的代码和 dubbo3-provider是一样的,创建2个 provider是为了验证它的负载均衡
2-4、consumer —消费者
2-4-1、pom
引入的依赖和【2-3-1】一样
2-4-2、启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class App {public static void main(String[] args) {SpringApplication.run(App.class, args);}
}
2-4-3、DemoConsumer —消费者测试
这里设置了超时时间为 1s,重试2次
import com.xdx97.dubbo3.api.DemoService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class DemoConsumer {@DubboReference(version = "1.0.0", timeout = 1000, retries = 1)private DemoService demoService;@GetMapping("/fun-test")public String funTest() {return demoService.getString();}}
三、测试
3-1、服务启动
服务有点多,按照下面顺序依次启动
- MySQL (nacos持久化的依赖)
- Nacos
- Dubbo-admin-service
- Dubbo-admin-ui
- consumer
- provider
- provider2
3-2、验证
多请求几次接口 http://localhost:8080/fun-test ,然后再去把 provider 或 provider2 中的dubboParams 参数修改掉,再次请求几次,可以得出下面的结论
- Dubbo 项目搭建成功
- Dubbo 可以自动负载
- Dubbo 的重试和超时机制
- Nacos的热更新
四、源代码获取
- 关注微信公众号:小道仙97
- 回复:dubbo3-demo-xdx