Search in sources :

Example 1 with SubscriberRegistration

use of com.alipay.sofa.registry.client.api.registration.SubscriberRegistration 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 SubscriberRegistration

use of com.alipay.sofa.registry.client.api.registration.SubscriberRegistration in project dubbo by alibaba.

the class SofaRegistryServiceDiscovery method registerServiceWatcher.

protected void registerServiceWatcher(String serviceName, ServiceInstancesChangedListener listener) {
    Subscriber subscriber = subscribers.get(serviceName);
    if (null == subscriber) {
        final CountDownLatch latch = new CountDownLatch(1);
        SubscriberRegistration subscriberRegistration = new SubscriberRegistration(serviceName, (dataId, data) -> {
            handleRegistryData(dataId, data, listener, latch);
        });
        subscriberRegistration.setGroup(DEFAULT_GROUP);
        subscriberRegistration.setScopeEnum(ScopeEnum.global);
        subscriber = registryClient.register(subscriberRegistration);
        subscribers.put(serviceName, subscriber);
        waitAddress(serviceName, latch);
    }
}
Also used : Subscriber(com.alipay.sofa.registry.client.api.Subscriber) CountDownLatch(java.util.concurrent.CountDownLatch) SubscriberRegistration(com.alipay.sofa.registry.client.api.registration.SubscriberRegistration)

Example 3 with SubscriberRegistration

use of com.alipay.sofa.registry.client.api.registration.SubscriberRegistration in project seata by seata.

the class SofaRegistryServiceImpl method subscribe.

@Override
public void subscribe(String cluster, SubscriberDataObserver listener) throws Exception {
    SubscriberRegistration subscriberRegistration = new SubscriberRegistration(cluster, listener);
    subscriberRegistration.setScopeEnum(ScopeEnum.global);
    subscriberRegistration.setGroup(registryProps.getProperty(PRO_GROUP_KEY));
    LISTENER_SERVICE_MAP.computeIfAbsent(cluster, key -> new ArrayList<>()).add(listener);
    getRegistryInstance().register(subscriberRegistration);
}
Also used : DefaultRegistryClientConfigBuilder(com.alipay.sofa.registry.client.provider.DefaultRegistryClientConfigBuilder) StringUtils(org.apache.commons.lang.StringUtils) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) NetUtil(io.seata.common.util.NetUtil) RegistryService(io.seata.discovery.registry.RegistryService) RegistryClient(com.alipay.sofa.registry.client.api.RegistryClient) RegistryType(com.alipay.sofa.registry.client.api.model.RegistryType) Configuration(io.seata.config.Configuration) Properties(java.util.Properties) RegistryClientConfig(com.alipay.sofa.registry.client.api.RegistryClientConfig) ScopeEnum(com.alipay.sofa.registry.core.model.ScopeEnum) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) InetSocketAddress(java.net.InetSocketAddress) FILE_CONFIG_SPLIT_CHAR(io.seata.config.ConfigurationKeys.FILE_CONFIG_SPLIT_CHAR) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) DefaultRegistryClient(com.alipay.sofa.registry.client.provider.DefaultRegistryClient) List(java.util.List) SubscriberDataObserver(com.alipay.sofa.registry.client.api.SubscriberDataObserver) SubscriberRegistration(com.alipay.sofa.registry.client.api.registration.SubscriberRegistration) FILE_ROOT_REGISTRY(io.seata.config.ConfigurationKeys.FILE_ROOT_REGISTRY) PublisherRegistration(com.alipay.sofa.registry.client.api.registration.PublisherRegistration) ConfigurationFactory(io.seata.config.ConfigurationFactory) ArrayList(java.util.ArrayList) SubscriberRegistration(com.alipay.sofa.registry.client.api.registration.SubscriberRegistration)

Example 4 with SubscriberRegistration

use of com.alipay.sofa.registry.client.api.registration.SubscriberRegistration in project dubbo by alibaba.

the class SofaRegistry method doSubscribe.

