【Nacos】Springboot整合Nacos配置中心(二) 多环境配置

时间:2023-03-09 03:29:18
【Nacos】Springboot整合Nacos配置中心(二)  多环境配置

本篇随笔接上一篇文章:Springboot整合Nacos配置中心(一),主要记录Nacos多环境的配置的方法

Nacos多环境的配置

方法一:

  1.在项目中的bootstrap.yaml文件中配置激活指定的配置文件

spring:
application:
name: gabriel
cloud:
nacos:
config:
server-addr: 127.0.0.1:8848
file-extension: yaml
profiles:
active: dev

  2.相应的在Nacos的网页上配置多个环境的配置文件

【Nacos】Springboot整合Nacos配置中心(二)  多环境配置

  测试如下:

  dev环境的配置文件

【Nacos】Springboot整合Nacos配置中心(二)  多环境配置

  test环境的配置文件【Nacos】Springboot整合Nacos配置中心(二)  多环境配置

【Nacos】Springboot整合Nacos配置中心(二)  多环境配置

【Nacos】Springboot整合Nacos配置中心(二)  多环境配置

  测试成功!

方法二:

  通过Gcoup来创建两个不同环境的配置内容

  【Nacos】Springboot整合Nacos配置中心(二)  多环境配置

【Nacos】Springboot整合Nacos配置中心(二)  多环境配置

【Nacos】Springboot整合Nacos配置中心(二)  多环境配置

测试:

  不同的group,激活了不同的环境的配置文件,如下图【Nacos】Springboot整合Nacos配置中心(二)  多环境配置

【Nacos】Springboot整合Nacos配置中心(二)  多环境配置

方法3: 使用namespce区分不同的环境:

【Nacos】Springboot整合Nacos配置中心(二)  多环境配置

然后在两个namespace dev和test 空间下面分别加入两个配置,【Nacos】Springboot整合Nacos配置中心(二)  多环境配置

【Nacos】Springboot整合Nacos配置中心(二)  多环境配置

测试:

    同样的在Nacos配置文件中,server.name 分别制定不同的值用于测试

    【Nacos】Springboot整合Nacos配置中心(二)  多环境配置

   【Nacos】Springboot整合Nacos配置中心(二)  多环境配置

【Nacos】Springboot整合Nacos配置中心(二)  多环境配置

【Nacos】Springboot整合Nacos配置中心(二)  多环境配置

三种不同配置的优缺点: 参考:https://www.cnblogs.com/didispace/p/10340685.html