1.启动类注解
@NacosPropertySource(dataId = demo.yml", autoRefreshed = true, type = ConfigType.YAML)
这里的autoRefreshed = true要开着
2.@Component 和 @RefreshScope要一起使用在类上
3.在属性上把@Value换成@NacosValue,同时需要打开autoRefreshed = true属性
参考代码
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableCaching
@EnableAsync
@EnableScheduling
@NacosPropertySource(dataId = "${spring.application.name}-${spring.profiles.active}.yml", autoRefreshed = true, type = ConfigType.YAML)
public class ResourceApplication {
public static void main(String[] args) {
SpringApplication.run(ResourceApplication.class, args);
}
}
@Data
@Component
@RefreshScope
public class Demo {
@NacosValue(value = "${demo.url}",autoRefreshed = true)
private String url;
}