nacos–基础–4.2–集成–SpringBoot–Config模块使用
代码位置
https://gitee.com/DanShenGuiZu/learnDemo/tree/master/nacos-learn/nacos-spring-boot
1、支持多种配置格式
- Nacos Spring Boot 底层是 Nacos Spring,支持yaml等格式
- 请参看 nacos–基础–集成–1.2–spring–Config模块使用 多配置文件支持
2、支持@ConditionalOnProperty的spring条件注解功能
如果想使用spring-boot的条件注解@ConditionXXX功能、@value注解
2.1、使用方法
# 开启配置预加载功能
nacos.config.bootstrap.enable=true
# 主配置服务器地址
nacos.config.server-addr=192.168.16.104:8848
# 主配置 data-id
nacos.config.data-id=people
# 主配置 group-id
nacos.config.group=DEFAULT_GROUP
# 主配置 配置文件类型
nacos.config.type=properties
# 主配置 最大重试次数
nacos.config.max-retry=10
# 主配置 开启自动刷新
nacos.config.auto-refresh=true
# 主配置 重试时间
nacos.config.config-retry-time=2333
# 主配置 配置监听长轮询超时时间
nacos.config.config-long-poll-timeout=46000
# 主配置 开启注册监听器预加载配置服务(除非特殊业务需求,否则不推荐打开该参数)
nacos.config.enable-remote-sync-config=true
# ext-config[index] 的优先级,index越小,优先级越高,从0开始
nacos.config.ext-config[0].data-id=test
nacos.config.ext-config[0].group=DEFAULT_GROUP
nacos.config.ext-config[0].max-retry=10
nacos.config.ext-config[0].type=yaml
nacos.config.ext-config[0].auto-refresh=true
nacos.config.ext-config[0].config-retry-time=2333
nacos.config.ext-config[0].config-long-poll-timeout=46000
nacos.config.ext-config[0].enable-remote-sync-config=true
2.2、案例
https://github.com/nacos-group/nacos-spring-boot-project/wiki/spring-boot-0.2.2-%E4%BB%A5%E5%8F%8A-0.1.2%E7%89%88%E6%9C%AC%E6%96%B0%E5%8A%9F%E8%83%BD%E4%BD%BF%E7%94%A8%E6%89%8B%E5%86%8C
https://github.com/nacos-group/nacos-spring-boot-project/tree/master/nacos-spring-boot-samples/nacos-config-sample
3、支持日志级别的加载时机
nacos.config.bootstrap.log.enable=true
如果需要集成dubbo,请使用此配置加载时机
4、支持spring的各种配置信息中读取nacos配置
4.1、案例
package com.alibaba.boot.nacos.config.util;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;
/**
* @author <a href="mailto:liaochunyhm@live.com">liaochuntao</a>
* @since 0.2.3
*/
public class AttributeExtractTask implements Callable<Map<String, String>> {
private final String prefix;
private final ConfigurableEnvironment environment;
public AttributeExtractTask(String prefix, ConfigurableEnvironment environment) {
this.prefix = prefix;
this.environment = environment;
}
@Override
public Map<String, String> call() throws Exception {
List<Map<String, String>> defer = new LinkedList<>();
MutablePropertySources mutablePropertySources = environment.getPropertySources();
for (PropertySource propertySource : mutablePropertySources) {
calculate(propertySource.getSource(), defer);
}
Map<String, String> result = new HashMap<>(32);
Collections.reverse(defer);
for (Map<String, String> item : defer) {
result.putAll(item);
}
return result;
}
private void calculate(Object source, List<Map<String, String>> defer) {
if (source instanceof PropertySource) {
calculate(((PropertySource) source).getSource(), defer);
}
if (source instanceof Map) {
Map<String, String> map = new HashMap<>(8);
for (Object entry : ((Map) source).entrySet()) {
Map.Entry<Object, Object> element = (Map.Entry<Object, Object>) entry;
String key = String.valueOf(element.getKey());
if (key.startsWith(prefix)) {
map.put(key, String.valueOf(element.getValue()));
}
}
if (!map.isEmpty()) {
defer.add(map);
}
}
if (source instanceof List || source instanceof Set) {
Collection sources = (Collection) source;
for (Object obj : sources) {
calculate(obj, defer);
}
}
}
}
4、支持配置data-ids的设置方式
# 主配置 data-id
nacos.config.data-ids=people,test
# 主配置 group-id
nacos.config.group=DEVELOP
# 主配置 配置文件类型
nacos.config.type=properties
# 主配置 开启自动刷新
nacos.config.auto-refresh=true
# 主配置 最大重试次数
nacos.config.max-retry=10
# 主配置 重试时间
nacos.config.config-retry-time=2333
# 主配置 配置监听长轮询超时时间
nacos.config.config-long-poll-timeout=46000
# 主配置 开启注册监听器预加载配置服务(除非特殊业务需求,否则不推荐打开该参数)
nacos.config.enable-remote-sync-config=true
5、权限控制功能
nacos.config.username=nacos
nacos.config.password=nacos
6、允许nacos上的配置优先于本地配置
consul 就是consul配置优先于本地配置
nacos.config.remote-first=true