上一篇文章中,Spring Boot 整合 Dubbo3 + Nacos 2.4.0 进行了简单的集成使用,此文简单进阶并记录踩坑日常;
- Nacos 2.4.0 增加鉴权的配置
- Nacos 2.4.0 配置 MySQL
- Nacos2.4.0 的热更新
- Dubbo3 自动负载
- Dubbo3 的重试和超时机制
- 踩坑记录
一、Nacos 2.4.0 增加鉴权的配置
1.1、修改nacos的配置文件application.properties
powershell
cd ../nacos/conf
vim application.properties
1.2、启动报错
1.3、增加账号和用户名的配置
在dubbo3-provider项目的application.yaml文件中增加username和password的配置
在 nacos的dubbo3-provider配置中增加username和password的参数
1.4、启动成功
1.5、consumer 进行同样修改,启动成功
二、Nacos 2.4.0 配置 MySQL
nacos2.4.0默认使用内置的 Derby数据库,实际使用中更多会使用 MySQL 数据库;
重启后,启动成功!
三、Nacos2.4.0 的热更新
java
@NacosValue(value = "${dubboParams}", autoRefreshed = true)
private String dubboParams;
配置文件中 增加 nacos.config.autoRefresh = true
详细可看源码中
配置生效
四、Dubbo3 自动负载
新增模块 dubbo3-provider2
模块内容和dubbo3-provider一致,只是修改了部分配置,此处说明修改部分,全部源码可在文末查看。
1、新增 nacos 配置
yaml
server:
port: 5657
dubbo:
application:
id: tyron-dubbo3-provider
name: tyron-dubbo3-provider
serialize-check-status: WARN
protocol:
id: dubbo
name: dubbo
host: 127.0.0.1
port: 7789
serialization: hessian2
registry:
address: nacos://${nacos.config.server-addr}
parameters.namespace: ${nacos.config.namespace}
parameters.username: ${nacos.config.username}
parameters.password: ${nacos.config.password}
dubboParams: tyron-dubbo3-provider2
2、修改 application.yaml
yaml
nacos:
config:
# 指定命名空间
namespace: 0f0809bb-4b47-****-1e7deb1d7ad2
#配置服务地址
server-addr: ******:8848
username: nacos
password: nacos
#data-ids 为新增加的data-id
data-ids: dubbo3-provider2
#配置类型
type: yaml
#是否启动刷新配置
autoRefresh: true
#运行时启用
bootstrap:
enable: true
3、nacos 服务列表中实例数2
五、Dubbo3 的重试和超时机制
yaml
// 生产者代码 dubbo3-provider
@DubboService(version = "1.0.0")
public class DemoServiceImpl implements DemoService {
@NacosValue(value = "${dubboParams}", autoRefreshed = true)
private String dubboParams;
private AtomicLong atomicLong = new AtomicLong(0);
/**
* 第一次调用时,睡眠时间为1秒,第二次调用时为900毫秒
*/
@Override
public String getString() {
long l = atomicLong.incrementAndGet();
System.out.println("atomicLong.incrementAndGet():" + l);
try {
Thread.sleep(1000 - (100 * l));
} catch (Exception e) {
e.printStackTrace();
}
return dubboParams;
}
}
// 消费者代码,dubbo3-consumer 增加超时时间和重试次数
@RestController
public class DemoConsumer {
// 引用远程服务,超时时间1000ms,重试次数1次
@DubboReference(version = "1.0.0", timeout = 1000, retries = 1)
private DemoService demoService;
@GetMapping("/tyron-test")
public String tyronTest() {
return demoService.getString();
}
}
接口调用成功,日志打印:
六、踩坑记录
由于nacos在服务器中部署,搭建好了,启动报错。
1、防火墙添加端口
yaml
java.lang.RuntimeException: Can not create registry service-discovery-registry://*******:8848/org.apache.dubbo.registry.RegistryService?application=tyron-dubbo3-provider&dubbo=2.0.2&executor-management-mode=isolation&file-cache=true&interface=org.apache.dubbo.registry.RegistryService&namespace=0f0809bb-4b47-4458-aaa4-1eeb1d7ad2&pid=13008®ister=false®istry=nacos&release=3.2.9&serialize.check.status=WARN
at org.apache.dubbo.registry.support.AbstractRegistryFactory.getRegistry(AbstractRegistryFactory.java:105) ~[dubbo-3.2.9.jar:3.2.9]
at org.apache.dubbo.registry.RegistryFactoryWrapper.getRegistry(RegistryFactoryWrapper.java:33) ~[dubbo-3.2.9.jar:3.2.9]
at org.apache.dubbo.registry.RegistryFactory$Adaptive.getRegistry(RegistryFactory$Adaptive.java) ~[dubbo-3.2.9.jar:3.2.9]
at org.apache.dubbo.registry.integration.RegistryProtocol.getRegistry(RegistryProtocol.java:487) ~[dubbo-3.2.9.jar:3.2.9]
at org.apache.dubbo.registry.integration.RegistryProtocol.export(RegistryProtocol.java:294) ~[dubbo-3.2.9.jar:3.2.9]
at org.apache.dubbo.rpc.protocol.ProtocolSecurityWrapper.export(ProtocolSecurityWrapper.java:84) ~[dubbo-3.2.9.jar:3.2.9]
at org.apache.dubbo.qos.protocol.QosProtocolWrapper.export(QosProtocolWrapper.java:79) ~[dubbo-3.2.9.jar:3.2.9]
at org.apache.dubbo.rpc.protocol.ProtocolListenerWrapper.export(ProtocolListenerWrapper.java:66) ~[dubbo-3.2.9.jar:3.2.9]
at org.apache.dubbo.rpc.cluster.filter.ProtocolFilterWrapper.export(ProtocolFilterWrapper.java:58) ~[dubbo-3.2.9.jar:3.2.9]
at org.apache.dubbo.rpc.protocol.ProtocolSerializationWrapper.export(ProtocolSerializationWrapper.java:50) ~[dubbo-3.2.9.jar:3.2.9]
at org.apache.dubbo.rpc.protocol.InvokerCountWrapper.export(InvokerCountWrapper.java:42) ~[dubbo-3.2.9.jar:3.2.9]
at org.apache.dubbo.rpc.Protocol$Adaptive.export(Protocol$Adaptive.java) ~[dubbo-3.2.9.jar:3.2.9]
at org.apache.dubbo.config.ServiceConfig.doExportUrl(ServiceConfig.java:944) ~[dubbo-3.2.9.jar:3.2.9]
看了网上教程,是防火墙端口为开放,需要配置三个端口地址:9848、9849、7848
nacos2.X版本无法注册、注册失败的几个原因以及解决方案(踩坑避雷!)_nacos开启权限校验后无法注册-CSDN博客
部署手册概览
2、数据库添加用户数据
仔细比较 2.4 版本的初始化MySQL脚本和 2.3 版本的初始化 MySQL脚本,2.3多了两句用户相关的SQL;
https://github.com/alibaba/nacos/blob/2.3.0/config/src/main/resources/META-INF/nacos-db.sql
https://github.com/alibaba/nacos/blob/2.4.0/config/src/main/resources/META-INF/mysql-schema.sql
sql
INSERT INTO users (username, password, enabled) VALUES ('nacos', '$2a$10$EuWPZHzz32dJN7jexM34MOeYirDdFAZm2kuWj7VEOJhhZkDrxfvUu', TRUE);
INSERT INTO roles (username, role) VALUES ('nacos', 'ROLE_ADMIN');
七、参考
Dubbo入门项目搭建【Dubbo3.2.9、Nacos2.3.0、SpringBoot 2.7.17、Dubbo-Admin 0.6.0】_哔哩哔哩_bilibili