使用Nacos作为配置中心@RefreshScope实现动态刷新报错!

使用Nacos作为配置中心

@RefreshScope实现动态刷新报错!

一:解决方案1更改版本:

使用版本:

SpringBoot:2.1.18.RELEASE
 
SpringCloud: Greenwich.SR6
 
SpringCloud Alibaba: 2.1.2.RELEASE
 
jasypt-spring-boot-starter:2.1.2

问题说明:

通过jasypt对Nacos中的敏感配置项进行加密处理后,当更新nacos更新配置项时,后台会报错:

 java.lang.IllegalArgumentException: Could not resolve placeholder 'project.version' in value "${project.version}"
    at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:178)
    at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:124)
    at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:239)
    at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:210)
    at org.springframework.core.env.AbstractEnvironment.resolveRequiredPlaceholders(AbstractEnvironment.java:575)
    at java.util.Optional.map(Optional.java:215)
    at com.ulisesbocchio.jasyptspringboot.resolver.DefaultPropertyResolver.resolvePropertyValue(DefaultPropertyResolver.java:38)
    at com.ulisesbocchio.jasyptspringboot.resolver.DefaultLazyPropertyResolver.resolvePropertyValue(DefaultLazyPropertyResolver.java:42)
    at com.ulisesbocchio.jasyptspringboot.EncryptablePropertySource.getProperty(EncryptablePropertySource.java:20)
    at com.ulisesbocchio.jasyptspringboot.caching.CachingDelegateEncryptablePropertySource.lambda$getProperty$0(CachingDelegateEncryptablePropertySource.java:34)
    at org.springframework.cache.concurrent.ConcurrentMapCache.lambda$get$0(ConcurrentMapCache.java:146)
    at java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1660)
    at org.springframework.cache.concurrent.ConcurrentMapCache.get(ConcurrentMapCache.java:144)
    at com.ulisesbocchio.jasyptspringboot.caching.CachingDelegateEncryptablePropertySource.getProperty(CachingDelegateEncryptablePropertySource.java:34)
    at com.ulisesbocchio.jasyptspringboot.wrapper.EncryptableMapPropertySourceWrapper.getProperty(EncryptableMapPropertySourceWrapper.java:31)
    at org.springframework.cloud.context.refresh.ContextRefresher.extract(ContextRefresher.java:255)
    at org.springframework.cloud.context.refresh.ContextRefresher.extract(ContextRefresher.java:231)
    at org.springframework.cloud.context.refresh.ContextRefresher.refreshEnvironment(ContextRefresher.java:91)
    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)

原因分析:

Spring Boot 会加载classpath下的application.properties

 

而 SpringCloud Alibaba: 2.1.2.RELEASE中 nacos-api:1.2.1和nacos-client:1.2.1中

application.properties里面有 version=${project.version} ;

导致jasypt解析时,找不到值报错。

解决方案:

将SpringCloud Alibaba升级到2.1.4.RELEASE可以解决该问题。升级后nacos-api:1.4.1和nacos-client:1.4.1中去掉了application.properties。

二:第二种解决方式如下:

在bootstrap.properties里增加配置:project.version=@project.version@

spring.cloud.nacos.config.prefix=
spring.cloud.nacos.config.server-addr=${nacos.config.addr:192.168.0.0:8848}
spring.cloud.nacos.config.file-extension=yaml
spring.cloud.nacos.config.group=${nacos.config.group:DEFAULT_GROUP}
spring.cloud.servicecomb.config.serverAddr=http://127.0.0.1:30113
project.version=@project.version@

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值