@Override
public void doSubscribe(URL url, final NotifyListener listener) {
    if (!url.getParameter(SUBSCRIBE_KEY, true) || PROVIDER_PROTOCOL.equals(url.getProtocol())) {
        return;
    }
    String serviceName = buildServiceName(url);
    // com.alipay.test.TestService:1.0:group@dubbo
    Subscriber listSubscriber = subscribers.get(serviceName);
    if (listSubscriber != null) {
        logger.warn("Service name [" + serviceName + "] have bean registered in SOFARegistry.");
        CountDownLatch countDownLatch = new CountDownLatch(1);
        handleRegistryData(listSubscriber.peekData(), listener, countDownLatch);
        waitAddress(serviceName, countDownLatch);
        return;
    }
    final CountDownLatch latch = new CountDownLatch(1);
    SubscriberRegistration subscriberRegistration = new SubscriberRegistration(serviceName, (dataId, data) -> {
        // record change
        printAddressData(dataId, data);
        handleRegistryData(data, listener, latch);
    });
    addAttributesForSub(subscriberRegistration);
    listSubscriber = registryClient.register(subscriberRegistration);
    subscribers.put(serviceName, listSubscriber);
    waitAddress(serviceName, latch);
}
Also used : Subscriber(com.alipay.sofa.registry.client.api.Subscriber) CountDownLatch(java.util.concurrent.CountDownLatch) SubscriberRegistration(com.alipay.sofa.registry.client.api.registration.SubscriberRegistration)

Example 5 with SubscriberRegistration

use of com.alipay.sofa.registry.client.api.registration.SubscriberRegistration in project dubbo by apache.

the class SofaRegistry method doSubscribe.

@Override
public void doSubscribe(URL url, final NotifyListener listener) {
    if (!url.getParameter(SUBSCRIBE_KEY, true) || PROVIDER_PROTOCOL.equals(url.getProtocol())) {
        return;
    }
    String serviceName = buildServiceName(url);
    // com.alipay.test.TestService:1.0:group@dubbo
    Subscriber listSubscriber = subscribers.get(serviceName);
    if (listSubscriber != null) {
        logger.warn("Service name [" + serviceName + "] have bean registered in SOFARegistry.");
        CountDownLatch countDownLatch = new CountDownLatch(1);
        handleRegistryData(listSubscriber.peekData(), listener, countDownLatch);
        waitAddress(serviceName, countDownLatch);
        return;
    }
    final CountDownLatch latch = new CountDownLatch(1);
    SubscriberRegistration subscriberRegistration = new SubscriberRegistration(serviceName, (dataId, data) -> {
        // record change
        printAddressData(dataId, data);
        handleRegistryData(data, listener, latch);
    });
    addAttributesForSub(subscriberRegistration);
    listSubscriber = registryClient.register(subscriberRegistration);
    subscribers.put(serviceName, listSubscriber);
    waitAddress(serviceName, latch);
}
Also used : Subscriber(com.alipay.sofa.registry.client.api.Subscriber) CountDownLatch(java.util.concurrent.CountDownLatch) SubscriberRegistration(com.alipay.sofa.registry.client.api.registration.SubscriberRegistration)

Aggregations

SubscriberRegistration (com.alipay.sofa.registry.client.api.registration.SubscriberRegistration)6 Subscriber (com.alipay.sofa.registry.client.api.Subscriber)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 Configurator (com.alipay.sofa.registry.client.api.Configurator)1 RegistryClient (com.alipay.sofa.registry.client.api.RegistryClient)1 RegistryClientConfig (com.alipay.sofa.registry.client.api.RegistryClientConfig)1 SubscriberDataObserver (com.alipay.sofa.registry.client.api.SubscriberDataObserver)1 RegistryType (com.alipay.sofa.registry.client.api.model.RegistryType)1 ConfiguratorRegistration (com.alipay.sofa.registry.client.api.registration.ConfiguratorRegistration)1 PublisherRegistration (com.alipay.sofa.registry.client.api.registration.PublisherRegistration)1 DefaultRegistryClient (com.alipay.sofa.registry.client.provider.DefaultRegistryClient)1 DefaultRegistryClientConfigBuilder (com.alipay.sofa.registry.client.provider.DefaultRegistryClientConfigBuilder)1 ScopeEnum (com.alipay.sofa.registry.core.model.ScopeEnum)1 ProviderInfoListener (com.alipay.sofa.rpc.listener.ProviderInfoListener)1 NetUtil (io.seata.common.util.NetUtil)1 Configuration (io.seata.config.Configuration)1 ConfigurationFactory (io.seata.config.ConfigurationFactory)1 FILE_CONFIG_SPLIT_CHAR (io.seata.config.ConfigurationKeys.FILE_CONFIG_SPLIT_CHAR)1 FILE_ROOT_REGISTRY (io.seata.config.ConfigurationKeys.FILE_ROOT_REGISTRY)1 RegistryService (io.seata.discovery.registry.RegistryService)1