Search in sources :

Example 1 with ServerFactory

use of com.alipay.sofa.rpc.server.ServerFactory 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

ProviderGroup (com.alipay.sofa.rpc.client.ProviderGroup)1 ProviderInfo (com.alipay.sofa.rpc.client.ProviderInfo)1 ApplicationConfig (com.alipay.sofa.rpc.config.ApplicationConfig)1 ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)1 ProviderConfig (com.alipay.sofa.rpc.config.ProviderConfig)1 ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)1 BaseNacosTest (com.alipay.sofa.rpc.registry.nacos.base.BaseNacosTest)1 ServerFactory (com.alipay.sofa.rpc.server.ServerFactory)1 Method (java.lang.reflect.Method)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Test (org.junit.Test)1