alibaba / nacos

an easy-to-use dynamic service discovery, configuration and service management platform for building cloud native applications.
https://nacos.io
Apache License 2.0
30.06k stars 12.8k forks source link

刷新配置加载druid失败 #3098

Closed zhujiebing closed 4 years ago

zhujiebing commented 4 years ago

Issue Description

配置文件中连接池使用druid,用户名密码加解密使用jasypt,启动正常,crud正常使用。 当修改配置里面的某一个非druid的普通key value配置时再发布,client端异常。 Type: bug report or feature request

Describe what happened (or what feature you want)

2020-06-17 23:50:22.465  INFO 29516 --- [127.0.0.1_8848] c.a.druid.pool.DruidAbstractDataSource   : password changed
2020-06-17 23:50:22.469 ERROR 30476 --- [-127.0.0.1_8848] c.a.nacos.client.config.impl.CacheData   : [fixed-127.0.0.1_8848] [notify-error] dataId=data-dev.yml, group=DEFAULT_GROUP, md5=d430ba78ac35b0a656b36406f12896d6, listener=com.alibaba.cloud.nacos.refresh.NacosContextRefresher$1@4a4cae29 tx={}

org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'spring.datasource.druid' to javax.sql.DataSource
    at org.springframework.boot.context.properties.bind.Binder.handleBindError(Binder.java:364)
    at org.springframework.boot.context.properties.bind.Binder.bind(Binder.java:324)
    at org.springframework.boot.context.properties.bind.Binder.bind(Binder.java:308)
    at org.springframework.boot.context.properties.bind.Binder.bind(Binder.java:238)
    at org.springframework.boot.context.properties.bind.Binder.bind(Binder.java:225)
    at org.springframework.boot.context.properties.ConfigurationPropertiesBinder.bind(ConfigurationPropertiesBinder.java:89)
    at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.bind(ConfigurationPropertiesBindingPostProcessor.java:107)
    at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.postProcessBeforeInitialization(ConfigurationPropertiesBindingPostProcessor.java:96)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:416)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1788)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:407)
    at org.springframework.cloud.context.properties.ConfigurationPropertiesRebinder.rebind(ConfigurationPropertiesRebinder.java:102)
    at org.springframework.cloud.context.properties.ConfigurationPropertiesRebinder.rebind(ConfigurationPropertiesRebinder.java:83)
    at org.springframework.cloud.context.properties.ConfigurationPropertiesRebinder.onApplicationEvent(ConfigurationPropertiesRebinder.java:128)
    at org.springframework.cloud.context.properties.ConfigurationPropertiesRebinder.onApplicationEvent(ConfigurationPropertiesRebinder.java:50)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:403)
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:360)
    at org.springframework.cloud.context.refresh.ContextRefresher.refreshEnvironment(ContextRefresher.java:96)
    at org.springframework.cloud.context.refresh.ContextRefresher.refresh(ContextRefresher.java:85)
    at org.springframework.cloud.endpoint.event.RefreshEventListener.handle(RefreshEventListener.java:72)
    at org.springframework.cloud.endpoint.event.RefreshEventListener.onApplicationEvent(RefreshEventListener.java:61)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:403)
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:360)
    at com.alibaba.cloud.nacos.refresh.NacosContextRefresher$1.innerReceive(NacosContextRefresher.java:133)
    at com.alibaba.nacos.api.config.listener.AbstractSharedListener.receiveConfigInfo(AbstractSharedListener.java:38)
    at com.alibaba.nacos.client.config.impl.CacheData$1.run(CacheData.java:203)
    at com.alibaba.nacos.client.config.impl.CacheData.safeNotifyListener(CacheData.java:233)
    at com.alibaba.nacos.client.config.impl.CacheData.checkListenerMd5(CacheData.java:174)
    at com.alibaba.nacos.client.config.impl.ClientWorker$LongPollingRunnable.run(ClientWorker.java:552)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266)
    at java.util.concurrent.FutureTask.run(FutureTask.java)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalStateException: Unable to set value for property username
    at org.springframework.boot.context.properties.bind.JavaBeanBinder$BeanProperty.setValue(JavaBeanBinder.java:349)
    at org.springframework.boot.context.properties.bind.JavaBeanBinder.bind(JavaBeanBinder.java:96)
    at org.springframework.boot.context.properties.bind.JavaBeanBinder.bind(JavaBeanBinder.java:79)
    at org.springframework.boot.context.properties.bind.JavaBeanBinder.bind(JavaBeanBinder.java:56)
    at org.springframework.boot.context.properties.bind.Binder.lambda$bindDataObject$5(Binder.java:452)
    at org.springframework.boot.context.properties.bind.Binder$$Lambda$42/1601687801.get(Unknown Source)
    at org.springframework.boot.context.properties.bind.Binder$Context.withIncreasedDepth(Binder.java:572)
    at org.springframework.boot.context.properties.bind.Binder$Context.withDataObject(Binder.java:558)
    at org.springframework.boot.context.properties.bind.Binder$Context.access$400(Binder.java:513)
    at org.springframework.boot.context.properties.bind.Binder.bindDataObject(Binder.java:450)
    at org.springframework.boot.context.properties.bind.Binder.bindObject(Binder.java:391)
    at org.springframework.boot.context.properties.bind.Binder.bind(Binder.java:320)
    ... 41 common frames omitted
