兰州北京网站建设,网络广告推广网站,网站客户续费,wordpress建多个网站文章目录 Nacos配置管理统一配置管理在nacos中添加配置文件从微服务拉取配置 配置热更新方式一方式二 配置共享1#xff09;添加一个环境共享配置2#xff09;在user-service中读取共享配置3#xff09;运行两个UserApplication#xff0c;使用不同的profile3#xff09;运… 文章目录 Nacos配置管理统一配置管理在nacos中添加配置文件从微服务拉取配置 配置热更新方式一方式二 配置共享1添加一个环境共享配置2在user-service中读取共享配置3运行两个UserApplication使用不同的profile3运行两个UserApplication使用不同的profile4配置共享的优先级 Nacos配置管理
Nacos除了可以做注册中心同样可以做配置管理来使用。
统一配置管理
微服务部署的实例越来越多达到数十数百时逐个修改微服务配置就会让人抓狂而且容易出错。我们需要一种统一配置管理方案可以集中管理所有实例的配置。 Nacos一方面可以将配置集中管理另一方面可以在配置变更时及时通知微服务实现配置的热更新。
在nacos中添加配置文件
如何在nacos中管理配置呢 然后在弹出的表单中填写配置信息 注意项目的核心配置需要热更新的配置才有放到nacos管理的必要。基本不会变更的一些配置还是保存在微服务本地比较好。 从微服务拉取配置
微服务要拉取nacos中管理的配置并且与本地的application.yml配置合并才能完成项目启动。
但如果尚未读取application.yml又如何得知nacos地址呢
因此spring引入了一种新的配置文件bootstrap.yaml文件会在application.yml之前被读取流程如下 1引入nacos-config依赖
首先在user-service服务中引入nacos-config的客户端依赖
!--nacos配置管理依赖--
dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-nacos-config/artifactId
/dependency2添加bootstrap.yaml
然后在user-service中添加一个bootstrap.yaml文件内容如下
spring:application:name: userservice # 服务名称profiles:active: dev #开发环境这里是dev cloud:nacos:server-addr: localhost:8848 # Nacos地址config:file-extension: yaml # 文件后缀名这里会根据spring.cloud.nacos.server-addr获取nacos地址再根据 ${spring.application.name}-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}作为文件id来读取配置。
本例中就是去读取userservice-dev.yaml 3读取nacos配置
在user-service中的UserController中添加业务逻辑读取pattern.dateformat配置 完整代码
package cn.itcast.user.web;import cn.itcast.user.pojo.User;
import cn.itcast.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;Slf4j
RestController
RequestMapping(/user)
public class UserController {Autowiredprivate UserService userService;Value(${pattern.dateformat})private String dateformat;GetMapping(now)public String now(){return LocalDateTime.now().format(DateTimeFormatter.ofPattern(dateformat));}// ...略}在页面访问可以看到效果
配置热更新
我们最终的目的是修改nacos中的配置后微服务中无需重启即可让配置生效也就是配置热更新。
要实现配置热更新可以使用两种方式
方式一
在Value注入的变量所在类上添加注解RefreshScope
方式二
使用ConfigurationProperties注解代替Value注解。 在user-service服务中添加一个类读取patterrn.dateformat属性
package cn.itcast.user.config;import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;Component
Data
ConfigurationProperties(prefix pattern)
public class PatternProperties {private String dateformat;
}在UserController中使用这个类代替Value 完整代码
package cn.itcast.user.web;import cn.itcast.user.config.PatternProperties;
import cn.itcast.user.pojo.User;
import cn.itcast.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;Slf4j
RestController
RequestMapping(/user)
public class UserController {Autowiredprivate UserService userService;Autowiredprivate PatternProperties patternProperties;GetMapping(now)public String now(){return LocalDateTime.now().format(DateTimeFormatter.ofPattern(patternProperties.getDateformat()));}// 略
}配置共享
其实微服务启动时会去nacos读取多个配置文件例如 [spring.application.name]-[spring.profiles.active].yaml例如userservice-dev.yaml [spring.application.name].yaml例如userservice.yaml
而[spring.application.name].yaml不包含环境因此可以被多个环境共享。
下面我们通过案例来测试配置共享
1添加一个环境共享配置
我们在nacos中添加一个userservice.yaml文件
2在user-service中读取共享配置
在user-service服务中修改PatternProperties类读取新添加的属性 在user-service服务中修改UserController添加一个方法
3运行两个UserApplication使用不同的profile
修改UserApplication2这个启动项改变其profile值 在user-service服务中修改UserController添加一个方法
3运行两个UserApplication使用不同的profile
修改UserApplication2这个启动项改变其profile值 这样UserApplication(8081)使用的profile是devUserApplication2(8082)使用的profile是test。
启动UserApplication和UserApplication2
访问http://localhost:8081/user/prop结果 访问http://localhost:8082/user/prop结果 可以看出来不管是dev还是test环境都读取到了envSharedValue这个属性的值。
4配置共享的优先级
当nacos、服务本地同时出现相同属性时优先级有高低之分