Search in sources :

Example 1 with NacosConfigParams

use of com.hummer.config.bo.NacosConfigParams in project hummer-framework by hummer-team.

the class NaCosConfig method registerConfigListener.

public void registerConfigListener(boolean addListener) throws NacosException {
    NacosConfigParams params = createNacosConfigParams();
    if (params == null) {
        return;
    }
    // nacos server instance
    ConfigService configService = NacosFactory.createConfigService(params.getProperties());
    for (int i = 0; i < params.getGroupIdList().size(); i++) {
        String groupId = params.getGroupIdList().get(i);
        String dataId = i <= params.getDataIdList().size() ? params.getDataIdList().get(i) : null;
        String configType = params.getConfigTypes().get(i);
        if (Strings.isNullOrEmpty(groupId) || Strings.isNullOrEmpty(dataId)) {
            continue;
        }
        if (addListener) {
            configService.addListener(dataId, groupId, new Listener() {

                @Override
                public Executor getExecutor() {
                    return null;
                }

                @Override
                public void receiveConfigInfo(String configInfo) {
                    handleConfigChange(groupId, dataId, configType, configInfo);
                }
            });
        }
        String value = configService.getConfig(dataId, groupId, 3000);
        if (!Strings.isNullOrEmpty(value)) {
            handleConfigLoad(groupId, dataId, configType, value);
        }
    }
    // 客户端配置上传至服务端
    uploadConfig();
}
Also used : NacosConfigParams(com.hummer.config.bo.NacosConfigParams) ConfigService(com.alibaba.nacos.api.config.ConfigService) Listener(com.alibaba.nacos.api.config.listener.Listener) AbstractConfigListener(com.hummer.config.listener.AbstractConfigListener) Executor(java.util.concurrent.Executor)

Example 2 with NacosConfigParams

use of com.hummer.config.bo.NacosConfigParams in project hummer-framework by hummer-team.

the class NaCosConfig method createNacosConfigParams.

private NacosConfigParams createNacosConfigParams() {
    String dataIds = PropertiesContainer.valueOfString("nacos.config.data-ids");
    if (Strings.isNullOrEmpty(dataIds)) {
        LOGGER.warn("no setting nacos data id,PropertiesContainer nacos config will is empty.");
        return null;
    }
    String groupIds = PropertiesContainer.valueOfString("nacos.config.group");
    if (Strings.isNullOrEmpty(groupIds)) {
        LOGGER.warn("no setting nacos group id,PropertiesContainer nacos config will is empty.");
        return null;
    }
    String configType = PropertiesContainer.valueOfString("nacos.config.type");
    if (StringUtils.isEmpty(configType)) {
        configType = PropertiesContainer.valueOfString("nacos.config.types");
    }
    if (StringUtils.isEmpty(configType)) {
        return null;
    }
    String namespace = PropertiesContainer.valueOfString("nacos.config.namespace");
    NacosConfigParams configParams = new NacosConfigParams();
    String service = PropertiesContainer.valueOfStringWithAssertNotNull("nacos.config.server-addr");
    // nacos properties
    Properties properties = new Properties();
    properties.put("serverAddr", service);
    if (StringUtils.isNotBlank(namespace)) {
        properties.put("namespace", namespace);
    }
    List<String> configTypes = Splitter.on(",").splitToList(configType);
    List<String> groupIdList = Splitter.on(",").splitToList(groupIds);
    List<String> dataIdList = Splitter.on(",").splitToList(dataIds);
    AppBusinessAssert.isTrue(dataIdList.size() == groupIdList.size() && (dataIdList.size() == configTypes.size() || configTypes.size() == 1), 400, "nocos config params error0 ,dataIds , groupIds ,configTypes size not match");
    configParams.setDataIdList(dataIdList);
    configParams.setGroupIdList(groupIdList);
    configParams.setConfigTypes(configTypes);
    configParams.setProperties(properties);
    return configParams;
}
Also used : NacosConfigParams(com.hummer.config.bo.NacosConfigParams) Properties(java.util.Properties)

Aggregations

NacosConfigParams (com.hummer.config.bo.NacosConfigParams)2 ConfigService (com.alibaba.nacos.api.config.ConfigService)1 Listener (com.alibaba.nacos.api.config.listener.Listener)1 AbstractConfigListener (com.hummer.config.listener.AbstractConfigListener)1 Properties (java.util.Properties)1 Executor (java.util.concurrent.Executor)1