本文記錄SpringBoot和SpringCloud與Nacos作為配置中心的整合過程及問題
Nacos官方使用文檔:https://nacos.io/zh-cn/docs/what-is-nacos.html
何為配置中心: https://www.cnblogs.com/yelao/p/10741156.html
本文僅記錄整合過程中的細節問題
1. Nacos簡介
參見官方文檔;
2. Nacos安裝
參見官方文檔,推薦使用 下載編譯后壓縮包方式 安裝
安裝完成后,按照官方文檔調試可能出現下圖問題:
startup.sh: [[: not found
問題原因:自行百度 bash與sh區別:
使用bash命令運行:
bash startup.sh -m standalone
結果如圖
3. nacos 之 spring boot 配置管理
參見官方文檔
問題一: 配置文件為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
4. nacos 之 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
5. 自動刷新Bean
- spring boot:
@NacosValue(value="${log.time}",autoRefreshed=true),會自動刷新其注解的屬性,或運行其注解的方法,根據Bean注入規則進行刷新 Bean
- spring cloud
使用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現象,你覺得它沒錯,但它就是錯了)