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