【nacos】nacos配置中心自制工具类 获取配置dataid级全文 不使用自带注入和refresh 观察者+监听器实现

nacosConfig 自带的不满足需求 自己搞个

import cn.exrick.xboot.common.exception.XbootException;
import cn.exrick.xboot.common.utils.SpringContextUtil;
import cn.hutool.core.collection.CollUtil;
import com.alibaba.cloud.nacos.NacosConfigManager;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.AbstractSharedListener;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 获取nacosconfig
 *
 * @author Sy
 * @version 2023/9/5
 */
@Slf4j
public class NacosConfig {

    private static final long timeoutMs = 2000;
    private static final String group = "DEFAULT_GROUP";
    private static ConfigService configService;
    //通过该方法获取的nacos动态配置缓存
    private final static ConcurrentHashMap<String, String> cacheMap = new ConcurrentHashMap<String, String>();
    //观察者
    private static Map<String, NacosConfigObservable> obs = CollUtil.newHashMap();


    public static String getConfigDefault(String dataId) {
        return getConfigDefault(dataId, group);
    }

    public static String getConfigDefault(String dataId, String group) {
        return getConfig(dataId, group, timeoutMs);
    }

    public static String getConfig(String dataId, String group, long timeoutMs) {
        try {
            //非主动初始化 原因不是所有项目使用nacosconfig
            if (Objects.isNull(configService)) {
                configService = ((NacosConfigManager) SpringContextUtil.getBean("nacosConfigManager")).getConfigService();
            }
            String key = getKey(dataId, group);
            //注册监听器 若缓存中没有该条目
            //优先使用缓存 监听会更新该缓存
            String configInfo = cacheMap.get(key);
            if (!cacheMap.containsKey(key)) {
                NacosListener nacosListener = new NacosListener();
                configService.addListener(dataId, group, nacosListener);
                configInfo = configService.getConfig(dataId, group, timeoutMs);
                //防止空指针
                if(Objects.nonNull(configInfo)){
                    cacheMap.put(key, configInfo);
                }
            }
            return configInfo;
        } catch (Exception e) {
            log.error("nacosConfig 获取失败",e);
            throw new XbootException("nacosConfig 获取失败");
        }
    }

    public static class NacosListener extends AbstractSharedListener {

        @Override
        public void innerReceive(String dataId, String group, String configInfo) {
            if (StringUtils.isAnyBlank(dataId, group)) {
                return;
            }
            String key = getKey(dataId, group);
            if(Objects.nonNull(configInfo)){
                cacheMap.put(key, configInfo);
            }else{
                cacheMap.remove(key);
            }
            //通知
            notifyObs(dataId, group, configInfo);
        }

    }

    private static String getKey(String dataId, String group) {
        return dataId + "::" + group;
    }

    public static void addNacosConfigObservable(NacosConfigObservable ob) {
        addNacosConfigObservable("NacosConfigObservable" + obs.size(), ob);
    }

    public static void addNacosConfigObservable(String key, NacosConfigObservable ob) {
        if (Objects.isNull(ob)) {
            log.error("addNacosConfigObservable error key {}", key);
            throw new XbootException("addNacosConfigObservable error");
        }
        obs.put(key, ob);
    }

    public static void notifyObs(String dataId, String group, String configInfo) {
        for (String key : obs.keySet()) {
            try {
                obs.get(key).onchange(dataId, group, configInfo);
            } catch (Exception e) {
                log.error("notifyObs error ", e);
            }
        }
    }

}


你可能感兴趣的:(java,spring,spring,boot,maven)