Here is achieved by namespace method, the other did not succeed.
Add dependent
<dependency> <groupId>com.alibaba.boot</groupId> <artifactId>nacos-config-spring-boot-starter</artifactId> </dependency>
Add Namespace
Create several configuration files, such as
application-dev.yaml
andapplication-test.yam
, which specify a different namespaceserver: port: 8081 nacos: config: server-addr: 127.0.0.1:8848 namespace: 5c3638e7-ca2c-46af-b47b-67b009c14fa1
Start time of the specified active profile.
The code will be able to call
@SpringBootApplication @RestController //@NacosPropertySource(dataId = "lou-nacos-demo-config",autoRefreshed = true) //这里不用指定上面source public class LouNacosDemo1App { public static void main(String[] args) { SpringApplication.run(LouNacosDemo1App.class,args); } @NacosValue("${userName:aaa}") private String userName; @GetMapping("hello") public String hello(){ return "hello world"; } @GetMapping("userName") public String getUserName(){ return userName; } }