use of com.alipay.sofa.rpc.config.ConsumerConfig in project sofa-rpc by sofastack.
the class PolarisRegistryTest method consumerConfig.
private ConsumerConfig<?> consumerConfig(String uniqueId) {
ConsumerConfig<?> consumer = new ConsumerConfig();
consumer.setInterfaceId(INTERFACE_ID).setUniqueId(uniqueId).setApplication(new ApplicationConfig().setAppName(APPNAME)).setProxy("javassist").setSubscribe(true).setSerialization("java").setInvokeType("sync").setTimeout(4444);
return consumer;
}
use of com.alipay.sofa.rpc.config.ConsumerConfig in project sofa-rpc by sofastack.
the class NacosRegistryTest method testProviderObserver.
/**
* 测试Zookeeper Provider Observer
*
* @throws Exception the exception
*/
@Test
public void testProviderObserver() throws Exception {
int timeoutPerSub = 2000;
// wait nacos startup ok
TimeUnit.SECONDS.sleep(10);
serverConfig = new ServerConfig().setProtocol("bolt").setHost("0.0.0.0").setPort(12200);
ProviderConfig<?> provider = new ProviderConfig();
provider.setInterfaceId("com.alipay.xxx.NacosTestService").setApplication(new ApplicationConfig().setAppName("test-server")).setUniqueId("nacos-test").setProxy("javassist").setRegister(true).setRegistry(registryConfig).setSerialization("hessian2").setServer(serverConfig).setWeight(222).setTimeout(3000);
// 注册
try {
registry.register(provider);
} catch (Throwable e) {
LOGGER.error("register provider fail", e);
Assert.fail("register provider fail");
}
Thread.sleep(1000);
ConsumerConfig<?> consumer = new ConsumerConfig();
consumer.setInterfaceId("com.alipay.xxx.NacosTestService").setApplication(new ApplicationConfig().setAppName("test-server")).setUniqueId("nacos-test").setProxy("javassist").setSubscribe(true).setSerialization("java").setInvokeType("sync").setTimeout(4444);
// 订阅
CountDownLatch latch = new CountDownLatch(1);
MockProviderInfoListener providerInfoListener = new MockProviderInfoListener();
providerInfoListener.setCountDownLatch(latch);
consumer.setProviderInfoListener(providerInfoListener);
List<ProviderGroup> all = registry.subscribe(consumer);
providerInfoListener.updateAllProviders(all);
Map<String, ProviderInfo> ps = providerInfoListener.getData();
Assert.assertEquals("after register: 1", 1, ps.size());
LOGGER.info("after register success {}", ps);
// 订阅 错误的uniqueId
ConsumerConfig<?> consumerNoUniqueId = new ConsumerConfig();
consumerNoUniqueId.setInterfaceId("com.alipay.xxx.NacosTestService").setApplication(new ApplicationConfig().setAppName("test-server")).setProxy("javassist").setSubscribe(true).setSerialization("java").setInvokeType("sync").setTimeout(4444);
latch = new CountDownLatch(1);
MockProviderInfoListener wrongProviderInfoListener = new MockProviderInfoListener();
wrongProviderInfoListener.setCountDownLatch(latch);
consumerNoUniqueId.setProviderInfoListener(wrongProviderInfoListener);
all = registry.subscribe(consumerNoUniqueId);
wrongProviderInfoListener.updateAllProviders(all);
ps = wrongProviderInfoListener.getData();
Assert.assertEquals("wrong uniqueId: 0", 0, ps.size());
LOGGER.info("wrong uniqueId {}", ps);
// 反注册
latch = new CountDownLatch(1);
providerInfoListener.setCountDownLatch(latch);
registry.unRegister(provider);
latch.await(timeoutPerSub, TimeUnit.MILLISECONDS);
Assert.assertEquals("after unregister: 0", 0, ps.size());
LOGGER.info("after unregister {}", ps);
// 一次发2个端口的再次注册
latch = new CountDownLatch(2);
providerInfoListener.setCountDownLatch(latch);
provider.getServer().add(new ServerConfig().setProtocol("bolt").setHost("0.0.0.0").setPort(12201));
registry.register(provider);
latch.await(timeoutPerSub * 2, TimeUnit.MILLISECONDS);
ps = providerInfoListener.getData();
// nacos has bug now
Assert.assertEquals("after register two servers: 1", 1, ps.size());
LOGGER.info("after register two servers {}", ps);
// 重复订阅
ConsumerConfig<?> consumer2 = new ConsumerConfig();
consumer2.setInterfaceId("com.alipay.xxx.NacosTestService").setUniqueId("nacos-test").setApplication(new ApplicationConfig().setAppName("test-server")).setProxy("javassist").setSubscribe(true).setSerialization("java").setInvokeType("sync").setTimeout(4444);
CountDownLatch latch2 = new CountDownLatch(1);
MockProviderInfoListener providerInfoListener2 = new MockProviderInfoListener();
providerInfoListener2.setCountDownLatch(latch2);
consumer2.setProviderInfoListener(providerInfoListener2);
providerInfoListener2.updateAllProviders(registry.subscribe(consumer2));
latch2.await(timeoutPerSub, TimeUnit.MILLISECONDS);
Map<String, ProviderInfo> ps2 = providerInfoListener2.getData();
Assert.assertEquals("after register duplicate: 1", 1, ps2.size());
LOGGER.info("after register duplicate {}", ps);
// 取消订阅者1
registry.unSubscribe(consumer);
// 批量反注册,判断订阅者2的数据
latch = new CountDownLatch(2);
providerInfoListener2.setCountDownLatch(latch);
List<ProviderConfig> providerConfigList = new ArrayList<ProviderConfig>();
providerConfigList.add(provider);
registry.batchUnRegister(providerConfigList);
latch.await(timeoutPerSub * 2, TimeUnit.MILLISECONDS);
Assert.assertEquals("after unregister: 1", 1, ps2.size());
LOGGER.info("after unregister consumer, and consumer2 {}", ps);
// 批量取消订阅
List<ConsumerConfig> consumerConfigList = new ArrayList<ConsumerConfig>();
consumerConfigList.add(consumer2);
registry.batchUnSubscribe(consumerConfigList);
}
use of com.alipay.sofa.rpc.config.ConsumerConfig in project sofa-rpc by sofastack.
the class NacosRegistryTest method testVirtualHostAndVirtualPort.
/**
* 测试NacosRegistry 对 serverconfig中 VirtualHost 和 VirtualPort 参数的支持情况
*
* @throws Exception the exception
*/
@Test
public void testVirtualHostAndVirtualPort() throws Exception {
// wait nacos startup ok
TimeUnit.SECONDS.sleep(10);
// 模拟的场景 client -> proxy:127.7.7.7:8888 -> netty:0.0.0.0:12200
String virtualHost = "127.7.7.7";
int virtualPort = 8888;
serverConfig = new ServerConfig().setProtocol("bolt").setHost("0.0.0.0").setPort(12200).setAdaptivePort(// Turn off adaptive port
false).setVirtualHost(virtualHost).setVirtualPort(virtualPort);
// Verify the influence of virtualHost and virtualPort on the parameters when creating rpcserver
Method m_resolveServerConfig = ServerFactory.class.getDeclaredMethod("resolveServerConfig", ServerConfig.class);
m_resolveServerConfig.setAccessible(true);
m_resolveServerConfig.invoke(new ServerFactory(), serverConfig);
Assert.assertNotEquals("boundhost should not be equal to virtualHost", serverConfig.getBoundHost(), virtualHost);
Assert.assertEquals("boundPort should be oriPort", serverConfig.getPort(), 12200);
ProviderConfig<?> provider = new ProviderConfig();
provider.setInterfaceId("com.alipay.xxx.NacosTestService2").setApplication(new ApplicationConfig().setAppName("test-server2")).setUniqueId("nacos-test2").setProxy("javassist").setRegister(true).setRegistry(registryConfig).setServer(serverConfig);
// 注册
registry.register(provider);
Thread.sleep(1000);
ConsumerConfig<?> consumer = new ConsumerConfig();
consumer.setInterfaceId("com.alipay.xxx.NacosTestService2").setApplication(new ApplicationConfig().setAppName("test-consumer2")).setUniqueId("nacos-test2").setProxy("javassist").setSubscribe(true).setSerialization("java").setInvokeType("sync").setTimeout(4444);
// 订阅
CountDownLatch latch = new CountDownLatch(1);
MockProviderInfoListener providerInfoListener = new MockProviderInfoListener();
providerInfoListener.setCountDownLatch(latch);
consumer.setProviderInfoListener(providerInfoListener);
List<ProviderGroup> all = registry.subscribe(consumer);
// providerInfoListener.updateAllProviders(all);
latch.await(2000, TimeUnit.MILLISECONDS);
Map<String, ProviderInfo> ps = providerInfoListener.getData();
Assert.assertEquals("after register: 1", 1, ps.size());
Map.Entry<String, ProviderInfo> psEntry = (Map.Entry) ps.entrySet().toArray()[0];
ProviderInfo pri = psEntry.getValue();
Assert.assertEquals("The provider's key should consist of virtualHost and virtualPort", psEntry.getKey(), virtualHost + ":" + virtualPort);
Assert.assertEquals("The provider's host should be virtualHost", virtualHost, pri.getHost());
Assert.assertEquals("The provider's port should be virtualPort", virtualPort, pri.getPort());
}
use of com.alipay.sofa.rpc.config.ConsumerConfig in project sofa-rpc by sofastack.
the class ConsulRegistryTest method consumerConfig.
private ConsumerConfig<?> consumerConfig(String uniqueId) {
ConsumerConfig<?> consumer = new ConsumerConfig();
consumer.setInterfaceId(INTERFACE_ID).setUniqueId(uniqueId).setApplication(new ApplicationConfig().setAppName("consul-registry-test")).setProxy("javassist").setSubscribe(true).setSerialization("java").setInvokeType("sync").setTimeout(4444);
return consumer;
}
use of com.alipay.sofa.rpc.config.ConsumerConfig 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);
}
Aggregations