Search in sources :

Example 46 with ProviderConfig

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

Example 47 with ProviderConfig

use of com.alipay.sofa.rpc.config.ProviderConfig in project sofa-rpc by sofastack.

the class ConsulRegistryTest method providerConfig.

private ProviderConfig<?> providerConfig(String uniqueId, int... ports) {
    ProviderConfig<?> provider = new ProviderConfig();
    provider.setInterfaceId(INTERFACE_ID).setUniqueId(uniqueId).setApplication(new ApplicationConfig().setAppName("consul-registry-test")).setProxy("javassist").setRegister(true).setRegistry(registryConfig).setSerialization("hessian2").setWeight(222).setTimeout(3000);
    IntStream.of(ports).mapToObj(port -> new ServerConfig().setProtocol("bolt").setHost("localhost").setPort(port)).forEach(provider::setServer);
    return provider;
}
Also used : IntStream(java.util.stream.IntStream) ConsulStarterBuilder(com.pszymczyk.consul.ConsulStarterBuilder) HealthService(com.ecwid.consul.v1.health.model.HealthService) RegistryFactory(com.alipay.sofa.rpc.registry.RegistryFactory) ProviderGroup(com.alipay.sofa.rpc.client.ProviderGroup) After(org.junit.After) Map(java.util.Map) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) Before(org.junit.Before) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) ConsulClient(com.ecwid.consul.v1.ConsulClient) ProviderInfo(com.alipay.sofa.rpc.client.ProviderInfo) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.junit.Test) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) ConsulProcess(com.pszymczyk.consul.ConsulProcess) RegistryConfig(com.alipay.sofa.rpc.config.RegistryConfig) Response(com.ecwid.consul.v1.Response) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ProviderInfoListener(com.alipay.sofa.rpc.listener.ProviderInfoListener) HealthServicesRequest(com.ecwid.consul.v1.health.HealthServicesRequest) Assert(org.junit.Assert) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig)

Example 48 with ProviderConfig

use of com.alipay.sofa.rpc.config.ProviderConfig in project sofa-rpc by sofastack.

the class MeshRegistryTest method testAll.

@Test
public void testAll() throws Exception {
    int timeoutPerSub = 1000;
    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 = MeshRegistryHelper.buildMeshKey(provider, serverConfig.getProtocol());
    String tag1 = MeshRegistryHelper.buildMeshKey(consumer, consumer.getProtocol());
    Assert.assertEquals(tag1, tag0);
    // 订阅
    MeshRegistryTest.MockProviderInfoListener providerInfoListener = new MeshRegistryTest.MockProviderInfoListener();
    consumer.setProviderInfoListener(providerInfoListener);
    List<ProviderGroup> groups = registry.subscribe(consumer);
    Assert.assertNull(groups);
    Thread.sleep(3000);
    Map<String, ProviderGroup> ps = providerInfoListener.getData();
    Assert.assertTrue(ps.toString(), ps.size() == 1);
    // 反注册
    CountDownLatch latch = new CountDownLatch(1);
    providerInfoListener.setCountDownLatch(latch);
    registry.unRegister(provider);
    latch.await(timeoutPerSub, TimeUnit.MILLISECONDS);
    // mesh 并不直接感知.
    Assert.assertTrue(ps.size() == 1);
    // 一次发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);
    // 重复订阅
    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);
    MeshRegistryTest.MockProviderInfoListener providerInfoListener2 = new MeshRegistryTest.MockProviderInfoListener();
    providerInfoListener2.setCountDownLatch(latch2);
    consumer2.setProviderInfoListener(providerInfoListener2);
    List<ProviderGroup> groups2 = registry.subscribe(consumer2);
    Assert.assertNull(groups);
    Thread.sleep(3000);
    Map<String, ProviderGroup> ps2 = providerInfoListener2.getData();
    Assert.assertTrue(ps2.size() == 1);
    // 取消订阅者1
    registry.unSubscribe(consumer);
    // 批量反注册,判断订阅者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() == 1);
    // 批量取消订阅
    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) MeshEndpoint(com.alipay.sofa.rpc.registry.mesh.model.MeshEndpoint) 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) Test(org.junit.Test)

Example 49 with ProviderConfig

use of com.alipay.sofa.rpc.config.ProviderConfig in project sofa-rpc by sofastack.

the class ProviderTracerFilter method invoke.

@Override
public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException {
    SofaTracerSpan serverSpan = null;
    try {
        SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
        serverSpan = sofaTraceContext.getCurrentSpan();
        if (serverSpan != null) {
            RpcInternalContext context = RpcInternalContext.getContext();
            serverSpan.setTag(RpcSpanTags.SERVICE, request.getTargetServiceUniqueName());
            serverSpan.setTag(RpcSpanTags.METHOD, request.getMethodName());
            // 客户端地址
            serverSpan.setTag(RpcSpanTags.REMOTE_IP, context.getRemoteHostName());
            // 从请求里获取ConsumerTracerFilter额外传递的信息
            serverSpan.setTag(RpcSpanTags.REMOTE_APP, (String) request.getRequestProp(HEAD_APP_NAME));
            serverSpan.setTag(RpcSpanTags.PROTOCOL, (String) request.getRequestProp(HEAD_PROTOCOL));
            serverSpan.setTag(RpcSpanTags.INVOKE_TYPE, (String) request.getRequestProp(HEAD_INVOKE_TYPE));
            ProviderConfig providerConfig = (ProviderConfig) invoker.getConfig();
            serverSpan.setTag(RpcSpanTags.LOCAL_APP, providerConfig.getAppName());
            serverSpan.setTag(RpcSpanTags.SERVER_THREAD_POOL_WAIT_TIME, (Number) context.getAttachment(RpcConstants.INTERNAL_KEY_PROCESS_WAIT_TIME));
        }
        return invoker.invoke(request);
    } finally {
        if (serverSpan != null) {
            serverSpan.setTag(RpcSpanTags.SERVER_BIZ_TIME, (Number) RpcInternalContext.getContext().getAttachment(RpcConstants.INTERNAL_KEY_IMPL_ELAPSE));
        }
    }
}
Also used : SofaTracerSpan(com.alipay.common.tracer.core.span.SofaTracerSpan) SofaTraceContext(com.alipay.common.tracer.core.context.trace.SofaTraceContext) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) RpcInternalContext(com.alipay.sofa.rpc.context.RpcInternalContext)

Example 50 with ProviderConfig

use of com.alipay.sofa.rpc.config.ProviderConfig in project sofa-boot by sofastack.

the class ProviderConfigContainer method exportAllDubboProvideConfig.

/**
 * export所有 Dubbo 类型的 ProviderConfig
 */
public void exportAllDubboProvideConfig() {
    for (ProviderConfig providerConfig : getAllProviderConfig()) {
        ServerConfig serverConfig = (ServerConfig) providerConfig.getServer().get(0);
        if (serverConfig.getProtocol().equalsIgnoreCase(SofaBootRpcConfigConstants.RPC_PROTOCOL_DUBBO)) {
            providerConfig.setRegister(true);
            providerConfig.export();
            if (LOGGER.isInfoEnabled()) {
                LOGGER.info("service published.  interfaceId[" + providerConfig.getInterfaceId() + "]; protocol[" + serverConfig.getProtocol() + "]");
            }
        }
    }
}
Also used : ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig)

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