Search in sources :

Example 16 with ConsumerConfig

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

the class FailoverClusterTest method testRpcDirectInvokeFromContextWithAvailableProviders.

@Test
public void testRpcDirectInvokeFromContextWithAvailableProviders() {
    ServerConfig serverConfig = new ServerConfig().setProtocol("bolt").setHost("0.0.0.0").setPort(13900);
    ProviderConfig<HelloService> provider = new ProviderConfig();
    provider.setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl("x-demo-invoke")).setApplication(new ApplicationConfig().setAppName("x-test-server")).setProxy("javassist").setSerialization("hessian2").setServer(serverConfig).setTimeout(3000);
    provider.export();
    ConsumerConfig<HelloService> consumer = new ConsumerConfig();
    consumer.setInterfaceId(HelloService.class.getName()).setApplication(new ApplicationConfig().setAppName("x-test-client")).setDirectUrl("bolt://127.0.0.1:65534").setProxy("javassist");
    HelloService proxy = consumer.refer();
    for (int i = 0; i < 3; i++) {
        RpcInvokeContext.getContext().setTargetURL("127.0.0.1:13900");
        Assert.assertEquals("x-demo-invoke", proxy.sayHello("x-demo-invoke", 1));
    }
    provider.unExport();
    consumer.unRefer();
}
Also used : ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) HelloService(com.alipay.sofa.rpc.test.HelloService) HelloServiceImpl(com.alipay.sofa.rpc.test.HelloServiceImpl) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) Test(org.junit.Test) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest)

Example 17 with ConsumerConfig

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

the class LazyConnectTest method testLazyFailWhenMultipleThreads.

@Test
public void testLazyFailWhenMultipleThreads() {
    ConsumerConfig<HelloService> consumerConfig = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setDirectUrl("bolt://127.0.0.1:22223").setTimeout(1000).setLazy(true).setRepeatedReferLimit(-1).setRegister(false);
    final HelloService helloService = consumerConfig.refer();
    int threads = 10;
    final AtomicInteger count1 = new AtomicInteger();
    final CountDownLatch latch = new CountDownLatch(threads);
    for (int i = 0; i < threads; i++) {
        try {
            Thread thread = new Thread(new Runnable() {

                @Override
                public void run() {
                    for (int j = 0; j < 10; j++) {
                        try {
                            helloService.sayHello("xxx", 20);
                            count1.incrementAndGet();
                        } catch (Exception ignore) {
                        }
                    }
                    latch.countDown();
                }
            }, "thread" + i);
            thread.start();
        } catch (Exception ignore) {
        }
    }
    try {
        latch.await(3000, TimeUnit.MILLISECONDS);
    } catch (Exception e) {
    }
    Assert.assertEquals(count1.get(), 0);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HelloService(com.alipay.sofa.rpc.test.HelloService) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest)

Example 18 with ConsumerConfig

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

the class LazyConnectTest method testLazy.

@Test
public void testLazy() {
    ConsumerConfig<HelloService> consumerConfig = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setDirectUrl("bolt://127.0.0.1:22222").setTimeout(1000).setLazy(true).setRepeatedReferLimit(-1).setRegister(false);
    final HelloService helloService = consumerConfig.refer();
    int count1 = 0;
    for (int i = 0; i < 4; i++) {
        try {
            helloService.sayHello("xxx", 20 + i);
            count1++;
        } catch (Exception ignore) {
        }
    }
    Assert.assertEquals(count1, 4);
}
Also used : HelloService(com.alipay.sofa.rpc.test.HelloService) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) Test(org.junit.Test) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest)

Example 19 with ConsumerConfig

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

the class AsyncCallbackTest method testAll.

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

        @Override
        public void onAppResponse(Object appResponse, String methodName, RequestBase request) {
            LOGGER.info("B get result: {}", appResponse);
            ret[0] = (String) 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);
            latch.countDown();
        }
    });
    String ret0 = helloService.sayHello("xxx", 22);
    // 第一次返回null
    Assert.assertNull(ret0);
    try {
        latch.await(60000, TimeUnit.MILLISECONDS);
    } catch (InterruptedException ignore) {
    }
    Assert.assertNotNull(ret[0]);
    // 过滤器生效
    assertTrue(ret[0].endsWith("append by async filter"));
    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) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) Test(org.junit.Test) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest)

Example 20 with ConsumerConfig

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

the class RpcContextTest method testAll.

