代码部分
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
dependency>
@RestController
@RequestMapping("/config")
@RefreshScope
public class ConfigController {
@Value("${yamlFiles.state}")
private String state;
@RequestMapping("/get")
public String get() {
return String.valueOf(state);
}
}
@RefreshScope:实现配置自动更新,spring cloud原生注解
nacos的配置中心的bootstrap的优先级高于application
入门使用
server:
port: 8889
# 官方dataId格式:${prefix}-${spring.profiles.active}.${file-extension}
spring:
application:
# 指定dataId的${prefix}
name: nacosConfigTest
profiles:
# 指定dataId的${spring.profiles.active}
active: dev
cloud:
nacos:
config:
server-addr: 192.168.116.1:8848
# 指定dataId的${file-extension}
file-extension: yaml
官方dataID参数说明说明:
${prefix}-${spring.profiles.active}.${file-extension}
获取路径:public命名空间–>nacosConfigTest-dev.yaml的dataId配置文件
未写spring.profiles.active的情况的路径路径: public命名空间–>nacosConfigTest.yaml的dataId配置文件*
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class NacosConfigApplication {
public static void main(String[] args) {
SpringApplication.run(NacosConfigApplication.class,args);
System.out.println("spring cloud start success!");
}
}
命名空间 及 组的使用
server:
port: 8889
# 官方dataId格式:${prefix}-${spring.profiles.active}.${file-extension}
spring:
application:
# 指定dataId的${prefix}
name: nacosConfigTest
profiles:
# 指定dataId的${spring.profiles.active}
active: dev
cloud:
nacos:
config:
server-addr: 192.168.116.1:8848
# 指定dataId的${file-extension}
file-extension: yaml
# 指定哪一个命名空间(命名空间ID:73ccb6fc-2d6e-4116-9ae4-a7f21bbb0835)
namespace: 73ccb6fc-2d6e-4116-9ae4-a7f21bbb0835
# 指定哪个组
group: DEFAULT_GROUP
获取路径:命名空间ID:73ccb6fc-2d6e-4116-9ae4-a7f21bbb0835命名空间–>DEFAULT_GROUP组–>nacosConfigTest-dev.yaml的dataId配置文件
多环境配置
@RestController
@RequestMapping("/config")
@RefreshScope
public class ConfigController {
@Value("${yamlFiles.id}")
private Integer id;
@Value("${yamlFiles.state}")
private String state;
@Value("${yamlFiles.name}")
private String name;
@Value("${yamlFiles.age}")
private Integer age;
@Value("${yamlFiles.sex}")
private Integer sex;
@RequestMapping("/get")
public String get() {
return id + "----" + name + "----" + sex + "----" + age + "----" + state;
}
}
server:
port: 8889
# 官方dataId格式:${prefix}-${spring.profiles.active}.${file-extension}
spring:
application:
# 指定dataId的${prefix}
name: nacosConfigTest
profiles:
# 指定dataId的${spring.profiles.active}
active: dev
cloud:
nacos:
config:
server-addr: 192.168.116.1:8848
# 指定dataId的${file-extension}
file-extension: yaml
# 指定哪一个命名空间(命名空间ID:73ccb6fc-2d6e-4116-9ae4-a7f21bbb0835)
namespace: 73ccb6fc-2d6e-4116-9ae4-a7f21bbb0835
# 指定哪个组
group: DEFAULT_GROUP
shared-configs[0]:
data-id: namespacetest.yaml
group: DEFAULT_GROUP
# 这个共享配置是否自动刷新
refresh: true
注:多配置可以依次类推
shared-configs[0]:
data-id: namespacetest.yaml
group: DEFAULT_GROUP
# 这个共享配置是否自动刷新
refresh: true
shared-configs[1]:
data-id: namespacetest.yaml
group: DEFAULT_GROUP
# 这个共享配置是否自动刷新
refresh: true
shared-configs[2]:
data-id: namespacetest.yaml
group: DEFAULT_GROUP
# 这个共享配置是否自动刷新
refresh: true
·······
*细节:config 的优先级 > **shared-configs[0]*的优先级
nacosConfigTest-dev.yaml 和 namespacetest.yaml 配置文件会合并
会优先使用 nacosConfigTest-dev.yaml 配置文件
如果 nacosConfigTest-dev.yaml配置文件,没有要获取得配置内容
会使用 namespacetest.yaml 的配置的内容
这就是为什么结果的id是1而不是2,因为使用了nacosConfigTest-dev.yaml的配置内容,
nacosConfigTest-dev.yaml的配置文件没有id,name,age,sex会采用namespacetest.yaml 的配置文件内容