Search in sources :

Example 41 with ProviderConfig

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);
}
Also used : ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) ProviderGroup(com.alipay.sofa.rpc.client.ProviderGroup) File(java.io.File) Test(org.junit.Test)

Example 42 with ProviderConfig

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());
}
Also used : RegistryConfig(com.alipay.sofa.rpc.config.RegistryConfig) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) ProviderGroup(com.alipay.sofa.rpc.client.ProviderGroup) File(java.io.File) Test(org.junit.Test)

Example 43 with ProviderConfig

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;
}
Also used : ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) IntStream(java.util.stream.IntStream) AfterClass(org.junit.AfterClass) GetAllInstancesRequest(com.tencent.polaris.api.rpc.GetAllInstancesRequest) BeforeClass(org.junit.BeforeClass) DiscoveryAPIFactory(com.tencent.polaris.factory.api.DiscoveryAPIFactory) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ConsumerAPI(com.tencent.polaris.api.core.ConsumerAPI) Test(org.junit.Test) IOException(java.io.IOException) List(java.util.List) RegistryFactory(com.alipay.sofa.rpc.registry.RegistryFactory) NamingServer(com.tencent.polaris.test.mock.discovery.NamingServer) ProviderGroup(com.alipay.sofa.rpc.client.ProviderGroup) ServiceKey(com.tencent.polaris.api.pojo.ServiceKey) InstancesResponse(com.tencent.polaris.api.rpc.InstancesResponse) RegistryConfig(com.alipay.sofa.rpc.config.RegistryConfig) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) Assert(org.junit.Assert) PolarisException(com.tencent.polaris.api.exception.PolarisException) SERVER_USER_ERROR(com.tencent.polaris.api.exception.ErrorCode.SERVER_USER_ERROR) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig)

Example 44 with ProviderConfig

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);
}
Also used : ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) Test(org.junit.Test)

Example 45 with ProviderConfig

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);
}
Also used : ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ProviderInfo(com.alipay.sofa.rpc.client.ProviderInfo) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) ProviderGroup(com.alipay.sofa.rpc.client.ProviderGroup) BaseNacosTest(com.alipay.sofa.rpc.registry.nacos.base.BaseNacosTest) Test(org.junit.Test)

Aggregations

ProviderConfig (com.alipay.sofa.rpc.config.ProviderConfig)121 ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)88 Test (org.junit.Test)73 ApplicationConfig (com.alipay.sofa.rpc.config.ApplicationConfig)52 ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)47 HelloService (com.alipay.sofa.rpc.test.HelloService)33 HelloServiceImpl (com.alipay.sofa.rpc.test.HelloServiceImpl)33 ActivelyDestroyTest (com.alipay.sofa.rpc.test.ActivelyDestroyTest)28 RegistryConfig (com.alipay.sofa.rpc.config.RegistryConfig)21 CountDownLatch (java.util.concurrent.CountDownLatch)19 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)17 ProviderGroup (com.alipay.sofa.rpc.client.ProviderGroup)10 Filter (com.alipay.sofa.rpc.filter.Filter)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)10 EchoService (com.alipay.sofa.rpc.test.EchoService)9 ArrayList (java.util.ArrayList)9 ProviderInfo (com.alipay.sofa.rpc.client.ProviderInfo)8 EchoServiceImpl (com.alipay.sofa.rpc.test.EchoServiceImpl)8 MethodConfig (com.alipay.sofa.rpc.config.MethodConfig)7 RpcInvokeContext (com.alipay.sofa.rpc.context.RpcInvokeContext)7