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;
}
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)));
}
}
}
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);
}
}
}
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);
}
}
}
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));
}
}
}
}
Aggregations