Search in sources :

Example 51 with ApplicationConfig

use of com.alipay.sofa.rpc.config.ApplicationConfig 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;
}
Also used : ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig)

Example 52 with ApplicationConfig

use of com.alipay.sofa.rpc.config.ApplicationConfig 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 53 with ApplicationConfig

use of com.alipay.sofa.rpc.config.ApplicationConfig 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 54 with ApplicationConfig

use of com.alipay.sofa.rpc.config.ApplicationConfig 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)

Example 55 with ApplicationConfig

use of com.alipay.sofa.rpc.config.ApplicationConfig 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());
}
Also used : ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) Method(java.lang.reflect.Method) ServerFactory(com.alipay.sofa.rpc.server.ServerFactory) 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) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) BaseNacosTest(com.alipay.sofa.rpc.registry.nacos.base.BaseNacosTest) Test(org.junit.Test)

Aggregations

ApplicationConfig (com.alipay.sofa.rpc.config.ApplicationConfig)113 ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)77 ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)64 Test (org.junit.Test)53 ProviderConfig (com.alipay.sofa.rpc.config.ProviderConfig)52 HelloService (com.alipay.sofa.rpc.test.HelloService)30 ActivelyDestroyTest (com.alipay.sofa.rpc.test.ActivelyDestroyTest)21 CountDownLatch (java.util.concurrent.CountDownLatch)20 HelloServiceImpl (com.alipay.sofa.rpc.test.HelloServiceImpl)15 MethodConfig (com.alipay.sofa.rpc.config.MethodConfig)14 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)14 SofaResponseCallback (com.alipay.sofa.rpc.core.invoke.SofaResponseCallback)13 ArrayList (java.util.ArrayList)12 RegistryConfig (com.alipay.sofa.rpc.config.RegistryConfig)11 RequestBase (com.alipay.sofa.rpc.core.request.RequestBase)11 ProviderGroup (com.alipay.sofa.rpc.client.ProviderGroup)10 EchoService (com.alipay.sofa.rpc.test.EchoService)10 EchoRequest (com.alipay.sofa.rpc.server.bolt.pb.EchoRequest)8 DemoServiceImpl (com.alipay.sofa.rpc.bootstrap.dubbo.demo.DemoServiceImpl)7 Filter (com.alipay.sofa.rpc.filter.Filter)7