Caused by: java.lang.reflect.InvocationTargetException: null
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.springframework.boot.context.properties.bind.JavaBeanBinder$BeanProperty.setValue(JavaBeanBinder.java:346)
    ... 52 common frames omitted
Caused by: java.lang.UnsupportedOperationException: null
    at com.alibaba.druid.pool.DruidAbstractDataSource.setUsername(DruidAbstractDataSource.java:1123)
    ... 57 common frames omitted

Tell us your environment

spring cloud alibaba 2.2.1.RELEASE;spring cloud Hoxton.SR1;spring boot 2.2.2.RELEASE; nacos server是linux外置mysql 1.3.0 ; druid 1.1.21

Anything else we need to know?

spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    druid:
      driver-class-name: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://xxx:3306/xxx?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8
      username: ENC(fYETKWQrhACP/gThnEcJ1RiL0tFwukPy)
      password: ENC(n2/T7Mg7PX8bthQfGQMIcey6to2+vPW1)
      stat-view-servlet:
        enabled: true
        url-pattern: /druid/*
        login-username: ENC(jDnE1O15eY/d2igBMGLOzw==)
        login-password: ENC(+Gu2IJMKrL6IjKNMbHBouZBpK921r5u+)
      filter:
        stat:
          enabled: true
          log-slow-sql: true
          slow-sql-millis: 10000
          merge-sql: false
        wall:
          config:
            multi-statement-allow: true
KomachiSion commented 4 years ago

@zhujiebing can you debug in your application to see whether the nacos has publish the new config content correctly?

If nacos return correct content, It may be druid bug, or the content has some problem.

I search for this error information, the message is that the filter caused.

I think it's may be the end of url-pattern: /druid/* properties make later configuration as comment.

chuntaojun commented 4 years ago

这个问题非nacos异常,druid本身的参与校验引起的

public void setUsername(String username) {
        if (StringUtils.equals(this.username, username)) {
            return;
        }

        if (inited) {
            throw new UnsupportedOperationException();
        }

        this.username = username;
    }
youdang123 commented 3 years ago

@zhujiebing 您好,这个问题最后如何处理的呢。我这边和您的一模一样,需要对密码加密

zhujiebing commented 3 years ago

@zhujiebing 您好,这个问题最后如何处理的呢。我这边和您的一模一样,需要对密码加密

直接明文了。

youdang123 commented 3 years ago

@zhujiebing 您好,这个问题最后如何处理的呢。我这边和您的一模一样,需要对密码加密

直接明文了。

我这边去官网看了下jasypt升级,3.0.3以上版本开始支持nacos动态刷新,现在没问题了,您也可以试试,是因为刷新配置,重新加载密文不会解密了。希望能帮到你 image

rujiaxiao commented 3 weeks ago

试过nacos-client:1.4.1版本同样有这个问题,将nacos-client版本升级到2.1.0版本后解决。 PS:留意高版本的alibaba.cloud包中引用了旧版本的nacos-client版本(此处踩过坑)。【例:springcloudAlibaba:2021.1 nacos-client:1.4.1】