使用 Spring Cloud Alibaba Nacos Config 作为配…

2020-04-12 16:10:15来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

使用 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 作为分布式配置中心》

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

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

《使用 Nacos Config 作为分布式配置中心》

相关配置

需要在 bootstrap.properties 中优先配置 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 开启动态刷新配置功能

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);
    }
}
  • 文章作者:彭超
  • 本文首发于个人博客:https://antoniopeng.com/2020/01/28/springcloudalibaba/%E4%BD%BF%E7%94%A8SpringCloudAlibabaNacos%E4%BD%9C%E4%B8%BA%E9%85%8D%E7%BD%AE%E4%B8%AD%E5%BF%83/
  • 版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 彭超 | Blog!

原文链接:https://www.cnblogs.com/antoniopeng/p/12687703.html
如有疑问请与原作者联系

标签:POSTAVimgIEPSCOM

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:使用 Spring Cloud Alibaba Nacos 实现服务注册与发现

下一篇:spring依赖注入