Search in sources :

Example 81 with ProviderConfig

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

the class TempClassLoader method testAttrUpdate.

@Test
public void testAttrUpdate() {
    // 发布一个服务
    ServerConfig serverConfig0 = new ServerConfig().setStopTimeout(0).setPort(22224).setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT).setQueues(100).setCoreThreads(5).setMaxThreads(5);
    ProviderConfig<HelloService> providerConfig0 = new ProviderConfig<HelloService>().setId("p-0").setInterfaceId(HelloService.class.getName()).setUniqueId("attr").setRef(new HelloServiceImpl()).setServer(serverConfig0).setRepeatedExportLimit(5);
    providerConfig0.export();
    ConsumerConfig<HelloService> consumerConfig0 = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setUniqueId("attr").setProxy("jdk").setDirectUrl("bolt://127.0.0.1:22224").setTimeout(500);
    HelloService proxy = consumerConfig0.refer();
    Invoker invoker = JDKProxy.parseInvoker(proxy);
    Cluster cluster = consumerConfig0.getConsumerBootstrap().getCluster();
    boolean error = false;
    try {
        proxy.sayHello("11", 11);
    } catch (Exception e) {
        error = true;
    }
    Assert.assertFalse(error);
    // wrong key
    error = false;
    try {
        providerConfig0.getConfigListener().attrUpdated(Collections.singletonMap("weighttttt", "asdasd"));
    } catch (Exception e) {
        error = true;
    }
    Assert.assertFalse(error);
    // wrong value
    error = false;
    try {
        providerConfig0.getConfigListener().attrUpdated(Collections.singletonMap("weight", "asdasd"));
    } catch (Exception e) {
        error = true;
    }
    Assert.assertFalse(error);
    // 切到一个没有的分组
    providerConfig0.getConfigListener().attrUpdated(Collections.singletonMap("uniqueId", "attr2"));
    // 切到一个有的分组
    error = false;
    try {
        proxy.sayHello("11", 11);
    } catch (Exception e) {
        error = true;
    }
    Assert.assertTrue(error);
    // 切到一个有的分组
    providerConfig0.getConfigListener().attrUpdated(Collections.singletonMap("uniqueId", "attr"));
    error = false;
    try {
        proxy.sayHello("11", 11);
    } catch (Exception e) {
        error = true;
    }
    Assert.assertFalse(error);
}
Also used : ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) Invoker(com.alipay.sofa.rpc.invoke.Invoker) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) HelloService(com.alipay.sofa.rpc.test.HelloService) Cluster(com.alipay.sofa.rpc.client.Cluster) HelloServiceImpl(com.alipay.sofa.rpc.test.HelloServiceImpl) SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException) Test(org.junit.Test) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest)

Example 82 with ProviderConfig

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

the class TempClassLoader method testProviderClassLoader.

