Search in sources :

Example 6 with ProviderInfoListener

use of com.alipay.sofa.rpc.listener.ProviderInfoListener in project sofa-rpc by sofastack.

the class MeshRegistry method subscribe.

@Override
public List<ProviderGroup> subscribe(final ConsumerConfig config) {
    final ProviderInfoListener providerInfoListener = config.getProviderInfoListener();
    asyncCreateConnectionExecutor.execute(new Runnable() {

        @Override
        public void run() {
            final String appName = config.getAppName();
            registerAppInfoOnce(appName);
            SubscribeServiceRequest subscribeRequest = buildSubscribeServiceRequest(config);
            SubscribeServiceResult subscribeServiceResult = client.subscribeService(subscribeRequest);
            if (subscribeServiceResult == null || !subscribeServiceResult.isSuccess()) {
                throw new RuntimeException("regist consumer occors error," + subscribeRequest);
            }
            List<ProviderGroup> providerGroups = new ArrayList<ProviderGroup>();
            ProviderGroup providerGroup = new ProviderGroup();
            List<ProviderInfo> providerInfos = new ArrayList<ProviderInfo>();
            String url = fillProtocolAndVersion(subscribeServiceResult, client.getHost(), "", config.getProtocol());
            ProviderInfo providerInfo = SofaRegistryHelper.parseProviderInfo(url);
            providerInfos.add(providerInfo);
            providerGroup.setProviderInfos(providerInfos);
            providerGroups.add(providerGroup);
            if (EventBus.isEnable(ConsumerSubEvent.class)) {
                ConsumerSubEvent event = new ConsumerSubEvent(config);
                EventBus.post(event);
            }
            if (providerInfoListener != null) {
                providerInfoListener.updateAllProviders(providerGroups);
            }
        }
    });
    // async
    return null;
}
Also used : ProviderInfoListener(com.alipay.sofa.rpc.listener.ProviderInfoListener) ConsumerSubEvent(com.alipay.sofa.rpc.event.ConsumerSubEvent) UnSubscribeServiceRequest(com.alipay.sofa.rpc.registry.mesh.model.UnSubscribeServiceRequest) SubscribeServiceRequest(com.alipay.sofa.rpc.registry.mesh.model.SubscribeServiceRequest) ProviderInfo(com.alipay.sofa.rpc.client.ProviderInfo) ArrayList(java.util.ArrayList) List(java.util.List) ProviderGroup(com.alipay.sofa.rpc.client.ProviderGroup) SubscribeServiceResult(com.alipay.sofa.rpc.registry.mesh.model.SubscribeServiceResult)

Example 7 with ProviderInfoListener

use of com.alipay.sofa.rpc.listener.ProviderInfoListener in project sofa-rpc by sofastack.

the class NacosRegistryProviderObserver method updateProviders.

/**
 * Update providers.
 *
 * @param config the config
 * @param instances the instances
 */
void updateProviders(ConsumerConfig config, List<Instance> instances) {
    if (LOGGER.isInfoEnabled(config.getAppName())) {
        LOGGER.infoWithApp(config.getAppName(), "Receive update provider: serviceName={}, size={}, data={}", NacosRegistryHelper.buildServiceName(config, config.getProtocol()), instances.size(), instances);
    }
    List<ProviderInfoListener> providerInfoListeners = providerListenerMap.get(config);
    if (CommonUtils.isNotEmpty(providerInfoListeners)) {
        List<ProviderInfo> providerInfos = NacosRegistryHelper.convertInstancesToProviders(instances);
        List<ProviderInfo> matchProviders = RegistryUtils.matchProviderInfos(config, providerInfos);
        for (ProviderInfoListener providerInfoListener : providerInfoListeners) {
            providerInfoListener.updateAllProviders(Collections.singletonList(new ProviderGroup().addAll(matchProviders)));
        }
    }
}
Also used : ProviderInfo(com.alipay.sofa.rpc.client.ProviderInfo) ProviderInfoListener(com.alipay.sofa.rpc.listener.ProviderInfoListener) ProviderGroup(com.alipay.sofa.rpc.client.ProviderGroup)

Example 8 with ProviderInfoListener

use of com.alipay.sofa.rpc.listener.ProviderInfoListener in project sofa-rpc by sofastack.

the class MockTestRegistry method unRegister.

