标签:alc bsp lan replace article system 解决 strong cat
本文记录SpringBoot和SpringCloud与Nacos作为配置中心的整合过程及问题
Nacos官方使用文档:https://nacos.io/zh-cn/docs/what-is-nacos.html
何为配置中心: https://www.cnblogs.com/yelao/p/10741156.html
本文仅记录整合过程中的细节问题
参见官方文档;
参见官方文档,推荐使用 下载编译后压缩包方式 安装
安装完成后,按照官方文档调试可能出现下图问题:
startup.sh: [[: not found
问题原因:自行百度 bash与sh区别:
使用bash命令运行:
bash startup.sh -m standalone
结果如图
参见官方文档
问题一: 配置文件为yaml格式(阶梯格式)时无法解析 ,如下
配置:
引用:
@NacosValue(value="${log.time}",autoRefreshed=true) private String time;
出现异常:无法解析
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder ‘log.time‘ in value "${log.time}" at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:172) ~[spring-core-5.0.7.RELEASE.jar:5.0.7.RELEASE] at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:124) ~[spring-core-5.0.7.RELEASE.jar:5.0.7.RELEASE] at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:237) ~[spring-core-5.0.7.RELEASE.jar:5.0.7.RELEASE] at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:211) ~[spring-core-5.0.7.RELEASE.jar:5.0.7.RELEASE] at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.lambda$processProperties$0(PropertySourcesPlaceholderConfigurer.java:175) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:839) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE] at com.alibaba.nacos.spring.context.annotation.config.NacosValueAnnotationBeanPostProcessor.doGetInjectedBean(NacosValueAnnotationBeanPostProcessor.java:82) ~[nacos-spring-context-0.2.2-RC1.jar:na] at com.alibaba.nacos.spring.context.annotation.config.NacosValueAnnotationBeanPostProcessor.doGetInjectedBean(NacosValueAnnotationBeanPostProcessor.java:54) ~[nacos-spring-context-0.2.2-RC1.jar:na] at com.alibaba.spring.beans.factory.annotation.AnnotationInjectedBeanPostProcessor.getInjectedObject(AnnotationInjectedBeanPostProcessor.java:330) ~[spring-context-support-1.0.1.jar:na] at com.alibaba.spring.beans.factory.annotation.AnnotationInjectedBeanPostProcessor$AnnotatedFieldElement.inject(AnnotationInjectedBeanPostProcessor.java:510) ~[spring-context-support-1.0.1.jar:na] at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:91) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE] at com.alibaba.spring.beans.factory.annotation.AnnotationInjectedBeanPostProcessor.postProcessPropertyValues(AnnotationInjectedBeanPostProcessor.java:118) ~[spring-context-support-1.0.1.jar:na] ... 22 common frames omitted
问题解决一:放弃使用yaml阶梯状格式,使用增量式,或改用properties
如:
问题解决二:放弃使用spring boot , 使用spring cloud
参见官方文档
此处介绍配置细节:resource/sbootstrap.properties
# Nacos 服务地址 spring.cloud.nacos.config.server-addr=192.168.52.128:8848 # Nacos 控制台添加配置: # Data ID:example-dev.properties # Group:test # 配置内容:useLocalCache=true
# 对应 dataId
spring.application.name=example
spring.profiles.active=dev # 指定配置的后缀,支持 properties、yaml、yml,默认为 properties spring.cloud.nacos.config.file-extension=properties #spring.cloud.nacos.config.file-extension=yaml
# 对应group
#spring.cloud.nacos.config.group=test
详情参见: https://blog.csdn.net/zjcjava/article/details/88316190
@NacosValue(value="${log.time}",autoRefreshed=true),会自动刷新其注解的属性,或运行其注解的方法,根据Bean注入规则进行刷新 Bean
使用spring cloud 原生注解 @RefreshScope 和 @Value
@RefreshScope注解的方法Bean 和 类 会自动刷新,重新生成
注意:
使用@RefreshScope注解的Bean才会重新生成
如,类上注解
@Component @RefreshScope public class TestBean { @Value("${log.time}") private String time; public String getTest() { return time; } }
方法注解
@Configuration public class Conff { @Value("${log.time}") private String time; @Bean @RefreshScope public TestBean getString() { System.out.println("hello"); return new TestBean(time); } }
具体的依赖注入内容还需对spring进行深入学习,以及@RefreshScope原理的把握
注意: Nacos服务并不稳定,会出现更新异常或读取异常的现象,如果在开发过程中出现不解异常或现象,请怀疑Nacos服务(直接重启,因为有些可能是你想不到的Nacos现象,你觉得它没错,但它就是错了)
标签:alc bsp lan replace article system 解决 strong cat
原文地址:https://www.cnblogs.com/yelao/p/10741948.html