微服务学习第四十五节 Nacos Config深入

Nacos Config深入

Nacos之服务配置中心

基础配置

Nacos不仅仅可以作为注册中心来使用,同时它支持作为配置中心

在这里插入图片描述

首先我们还是新建Model:cloudalibaba-config-3377

pom文件

​ 这里我们主要要引入的是此依赖,这个依赖依据在官网上可以找到:https://spring-cloud-alibaba-group.github.io/github-pages/greenwich/spring-cloud-alibaba.html#_an_example_of_using_nacos_discovery_for_service_registrationdiscovery_and_call

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

YML配置

​ 要注意的是这里我们要配置两个,因为Nacos同SpringCloud-config一样,在项目初始化时,要保证先从配置中心进行配置拉取,拉取配置之后,才能保证项目的正常启动。

​ springboot中配置文件的加载是存在优先级顺序的,bootstrap优先级高于application

​ 分别要配置的是,这里bootstrap.yml配置好了以后,作用是两个,第一个让3377这个服务注册到Nacos中,第二个作用就是去Nacos中去读取指定后缀为yaml的配置文件:

bootstrap.yml

# nacos配置
server:
  port: 3377

spring:
  application:
    name: nacos-config-client
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 #Nacos服务注册中心地址
      config:
        server-addr: localhost:8848 #Nacos作为配置中心地址
        file-extension: yaml #指定yaml格式的配置

application.yml

spring:
  profiles:
    active: dev # 表示开发环境

主启动

package com.mashibing.cloudalibabaconfig3377;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class CloudalibabaConfig3377Application {

    public static void main(String[] args) {
        SpringApplication.run(CloudalibabaConfig3377Application.class, args);
    }

}

业务类

​ 这里的@RefreshScope实现配置自动更新,意思为如果想要使配置文件中的配置修改后不用重启项目即生效,可以使用@RefreshScope配置来实现

package com.mashibing.cloudalibabaconfig3377.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RefreshScope //支持Nacos的动态刷新功能
public class ConfigClientController {

    @Value("${config.info}")
    private String configInfo;

    @Value("${/config/info}")
    public String getConfigInfo(){
        return configInfo;
    }

}

Nacos配置规则

​ 在 Nacos Spring Cloud 中,dataId 的完整格式如下(详情可以参考官网 https://nacos.io/zh-cn/docs/quick-start-spring-cloud.html):

${prefix}-${spring.profiles.active}.${file-extension}
1. `prefix` 默认为 `spring.application.name` 的值,也可以通过配置项 `spring.cloud.nacos.config.prefix`来配置。
2. `spring.profiles.active` 即为当前环境对应的 profile,注意:**当 `spring.profiles.active` 为空时,对应的连接符 `-` 也将不存在,dataId 的拼接格式变成 `${prefix}.${file-extension}`**(不能删除)
3. `file-exetension` 为配置内容的数据格式,可以通过配置项 `spring.cloud.nacos.config.file-extension` 来配置。目前只支持 `properties` 和 `yaml` 类型。
4. 通过 Spring Cloud 原生注解 `@RefreshScope` 实现配置自动更新:
5. 所以根据官方给出的规则我们最终需要在Nacos配置中心添加的配置文件的名字规则和名字为:
# ${spring.application.name}-${spring.profiles.active}.${file-extension}
# nacos-config-client-dev.yaml
# 微服务名称-当前环境-文件格式

在这里插入图片描述

Nacos平台创建配置操作

增加配置

在这里插入图片描述
在这里插入图片描述

config: 
    info: nacos config center,version = 1

然后在配置中心就会看到刚刚发布的配置

在这里插入图片描述

自动配置更新

修改Nacos配置,不需要重启项目即可自动刷新

在这里插入图片描述

修改版本号为2,点击发布

在这里插入图片描述

测试

启动服务访问服务来测试(没有修改之前是1,修改之后不需要重启项目既可以直接获取最新配置):http://localhost:3377/config/info

在这里插入图片描述

Nacos Config进阶

解决不同环境相同配置问题-自定义Data ID配置

在实际的开发过程中,我们的项目所用到的配置参数有的时候并不需要根据不同的环境进行区分,生产、测试、开发环境所用到的参数值是相同的。那么解决同一服务在多环境中,引用相同的配置的问题?Nacos Config也提供了相应的解决方案。

在这里插入图片描述

那么我们可以通过服务名+拓展名的方式,来实现同一个微服务下不同的环境,共享的配置文件。

具体配置

我们在Nacos Config中添加配置,data_id为nacos-config-clinet.yaml

在这里插入图片描述

控制器代码更改

@RestController
@RefreshScope //支持Nacos动态刷新功能
public class ConfigController {

    @Value("${config.info}")
    private String configInfo;

    //通用
    @Value("${config.common}")
    private String configCommon;

    @GetMapping("/config/info")
    public String getConfigInfo(){
        return configInfo;
    }

    //通用
    @GetMapping("/config/common")
    public String getCommon(){
        return configCommon;
    }
}

测试可以直接读取:http://localhost:3377/config/common

在这里插入图片描述

不同微服务之间如何共享配置

当前这种配置方式是最基础的配置方式,但是在实际开发中骂我们一般会涉及到多个微服务之间共享配置。比如说redis地址,服务注册中心公共组件等等,那么这些组件是多个微服务共享的,所以我们可以使用Nacos Config提供的共享配置方式来配置共享的配置文件。

配置文件名字没有固定要求

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-fybyQ1Dh-1657029975052)(image-20220511180856987.png)]

