2.sofaboot集成nacos配置中心
sofaboot 集成nacos配置中心与注册中心
网上有很多文章关于集成nacos配置中心与注册中心,但是都是基于springboot版本与springcloudalibaba版本,几乎没有关于sofaboot版本的集成文章,所以这里我写一篇集成文章。
前置条件&环境
- springboot:3.4.3
- springcloud: 2023.0.3
- springcloud-alibaba: 2023.0.1.2
- sofaboot: 4.3.0
- nacos-server: 2.4.3
需要自行安装nacos-server,可参考文章:nacos-server
集成nacos配置中心
1.1 添加依赖
<!-- 配置中心 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!-- 注册中心 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<exclusions>
<exclusion>
<artifactId>jsr305</artifactId>
<groupId>com.google.code.findbugs</groupId>
</exclusion>
</exclusions>
</dependency>
为了能使nacos-config
生效,需要增加以下依赖并创建bootstrap-{active}.yaml
新增依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
创建bootstrap-{active}.yaml
文件,其中{active}为当前环境标识,比如dev、test、prod等
server:
port: 8082
spring:
application:
name: accumulate-admin
cloud:
nacos:
username: xxx
password: xxxxxx
# 注册中心
discovery:
server-addr: http://localhost:8848
# 配置中心
config:
server-addr: http://localhost:8848
config:
import:
- nacos:accumulate-admin.yaml?refreshEnabled=true
- nacos:accumulate-common.yaml?refreshEnabled=true
- nacos:accumulate-admin-common.yaml?refreshEnabled=true
- nacos:test-nacos-extension-config.properties?refreshEnabled=true
配置文件根据自己的项目自行定义这里只是为了演示
启动项目
启动以后再nacos中动态的更新一下配置(任意配置),然后你会发现控制台一直循环输出:
2025-01-23T07:30:31.558517Z com.alibaba.nacos.client.Worker.0 WARN The use of package scanning to locate plugins is deprecated and will be removed in a future release
2025-01-23T07:30:31.558660Z com.alibaba.nacos.client.Worker.0 WARN The use of package scanning to locate plugins is deprecated and will be removed in a future release
2025-01-23T07:30:31.559067Z com.alibaba.nacos.client.Worker.0 WARN The use of package scanning to locate plugins is deprecated and will be removed in a future release
2025-01-23T07:30:31.559293Z com.alibaba.nacos.client.Worker.0 WARN The use of package scanning to locate plugins is deprecated and will be removed in a future release
2025-01-23T07:30:31.561329Z com.alibaba.nacos.client.Worker.0 WARN No Root logger was configured, creating default ERROR-level Root logger with Console appender
2025-01-23T07:30:31.566035Z com.alibaba.nacos.client.Worker.0 WARN The use of package scanning to locate plugins is deprecated and will be removed in a future release
2025-01-23T07:30:31.566125Z com.alibaba.nacos.client.Worker.0 WARN The use of package scanning to locate plugins is deprecated and will be removed in a future release
2025-01-23T07:30:31.566360Z com.alibaba.nacos.client.Worker.0 WARN The use of package scanning to locate plugins is deprecated and will be removed in a future release
2025-01-23T07:30:31.566513Z com.alibaba.nacos.client.Worker.0 WARN The use of package scanning to locate plugins is deprecated and will be removed in a future release
2025-01-23T07:30:31.568218Z com.alibaba.nacos.client.Worker.0 WARN No Root logger was configured, creating default ERROR-level Root logger with Console appender
2025-01-23 15:30:31,579 INFO com.alibaba.nacos.client.Worker.0 - total supported commands:health, queryAllBiz, help, uninstallBiz, switchBiz, installBiz, queryBizOps
2025-01-23 15:30:31.579 INFO --- [client.Worker.0] rtup.StartupSpringApplicationRunListener : Started accumulate-admin in 0.384 seconds
2025-01-23T07:30:31.581513Z com.alibaba.nacos.client.Worker.0 WARN The use of package scanning to locate plugins is deprecated and will be removed in a future release
2025-01-23T07:30:31.581590Z com.alibaba.nacos.client.Worker.0 WARN The use of package scanning to locate plugins is deprecated and will be removed in a future release
2025-01-23T07:30:31.581791Z com.alibaba.nacos.client.Worker.0 WARN The use of package scanning to locate plugins is deprecated and will be removed in a future release
2025-01-23T07:30:31.581911Z com.alibaba.nacos.client.Worker.0 WARN The use of package scanning to locate plugins is deprecated and will be removed in a future release
2025-01-23T07:30:31.583248Z com.alibaba.nacos.client.Worker.0 WARN No Root logger was configured, creating default ERROR-level Root logger with Console appender
2025-01-23 15:30:31.818 INFO 49820 --- [client.Worker.0] b.c.PropertySourceBootstrapConfiguration : Located property source: [BootstrapPropertySource {name='bootstrapProperties-accumulate-admin-test.properties,DEFAULT_GROUP'}, BootstrapPropertySource {name='bootstrapProperties-accumulate-admin.properties,DEFAULT_GROUP'}, BootstrapPropertySource {name='bootstrapProperties-accumulate-admin,DEFAULT_GROUP'}]
2025-01-23T07:30:31.848461Z com.alibaba.nacos.client.Worker.0 WARN The use of package scanning to locate plugins is deprecated and will be removed in a future release
2025-01-23T07:30:31.848534Z com.alibaba.nacos.client.Worker.0 WARN The use of package scanning to locate plugins is deprecated and will be removed in a future release
2025-01-23T07:30:31.848706Z com.alibaba.nacos.client.Worker.0 WARN The use of package scanning to locate plugins is deprecated and will be removed in a future release
2025-01-23T07:30:31.848807Z com.alibaba.nacos.client.Worker.0 WARN The use of package scanning to locate plugins is deprecated and will be removed in a future release
2025-01-23T07:30:31.849959Z com.alibaba.nacos.client.Worker.0 WARN No Root logger was configured, creating default ERROR-level Root logger with Console appender
2025-01-23 15:30:31.850 INFO 49820 --- [client.Worker.0] o.s.b.SpringApplication : The following 1 profile is active: "test"
2025-01-23 15:30:31.852 INFO 49820 --- [client.Worker.0] o.s.b.SpringApplication : Started application in 0.661 seconds (process running for 61.719)
2025-01-23 15:30:31.852 INFO --- [client.Worker.0] rtup.StartupSpringApplicationRunListener : Started accumulate-admin in 0.661 seconds
2025-01-23 15:30:31.872 INFO 49820 --- [client.Worker.0] ACCUMULATE-CORE-DETAIL-LOG : 配置更新前值:1.0.28
2025-01-23 15:30:31.872 INFO 49820 --- [client.Worker.0] ACCUMULATE-CORE-DETAIL-LOG : 配置更新后值:1.0.28
控制台还一直没有错误日志产生,但是这个情况说明,一定有异常,并且是catch以后把异常没有打印到控制台,也没有打印到日志文件中。
如果对nacos源码比较了解应该很快的能定位问题,但是对nacos源码不熟悉的话,就需要耐心的查阅资料和debug才能解决这个问题。
下面是一些参考资料:
- https://blog.csdn.net/harmful_sheep/article/details/140792499
- https://blog.csdn.net/Zong_0915/article/details/113089265
直接看日志:com.alibaba.nacos.client.Worker
应该是和nacos-client的com.alibaba.nacos.client.config.impl.ClientWorker
有关,查看相关源码打断点
在构造器的地方通过一个定时任务去轮询nacos pull配置
public ClientWorker(final ConfigFilterChainManager configFilterChainManager, ServerListManager serverListManager,
final NacosClientProperties properties) throws NacosException {
this.configFilterChainManager = configFilterChainManager;
init(properties);
agent = new ConfigRpcTransportClient(properties, serverListManager);
ScheduledExecutorService executorService = Executors.newScheduledThreadPool(initWorkerThreadCount(properties),
new NameThreadFactory("com.alibaba.nacos.client.Worker"));
agent.setExecutor(executorService);
agent.start();
}
追踪源码发现通过判断MD5是否相同来判断是否需要更新配置,如果MD5相同则不更新配置,如果MD5不同则更新配置 com.alibaba.nacos.client.config.impl.CacheData#checkListenerMd5
void checkListenerMd5() {
for (ManagerListenerWrap wrap : listeners) {
if (!md5.equals(wrap.lastCallMd5)) {
safeNotifyListener(dataId, group, content, type, md5, encryptedDataKey, wrap);
}
}
}
猜想一下,我们跟新配置以后,需要将新的MD5值赋值给 CacheData 的md5属性,所以我跟踪源码找到这个代码打上点断
两个catch语句块也要添加断点,然后启动项目更新配置,查看断点:
可以发现是类找不到,所以我们添加上依赖即可:
<!--解决sofa-boot类隔离导致的 classNotFound 的问题-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.12.0</version>
</dependency>
启动以后重新测试,可以发现没有问题了。日志只打印一遍并且能走到 listenerWrap.lastCallMd5 = md5;
断点处
自此nacos集成完毕
未知问题:
LoggerFactory.getLogger(ClientWorker.class).info(“ClientWorker log”);
无法答应日志到控制台