当前位置: 代码迷 >> 综合 >> springcloud整合nacos报错ExistingValue must be an instance of com.zaxxer.hikari.HikariDataSource
  详细解决方案

springcloud整合nacos报错ExistingValue must be an instance of com.zaxxer.hikari.HikariDataSource

热度:55   发布时间:2023-10-16 15:47:05.0

springcloud整合nacos启动读取配置正常,但是动态更新配置不生效,也没有看到具体的报错信息只有一条ERROR日志

ERROR c.a.n.client.config.impl.CacheData - [fixed-127.0.0.1_8848] [notify-error] dataId=cmp-server-local.yml, group=DEFAULT_GROUP, md5=f95e93166e82d6b7d103cec47357a1c0, listener=com.alibaba.cloud.nacos.refresh.NacosContextRefresher$1@c63a539 tx=null

于是在c.a.n.client.config.impl.CacheData中打断点一步步跟踪下去,后边发现具体的报错信息为ExistingValue must be an instance of com.zaxxer.hikari.HikariDataSource

具体原因:

spring收到更新配置的event,会将所有的配置类都更新一遍,但我们在项目中使用了com.alipay.sofa 用于跟踪记录数据库的一些信息,sofa组件会在项目启动后将我们的Hikari连接池换成SmartDataSource,spring在更新的时候在上下文中拿到的是SmartDataSource但spring以为我们在用的是HikariDataSource,所以spring尝试将SmartDataSource设置到HikariDataSource的properties中所以报错了..

org.springframework.boot.context.properties.ConfigurationPropertiesBean#create方法中尝试将SmartDataSource绑定到HikariDataSource上bindTarget = bindTarget.withExistingValue(instance);

解决方案:

在调试的过程中发现了spring的一个配置可以在刷新时排除一些类,直接在配置文件中排除掉SmartDataSource就可以了

spring.cloud.refresh.never-refreshable=com.alipay.sofa.tracer.plugins.datasource.SmartDataSource

调了一天,网上也没有发现类似的文章,留个痕希望大家别再被坑了

  相关解决方案