Search in sources :

Example 1 with ConsumerSubEvent

use of com.alipay.sofa.rpc.event.ConsumerSubEvent in project sofa-rpc by sofastack.

the class ConsumerSubTest method testSubLookout.

@Test
public void testSubLookout() {
    Registry registry = new DefaultRegistry();
    if (Lookout.registry() == NoopRegistry.INSTANCE) {
        Lookout.setRegistry(registry);
    }
    LookoutModule lookoutModule = new LookoutModule();
    Assert.assertEquals(true, lookoutModule.needLoad());
    lookoutModule.install();
    ConsumerConfig consumerConfig = new ConsumerConfig();
    consumerConfig.setInterfaceId("a");
    EventBus.post(new ConsumerSubEvent(consumerConfig));
    try {
        TimeUnit.SECONDS.sleep(5);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    RpcLookoutId rpcLookoutId = new RpcLookoutId();
    InfoWrapper result = Lookout.registry().get(rpcLookoutId.fetchConsumerSubId());
    final Object value = result.value();
    Assert.assertTrue(value instanceof ConsumerConfig);
    consumerConfig = (ConsumerConfig) value;
    Assert.assertEquals("a", consumerConfig.getInterfaceId());
}
Also used : ConsumerSubEvent(com.alipay.sofa.rpc.event.ConsumerSubEvent) DefaultRegistry(com.alipay.lookout.core.DefaultRegistry) RpcLookoutId(com.alipay.sofa.rpc.metrics.lookout.RpcLookoutId) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) NoopRegistry(com.alipay.lookout.api.NoopRegistry) Registry(com.alipay.lookout.api.Registry) DefaultRegistry(com.alipay.lookout.core.DefaultRegistry) InfoWrapper(com.alipay.lookout.core.InfoWrapper) Test(org.junit.Test)

Example 2 with ConsumerSubEvent

use of com.alipay.sofa.rpc.event.ConsumerSubEvent in project sofa-rpc by sofastack.

the class ZookeeperRegistry method subscribe.

@Override
public List<ProviderGroup> subscribe(final ConsumerConfig config) {
    String appName = config.getAppName();
    if (!registryConfig.isSubscribe()) {
        // 注册中心不订阅
        if (LOGGER.isInfoEnabled(appName)) {
            LOGGER.infoWithApp(appName, LogCodes.getLog(LogCodes.INFO_REGISTRY_IGNORE));
        }
        return null;
    }
    // 订阅如果有必要
    subscribeConsumerUrls(config);
    if (config.isSubscribe()) {
        List<ProviderInfo> matchProviders;
        // 订阅配置
        if (!INTERFACE_CONFIG_CACHE.containsKey(buildConfigPath(rootPath, config))) {
            // 订阅接口级配置
            subscribeConfig(config, config.getConfigListener());
        }
        if (!INTERFACE_OVERRIDE_CACHE.containsKey(buildOverridePath(rootPath, config))) {
            // 订阅IP级配置
            subscribeOverride(config, config.getConfigListener());
        }
        // 订阅Providers节点
        try {
            if (providerObserver == null) {
                // 初始化
                providerObserver = new ZookeeperProviderObserver();
            }
            final String providerPath = buildProviderPath(rootPath, config);
            if (LOGGER.isInfoEnabled(appName)) {
                LOGGER.infoWithApp(appName, LogCodes.getLog(LogCodes.INFO_ROUTE_REGISTRY_SUB, providerPath));
            }
            PathChildrenCache pathChildrenCache = INTERFACE_PROVIDER_CACHE.get(config);
            if (pathChildrenCache == null) {
                // 监听配置节点下 子节点增加、子节点删除、子节点Data修改事件
                ProviderInfoListener providerInfoListener = config.getProviderInfoListener();
                providerObserver.addProviderListener(config, providerInfoListener);
                // TODO 换成监听父节点变化(只是监听变化了,而不通知变化了什么,然后客户端自己来拉数据的)
                pathChildrenCache = new PathChildrenCache(zkClient, providerPath, true);
                final PathChildrenCache finalPathChildrenCache = pathChildrenCache;
                pathChildrenCache.getListenable().addListener(new PathChildrenCacheListener() {

                    @Override
                    public void childEvent(CuratorFramework client1, PathChildrenCacheEvent event) throws Exception {
                        if (LOGGER.isDebugEnabled(config.getAppName())) {
                            LOGGER.debugWithApp(config.getAppName(), "Receive zookeeper event: " + "type=[" + event.getType() + "]");
                        }
                        switch(event.getType()) {
                            case // 加了一个provider
                            CHILD_ADDED:
                                providerObserver.addProvider(config, providerPath, event.getData(), finalPathChildrenCache.getCurrentData());
                                break;
                            case // 删了一个provider
                            CHILD_REMOVED:
                                providerObserver.removeProvider(config, providerPath, event.getData(), finalPathChildrenCache.getCurrentData());
                                break;
                            case // 更新一个Provider
                            CHILD_UPDATED:
                                providerObserver.updateProvider(config, providerPath, event.getData(), finalPathChildrenCache.getCurrentData());
                                break;
                            default:
                                break;
                        }
                    }
                });
                pathChildrenCache.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);
                INTERFACE_PROVIDER_CACHE.put(config, pathChildrenCache);
            }
            List<ProviderInfo> providerInfos = ZookeeperRegistryHelper.convertUrlsToProviders(providerPath, pathChildrenCache.getCurrentData());
            matchProviders = ZookeeperRegistryHelper.matchProviderInfos(config, providerInfos);
        } catch (Exception e) {
            throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_SUB_PROVIDER, EXT_NAME), e);
        }
        if (EventBus.isEnable(ConsumerSubEvent.class)) {
            ConsumerSubEvent event = new ConsumerSubEvent(config);
            EventBus.post(event);
        }
        return Collections.singletonList(new ProviderGroup().addAll(matchProviders));
    }
    return null;
}
Also used : PathChildrenCacheListener(org.apache.curator.framework.recipes.cache.PathChildrenCacheListener) PathChildrenCacheEvent(org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent) ProviderInfoListener(com.alipay.sofa.rpc.listener.ProviderInfoListener) ConsumerSubEvent(com.alipay.sofa.rpc.event.ConsumerSubEvent) KeeperException(org.apache.zookeeper.KeeperException) SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException) CuratorFramework(org.apache.curator.framework.CuratorFramework) ProviderInfo(com.alipay.sofa.rpc.client.ProviderInfo) SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException) PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache) ProviderGroup(com.alipay.sofa.rpc.client.ProviderGroup)

