• SpringCloudAlibaba【四】Nacos Config 多环境切换与公共配置


    背景

    前面我们整合了Nacos Config,并验证了动态配置刷新的效果了,现在我们来进一步学习一下Nacos Config的多环境配置和通用环境配置相关的内容

    环境

    Windows10
    JDK1.8
    IDEA2021
    Maven2.6
    
    • 1
    • 2
    • 3
    • 4

    下载安装Nacos

    Windows安装Nacos

    修改配置

    基于上文的项目,进行修改

    YML

    spring:
      application:
        name: nacos-config
      cloud:
        nacos:
          config:
            server-addr: localhost:8848
            file-extension: yml
      profiles:
        active: prod
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    Nacos配置

    在这里插入图片描述

    注:Data Id = spring.application.name + [-spring.profile.active]. + file-extension

    测试

    下面从Nacos的不同环境配置进一步了解Nacos Config

    生产环境

    YML配置如上

    启动项目,浏览器输入接口地址

    在这里插入图片描述

    测试环境

    修改YML配置

    spring:
      application:
        name: nacos-config
      cloud:
        nacos:
          config:
            server-addr: localhost:8848
            file-extension: yml
      profiles:
        active: test
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    Nacos新增test环境配置

    在这里插入图片描述

    重启项目

    在这里插入图片描述

    浏览器输入接口地址

    在这里插入图片描述

    公共环境

    YML

    spring:
      application:
        name: nacos-config
      cloud:
        nacos:
          config:
            server-addr: localhost:8848
            file-extension: yml
      profiles:
        active: test
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    Nacos新建公共配置,Data Id以spring.application.name命名

    在这里插入图片描述

    通用配置内容

    在这里插入图片描述

    修改接口,我们看是否能够在test环境下同时访问到通用配置内容

    package com.nacos.controller;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.cloud.context.config.annotation.RefreshScope;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RefreshScope
    public class NacosConfigController {
    
        @Value("${str}")
        private String str;
    
        @Value("${commonStr}")
        private String commonStr;
    
        @GetMapping("/")
        public String getConfig() {
            return str;
        }
    
        @GetMapping("/common")
        public String getCommonConfig() {
            return commonStr;
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    重启服务,访问接口地址

    • 测试环境配置

    在这里插入图片描述

    • 公共环境配置

    在这里插入图片描述

    小结:即使是在测试环境下,也可以获取到公共的配置

    共享环境

    如果是想要用到其他的Nacos配置文件,要怎么处理?

    下面在Nacos新建两个配置文件,如下

    • 共享Redis配置

    在这里插入图片描述

    • 共享MQ配置

    在这里插入图片描述

    修改YML

    spring:
      application:
        name: nacos-config
      cloud:
        nacos:
          config:
            server-addr: localhost:8848
            file-extension: yml
            shared-configs[0]:
              data_id: redis.yml
              refresh: true
            shared-configs[1]:
              data_id: mq.yml
              refresh: true
      profiles:
        active: test
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    修改接口,添加获取Redis和mq的接口

    package com.nacos.controller;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.cloud.context.config.annotation.RefreshScope;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RefreshScope
    public class NacosConfigController {
    
        @Value("${str}")
        private String str;
    
        @Value("${commonStr}")
        private String commonStr;
    
        @Value("${redis}")
        private String redis;
    
        @Value("${mq}")
        private String mq;
    
        @GetMapping("/")
        public String getConfig() {
            return str;
        }
    
        @GetMapping("/common")
        public String getCommonConfig() {
            return commonStr;
        }
    
        @GetMapping("/redis")
        public String getRedisConfig() {
            return redis;
        }
    
        @GetMapping("/mq")
        public String getMqCommonConfig() {
            return mq;
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

    重启项目,浏览器输入对应接口地址

    • redis

    在这里插入图片描述

    • mq

    在这里插入图片描述

    小结:在test环境下可以通过shared-configs[0]或者extension-configs[0]配置多个其他配置文件

    项目代码

    代码

  • 相关阅读:
    自制肥鲨HDO2电源升压延长线
    组合数学(上):数列、排列、组合
    如何自制一个安装程序,将程序打包生成安装程序的办法
    MySQL索引分类及相关概念辨析
    AI趋势量化系统(Binance升级版)
    第四十六章 开发自定义标签 - 标签匹配 操作中的运行时表达式
    docker 命令
    MES系统成为工业4.0首选,制造业真正数字化车间你看过吗?
    微服务(SpringCloud)使用汇总
    管理类联考——数学——汇总篇——记忆——歌诀记忆法
  • 原文地址:https://blog.csdn.net/weixin_41405524/article/details/127438517