@Test
public void testProviderClassLoader() throws Throwable {
    // 发布一个服务
    ServerConfig serverConfig = new ServerConfig().setStopTimeout(0).setPort(22223).setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT).setQueues(100).setCoreThreads(5).setMaxThreads(5);
    ProviderConfig<HelloService> providerConfig0 = new ProviderConfig<HelloService>().setId("p-0").setUniqueId("p-0").setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl()).setServer(serverConfig).setRegister(false);
    providerConfig0.export();
    // incompatible with JDK 9+
    URLClassLoader currentClassLoader = (URLClassLoader) this.getClass().getClassLoader();
    TempClassLoader tempClassLoader = new TempClassLoader(currentClassLoader.getURLs(), null);
    Class helloService = tempClassLoader.loadClass(HelloService.class.getCanonicalName());
    Class helloServiceImpl = tempClassLoader.loadClass(HelloServiceImpl.class.getCanonicalName());
    ProviderConfig<Object> providerConfig1 = new ProviderConfig<Object>().setId("p-1").setUniqueId("p-1").setInterfaceId(HelloService.class.getName()).setProxyClass(helloService).setRef(helloServiceImpl.getConstructor(int.class).newInstance(2000)).setServer(serverConfig).setRegister(false);
    providerConfig1.export();
    // refer a service
    ConsumerConfig<HelloService> consumerConfig0 = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setUniqueId("p-0").setProxy("jdk").setDirectUrl("bolt://127.0.0.1:22223").setTimeout(500);
    HelloService proxy = consumerConfig0.refer();
    proxy.sayHello("11", 11);
    ClassLoader cl0 = ReflectCache.getServiceClassLoader(ConfigUniqueNameGenerator.getUniqueName(providerConfig0));
    ClassLoader cl1 = ReflectCache.getServiceClassLoader(ConfigUniqueNameGenerator.getUniqueName(providerConfig1));
    Assert.assertEquals(currentClassLoader, cl0);
    Assert.assertEquals(tempClassLoader, cl1);
    providerConfig0.unExport();
    providerConfig1.unExport();
    cl0 = ReflectCache.getServiceClassLoader(ConfigUniqueNameGenerator.getUniqueName(providerConfig0));
    cl1 = ReflectCache.getServiceClassLoader(ConfigUniqueNameGenerator.getUniqueName(providerConfig1));
    Assert.assertEquals(ClassLoaderUtils.getCurrentClassLoader(), cl0);
    Assert.assertEquals(ClassLoaderUtils.getCurrentClassLoader(), cl1);
    Method methodCache = ReflectCache.getMethodCache(ConfigUniqueNameGenerator.getUniqueName(providerConfig0), "sayHello");
    Assert.assertNull(methodCache);
    methodCache = ReflectCache.getOverloadMethodCache(ConfigUniqueNameGenerator.getUniqueName(providerConfig0), "sayHello", new String[] { "java.lang.Stringint" });
    Assert.assertNull(methodCache);
    String[] sig = ReflectCache.getMethodSigsCache(ConfigUniqueNameGenerator.getUniqueName(providerConfig0), "sayHello");
    Assert.assertNull(sig);
}
Also used : ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) HelloService(com.alipay.sofa.rpc.test.HelloService) HelloServiceImpl(com.alipay.sofa.rpc.test.HelloServiceImpl) Method(java.lang.reflect.Method) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) Test(org.junit.Test) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest)

Example 83 with ProviderConfig

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

the class BaggageCallbackTest method doTest.