通过shard-configs方式

具体实现

在nacos-config中添加 redis.yml,添加配置 redisip: 111.11.11.01

在这里插入图片描述

更改yml配置

spring:
  application:
    name: nacos-config-client
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 #Nacos服务注册中心地址
      config:
        server-addr: localhost:8848 #Nacos作为配置中心地址
        file-extension: yaml #指定yaml格式的配置
        shared-configs[0]:  #shared-configs是一个列表,可以添加多项
          data_id: redis.yml #具体配置
          group: DEFAULT_GROUP #默认可以不写
          refresh: true #是否开启自动刷新,默认为false,必须搭配@RefreshScope注解

更改测试类

@RestController
@RefreshScope //支持Nacos动态刷新功能
public class ConfigController {

    @Value("${config.info}")
    private String configInfo;

    //通用
    @Value("${config.common}")
    private String configCommon;

    @GetMapping("/config/info")
    public String getConfigInfo(){
        return configInfo;
    }

    //共享
    @Value("${redisip}")
    private String redisIp;

    //通用
    @GetMapping("/config/common")
    public String getCommon(){
        return configCommon;
    }

    //通用
    @GetMapping("/config/redisip")
    public String getRedisIp(){
        return redisIp;
    }
}

测试结果:
在这里插入图片描述

当然也支持多个配置,只需要在shared-configs[n],增加n的数值即可

cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 #Nacos服务注册中心地址
      config:
        server-addr: localhost:8848 #Nacos作为配置中心地址
        file-extension: yaml #指定yaml格式的配置
        shared-configs[0]:  #shared-configs是一个列表,可以添加多项
          data_id: redis.yml #具体配置
          group: DEFAULT_GROUP #默认可以不写
          refresh: true #是否开启自动刷新,默认为false,必须搭配@RefreshScope注解
        shared-configs[1]: #shared-configs是一个列表,可以添加多项
          data_id: common.yml #具体配置
          group: DEFAULT_GROUP #默认可以不写
          refresh: true #是否开启自动刷新,默认为false,必须搭配@RefreshScope注解

注意:多个 Data Id 同时配置时,他的优先级关系是 spring.cloud.nacos.config.extension-configs[n].data-id 其中 n 的值越大,优先级越高。

通过extension-configs方式

其实以上的实现还可以通过extension-configs方式来完成,其实作用基本一致,只不过语义上可以更好的区分,如果我们需要在一个微服务上配置多个配置文件,可以使用extension-configs,如果需要多个配置文件共享,可以使用shard-configs配置方式,当然其实两种方式所实现的效果和配置方法基本一致。

所以通过自定义扩展的 Data Id 配置,既可以解决多个应用间配置共享的问题,又可以支持一个应用有多个配置文件。

具体实现

更改yml,只需要将shared-configs改为extension-configs即可

spring:
  application:
    name: nacos-config-client
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 #Nacos服务注册中心地址
      config:
        server-addr: localhost:8848 #Nacos作为配置中心地址
        file-extension: yaml #指定yaml格式的配置
        extension-configs[0]:  #shared-configs是一个列表,可以添加多项
          data_id: redis.yml #具体配置
          group: DEFAULT_GROUP #默认可以不写
          refresh: true #是否开启自动刷新,默认为false,必须搭配@RefreshScope注解
        extension-configs[1]: #shared-configs是一个列表,可以添加多项
          data_id: common.yml #具体配置
          group: DEFAULT_GROUP #默认可以不写
          refresh: true #是否开启自动刷新,默认为false,必须搭配@RefreshScope注解

整体配置优先级

Spring Cloud Alibaba Nacos Config 目前提供了三种配置能力从 Nacos 拉取相关的配置。

  • A: 通过 spring.cloud.nacos.config.shared-configs[n].data-id 支持多个共享 Data Id 的配置
  • B: 通过 spring.cloud.nacos.config.extension-configs[n].data-id 的方式支持多个扩展 Data Id 的配置
  • C: 通过内部相关规则(应用名、应用名+ Profile )自动生成相关的 Data Id 配置

当三种方式共同使用时,他们的一个优先级关系是:A < B < C

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值