nacos 多环境配置
多环境配置
NacosConfigProperties:配置属性类
@ConfigurationProperties("spring.cloud.nacos.config")
public class NacosConfigProperties {
public static final String PREFIX = "spring.cloud.nacos.config";
public static final String COMMAS = ",";
public static final String SEPARATOR = "[,]";
private static final Pattern PATTERN = Pattern.compile("-(\\w)");
private static final Logger log = LoggerFactory.getLogger(NacosConfigProperties.class);
@Autowired
@JsonIgnore
private Environment environment;
private String serverAddr;
private String group = "DEFAULT_GROUP"; //分组
private String prefix; //dataId前缀
private String fileExtension = "properties"; //文件拓展格式(yaml、properties等)
/*
{
"name": "spring.cloud.nacos.config.prefix",
"type": "java.lang.String",
"defaultValue": "${spring.application.name}", //默认值:spring.application.name
"description": "the prefix of dataId, nacos config data meta info. dataId = prefix + '-' + ${spring.active.profile} + `.` + ${spring.cloud.nacos.config.file-extension}."
//dataId拼接规则:prefix + "-" + ${spring.active.profile} + "." + ${spring.cloud.nacos.config.file-extension}
},
*/
private String username;
private String password;
private String encode;
private int timeout = 3000;
private String maxRetry;
private String configLongPollTimeout;
private String configRetryTime;
private boolean enableRemoteSyncConfig = false;
private String endpoint;
private String namespace;
private String accessKey;
private String secretKey;
private String contextPath;
private String clusterName;
private String name;
private List<NacosConfigProperties.Config> sharedConfigs;
private List<NacosConfigProperties.Config> extensionConfigs;
private boolean refreshEnabled = true;
public NacosConfigProperties() {
}
多环境切换
# 配置中心:创建多dataId配置
dataId: application.properties
dataId2: application-dev.properties
datId3: application-prod.properties
# 客户端:bootstrap.yml指定读取的配置
spring:
application:
name: nacos-config
cloud:
nacos:
config:
server-addr: localhost:8848
prefix: application
profiles:
active: dev
说明:该配置会同时读取application.properties、application-dev配置
使用示例
bootstrap.yml
spring:
application:
name: nacos-config
cloud:
nacos:
config:
server-addr: localhost:8848
prefix: application
profiles:
active: dev
Person
@Data
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private String name;
private Integer age;
}
Student
@Data
@Component
@ConfigurationProperties(prefix = "student")
public class Student {
private String name;
private Integer age;
}
HelloController
@RestController
public class HelloController {
@Resource
private Person person;
@Resource
private Student student;
@RequestMapping("/hello")
public Person hello(){
System.out.println(person);
System.out.println(student);
return person;
}
}
*************
使用测试
nacos 配置中心
localhost:8080/hello,控制台输出:
2022-03-13 16:46:48.039 INFO 1275 --- [-localhost_8848] b.c.PropertySourceBootstrapConfiguration : Located property source: [BootstrapPropertySource {name='bootstrapProperties-application-dev.properties,DEFAULT_GROUP'}, BootstrapPropertySource {name='bootstrapProperties-application.properties,DEFAULT_GROUP'}, BootstrapPropertySource {name='bootstrapProperties-application,DEFAULT_GROUP'}]
2022-03-13 16:46:48.044 INFO 1275 --- [-localhost_8848] o.s.boot.SpringApplication : The following profiles are active: dev
2022-03-13 16:46:48.053 INFO 1275 --- [-localhost_8848] o.s.boot.SpringApplication : Started application in 0.2 seconds (JVM running for 563.708)
2022-03-13 16:46:48.131 INFO 1275 --- [-localhost_8848] o.s.c.e.event.RefreshEventListener : Refresh keys changed: [student.age]
2022-03-13 16:46:48.132 INFO 1275 --- [-localhost_8848] c.a.nacos.client.config.impl.CacheData : [fixed-localhost_8848] [notify-ok] dataId=application-dev.properties, group=DEFAULT_GROUP, md5=3ca75d8c7384d916b154b1f84d22d3fe, listener=com.alibaba.cloud.nacos.refresh.NacosContextRefresher$1@7588f080
2022-03-13 16:46:48.132 INFO 1275 --- [-localhost_8848] c.a.nacos.client.config.impl.CacheData : [fixed-localhost_8848] [notify-listener] time cost=304ms in ClientWorker, dataId=application-dev.properties, group=DEFAULT_GROUP, md5=3ca75d8c7384d916b154b1f84d22d3fe, listener=com.alibaba.cloud.nacos.refresh.NacosContextRefresher$1@7588f080
Person(name=瓜田李下, age=20)
Student(name=瓜田李下, age=21)