@Override
public void doTest() throws Exception {
    ServerConfig serverConfig = new ServerConfig().setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT).setPort(12299);
    // C服务的服务端
    CSampleServiceImpl refC = new CSampleServiceImpl();
    ProviderConfig<SampleService> serviceBeanC = new ProviderConfig<SampleService>();
    serviceBeanC.setInterfaceId(SampleService.class.getName());
    serviceBeanC.setApplication(new ApplicationConfig().setAppName("CCC"));
    serviceBeanC.setUniqueId("C3");
    serviceBeanC.setRef(refC);
    serviceBeanC.setServer(serverConfig);
    serviceBeanC.setRegister(false);
    serviceBeanC.export();
    // D服务的服务端
    DSampleServiceImpl refD = new DSampleServiceImpl();
    ProviderConfig<SampleService> serviceBeanD = new ProviderConfig<SampleService>();
    serviceBeanD.setInterfaceId(SampleService.class.getName());
    serviceBeanD.setApplication(new ApplicationConfig().setAppName("DDD"));
    serviceBeanD.setUniqueId("D3");
    serviceBeanD.setRef(refD);
    serviceBeanD.setServer(serverConfig);
    serviceBeanD.setRegister(false);
    serviceBeanD.export();
    // B服务里的C服务客户端
    ConsumerConfig referenceBeanC = new ConsumerConfig();
    referenceBeanC.setApplication(new ApplicationConfig().setAppName("BBB"));
    referenceBeanC.setInterfaceId(SampleService.class.getName());
    referenceBeanC.setUniqueId("C3");
    referenceBeanC.setDirectUrl("localhost:12299");
    referenceBeanC.setTimeout(1000);
    MethodConfig methodConfigC = new MethodConfig().setName("hello").setInvokeType(RpcConstants.INVOKER_TYPE_CALLBACK);
    referenceBeanC.setMethods(Collections.singletonList(methodConfigC));
    SampleService sampleServiceC = (SampleService) referenceBeanC.refer();
    // B服务里的D服务客户端
    ConsumerConfig referenceBeanD = new ConsumerConfig();
    referenceBeanD.setApplication(new ApplicationConfig().setAppName("BBB"));
    referenceBeanD.setInterfaceId(SampleService.class.getName());
    referenceBeanD.setUniqueId("D3");
    referenceBeanD.setDirectUrl("localhost:12299?p=1&v=4.0");
    referenceBeanD.setTimeout(1000);
    MethodConfig methodConfigD = new MethodConfig().setName("hello").setInvokeType(RpcConstants.INVOKER_TYPE_CALLBACK);
    referenceBeanD.setMethods(Collections.singletonList(methodConfigD));
    SampleService sampleServiceD = (SampleService) referenceBeanD.refer();
    // B服务的服务端
    BCallbackSampleServiceImpl refB = new BCallbackSampleServiceImpl(sampleServiceC, sampleServiceD);
    ProviderConfig<SampleService> ServiceBeanB = new ProviderConfig<SampleService>();
    ServiceBeanB.setInterfaceId(SampleService.class.getName());
    ServiceBeanB.setApplication(new ApplicationConfig().setAppName("BBB"));
    ServiceBeanB.setUniqueId("B3");
    ServiceBeanB.setRef(refB);
    ServiceBeanB.setServer(serverConfig);
    ServiceBeanB.setRegister(false);
    ServiceBeanB.export();
    // A 服务
    final String[] str = new String[1];
    final CountDownLatch[] latch = new CountDownLatch[] { new CountDownLatch(1) };
    final RpcInvokeContext[] contexts = new RpcInvokeContext[1];
    ConsumerConfig referenceBeanA = new ConsumerConfig();
    referenceBeanA.setApplication(new ApplicationConfig().setAppName("AAA"));
    referenceBeanA.setUniqueId("B3");
    referenceBeanA.setInterfaceId(SampleService.class.getName());
    referenceBeanA.setDirectUrl("localhost:12299");
    referenceBeanA.setTimeout(3000);
    MethodConfig methodConfigA = new MethodConfig().setName("hello").setInvokeType(RpcConstants.INVOKER_TYPE_CALLBACK);
    methodConfigA.setOnReturn(new SofaResponseCallback() {

        @Override
        public void onAppResponse(Object appResponse, String methodName, RequestBase request) {
            Assert.assertNotSame(RpcInvokeContext.getContext(), contexts[0]);
            str[0] = (String) appResponse;
            latch[0].countDown();
        }

        @Override
        public void onAppException(Throwable t, String methodName, RequestBase request) {
            assertEquals("sampleService", t.getMessage());
            assertEquals("sayException", methodName);
        }

        @Override
        public void onSofaException(SofaRpcException sofaException, String methodName, RequestBase request) {
            // never go to this
            assertEquals("sampleService", sofaException.getMessage());
            assertEquals("sayException", methodName);
        }
    });
    referenceBeanA.setMethods(Collections.singletonList(methodConfigA));
    SampleService service = (SampleService) referenceBeanA.refer();
    // 开始测试
    RpcInvokeContext context = RpcInvokeContext.getContext();
    contexts[0] = context;
    context.putRequestBaggage("reqBaggageB", "a2bbb");
    context.putRequestBaggage("reqBaggageC", "a2ccc");
    context.putRequestBaggage("reqBaggageD", "a2ddd");
    String ret = service.hello();
    Assert.assertEquals(ret, null);
    latch[0].await(5000, TimeUnit.MILLISECONDS);
    ret = str[0];
    Assert.assertEquals(ret, "hello world chello world d");
    Assert.assertEquals(refB.getReqBaggage(), "a2bbb");
    Assert.assertEquals(refC.getReqBaggage(), "a2ccc");
    Assert.assertEquals(refD.getReqBaggage(), "a2ddd");
    Assert.assertEquals(context.getResponseBaggage("respBaggageB"), "b2aaa");
    Assert.assertEquals(context.getResponseBaggage("respBaggageC"), "c2aaa");
    Assert.assertEquals(context.getResponseBaggage("respBaggageD"), "d2aaa");
    Assert.assertNull(context.getResponseBaggage("respBaggageB_force"));
    Assert.assertNull(context.getResponseBaggage("respBaggageC_force"));
    Assert.assertNull(context.getResponseBaggage("respBaggageD_force"));
    RpcInvokeContext.removeContext();
    context = RpcInvokeContext.getContext();
    contexts[0] = context;
    latch[0] = new CountDownLatch(1);
    str[0] = null;
    ret = null;
    ret = service.hello();
    Assert.assertEquals(ret, null);
    latch[0].await(5000, TimeUnit.MILLISECONDS);
    ret = str[0];
    Assert.assertEquals(ret, "hello world chello world d");
    Assert.assertNull(refB.getReqBaggage());
    Assert.assertNull(refC.getReqBaggage());
    Assert.assertNull(refD.getReqBaggage());
    Assert.assertNull(context.getResponseBaggage("respBaggageB"));
    Assert.assertNull(context.getResponseBaggage("respBaggageC"));
    Assert.assertNull(context.getResponseBaggage("respBaggageD"));
    Assert.assertEquals(context.getResponseBaggage("respBaggageB_force"), "b2aaaff");
    Assert.assertEquals(context.getResponseBaggage("respBaggageC_force"), "c2aaaff");
    Assert.assertEquals(context.getResponseBaggage("respBaggageD_force"), "d2aaaff");
}
Also used : RpcInvokeContext(com.alipay.sofa.rpc.context.RpcInvokeContext) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) SofaResponseCallback(com.alipay.sofa.rpc.core.invoke.SofaResponseCallback) CountDownLatch(java.util.concurrent.CountDownLatch) RequestBase(com.alipay.sofa.rpc.core.request.RequestBase) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) MethodConfig(com.alipay.sofa.rpc.config.MethodConfig) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig)

