Search in sources :

Example 41 with Listener

use of com.alibaba.nacos.api.config.listener.Listener in project hummer-framework by hummer-team.

the class ListenerConfig method addListener.

private void addListener(List<String> gatewayGroup, String groupId, long timeoutMillis, ConfigService configService, boolean isFirstLoading) throws NacosException {
    Set<DegradeRule> degradeRules = Sets.newConcurrentHashSet();
    Set<GatewayFlowRule> gatewayFlowRules = Sets.newConcurrentHashSet();
    Set<ApiDefinition> apiDefinitions = Sets.newConcurrentHashSet();
    for (String dataId : gatewayGroup) {
        Listener listener = getListener();
        String configVal = configService.getConfigAndSignListener(dataId, groupId, timeoutMillis, listener);
        if (!Strings.isNullOrEmpty(configVal)) {
            // refresh gateway config
            log.debug("refresh config done,config value is \n{}", configVal);
            DoorGoodConfig doorGoodConfig = JSON.parseObject(configVal, new TypeReference<DoorGoodConfig>() {
            });
            refreshGatewayRule(doorGoodConfig);
            listenerMap.put(dataId, ListenerEvent.builder().dataId(dataId).groupId(groupId).listener(listener).sentinelConfig(doorGoodConfig.getSentinelConfig()).build());
            if (isFirstLoading) {
                addSentinelConfigForInitLoad(degradeRules, gatewayFlowRules, apiDefinitions, doorGoodConfig);
            } else {
                // refresh sentinel config
                refreshSentinelConfig(doorGoodConfig);
            }
        }
    }
    firstLoadingSentinel(isFirstLoading, degradeRules, gatewayFlowRules, apiDefinitions);
}
Also used : Listener(com.alibaba.nacos.api.config.listener.Listener) DoorGoodConfig(com.hummer.doorgod.service.domain.configuration.DoorGoodConfig) ApiDefinition(com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition) GatewayFlowRule(com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule) DegradeRule(com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule)

Example 42 with Listener

use of com.alibaba.nacos.api.config.listener.Listener in project camellia by netease-im.

the class CamelliaRedisProxyNacosConfiguration method nacosConfigService.

@Bean
public NacosProxyDamicConfSupport nacosConfigService(CamelliaRedisProxyNacosProperties properties) {
    try {
        NacosProxyDamicConfSupport support = new NacosProxyDamicConfSupport();
        if (properties.isEnable()) {
            Properties nacosProps = new Properties();
            if (properties.getServerAddr() != null) {
                nacosProps.put("serverAddr", properties.getServerAddr());
            }
            Map<String, String> nacosConf = properties.getNacosConf();
            if (nacosConf != null) {
                for (Map.Entry<String, String> entry : nacosConf.entrySet()) {
                    nacosProps.put(entry.getKey(), entry.getValue());
                }
            }
            ConfigService configService = NacosFactory.createConfigService(nacosProps);
            support.setConfigService(configService);
            for (CamelliaRedisProxyNacosProperties.ConfFile confFile : properties.getConfFileList()) {
                NacosProxyDamicConfSupport.ReloadCallback reloadCallback = () -> {
                    String content = configService.getConfig(confFile.getDataId(), confFile.getGroup(), properties.getTimeoutMs());
                    updateConf(content, confFile.getFileName());
                };
                reloadCallback.reload();
                support.addReloadCallback(reloadCallback);
                configService.addListener(confFile.getDataId(), confFile.getGroup(), new Listener() {

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

                    @Override
                    public void receiveConfigInfo(String content) {
                        updateConf(content, confFile.getFileName());
                    }
                });
            }
        } else {
            logger.info("camellia redis proxy nacos not enable");
        }
        return support;
    } catch (Exception e) {
        throw new IllegalArgumentException(e);
    }
}
Also used : Listener(com.alibaba.nacos.api.config.listener.Listener) Properties(java.util.Properties) EnableConfigurationProperties(org.springframework.boot.context.properties.EnableConfigurationProperties) ConfigService(com.alibaba.nacos.api.config.ConfigService) Executor(java.util.concurrent.Executor) Map(java.util.Map) Bean(org.springframework.context.annotation.Bean)

Example 43 with Listener

use of com.alibaba.nacos.api.config.listener.Listener in project simple4h-study by simplecxm.

the class NacosDynamicRoutingConfig method refreshRouting.

@Bean
public void refreshRouting() throws NacosException {
    Properties properties = new Properties();
    properties.put(PropertyKeyConst.SERVER_ADDR, nacosServer);
    // properties.put(PropertyKeyConst.NAMESPACE, "8282c713-da90-486a-8438-2a5a212ef44f");
    ConfigService configService = NacosFactory.createConfigService(properties);
    configService.addListener(DATA_ID, GROUP, new Listener() {

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

        @Override
        public void receiveConfigInfo(String configInfo) {
            log.info(configInfo);
            boolean refreshGatewayRoute = JSONObject.parseObject(configInfo).getBoolean("refreshGatewayRoute");
            if (refreshGatewayRoute) {
                List<RouteEntity> list = JSON.parseArray(JSONObject.parseObject(configInfo).getString("routeList")).toJavaList(RouteEntity.class);
                for (RouteEntity route : list) {
                    update(assembleRouteDefinition(route));
                }
            } else {
                log.info("路由未发生变更");
            }
        }
    });
}
Also used : ConfigService(com.alibaba.nacos.api.config.ConfigService) Listener(com.alibaba.nacos.api.config.listener.Listener) Executor(java.util.concurrent.Executor) ArrayList(java.util.ArrayList) List(java.util.List) Properties(java.util.Properties) RouteEntity(com.simple4h.gateway.entity.RouteEntity) Bean(org.springframework.context.annotation.Bean)

Example 44 with Listener

use of com.alibaba.nacos.api.config.listener.Listener in project tutorials-java by Artister.

the class NacosConfigRunner method run.

@Override
public void run(ApplicationArguments args) throws Exception {
    System.out.println(String.format("Initial name=%s, age=%d", name, age));
    nacosConfigProperties.configServiceInstance().addListener("nacos-config-example.properties", "DEFAULT_GROUP", new Listener() {

        /**
         * Callback with latest config data.
         *
         * For example, config data in Nacos is:
         *
         * user.name=Nacos user.age=25
         *
         * @param configInfo latest config data for specific dataId in Nacos
         * server
         */
        @Override
        public void receiveConfigInfo(String configInfo) {
            Properties properties = new Properties();
            try {
                properties.load(new StringReader(configInfo));
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("config changed: " + properties);
        }

        @Override
        public Executor getExecutor() {
            return null;
        }
    });
}
Also used : Listener(com.alibaba.nacos.api.config.listener.Listener) Executor(java.util.concurrent.Executor) StringReader(java.io.StringReader) IOException(java.io.IOException) Properties(java.util.Properties) NacosConfigProperties(com.alibaba.cloud.nacos.NacosConfigProperties)

Example 45 with Listener

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

the class NacosCloudRefresherHandler method afterPropertiesSet.

@Override
public void afterPropertiesSet() throws Exception {
    Map<String, String> nacosConfig = bootstrapCoreProperties.getNacos();
    nacosConfigManager.getConfigService().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