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=dev
spring.application.name=service-provider-config
spring.cloud.nacos.config.server-addr=192.168.127.132:8848
spring.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
@RefreshScope
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringCloud如何利用Feign訪問外部http請求
這篇文章主要介紹了SpringCloud如何利用Feign訪問外部http請求,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
SpringSecurity實現(xiàn)前后端分離的示例詳解
Spring Security默認提供賬號密碼認證方式,具體實現(xiàn)是在UsernamePasswordAuthenticationFilter 中,這篇文章主要介紹了SpringSecurity實現(xiàn)前后端分離的示例詳解,需要的朋友可以參考下2023-03-03
基于SqlSessionFactory的openSession方法使用
這篇文章主要介紹了SqlSessionFactory的openSession方法使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12
關(guān)于Spring?Cache?緩存攔截器(?CacheInterceptor)
這篇文章主要介紹了關(guān)于Spring?Cache緩存攔截器(?CacheInterceptor),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12
MyBatisPlus條件構(gòu)造器的實現(xiàn)示例
本文主要介紹了MyBatisPlus條件構(gòu)造器的實現(xiàn)示例,主要包括了QueryWrapper,UpdateWrapper,LambdaQueryWrapper,LambdaUpdateWrapper這四種,具有一定的參考價值,感興趣的可以了解下2023-12-12
SpringBoot實現(xiàn)發(fā)送驗證碼功能(圖片驗證碼)
這篇文章主要介紹了SpringBoot實現(xiàn)發(fā)送驗證碼功能(圖片驗證碼),本次內(nèi)容主要學(xué)習(xí)如何做一個發(fā)送驗證碼和識別驗證碼的功能,需要的朋友可以參考下2024-06-06

