ConsulRegistry extends Registry { @Override public List<ProviderGroup> subscribe(ConsumerConfig config) { 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; } try { List<ProviderInfo> providers = lookupHealthService(config); if (EventBus.isEnable(ConsumerSubEvent.class)) { ConsumerSubEvent event = new ConsumerSubEvent(config); EventBus.post(event); } return Collections.singletonList(new ProviderGroup().addAll(providers)); } catch (SofaRpcRuntimeException e){ throw e; }catch (Exception e) { throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_SUB_PROVIDER ,EXT_NAME), e); } } protected  ConsulRegistry(RegistryConfig registryConfig); @Override void init(); @Override void destroy(); @Override void destroy(DestroyHook hook); @Override boolean start(); @Override void register(ProviderConfig config); @Override void unRegister(ProviderConfig config); @Override void batchUnRegister(List<ProviderConfig> configs); @Override List<ProviderGroup> subscribe(ConsumerConfig config); @Override void unSubscribe(ConsumerConfig config); @Override void batchUnSubscribe(List<ConsumerConfig> configs); static final String EXT_NAME; }
@Test public void testSubscribe() { ProviderConfig<?> providerConfig = providerConfig("consul-test-1", 12200, 12201, 12202); registry.register(providerConfig); ConsumerConfig<?> consumerConfig = consumerConfig("consul-test-1"); assertUntil(() -> { List<ProviderGroup> providerGroups = registry.subscribe(consumerConfig); Assert.assertEquals(1, providerGroups.size()); Assert.assertEquals(3, providerGroups.get(0).size()); }, 10, TimeUnit.SECONDS); ConsumerConfig<?> consumerConfigWithAnotherUniqueId = consumerConfig("consul-test-2"); assertUntil(() -> { List<ProviderGroup> providerGroups = registry.subscribe(consumerConfigWithAnotherUniqueId); Assert.assertEquals(1, providerGroups.size()); Assert.assertEquals(0, providerGroups.get(0).size()); }, 10, TimeUnit.SECONDS); registry.unSubscribe(consumerConfig); registry.unSubscribe(consumerConfigWithAnotherUniqueId); }