@Override
public void unRegister(ProviderConfig config) {
    String key = buildKey(config);
    ProviderGroup group = memoryCache.get(key);
    if (group != null) {
        List<ServerConfig> serverConfigs = config.getServer();
        if (CommonUtils.isNotEmpty(serverConfigs)) {
            for (ServerConfig server : serverConfigs) {
                group.remove(ProviderHelper.toProviderInfo(convertProviderToUrls(config, server)));
            }
        }
    }
    Map<ConsumerConfig, ProviderInfoListener> listeners = notifyListeners.get(key);
    if (listeners != null) {
        for (ProviderInfoListener listener : listeners.values()) {
            listener.updateProviders(group);
        }
    }
}
Also used : ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ProviderInfoListener(com.alipay.sofa.rpc.listener.ProviderInfoListener) ProviderGroup(com.alipay.sofa.rpc.client.ProviderGroup) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig)

Example 9 with ProviderInfoListener

use of com.alipay.sofa.rpc.listener.ProviderInfoListener in project sofa-rpc by sofastack.

the class MockTestRegistry method register.

@Override
public void register(ProviderConfig config) {
    String key = buildKey(config);
    ProviderGroup group = memoryCache.get(key);
    if (group == null) {
        group = buildProviderGroup();
        ProviderGroup old = memoryCache.putIfAbsent(key, group);
        if (old != null) {
            group = old;
        }
    }
    List<ServerConfig> serverConfigs = config.getServer();
    if (CommonUtils.isNotEmpty(serverConfigs)) {
        for (ServerConfig server : serverConfigs) {
            group.add(ProviderHelper.toProviderInfo(convertProviderToUrls(config, server)));
        }
    }
    Map<ConsumerConfig, ProviderInfoListener> listeners = notifyListeners.get(key);
    if (listeners != null) {
        for (ProviderInfoListener listener : listeners.values()) {
            listener.updateProviders(group);
        }
    }
}
Also used : ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ProviderInfoListener(com.alipay.sofa.rpc.listener.ProviderInfoListener) ProviderGroup(com.alipay.sofa.rpc.client.ProviderGroup) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig)

Example 10 with ProviderInfoListener

use of com.alipay.sofa.rpc.listener.ProviderInfoListener in project sofa-rpc by sofastack.

the class ZookeeperProviderObserver method notifyListeners.

private void notifyListeners(ConsumerConfig config, String providerPath, List<ChildData> currentData, boolean add) throws UnsupportedEncodingException {
    List<ProviderInfoListener> providerInfoListeners = providerListenerMap.get(config);
    if (CommonUtils.isNotEmpty(providerInfoListeners)) {
        List<ProviderInfo> providerInfos = ZookeeperRegistryHelper.convertUrlsToProviders(providerPath, currentData);
        List<ProviderInfo> providerInfosForProtocol = RegistryUtils.matchProviderInfos(config, providerInfos);
        for (ProviderInfoListener listener : providerInfoListeners) {
            if (add) {
                listener.addProvider(new ProviderGroup(providerInfosForProtocol));
            } else {
                listener.updateProviders(new ProviderGroup(providerInfosForProtocol));
            }
        }
    }
}
Also used : ProviderInfo(com.alipay.sofa.rpc.client.ProviderInfo) ProviderInfoListener(com.alipay.sofa.rpc.listener.ProviderInfoListener) ProviderGroup(com.alipay.sofa.rpc.client.ProviderGroup)

Aggregations

ProviderInfoListener (com.alipay.sofa.rpc.listener.ProviderInfoListener)11 ProviderGroup (com.alipay.sofa.rpc.client.ProviderGroup)10 ProviderInfo (com.alipay.sofa.rpc.client.ProviderInfo)5 ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)4 ArrayList (java.util.ArrayList)4 SofaRpcRuntimeException (com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException)3 Configurator (com.alipay.sofa.registry.client.api.Configurator)2 Subscriber (com.alipay.sofa.registry.client.api.Subscriber)2 ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)2 ConsumerSubEvent (com.alipay.sofa.rpc.event.ConsumerSubEvent)2 List (java.util.List)2 NacosException (com.alibaba.nacos.api.exception.NacosException)1 Event (com.alibaba.nacos.api.naming.listener.Event)1 EventListener (com.alibaba.nacos.api.naming.listener.EventListener)1 NamingEvent (com.alibaba.nacos.api.naming.listener.NamingEvent)1 Instance (com.alibaba.nacos.api.naming.pojo.Instance)1 ConfiguratorRegistration (com.alipay.sofa.registry.client.api.registration.ConfiguratorRegistration)1 SubscriberRegistration (com.alipay.sofa.registry.client.api.registration.SubscriberRegistration)1 RegistryConfig (com.alipay.sofa.rpc.config.RegistryConfig)1 Registry (com.alipay.sofa.rpc.registry.Registry)1