rt
@RestController @RefreshScope public class EchoController {
@Value("${useLocalCache:cc}")
private String useLocalCache;
@RequestMapping("/get")
@ResponseBody
public String get() {
return useLocalCache;
}
public void setUseLocalCache(String useLocalCache) {
this.useLocalCache = useLocalCache;
}
}
@SpringBootApplication @Slf4j public class NacosApplication { public static void main(String[] args) { ConfigurableApplicationContext applicationContext = SpringApplication.run(NacosApplication.class); // while (true) { // //当动态配置刷新时,会更新到 Enviroment中,因此这里每隔一秒中从Enviroment中获取配置 // String userName = applicationContext.getEnvironment().getProperty("name"); // String userAge = applicationContext.getEnvironment().getProperty("gg"); // log.info("配置信息,name:{},gg:{}", userName, userAge); // try { // TimeUnit.SECONDS.sleep(1); // } catch (InterruptedException e) { // e.printStackTrace(); // } // } } }
spring.application.name=nacos spring.cloud.nacos.config.server-addr=127.0.0.1:8848
spring.application.name=nacos spring.cloud.nacos.config.server-addr=127.0.0.1:8848
cloud top.huzhurong.alibaba 1.0-SNAPSHOT
4.0.0
nacos-producer
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version>
org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-web
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
org.springframework.boot spring-boot-maven-plugin ${spring-boot.version} repackage top.huzhurong.cloud.nacos.discovery.NacosApplication
parent#pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>top.huzhurong.alibaba</groupId>
<artifactId>cloud</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>nacos-producer</module>
<module>nacos-consumer</module>
</modules>
<properties>
<spring-boot.version>2.0.4.RELEASE</spring-boot.version>
<spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>0.2.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<target>8</target>
<source>8</source>
<encoding>utf-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
无法刷新配置:)@mercyblitz
原提问者GitHub用户chenshun00
参考https://github.com/spring-cloud-incubator/spring-cloud-alibaba/wiki/Nacos-config,请确保bootstrap.yml具有正确的配置。
原回答者GitHub用户fangjian0423
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
阿里云拥有国内全面的云原生产品技术以及大规模的云原生应用实践,通过全面容器化、核心技术互联网化、应用 Serverless 化三大范式,助力制造业企业高效上云,实现系统稳定、应用敏捷智能。拥抱云原生,让创新无处不在。