您的位置:首页技术文章
文章详情页

Spring Cloud Alibaba Nacos Config配置中心实现

【字号: 日期:2023-09-11 11:26:28浏览:4作者:猪猪

什么是 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 控制台,在配置列表中新建一个配置

Spring Cloud Alibaba Nacos Config配置中心实现

在该页面中添加项目配置信息

注:Nacos Config 中的配置内容不能带有注释,否则项目启动会失败

Spring Cloud Alibaba 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); }}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持好吧啦网。

标签: Spring
相关文章: