Nacos实现配置的热更新
方式一:
通过@Value注解注入,需要在@Value注入的变量所在类上添加注解@RefreshScope
方式二:
通过添加配置类,使用@ConfigurationProperties注解来实现。
例如:
@Data
@Component
@ConfigurationProperties(prefix = "hello.gateway")
public class XxGatewayConfig {
private Integer redisAuthTimeout;
// ......
}
@Slf4j
@Component
@Order(1)
public class LoginFilter implements GlobalFilter {
@Autowired
private XxGatewayConfig xxGatewayConfig;
// ......
}
多环境配置共享:
微服务会从nacos读取的配置文件:
①[服务名]-[spring.profile.active].yaml,环境配置
②[服务名].yaml,默认配置,多环境共享
如果在不同的配置中有相同的内容,微服务读取配置文件有优先级:
[服务名]-[环境].yaml > [服务名].yaml > 本地配置