Example 84 with ProviderConfig

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

the class TripleHessianInvokeTest method testTripleRpcInvokeContext.

@Test
public void testTripleRpcInvokeContext() {
    ApplicationConfig clientApp = new ApplicationConfig().setAppName("triple-client");
    ApplicationConfig serverApp = new ApplicationConfig().setAppName("triple-server");
    int port = getPort();
    ServerConfig serverConfig = new ServerConfig().setProtocol(RpcConstants.PROTOCOL_TYPE_TRIPLE).setCoreThreads(1).setMaxThreads(1).setQueues(10).setPort(port);
    TripleHessianInterfaceImpl ref = new TripleHessianInterfaceImpl();
    ProviderConfig<TripleHessianInterface> providerConfig = getProviderConfig().setApplication(serverApp).setBootstrap(RpcConstants.PROTOCOL_TYPE_TRIPLE).setInterfaceId(TripleHessianInterface.class.getName()).setRef(ref).setServer(serverConfig).setRegister(false);
    providerConfig.export();
    ConsumerConfig<TripleHessianInterface> consumerConfig = new ConsumerConfig<TripleHessianInterface>();
    consumerConfig.setInterfaceId(TripleHessianInterface.class.getName()).setProtocol(RpcConstants.PROTOCOL_TYPE_TRIPLE).setDirectUrl("localhost:" + port).setTimeout(300000).setRegister(false).setApplication(clientApp);
    TripleHessianInterface helloService = consumerConfig.refer();
    Predicate<String> firstThreadInPool = s -> s.endsWith("T1");
    // setThreadLocal
    String key1 = "key1";
    String value1 = "value1";
    String value2 = "value2";
    String threadName1 = helloService.setRpcInvokeContext(key1, value1);
    String threadName2 = helloService.setRpcInvokeContext(key1, value2);
    Assert.assertTrue(firstThreadInPool.test(threadName1));
    Assert.assertTrue(firstThreadInPool.test(threadName2));
    // getThreadLocal
    String value = helloService.getRpcInvokeContext(key1);
    Assert.assertNull(value);
}
Also used : ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) SofaTracerSpan(com.alipay.common.tracer.core.span.SofaTracerSpan) Logger(com.alipay.sofa.rpc.log.Logger) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) Predicate(java.util.function.Predicate) Test(org.junit.Test) SofaTraceContextHolder(com.alipay.common.tracer.core.holder.SofaTraceContextHolder) LoggerFactory(com.alipay.sofa.rpc.log.LoggerFactory) RpcConstants(com.alipay.sofa.rpc.common.RpcConstants) SofaTracer(com.alipay.common.tracer.core.SofaTracer) RandomUtils(org.apache.commons.lang.math.RandomUtils) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) RpcRunningState(com.alipay.sofa.rpc.context.RpcRunningState) TracerUtils(com.alipay.common.tracer.core.utils.TracerUtils) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) Assert(org.junit.Assert) Before(org.junit.Before) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) Test(org.junit.Test)

