nocas 添加配置列表
Group 可不填 默认即可
Data ID 是工程使用配置中心配置的标识
最后点击发布
导包
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-config-spring-boot-starter</artifactId>
<version>0.2.1</version>
</dependency>
controller
package com.yss.controller;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author yss
*/
@RestController
public class getMessage {
@NacosValue(value = "${message}",autoRefreshed = true)
private String message;
@GetMapping("getMessage")
public String getMessage(){
return message;
}
}
autoRefreshed 必须配置为true,在下面会有说明为什么必须配置
yml 配置
nacos:
config:
server-addr: localhost:8848
discovery:
server-addr: localhost:8848
主函数配置
package com.yss;
import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@NacosPropertySource(dataId = "yss", autoRefreshed = true)
@EnableDiscoveryClient
public class SameConfigureApplication {
public static void main(String[] args) {
SpringApplication.run(SameConfigureApplication.class, args);
}
}
其中的dataId就是nacos配置的data id
autoRefreshed 必须配置为true
这里一共配置了autoRefreshed 一个在主函数了,一个在引用配置中心的字段的地方,两个必须配置,原因是来完成热加载.