目录
一、nacos配置中心简介
二、nacos配置实时更新及同一个微服务不同环境的差异化配置
准备工作
针对商品微服务实现实时更新(以商品微服务为例)
三、nacos同一个微服务不同环境的共享配置
同一个微服务修改配置才能访问不同环境
四、nacos不同微服务的共享配置
bootstrap.yml
五、nacos的命名空间&组
作用:nacos配置中心为了帮助解决配置文件反复修改的问题
首先我们来看一下,微服务架构下关于配置文件的一些问题:
配置文件相对分散。在一个微服务架构下,配置文件会随着微服务的增多变的越来越多,而且分散 在各个微服务中,不好统一配置和管理。
配置文件无法区分环境。微服务项目可能会有多个环境,例如:测试环境、预发布环境、生产环 境。每一个环境所使用的配置理论上都是不同的,一旦需要修改,就需要我们去各个微服务下手动 维护,这比较困难。
配置文件无法实时更新。我们修改了配置文件之后,必须重新启动微服务才能使配置生效,这对一 个正在运行的项目来说是非常不友好的。 基于上面这些问题,我们就需要配置中心的加入来解决这些问题。
配置中心的思路是:
首先把项目中各种配置全部都放到一个集中的地方进行统一管理,并提供一套标准的接口。
当各个服务需要获取配置的时候,就来配置中心的接口拉取自己的配置。
当配置中心中的各种参数有更新的时候,也能通知到各个服务实时的过来同步最新的信息,使之动 态更新。
启动nacos
在微服务公共模块中引入nacos的依赖
注:由于配置中心的依赖在多个微服务中都需要引入,所以此处建议将其加入到common公共模块中。
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-config
spcloud-shop com.cdl 1.0-SNAPSHOT 4.0.0 shop-common
org.projectlombok lombok com.alibaba fastjson 1.2.56 mysql mysql-connector-java 5.1.44 com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery com.alibaba.cloud spring-cloud-starter-alibaba-nacos-config org.springframework.cloud spring-cloud-starter-openfeign
访问一下nacos
不能使用原来的application.yml作为配置文件,而是新建一个bootstrap.yml作为配置文件
配置文件优先级(由高到低):
bootstrap.properties -> bootstrap.yml -> application.properties -> application.ym
bootstrap.yml
spring:application:name: shop-productcloud:nacos:config:server-addr: localhost:8848 # nacos的服务端地址file-extension: yml # 配置文件格式profiles:active: dev # 环境标识
在nacos中添加配置
示例:
实操:
新建配置成功
我们实现了配置的远程存放,但是此时如果修改了配置,我们的程序是无法读取到 的,因此,我们需要开启配置的动态刷新功能。
方式一:硬编码方式
NacosConfigController
package com.cdl.shopproduct.Controller;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/nacos-config")
public class NacosConfigController {@Autowiredprivate ConfigurableApplicationContext applicationContext;//1 硬编码方式@GetMapping("/test1")public String nacosConfingTest1() {return applicationContext.getEnvironment().getProperty("config.appName");}
}
运行项目 访问测试代码
读的内容是配置中心的
测试一下实时效果,修改配置中心
访问页面刷新
由此可见,实现了可实时更新(不需要重启项目就能更新)
方式二: 注解方式(推荐)
NacosConfigController
@RefreshScope//只需要在需要动态读取配置的类上添加此注解就可以
package com.cdl.shopproduct.Controller;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/nacos-config")
@RefreshScope
public class NacosConfigController {@Autowiredprivate ConfigurableApplicationContext applicationContext;//1 硬编码方式@GetMapping("/test1")public String nacosConfingTest1() {return applicationContext.getEnvironment().getProperty("config.appName");}@Value("${config.appName}")private String appName;//2 注解方式@GetMapping("/test2")public String nacosConfingTest2() {return appName;}}
修改bootstrap.yml
同一个微服务的不同环境之间共享配置
不管切哪个环境访问的都是一个配置
可对应这种情况
共享配置文件的名字必须和spring的应用名(bootstrap.yml 中的名字)一致
新建一个共享配置
增加测试的代码
NacosConfigController
package com.cdl.shopproduct.Controller;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/nacos-config")
@RefreshScope
public class NacosConfigController {@Autowiredprivate ConfigurableApplicationContext applicationContext;//1 硬编码方式@GetMapping("/test1")public String nacosConfingTest1() {return applicationContext.getEnvironment().getProperty("config.appName");}@Value("${config.appName}")private String appName;//2 注解方式@GetMapping("/test2")public String nacosConfingTest2() {return appName;}//3 读取shop-product所有环境共享配置@Value("${config.os}")private String os;@RequestMapping("/test3")public String nacosConfingTest3() {return os;}}
此时
修改bootstrap.yml为dev
读取bootstrap.yml的信息
读取共享配置
覆盖配置:
注意:此时的共享配置只针对于商品微服务
对应这种情况
将三个微服务共有的配置信息加到公有的配置文件中去
这个共享配置的文件名可以随便取
还是以商品微服务为例子,读取公共微服务的配置信息,此时修改bootstrap.yml
以前是这样子加的
显示已经过期了的,不推荐使用
spring:application:name: shop-productcloud:nacos:config:server-addr: localhost:8848 # nacos的服务端地址file-extension: yml # 配置文件格式
# shared-dataids: all-service.yml # 配置要引入的配置
# refreshable-dataids: all-service.yml # 配置要实现动态配置刷新的配置extension-configs[0]:data-id: all-service.ymlgroup: DEFAULT_GROUPrefresh: trueshared-configs[0]:data-id: all-service.ymlgroup: DEFAULT_GROUPrefresh: trueprofiles:active: dev # 环境标识
写测试代码
NacosConfigController
package com.cdl.shopproduct.Controller;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/nacos-config")
@RefreshScope
public class NacosConfigController {@Autowiredprivate ConfigurableApplicationContext applicationContext;//1 硬编码方式@GetMapping("/test1")public String nacosConfingTest1() {return applicationContext.getEnvironment().getProperty("config.appName");}@Value("${config.appName}")private String appName;//2 注解方式@GetMapping("/test2")public String nacosConfingTest2() {return appName;}//3 读取shop-product所有环境共享配置@Value("${config.os}")private String os;@RequestMapping("/test3")public String nacosConfingTest3() {return os;}//4 读取不同环境共享配置@Value("${config.rabbitMQ}")private String ip;@RequestMapping("/test4")public String nacosConfingTest4() {return ip;}}
nacos的几个概念
命名空间(Namespace) 命名空间可用于进行不同环境的配置隔离。一般一个环境划分到一个命名空间
配置分组(Group) 配置分组用于将不同的服务可以归类到同一分组。一般将一个项目的配置分到一组
配置集(Data ID) 在系统中,一个配置文件通常就是一个配置集。一般微服务的配置就是一个配置集
新建命名空间
新建成功
将public中的克隆一份到test中
克隆成功
要访问到修改后的166的 修改bootstrap.yml文件
重启项目
进行分组
成功
修改bootstap.yml
启动项目
此时会报错
只需补全
成功
上一篇:【Linux】权限讲解
下一篇:沉睡者IT - 什么是NFT?