Search in sources :

Example 6 with Listener

use of com.alibaba.nacos.api.config.listener.Listener 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 7 with Listener

use of com.alibaba.nacos.api.config.listener.Listener in project anyline by anylineorg.

the class NacosUtil method config.

public String config(String group, String data, final Class<? extends AnylineConfig> T) throws NacosException {
    if (BasicUtil.isEmpty(group)) {
        group = config.GROUP;
    }
    log.warn("[nacos config][group:{}][data:{}][class:{}]", group, data, T.getName());
    final String gp = group;
    final String dt = data;
    Listener listener = new Listener() {

        @Override
        public void receiveConfigInfo(String content) {
            log.warn("[nacos reload config][group:{}][data:{}][class:{}]", gp, dt, T.getName());
            parse(T, content);
        }

        @Override
        public Executor getExecutor() {
            return null;
        }
    };
    String config = config(group, data, listener);
    parse(T, config);
    return config;
}
Also used : Listener(com.alibaba.nacos.api.config.listener.Listener)

Example 8 with Listener

use of com.alibaba.nacos.api.config.listener.Listener in project matecloud by matevip.

the class DynamicRouteInit method initRoute.

@PostConstruct
public void initRoute() {
    try {
        Properties properties = new Properties();
        properties.put(PropertyKeyConst.SERVER_ADDR, nacosProperties.getServerAddr());
        properties.put(PropertyKeyConst.USERNAME, nacosProperties.getUsername());
        properties.put(PropertyKeyConst.PASSWORD, nacosProperties.getPassword());
        properties.put(PropertyKeyConst.NAMESPACE, nacosProperties.getNamespace());
        ConfigService configService = NacosFactory.createConfigService(properties);
        String content = configService.getConfig(MateConstant.CONFIG_DATA_ID_DYNAMIC_ROUTES, nacosProperties.getGroup(), MateConstant.CONFIG_TIMEOUT_MS);
        log.info("初始化网关路由开始");
        updateRoute(content);
        log.info("初始化网关路由完成");
        // 开户监听,实现动态
        configService.addListener(MateConstant.CONFIG_DATA_ID_DYNAMIC_ROUTES, nacosProperties.getGroup(), new Listener() {

            @Override
            public void receiveConfigInfo(String configInfo) {
                log.info("更新网关路由开始");
                updateRoute(configInfo);
                log.info("更新网关路由完成");
            }

            @Override
            public Executor getExecutor() {
                return null;
            }
        });
    } catch (NacosException e) {
        log.error("加载路由出错:{}", e.getErrMsg());
    }
}
Also used : ConfigService(com.alibaba.nacos.api.config.ConfigService) Listener(com.alibaba.nacos.api.config.listener.Listener) Executor(java.util.concurrent.Executor) Properties(java.util.Properties) NacosConfigProperties(com.alibaba.cloud.nacos.NacosConfigProperties) NacosException(com.alibaba.nacos.api.exception.NacosException) PostConstruct(javax.annotation.PostConstruct)

Example 9 with Listener

use of com.alibaba.nacos.api.config.listener.Listener in project wakaka by Crysmart.

the class NacosServerApplication method withListener.

public static void withListener() throws Exception {
    Properties properties = new Properties();
    properties.put(PropertyKeyConst.SERVER_ADDR, "localhost:8848");
    properties.put(PropertyKeyConst.NAMESPACE, "bc8f6500-589a-4a46-a579-a5880a383300");
    ConfigService configService = NacosFactory.createConfigService(properties);
    configService.addListener("test.yml", "DEFAULT_GROUP", new Listener() {

        @Override
        public Executor getExecutor() {
            System.out.println("getExecutor");
            return null;
        }

        @Override
        public void receiveConfigInfo(String configInfo) {
            System.out.println(configInfo);
        }
    });
    while (true) {
        Thread.sleep(1111l);
        System.out.println("==");
    }
}
Also used : ConfigService(com.alibaba.nacos.api.config.ConfigService) Listener(com.alibaba.nacos.api.config.listener.Listener) Executor(java.util.concurrent.Executor) Properties(java.util.Properties)

Example 10 with Listener

use of com.alibaba.nacos.api.config.listener.Listener in project dynamic-threadpool by acmenlt.

the class NacosRefresherHandler method afterPropertiesSet.

@Override
public void afterPropertiesSet() throws Exception {
    Map<String, String> nacosConfig = bootstrapCoreProperties.getNacos();
    configService.addListener(nacosConfig.get("data-id"), nacosConfig.get("group"), new Listener() {

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

        @Override
        public void receiveConfigInfo(String configInfo) {
            dynamicRefresh(configInfo);
        }
    });
}
Also used : Listener(com.alibaba.nacos.api.config.listener.Listener) Executor(java.util.concurrent.Executor)

Aggregations

Listener (com.alibaba.nacos.api.config.listener.Listener)45 Executor (java.util.concurrent.Executor)22 Test (org.junit.Test)14 Properties (java.util.Properties)13 ConfigService (com.alibaba.nacos.api.config.ConfigService)11 AbstractListener (com.alibaba.nacos.api.config.listener.AbstractListener)8 NacosException (com.alibaba.nacos.api.exception.NacosException)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 ConfigFilterChainManager (com.alibaba.nacos.client.config.filter.impl.ConfigFilterChainManager)4 ConnectionEventListener (com.alibaba.nacos.common.remote.client.ConnectionEventListener)3 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 NacosConfigProperties (com.alibaba.cloud.nacos.NacosConfigProperties)2 AbstractSharedListener (com.alibaba.nacos.api.config.listener.AbstractSharedListener)2 ConfigResponse (com.alibaba.nacos.client.config.filter.impl.ConfigResponse)2 DoorGoodConfig (com.hummer.doorgod.service.domain.configuration.DoorGoodConfig)2 IOException (java.io.IOException)2 HashSet (java.util.HashSet)2 List (java.util.List)2 PostConstruct (javax.annotation.PostConstruct)2