use of com.alipay.sofa.rpc.config.ProviderConfig in project sofa-rpc by sofastack.
the class LocalRegistryTest method testAll.
@Test
public void testAll() throws Exception {
// test for notifyConsumer
notifyConsumerTest();
int timeoutPerSub = 5000;
ServerConfig serverConfig = new ServerConfig().setProtocol("bolt").setHost("0.0.0.0").setPort(12200);
ProviderConfig<?> provider = new ProviderConfig();
provider.setInterfaceId("com.alipay.xxx.TestService").setUniqueId("unique123Id").setApplication(new ApplicationConfig().setAppName("test-server")).setProxy("javassist").setRegister(true).setRegistry(registryConfig).setSerialization("hessian2").setServer(serverConfig).setWeight(222).setTimeout(3000);
// 注册
registry.register(provider);
ConsumerConfig<?> consumer = new ConsumerConfig();
consumer.setInterfaceId("com.alipay.xxx.TestService").setUniqueId("unique123Id").setApplication(new ApplicationConfig().setAppName("test-server")).setProxy("javassist").setSubscribe(true).setSerialization("java").setInvokeType("sync").setTimeout(4444);
String tag0 = LocalRegistryHelper.buildListDataId(provider, serverConfig.getProtocol());
String tag1 = LocalRegistryHelper.buildListDataId(consumer, consumer.getProtocol());
Assert.assertEquals(tag1, tag0);
String content = FileUtils.file2String(new File(file));
Assert.assertTrue(content.startsWith(tag0));
// 订阅
LocalRegistryTest.MockProviderInfoListener providerInfoListener = new LocalRegistryTest.MockProviderInfoListener();
consumer.setProviderInfoListener(providerInfoListener);
List<ProviderGroup> groups = registry.subscribe(consumer);
providerInfoListener.updateAllProviders(groups);
Map<String, ProviderGroup> ps = providerInfoListener.getData();
Assert.assertTrue(ps.size() > 0);
Assert.assertNotNull(ps.get(RpcConstants.ADDRESS_DEFAULT_GROUP));
Assert.assertTrue(ps.get(RpcConstants.ADDRESS_DEFAULT_GROUP).size() == 1);
// 反注册
CountDownLatch latch = new CountDownLatch(1);
providerInfoListener.setCountDownLatch(latch);
registry.unRegister(provider);
latch.await(timeoutPerSub, TimeUnit.MILLISECONDS);
Assert.assertTrue(ps.size() > 0);
Assert.assertNotNull(ps.get(RpcConstants.ADDRESS_DEFAULT_GROUP));
Assert.assertTrue(ps.get(RpcConstants.ADDRESS_DEFAULT_GROUP).size() == 0);
// 一次发2个端口的再次注册
latch = new CountDownLatch(1);
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);
Assert.assertTrue(ps.size() > 0);
Assert.assertNotNull(ps.get(RpcConstants.ADDRESS_DEFAULT_GROUP));
Assert.assertTrue(ps.get(RpcConstants.ADDRESS_DEFAULT_GROUP).size() == 2);
// 重复订阅
ConsumerConfig<?> consumer2 = new ConsumerConfig();
consumer2.setInterfaceId("com.alipay.xxx.TestService").setUniqueId("unique123Id").setApplication(new ApplicationConfig().setAppName("test-server")).setProxy("javassist").setSubscribe(true).setSerialization("java").setInvokeType("sync").setTimeout(4444);
CountDownLatch latch2 = new CountDownLatch(1);
LocalRegistryTest.MockProviderInfoListener providerInfoListener2 = new LocalRegistryTest.MockProviderInfoListener();
providerInfoListener2.setCountDownLatch(latch2);
consumer2.setProviderInfoListener(providerInfoListener2);
List<ProviderGroup> groups2 = registry.subscribe(consumer2);
providerInfoListener2.updateAllProviders(groups2);
Map<String, ProviderGroup> ps2 = providerInfoListener2.getData();
Assert.assertTrue(ps2.size() > 0);
Assert.assertNotNull(ps2.get(RpcConstants.ADDRESS_DEFAULT_GROUP));
Assert.assertTrue(ps2.get(RpcConstants.ADDRESS_DEFAULT_GROUP).size() == 2);
Assert.assertTrue(registry.memoryCache.get(tag1).size() == 2);
// 取消订阅者1
registry.unSubscribe(consumer);
List<ConsumerConfig> callback = registry.notifyListeners.get(tag1);
Assert.assertFalse(callback.contains(consumer));
Assert.assertTrue(callback.size() == 1);
// 批量反注册,判断订阅者2的数据
latch = new CountDownLatch(1);
providerInfoListener2.setCountDownLatch(latch);
List<ProviderConfig> providerConfigList = new ArrayList<ProviderConfig>();
providerConfigList.add(provider);
registry.batchUnRegister(providerConfigList);
latch.await(timeoutPerSub, TimeUnit.MILLISECONDS);
Assert.assertTrue(ps2.size() > 0);
Assert.assertNotNull(ps2.get(RpcConstants.ADDRESS_DEFAULT_GROUP));
Assert.assertTrue(ps2.get(RpcConstants.ADDRESS_DEFAULT_GROUP).size() == 0);
Assert.assertTrue(registry.notifyListeners.size() == 1);
// 批量取消订阅
List<ConsumerConfig> consumerConfigList = new ArrayList<ConsumerConfig>();
consumerConfigList.add(consumer2);
registry.batchUnSubscribe(consumerConfigList);
Assert.assertTrue(registry.notifyListeners.size() == 0);
}
use of com.alipay.sofa.rpc.config.ProviderConfig in project sofa-rpc by sofastack.
the class LocalRegistryTest method testLoadFile.
@Test
public void testLoadFile() {
ServerConfig serverConfig = new ServerConfig().setProtocol("bolt").setHost("0.0.0.0").setPort(12200);
ProviderConfig<?> provider = new ProviderConfig();
provider.setInterfaceId("com.alipay.xxx.TestService").setUniqueId("unique123Id").setRegister(true).setRegistry(registryConfig).setServer(serverConfig);
registry.register(provider);
registry.destroy();
// registry 关闭,但是 provider 信息保存到本地
Assert.assertTrue(new File(file).exists());
// 创建一个新的 localRegistry,会立即加载到缓存
RegistryConfig newRegistryConfig = new RegistryConfig().setProtocol("local").setSubscribe(true).setFile(file).setRegister(true);
LocalRegistry newRegistry = (LocalRegistry) RegistryFactory.getRegistry(newRegistryConfig);
newRegistry.init();
Assert.assertFalse(newRegistry.memoryCache.isEmpty());
// consumer 订阅时应该能立刻读到数据
ConsumerConfig<?> consumer = new ConsumerConfig();
consumer.setInterfaceId("com.alipay.xxx.TestService").setUniqueId("unique123Id").setRegistry(registryConfig).setSubscribe(true);
List<ProviderGroup> subscribe = newRegistry.subscribe(consumer);
Assert.assertFalse(subscribe.isEmpty());
Assert.assertFalse(subscribe.get(0).getProviderInfos().isEmpty());
}
use of com.alipay.sofa.rpc.config.ProviderConfig in project sofa-rpc by sofastack.
the class PolarisRegistryTest method providerConfig.
private ProviderConfig<?> providerConfig(String uniqueId, int... ports) {
ProviderConfig<?> provider = new ProviderConfig();
provider.setInterfaceId(INTERFACE_ID).setUniqueId(uniqueId).setApplication(new ApplicationConfig().setAppName(APPNAME)).setProxy("javassist").setRegister(true).setRegistry(registryConfig).setSerialization("hessian2").setWeight(222).setTimeout(3000);
IntStream.of(ports).mapToObj(port -> new ServerConfig().setProtocol("bolt").setHost("127.0.0.1").setPort(port)).forEach(provider::setServer);
return provider;
}
use of com.alipay.sofa.rpc.config.ProviderConfig in project sofa-rpc by sofastack.
the class NacosRegistryHelperTest method buildServiceName.
@Test
public void buildServiceName() {
ServerConfig serverConfig = new ServerConfig().setProtocol("bolt").setHost("0.0.0.0").setPort(12200);
ProviderConfig<?> provider = new ProviderConfig();
provider.setInterfaceId("com.alipay.xxx.TestService").setApplication(new ApplicationConfig().setAppName("test-server")).setUniqueId("nacos-test").setProxy("javassist").setRegister(true).setSerialization("hessian2").setServer(serverConfig).setWeight(222).setTimeout(3000);
String serviceName = NacosRegistryHelper.buildServiceName(provider, RpcConstants.PROTOCOL_TYPE_BOLT);
assertEquals(serviceName, "com.alipay.xxx.TestService:nacos-test:DEFAULT");
serviceName = NacosRegistryHelper.buildServiceName(provider, RpcConstants.PROTOCOL_TYPE_TR);
assertEquals(serviceName, "com.alipay.xxx.TestService:nacos-test:DEFAULT");
serviceName = NacosRegistryHelper.buildServiceName(provider, RpcConstants.PROTOCOL_TYPE_TRIPLE);
assertEquals(serviceName, "com.alipay.xxx.TestService:nacos-test:" + RpcConstants.PROTOCOL_TYPE_TRIPLE);
serviceName = NacosRegistryHelper.buildServiceName(provider, RpcConstants.PROTOCOL_TYPE_REST);
assertEquals(serviceName, "com.alipay.xxx.TestService:nacos-test:" + RpcConstants.PROTOCOL_TYPE_REST);
}
use of com.alipay.sofa.rpc.config.ProviderConfig 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);
}
Aggregations