Spring Cloud Alibaba Nacos Config配置中心實現(xiàn)
什么是 Nacos Config
在分布式系統(tǒng)中,由于服務(wù)數(shù)量巨多,為了方便服務(wù) 配置文件統(tǒng)一管理,實時更新,所以需要分布式配置中心組件。
Spring Cloud Alibaba Nacos Config 是 Spring Cloud Config 的替代方案。
Nacos Config 的存儲配置功能為分布式系統(tǒng)中的外部化配置提供服務(wù)器端和客戶端支持,可以在 Nacos 中集中管理 Spring Cloud 應(yīng)用的外部屬性配置。
引入依賴
在 pom.xml 中添加 spring-cloud-starter-alibaba-nacos-config 依賴
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId></dependency>
在 Nacos 控制臺中發(fā)布配置
訪問 Nacos 控制臺,在配置列表中新建一個配置

在該頁面中添加項目配置信息
注:Nacos Config 中的配置內(nèi)容不能帶有注釋,否則項目啟動會失敗

相關(guān)配置
需要在 bootstrap.properties 中優(yōu)先配置 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 開啟動態(tài)刷新配置功能
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); }}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:

網(wǎng)公網(wǎng)安備