Example 3 with ConsumerSubEvent

use of com.alipay.sofa.rpc.event.ConsumerSubEvent in project sofa-rpc by sofastack.

the class LocalRegistry method subscribe.

@Override
public List<ProviderGroup> subscribe(ConsumerConfig config) {
    String key = LocalRegistryHelper.buildListDataId(config, config.getProtocol());
    List<ConsumerConfig> listeners = notifyListeners.get(key);
    if (listeners == null) {
        listeners = new ArrayList<ConsumerConfig>();
        notifyListeners.put(key, listeners);
    }
    listeners.add(config);
    // 返回已经加载到内存的列表(可能不是最新的)
    ProviderGroup group = memoryCache.get(key);
    if (group == null) {
        group = new ProviderGroup();
        memoryCache.put(key, group);
    }
    if (EventBus.isEnable(ConsumerSubEvent.class)) {
        ConsumerSubEvent event = new ConsumerSubEvent(config);
        EventBus.post(event);
    }
    return Collections.singletonList(group);
}
Also used : ConsumerSubEvent(com.alipay.sofa.rpc.event.ConsumerSubEvent) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) ProviderGroup(com.alipay.sofa.rpc.client.ProviderGroup)

Example 4 with ConsumerSubEvent

use of com.alipay.sofa.rpc.event.ConsumerSubEvent in project sofa-rpc by sofastack.

the class MulticastRegistry method subscribe.

@Override
public List<ProviderGroup> subscribe(ConsumerConfig config) {
    if (!config.isSubscribe()) {
        return null;
    }
    String key = MulticastRegistryHelper.buildListDataId(config, config.getProtocol());
    List<ConsumerConfig> listeners = notifyListeners.get(key);
    if (listeners == null) {
        listeners = new ArrayList<ConsumerConfig>();
        notifyListeners.put(key, listeners);
    }
    listeners.add(config);
    multicast(SUBSCRIBE + key);
    ProviderGroup group = allProviderCache.get(key);
    if (group == null) {
        group = new ProviderGroup();
        allProviderCache.put(key, group);
    }
    if (EventBus.isEnable(ConsumerSubEvent.class)) {
        ConsumerSubEvent event = new ConsumerSubEvent(config);
        EventBus.post(event);
    }
    return Collections.singletonList(group);
}
Also used : ConsumerSubEvent(com.alipay.sofa.rpc.event.ConsumerSubEvent) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) ProviderGroup(com.alipay.sofa.rpc.client.ProviderGroup)

Example 5 with ConsumerSubEvent

use of com.alipay.sofa.rpc.event.ConsumerSubEvent 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)

Aggregations

ConsumerSubEvent (com.alipay.sofa.rpc.event.ConsumerSubEvent)8 ProviderGroup (com.alipay.sofa.rpc.client.ProviderGroup)6 ProviderInfo (com.alipay.sofa.rpc.client.ProviderInfo)4 ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)4 SofaRpcRuntimeException (com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException)3 ProviderInfoListener (com.alipay.sofa.rpc.listener.ProviderInfoListener)2 Test (org.junit.Test)2 NoopRegistry (com.alipay.lookout.api.NoopRegistry)1 Registry (com.alipay.lookout.api.Registry)1 DefaultRegistry (com.alipay.lookout.core.DefaultRegistry)1 InfoWrapper (com.alipay.lookout.core.InfoWrapper)1 ProviderConfig (com.alipay.sofa.rpc.config.ProviderConfig)1 ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)1 SofaRequest (com.alipay.sofa.rpc.core.request.SofaRequest)1 SofaResponse (com.alipay.sofa.rpc.core.response.SofaResponse)1 ClientEndInvokeEvent (com.alipay.sofa.rpc.event.ClientEndInvokeEvent)1 ProviderPubEvent (com.alipay.sofa.rpc.event.ProviderPubEvent)1 ServerSendEvent (com.alipay.sofa.rpc.event.ServerSendEvent)1 ServerStartedEvent (com.alipay.sofa.rpc.event.ServerStartedEvent)1 ServerStoppedEvent (com.alipay.sofa.rpc.event.ServerStoppedEvent)1