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

背景

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

环境

Windows10
JDK1.8
IDEA2021
Maven2.6

下载安装Nacos

Windows安装Nacos

修改配置

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

YML

spring:
  application:
    name: nacos-config
  cloud:
    nacos:
      config:
        server-addr: localhost:8848
        file-extension: yml
  profiles:
    active: prod

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

Nacos新增test环境配置

在这里插入图片描述

重启项目

在这里插入图片描述

浏览器输入接口地址

在这里插入图片描述

公共环境

YML

spring:
  application:
    name: nacos-config
  cloud:
    nacos:
      config:
        server-addr: localhost:8848
        file-extension: yml
  profiles:
    active: test

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;
    }

}

重启服务,访问接口地址

  • 测试环境配置

在这里插入图片描述

  • 公共环境配置

在这里插入图片描述

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

共享环境

如果是想要用到其他的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

修改接口,添加获取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;
    }

}

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

  • redis

在这里插入图片描述

  • mq

在这里插入图片描述

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

项目代码

代码

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BirdMan98

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值