Search in sources :

Example 1 with Configurator

use of com.alipay.sofa.registry.client.api.Configurator in project sofa-rpc by sofastack.

the class SofaRegistry method subscribe.

@Override
public List<ProviderGroup> subscribe(ConsumerConfig config) {
    ProviderInfoListener providerInfoListener = config.getProviderInfoListener();
    String appName = config.getAppName();
    if (!registryConfig.isSubscribe()) {
        // 注册中心不订阅
        if (LOGGER.isInfoEnabled(appName)) {
            LOGGER.infoWithApp(appName, LogCodes.getLog(LogCodes.INFO_REGISTRY_IGNORE));
        }
        return null;
    }
    if (!config.isSubscribe()) {
        // 服务不订阅
        return null;
    }
    String serviceName = SofaRegistryHelper.buildListDataId(config, config.getProtocol());
    SofaRegistrySubscribeCallback callback;
    Subscriber listSubscriber = subscribers.get(serviceName);
    Configurator attrSubscriber;
    if (listSubscriber != null && providerInfoListener != null) {
        // 已经有人订阅过这个Key,那么地址已经存在了,
        callback = (SofaRegistrySubscribeCallback) listSubscriber.getDataObserver();
        callback.addProviderInfoListener(serviceName, config, providerInfoListener);
        // 使用旧数据通知下
        callback.handleDataToListener(serviceName, config, providerInfoListener);
    } else {
        callback = new SofaRegistrySubscribeCallback();
        callback.addProviderInfoListener(serviceName, config, providerInfoListener);
        // 生成订阅对象,并添加额外属性
        SubscriberRegistration subscriberRegistration = new SubscriberRegistration(serviceName, callback);
        String groupId = config.getParameter(SofaRegistryConstants.SOFA_GROUP_KEY);
        groupId = groupId == null ? SofaRegistryHelper.SUBSCRIBER_LIST_GROUP_ID : groupId;
        addAttributes(subscriberRegistration, groupId);
        ConfiguratorRegistration configRegistration = new ConfiguratorRegistration(serviceName, callback);
        addAttributes(configRegistration, SofaRegistryHelper.SUBSCRIBER_CONFIG_GROUP_ID);
        // 去配置中心订阅
        // 去注册
        listSubscriber = SofaRegistryClient.getRegistryClient(appName, registryConfig).register(subscriberRegistration);
        attrSubscriber = SofaRegistryClient.getRegistryClient(appName, registryConfig).register(configRegistration);
        // 放入缓存
        subscribers.put(serviceName, listSubscriber);
        configurators.put(serviceName, attrSubscriber);
    }
    // 统一走异步获取地址,所以此处返回null
    return null;
}
Also used : ProviderInfoListener(com.alipay.sofa.rpc.listener.ProviderInfoListener) Subscriber(com.alipay.sofa.registry.client.api.Subscriber) Configurator(com.alipay.sofa.registry.client.api.Configurator) ConfiguratorRegistration(com.alipay.sofa.registry.client.api.registration.ConfiguratorRegistration) SubscriberRegistration(com.alipay.sofa.registry.client.api.registration.SubscriberRegistration)

Example 2 with Configurator

use of com.alipay.sofa.registry.client.api.Configurator in project sofa-rpc by sofastack.

the class SofaRegistrySubscribeCallbackTest method handleData.

@Test
public void handleData() throws Exception {
    Subscriber listSub = new MockSubscribe(5);
    Configurator attrSub = new MockConfigurator(2);
    final AtomicInteger ps = new AtomicInteger(0);
    ProviderInfoListener listener = new ProviderInfoListener() {

        @Override
        public void addProvider(ProviderGroup providerGroup) {
            ps.addAndGet(providerGroup.size());
        }

        @Override
        public void removeProvider(ProviderGroup providerGroup) {
            ps.addAndGet(-providerGroup.size());
        }

        @Override
        public void updateProviders(ProviderGroup providerGroup) {
            ps.set(providerGroup.size());
        }

        @Override
        public void updateAllProviders(List<ProviderGroup> providerGroups) {
            ps.set(0);
            for (ProviderGroup providerGroup : providerGroups) {
                ps.addAndGet(providerGroup.size());
            }
        }
    };
    SofaRegistrySubscribeCallback callback = new SofaRegistrySubscribeCallback();
    callback.addProviderInfoListener("xxxxx", new ConsumerConfig(), listener);
    Assert.assertTrue((!callback.flag[0].get()) && (!callback.flag[1].get()));
    callback.handleData("xxxxx", buildConfigPs(2));
    try {
        Thread.sleep(200);
    } finally {
    }
    Assert.assertTrue(callback.flag[1].get());
    Assert.assertTrue(ps.get() == 0);
    callback.handleData("xxxxx", buildPs(5));
    try {
        Thread.sleep(200);
    } finally {
    }
    Assert.assertTrue(callback.flag == null);
    // default+localZone
    Assert.assertEquals(ps.get(), 5 + 5);
    callback = new SofaRegistrySubscribeCallback();
    ps.set(0);
    callback.addProviderInfoListener("yyyyy", new ConsumerConfig(), listener);
    callback.handleData("yyyyy", buildPs(5));
    try {
        Thread.sleep(200);
    } finally {
    }
    callback.handleData("yyyyy", buildConfigPs(2));
    try {
        Thread.sleep(200);
    } finally {
    }
    Assert.assertTrue(callback.flag == null);
    Assert.assertEquals(ps.get(), 5 + 5);
}
Also used : Subscriber(com.alipay.sofa.registry.client.api.Subscriber) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ProviderInfoListener(com.alipay.sofa.rpc.listener.ProviderInfoListener) Configurator(com.alipay.sofa.registry.client.api.Configurator) ProviderGroup(com.alipay.sofa.rpc.client.ProviderGroup) ArrayList(java.util.ArrayList) List(java.util.List) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) Test(org.junit.Test)

Aggregations

Configurator (com.alipay.sofa.registry.client.api.Configurator)2 Subscriber (com.alipay.sofa.registry.client.api.Subscriber)2 ProviderInfoListener (com.alipay.sofa.rpc.listener.ProviderInfoListener)2 ConfiguratorRegistration (com.alipay.sofa.registry.client.api.registration.ConfiguratorRegistration)1 SubscriberRegistration (com.alipay.sofa.registry.client.api.registration.SubscriberRegistration)1 ProviderGroup (com.alipay.sofa.rpc.client.ProviderGroup)1 ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Test (org.junit.Test)1