Spring Cloud Alibaba Nacos Config配置中心實現
什么是 Nacos Config
在分布式系統中,由于服務數量巨多,為了方便服務 配置文件統一管理,實時更新,所以需要分布式配置中心組件。
Spring Cloud Alibaba Nacos Config 是 Spring Cloud Config 的替代方案。
Nacos Config 的存儲配置功能為分布式系統中的外部化配置提供服務器端和客戶端支持,可以在 Nacos 中集中管理 Spring Cloud 應用的外部屬性配置。
引入依賴
在 pom.xml 中添加 spring-cloud-starter-alibaba-nacos-config 依賴
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId></dependency>
在 Nacos 控制臺中發布配置
訪問 Nacos 控制臺,在配置列表中新建一個配置
在該頁面中添加項目配置信息
注:Nacos Config 中的配置內容不能帶有注釋,否則項目啟動會失敗
相關配置
需要在 bootstrap.properties 中優先配置 Nacos Config 客戶端
spring.profiles.active=devspring.application.name=service-provider-configspring.cloud.nacos.config.server-addr=192.168.127.132:8848spring.cloud.nacos.config.file-extension=yaml
注:Spring Boot 配置文件的加載順序,依次為 bootstrap.properties > bootstrap.yaml > application.properties > application.yaml
在 Application 入口類中添加注解 @RefreshScope 開啟動態刷新配置功能
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.context.config.annotation.RefreshScope;@SpringBootApplication@RefreshScopepublic class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); }}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章:
