1、安装nacos

2、Spring cloud工程中引入依赖

        <!--nacos客户端-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>


        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>

  

3、创建命名空间

分别为dev 和 test

Spring Cloud集成Nacos 动态配置

 

 

 

创建配置

Spring Cloud集成Nacos 动态配置

 

 

 例如interfaces-dev.yml

Spring Cloud集成Nacos 动态配置

 

 

 

 

4、服务配置

服务使用的版本信息  Spring Cloud:Greenwich.RELEASE, Spring Boot 2.1.3; Spring Cloud Alibaba: 2.1.2.RELEASE

1) bootstrap.yml

Spring Cloud集成Nacos 动态配置

 

 

 

2) PropertiesConfig.java

@Data
@RefreshScope
@ConfigurationProperties(prefix = "dynamic.interfaces")
public class PropertiesConfig {

    private String info = "动态信息";
}

  

3) 增加注解EnableConfigurationProperties

Spring Cloud集成Nacos 动态配置

 

 

4) 测试

    @Autowired
    PropertiesConfig propertiesConfig;

    @RequestMapping("/say")
    public String sayHello() {
        return propertiesConfig.getInfo();

    }  

 

调用接口后返回

Spring Cloud集成Nacos 动态配置

 

 

5、修改interfaces-dev.yml,返回发布

发布成功后,工程将收到消息,刷新配置信息。

再次调用接口,可以发现内容已经修改过了,从而实现了动态属性的修改。

 

 参考:

nacos官方文档:  https://nacos.io/zh-cn/docs/quick-start-spring-cloud.html

nacos配置中心原理 https://www.jianshu.com/p/38b5452c9fec

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-03-01
  • 2021-08-27
  • 2021-08-02
  • 2021-09-01
  • 2022-02-18
  • 2022-12-23
猜你喜欢
  • 2021-08-10
  • 2021-06-19
  • 2022-12-23
  • 2022-12-23
  • 2021-11-07
  • 2021-10-08
  • 2021-11-02
相关资源
相似解决方案