Nacos:配置热更新


配置自动更新

Nacos的配置文件变更后,微服务无需重启就可以感知,不过需要通过下面两种配置方式实现:

1.方式一:在@Value注入的变量所在类上添加注解@RefreshScope

@Slf4j
@RestController
@RequestMapping("/user")
@RefreshScope
public class UserController {

    @Autowired
    private UserService userService;

    @Value("${pattern.dateformat}") //Value注解 可以读取配置
    private String dateformat;

2.方式二:使用@ConfigurationProperties注解(可以新建一个类专门用来完成配置加载)

@Data
@Component
@ConfigurationProperties(prefix = "pattern")
public class PatternProperties {
    private String dateFormat;
}
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private PatternProperties patternProperties;

//    @Value("${pattern.dateformat}") //Value注解 可以读取配置
//    private String dateformat;

    @GetMapping("now")
    public  String  now(){
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern(patternProperties.getDateFormat()));
    }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM