Search in sources :

Example 36 with Listener

use of com.alibaba.nacos.api.config.listener.Listener in project spring-cloud-alibaba by alibaba.

the class NacosContextRefresher method registerNacosListener.

private void registerNacosListener(final String groupKey, final String dataKey) {
    String key = NacosPropertySourceRepository.getMapKey(dataKey, groupKey);
    Listener listener = listenerMap.computeIfAbsent(key, lst -> new AbstractSharedListener() {

        @Override
        public void innerReceive(String dataId, String group, String configInfo) {
            refreshCountIncrement();
            nacosRefreshHistory.addRefreshRecord(dataId, group, configInfo);
            // todo feature: support single refresh for listening
            applicationContext.publishEvent(new RefreshEvent(this, null, "Refresh Nacos config"));
            if (log.isDebugEnabled()) {
                log.debug(String.format("Refresh Nacos config group=%s,dataId=%s,configInfo=%s", group, dataId, configInfo));
            }
        }
    });
    try {
        configService.addListener(dataKey, groupKey, listener);
    } catch (NacosException e) {
        log.warn(String.format("register fail for nacos listener ,dataId=[%s],group=[%s]", dataKey, groupKey), e);
    }
}
Also used : AbstractSharedListener(com.alibaba.nacos.api.config.listener.AbstractSharedListener) ApplicationListener(org.springframework.context.ApplicationListener) AbstractSharedListener(com.alibaba.nacos.api.config.listener.AbstractSharedListener) Listener(com.alibaba.nacos.api.config.listener.Listener) RefreshEvent(org.springframework.cloud.endpoint.event.RefreshEvent) NacosException(com.alibaba.nacos.api.exception.NacosException)

Example 37 with Listener

use of com.alibaba.nacos.api.config.listener.Listener in project incubator-shenyu by apache.

the class NacosCacheHandler method watcherData.

protected void watcherData(final String dataId, final OnChange oc) {
    Listener listener = new Listener() {

        @Override
        public void receiveConfigInfo(final String configInfo) {
            oc.change(configInfo);
        }

        @Override
        public Executor getExecutor() {
            return null;
        }
    };
    oc.change(getConfigAndSignListener(dataId, listener));
    LISTENERS.computeIfAbsent(dataId, key -> new ArrayList<>()).add(listener);
}
Also used : JsonParseException(com.google.gson.JsonParseException) LoggerFactory(org.slf4j.LoggerFactory) GsonUtils(org.apache.shenyu.common.utils.GsonUtils) ArrayList(java.util.ArrayList) MetaDataSubscriber(org.apache.shenyu.sync.data.api.MetaDataSubscriber) Map(java.util.Map) NacosException(com.alibaba.nacos.api.exception.NacosException) PluginData(org.apache.shenyu.common.dto.PluginData) PluginDataSubscriber(org.apache.shenyu.sync.data.api.PluginDataSubscriber) Listener(com.alibaba.nacos.api.config.listener.Listener) SelectorData(org.apache.shenyu.common.dto.SelectorData) ConfigService(com.alibaba.nacos.api.config.ConfigService) AppAuthData(org.apache.shenyu.common.dto.AppAuthData) Logger(org.slf4j.Logger) Executor(java.util.concurrent.Executor) Collection(java.util.Collection) RuleData(org.apache.shenyu.common.dto.RuleData) NacosPathConstants(org.apache.shenyu.common.constant.NacosPathConstants) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) AuthDataSubscriber(org.apache.shenyu.sync.data.api.AuthDataSubscriber) List(java.util.List) Optional(java.util.Optional) MetaData(org.apache.shenyu.common.dto.MetaData) Listener(com.alibaba.nacos.api.config.listener.Listener) ArrayList(java.util.ArrayList)

Example 38 with Listener

use of com.alibaba.nacos.api.config.listener.Listener in project solon by noear.

the class CloudConfigServiceNacosImp method attention.

/**
 * 关注配置
 */
@Override
public void attention(String group, String key, CloudConfigHandler observer) {
    if (observerMap.containsKey(observer)) {
        return;
    }
    if (Utils.isEmpty(group)) {
        group = Solon.cfg().appGroup();
    }
    CloudConfigObserverEntity entity = new CloudConfigObserverEntity(group, key, observer);
    observerMap.put(observer, entity);
    try {
        group = groupReview(group);
        real.addListener(key, group, new Listener() {

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

            @Override
            public void receiveConfigInfo(String value) {
                entity.handle(new Config(entity.group, entity.key, value, 0));
            }
        });
    } catch (NacosException ex) {
        throw new RuntimeException(ex);
    }
}
Also used : CloudConfigObserverEntity(org.noear.solon.cloud.service.CloudConfigObserverEntity) Listener(com.alibaba.nacos.api.config.listener.Listener) Executor(java.util.concurrent.Executor) Config(org.noear.solon.cloud.model.Config) NacosException(com.alibaba.nacos.api.exception.NacosException)

Example 39 with Listener

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

the class GatewayContextInit method loadNacosConfig.

private void loadNacosConfig() throws NacosException, IOException {
    Properties properties = getConfigServerProperties();
    properties.setProperty("namespace", "bumblebee-service-panli-com");
    String groupId = PropertiesContainer.valueOfString("config.center.gateway.group.id", "DEFAULT_GROUP");
    long timeoutMillis = PropertiesContainer.valueOf("config.center.gateway.timeout.millis", Long.class, 5000L);
    // load all gateway group config
    ConfigService configService = NacosFactory.createConfigService(properties);
    String val = configService.getConfigAndSignListener(getDataIdByEvn(), groupId, timeoutMillis, new Listener() {

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

        @Override
        public void receiveConfigInfo(String configInfo) {
            try {
                fillByProperties(configInfo, getDataIdByEvn(), groupId);
                registerSystemProperty();
                registerFlowClusterClientConfig();
                registerSentinelSystemRuleConfig();
            } catch (IOException e) {
                log.warn("reset properties to PropertiesContainer failed", e);
            // ignore
            }
        }
    });
    fillByProperties(val, getDataIdByEvn(), groupId);
}
Also used : ConfigService(com.alibaba.nacos.api.config.ConfigService) Listener(com.alibaba.nacos.api.config.listener.Listener) Executor(java.util.concurrent.Executor) IOException(java.io.IOException) Properties(java.util.Properties)

Example 40 with Listener

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

the class ListenerConfig method getListener.

private Listener getListener() {
    return new Listener() {

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

        @Override
        public void receiveConfigInfo(String configInfo) {
            if (Strings.isNullOrEmpty(configInfo)) {
                return;
            }
            log.debug("refresh config done,config value is \n{}", configInfo);
            // parse config instance
            DoorGoodConfig doorGoodConfig = JSON.parseObject(configInfo, new TypeReference<DoorGoodConfig>() {
            });
            // refresh gateway config
            refreshGatewayRule(doorGoodConfig);
            // refresh sentinel config
            refreshSentinelConfig(doorGoodConfig);
        }
    };
}
Also used : Listener(com.alibaba.nacos.api.config.listener.Listener) DoorGoodConfig(com.hummer.doorgod.service.domain.configuration.DoorGoodConfig)

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