Example 85 with ProviderConfig

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

the class AsyncCallbackTest method testTimeoutException.

@Test
public void testTimeoutException() {
    serverConfig = new ServerConfig().setPort(22221).setDaemon(false);
    // C服务的服务端
    CProvider = new ProviderConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl(500)).setServer(serverConfig);
    CProvider.export();
    // B调C的客户端
    Filter filter = new TestAsyncFilter();
    BConsumer = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setInvokeType(RpcConstants.INVOKER_TYPE_CALLBACK).setTimeout(1).setFilterRef(Arrays.asList(filter)).setDirectUrl("bolt://127.0.0.1:22221");
    HelloService helloService = BConsumer.refer();
    final CountDownLatch latch = new CountDownLatch(1);
    final String[] ret = { null };
    final boolean[] hasExp = { false };
    RpcInvokeContext.getContext().setResponseCallback(new SofaResponseCallback() {

        @Override
        public void onAppResponse(Object appResponse, String methodName, RequestBase request) {
            LOGGER.info("B get result: {}", appResponse);
            latch.countDown();
        }

        @Override
        public void onAppException(Throwable throwable, String methodName, RequestBase request) {
            LOGGER.info("B get app exception: {}", throwable);
            latch.countDown();
        }

        @Override
        public void onSofaException(SofaRpcException sofaException, String methodName, RequestBase request) {
            LOGGER.info("B get sofa exception: {}", sofaException);
            if (sofaException instanceof SofaTimeOutException) {
                hasExp[0] = true;
            }
            latch.countDown();
        }
    });
    String ret0 = helloService.sayHello("xxx", 22);
    // 第一次返回null
    Assert.assertNull(ret0);
    try {
        latch.await(2000, TimeUnit.MILLISECONDS);
    } catch (InterruptedException ignore) {
    }
    // 一定是一个超时异常
    assertTrue(hasExp[0]);
    RpcInvokeContext.removeContext();
}
Also used : ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) HelloService(com.alipay.sofa.rpc.test.HelloService) SofaResponseCallback(com.alipay.sofa.rpc.core.invoke.SofaResponseCallback) HelloServiceImpl(com.alipay.sofa.rpc.test.HelloServiceImpl) CountDownLatch(java.util.concurrent.CountDownLatch) RequestBase(com.alipay.sofa.rpc.core.request.RequestBase) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) Filter(com.alipay.sofa.rpc.filter.Filter) SofaTimeOutException(com.alipay.sofa.rpc.core.exception.SofaTimeOutException) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) Test(org.junit.Test) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest)

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