@Test
public void testAll() {
    // 只有1个线程 执行
    ServerConfig serverConfig = new ServerConfig().setPort(22222).setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT).setQueues(100).setCoreThreads(1).setMaxThreads(2);
    // 发布一个服务,每个请求要执行1秒
    CtxHelloServiceImpl helloServiceImpl = new CtxHelloServiceImpl();
    ProviderConfig<HelloService> providerConfig = new ProviderConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setApplication(new ApplicationConfig().setAppName("test-server")).setRef(helloServiceImpl).setServer(serverConfig).setRegister(false);
    providerConfig.export();
    {
        ConsumerConfig<HelloService> consumerConfig = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setApplication(new ApplicationConfig().setAppName("test-client")).setDirectUrl("bolt://127.0.0.1:22222?appName=test-server").setTimeout(30000).setRegister(false);
        final HelloService helloService = consumerConfig.refer();
        String str = helloService.sayHello("xxx", 123);
        RpcServiceContext serviceContext = RpcContextManager.currentServiceContext(false);
        RpcReferenceContext referenceContext = RpcContextManager.lastReferenceContext(false);
        Assert.assertNull(serviceContext);
        Assert.assertNotNull(referenceContext);
        serviceContext = helloServiceImpl.serviceContext;
        Assert.assertNotNull(serviceContext);
        Assert.assertEquals(serviceContext.getCallerAppName(), "test-client");
        Assert.assertEquals(referenceContext.getTargetAppName(), "test-server");
        Assert.assertNotNull(referenceContext.getClientIP());
        Assert.assertTrue(referenceContext.getClientPort() > 0);
    }
    {
        final CountDownLatch latch = new CountDownLatch(1);
        final String[] ret = { null };
        ConsumerConfig<HelloService> consumerConfig2 = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setApplication(new ApplicationConfig().setAppName("test-client")).setDirectUrl("bolt://127.0.0.1:22222?appName=test-server").setTimeout(2000).setInvokeType("callback").setOnReturn(new SofaResponseCallback() {

            @Override
            public void onAppResponse(Object appResponse, String methodName, RequestBase request) {
                ret[0] = (String) appResponse;
                latch.countDown();
            }

            @Override
            public void onAppException(Throwable throwable, String methodName, RequestBase request) {
                latch.countDown();
            }

            @Override
            public void onSofaException(SofaRpcException sofaException, String methodName, RequestBase request) {
                latch.countDown();
            }
        }).setRegister(false);
        final HelloService helloServiceCallback = consumerConfig2.refer();
        String ret0 = helloServiceCallback.sayHello("xxx", 22);
        // 第一次返回null
        Assert.assertNull(ret0);
        RpcServiceContext serviceContext = RpcContextManager.currentServiceContext(false);
        RpcReferenceContext referenceContext = RpcContextManager.lastReferenceContext(false);
        Assert.assertNull(serviceContext);
        Assert.assertNotNull(referenceContext);
        serviceContext = helloServiceImpl.serviceContext;
        Assert.assertNotNull(serviceContext);
        Assert.assertEquals(serviceContext.getCallerAppName(), "test-client");
        Assert.assertEquals(referenceContext.getTargetAppName(), "test-server");
        Assert.assertNotNull(referenceContext.getClientIP());
        Assert.assertTrue(referenceContext.getClientPort() > 0);
        try {
            latch.await(5000, TimeUnit.MILLISECONDS);
        } catch (InterruptedException ignore) {
        }
        Assert.assertNotNull(ret[0]);
    }
}
Also used : HelloService(com.alipay.sofa.rpc.test.HelloService) SofaResponseCallback(com.alipay.sofa.rpc.core.invoke.SofaResponseCallback) CountDownLatch(java.util.concurrent.CountDownLatch) RpcServiceContext(com.alipay.sofa.rpc.api.context.RpcServiceContext) RequestBase(com.alipay.sofa.rpc.core.request.RequestBase) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) RpcReferenceContext(com.alipay.sofa.rpc.api.context.RpcReferenceContext) Test(org.junit.Test) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest)

Aggregations

ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)139 Test (org.junit.Test)86 ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)68 ApplicationConfig (com.alipay.sofa.rpc.config.ApplicationConfig)61 ProviderConfig (com.alipay.sofa.rpc.config.ProviderConfig)44 ActivelyDestroyTest (com.alipay.sofa.rpc.test.ActivelyDestroyTest)40 HelloService (com.alipay.sofa.rpc.test.HelloService)38 CountDownLatch (java.util.concurrent.CountDownLatch)27 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)21 HelloServiceImpl (com.alipay.sofa.rpc.test.HelloServiceImpl)21 ArrayList (java.util.ArrayList)19 ProviderGroup (com.alipay.sofa.rpc.client.ProviderGroup)18 SofaResponseCallback (com.alipay.sofa.rpc.core.invoke.SofaResponseCallback)14 RegistryConfig (com.alipay.sofa.rpc.config.RegistryConfig)12 RequestBase (com.alipay.sofa.rpc.core.request.RequestBase)12 MethodConfig (com.alipay.sofa.rpc.config.MethodConfig)10 ProviderInfo (com.alipay.sofa.rpc.client.ProviderInfo)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 ConsumerBootstrap (com.alipay.sofa.rpc.bootstrap.ConsumerBootstrap)8 SofaTimeOutException (com.alipay.sofa.rpc.core.exception.SofaTimeOutException)8