当前位置:网站首页>Nacos配置管理——配置热更新
Nacos配置管理——配置热更新
2022-07-19 09:12:00 【流楚丶格念】
Nacos配置热更新
我们引入Nacos配置中心的最终目的,是修改nacos中的配置后,微服务中无需重启即可让配置生效,也就是配置热更新。
要实现配置热更新,可以使用两种方式:
1.方式一
在@Value注入的变量所在类上添加注解@RefreshScope:
@Slf4j
@RestController
@RequestMapping("/user")
@RefreshScope
public class UserController {
@Value("${pattern.dateformat}")
private String dateformat;
@GetMapping("now")
public String now(){
return LocalDateTime.now().format(DateTimeFormatter.ofPattern(dateformat));
}
}
访问没有问题:
2.方式二
使用@ConfigurationProperties注解代替@Value注解。
在user-service服务中,添加一个类,读取patterrn.dateformat属性:
package com.yyl.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,然后通过成员变量的get 方法拿到这个值。
package com.yyl.user.web;
import com.yyl.user.config.PatternProperties;
import com.yyl.user.pojo.User;
import com.yyl.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.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
@Slf4j
@RestController
@RequestMapping("/user")
@RefreshScope
public class UserController {
// @Value("${pattern.dateformat}")
// private String dateformat;
@Autowired
private PatternProperties patternProperties;
@GetMapping("now")
public String now(){
// return LocalDateTime.now().format(DateTimeFormatter.ofPattern(dateformat));
return LocalDateTime.now().format(DateTimeFormatter.ofPattern(patternProperties.getDateformat()));
}
}
在进行请求也是没问题的:
边栏推荐
- Vite 入门
- 科技竞争力评估体系研究与实践
- Isn't it too much to play Gobang in idea?
- Druid 配置和監控
- Reflective Decoding: Beyond Unidirectional Generation with Off-the-Shelf Languag
- apt的学习
- C CRC verification help class
- 同花顺可靠么?请问手机开户股票开户安全吗?
- 【2022】【论文笔记】太赫兹波在非磁化等离子体中的——
- Discussion on segment tree · number of ordinary segments | number of multiplicative segments | chairman tree
猜你喜欢
Rancher安装部署及基本使用
牛津大学:许多常见失眠药物缺乏长期安全数据
Solution: the configuration and path are completely correct, but the gateway cannot be accessed
Sending the email to the following server failed.IOException while sending message问题解决
C explanation of message dialog box based on MessageBox class
DolphinScheduler
开发那些事儿:Go与C类型转换出现内存泄漏该如何解决?
使用OneDNS完美解决办公网络优化问题
Vite 入门
为什么 PSP22 对 Polkadot 生态系统很重要
随机推荐
Sending the email to the following server failed. IOException while sending message problem solving
广汽埃安新一轮引战增资正式预挂牌,资金将重点用于新产品开发、新一代电池等方面
[HDU 6095] Rikka with Competition
Output of STL Vector
Goto of C language
EVO评估工具的使用
Druid configuration and monitoring
解决:配置、路径完全没错 gateway就是访问不了
从工程师到技术leader思维升级
Part 14: can bus communication of stm32
.NET 7 新增速率限制 (Rate Limiting) 功能,轻松限制请求数量
Shuttle + alluxio accelerated memory shuffle take-off
Docker安装常用软件-Redis
SLT 定义vetctor对象,vector构造函数
为什么独立站卖家喜欢做电子邮件营销?原来转化率能提高这么多!
NFT game interoperability: technology is not a roadblock
LED light of 51 single chip microcomputer
【延期公告】2022年网络与信息安全国际会议(NISecurity 2022)
JUC_synchronized关键字详解
浏览器调试