naocs多节点配置:
package com.common.security.utils;
import java.util.Map;
/**
动态获取nacos配置
@author
*/
@Component
@Data
@ConfigurationProperties(prefix = “qdm”)
public class NacosConfigUtils {
//注意这里的factoryMap要与yml里的配置名对应
private Map<String,Map<String,String>> factoryMap;
/**
根据参数获取nacos url配置,默认读当前用户选中的工厂配置
@param key 获取参数key
@param factoryKey 获取此工厂的配置
*/
public String getNanosFactoryConfig(String key, String factoryKey){
String value = Strings.EMPTY;
String factory = SecurityUtils.getLoginUser().getCurrentFactory();
if(StringUtils.isBlank(factoryKey)){
factoryKey = factory;
}
Map<String,String> paramUrl = factoryMap.get(factoryKey);
if(!CollectionUtils.isEmpty(paramUrl)){
value = paramUrl.get(key);
}
return value;
}
}
配置类和静态类分开
public class ConfigUtils
{
/**
* 获取多工厂配置
*
* @param key 缓存的键
* @param factory 厂别,不传取默认
*/
public static String getConfigByKey(String key,String factory)
{
return SpringUtils.getBean(NacosConfigUtils.class).getNanosFactoryConfig(key,factory);
}
}