Search in sources :

Example 11 with ConsumerConfig

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

the class Http2ClearTextHessianTest method testHessian.

@Test
public void testHessian() {
    // 只有1个线程 执行
    ServerConfig serverConfig = new ServerConfig().setStopTimeout(60000).setPort(12300).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C).setDaemon(true);
    // 发布一个服务,每个请求要执行1秒
    ProviderConfig<HttpService> providerConfig = new ProviderConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setRef(new HttpServiceImpl()).setApplication(new ApplicationConfig().setAppName("serverApp")).setServer(serverConfig).setRegister(false);
    providerConfig.export();
    {
        ConsumerConfig<HttpService> consumerConfig = new ConsumerConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setDirectUrl("h2c://127.0.0.1:12300").setApplication(new ApplicationConfig().setAppName("clientApp")).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C);
        HttpService httpService = consumerConfig.refer();
        ExampleObj request = new ExampleObj();
        request.setId(200);
        request.setName("yyy");
        ExampleObj response = httpService.object(request);
        Assert.assertEquals(200, response.getId());
        Assert.assertEquals("yyyxx", response.getName());
    }
    {
        ConsumerConfig<HttpService> consumerConfig2 = new ConsumerConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setDirectUrl("h2c://127.0.0.1:12300").setApplication(new ApplicationConfig().setAppName("clientApp")).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C).setInvokeType(RpcConstants.INVOKER_TYPE_ONEWAY).setRepeatedReferLimit(-1);
        HttpService httpService2 = consumerConfig2.refer();
        EchoRequest request = EchoRequest.newBuilder().setGroup(Group.A).setName("xxx").build();
        try {
            httpService2.echoPb(request);
            // NOT SUPPORTED NOW, If want support this, need add key to head.
            Assert.fail();
        } catch (Exception e) {
        }
    }
    {
        ConsumerConfig<HttpService> consumerConfig3 = new ConsumerConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setDirectUrl("h2c://127.0.0.1:12300").setApplication(new ApplicationConfig().setAppName("clientApp")).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C).setInvokeType(RpcConstants.INVOKER_TYPE_FUTURE).setRepeatedReferLimit(-1);
        HttpService httpService3 = consumerConfig3.refer();
        ExampleObj request = new ExampleObj();
        request.setId(200);
        request.setName("yyy");
        ExampleObj response = httpService3.object(request);
        Assert.assertNull(response);
        ResponseFuture<ExampleObj> future = RpcInvokeContext.getContext().getFuture();
        try {
            response = future.get();
            Assert.assertEquals(200, response.getId());
            Assert.assertEquals("yyyxx", response.getName());
        } catch (Exception e) {
            e.printStackTrace();
            Assert.fail();
        }
    }
    {
        final ExampleObj[] result = new ExampleObj[1];
        final CountDownLatch latch = new CountDownLatch(1);
        ConsumerConfig<HttpService> consumerConfig4 = new ConsumerConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setDirectUrl("h2c://127.0.0.1:12300").setApplication(new ApplicationConfig().setAppName("clientApp")).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C).setInvokeType(RpcConstants.INVOKER_TYPE_CALLBACK).setOnReturn(new SofaResponseCallback() {

            @Override
            public void onAppResponse(Object appResponse, String methodName, RequestBase request) {
                result[0] = (ExampleObj) 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();
            }
        }).setRepeatedReferLimit(-1);
        HttpService httpService4 = consumerConfig4.refer();
        ExampleObj request = new ExampleObj();
        request.setId(200);
        request.setName("yyy");
        ExampleObj response = httpService4.object(request);
        Assert.assertNull(response);
        try {
            latch.await(2000, TimeUnit.MILLISECONDS);
            response = result[0];
            Assert.assertEquals(200, response.getId());
            Assert.assertEquals("yyyxx", response.getName());
        } catch (Exception e) {
            e.printStackTrace();
            Assert.fail();
        }
    }
}
Also used : EchoRequest(com.alipay.sofa.rpc.server.bolt.pb.EchoRequest) SofaResponseCallback(com.alipay.sofa.rpc.core.invoke.SofaResponseCallback) ResponseFuture(com.alipay.sofa.rpc.message.ResponseFuture) CountDownLatch(java.util.concurrent.CountDownLatch) HttpServiceImpl(com.alipay.sofa.rpc.server.http.HttpServiceImpl) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) 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) ExampleObj(com.alipay.sofa.rpc.server.http.ExampleObj) HttpService(com.alipay.sofa.rpc.server.http.HttpService) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) Test(org.junit.Test) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest)

Example 12 with ConsumerConfig

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

the class AllConnectConnectionHolderTest method getAvailableClientTransport2.

@Test
public void getAvailableClientTransport2() throws Exception {
    ConsumerConfig<HelloService> consumerConfig = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setDirectUrl("bolt://127.0.0.1:22223,bolt://127.0.0.1:22224").setConnectionHolder("all").setRegister(false).setLazy(true).setTimeout(3000);
    HelloService helloService = consumerConfig.refer();
    ClientProxyInvoker invoker = (ClientProxyInvoker) ProxyFactory.getInvoker(helloService, consumerConfig.getProxy());
    Cluster cluster = invoker.getCluster();
    Assert.assertTrue(cluster.getConnectionHolder() instanceof AllConnectConnectionHolder);
    AllConnectConnectionHolder holder = (AllConnectConnectionHolder) cluster.getConnectionHolder();
    Assert.assertTrue(holder.isAvailableEmpty());
    Assert.assertNotNull(holder.getAvailableClientTransport(ProviderHelper.toProviderInfo("bolt://127.0.0.1:22223")));
    Assert.assertNotNull(holder.getAvailableClientTransport(ProviderHelper.toProviderInfo("bolt://127.0.0.1:22224")));
    consumerConfig.unRefer();
}
Also used : AllConnectConnectionHolder(com.alipay.sofa.rpc.client.AllConnectConnectionHolder) HelloService(com.alipay.sofa.rpc.test.HelloService) Cluster(com.alipay.sofa.rpc.client.Cluster) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) ClientProxyInvoker(com.alipay.sofa.rpc.client.ClientProxyInvoker) Test(org.junit.Test) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest)

Example 13 with ConsumerConfig

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

the class FailoverClusterTest method testPinpoint.

@Test
public void testPinpoint() {
    // 发布一个服务,每个请求要执行2秒
    ServerConfig serverConfig = new ServerConfig().setStopTimeout(0).setPort(22225).setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT).setQueues(100).setCoreThreads(5).setMaxThreads(5);
    ProviderConfig<HelloService> providerConfig = new ProviderConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl("55")).setServer(serverConfig).setRepeatedExportLimit(-1).setRegister(false);
    providerConfig.export();
    // 再发布一个服务,不等待
    ServerConfig serverConfig2 = new ServerConfig().setStopTimeout(0).setPort(22226).setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT).setQueues(100).setCoreThreads(5).setMaxThreads(5);
    ProviderConfig<HelloService> providerConfig2 = new ProviderConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl("66")).setServer(serverConfig2).setRepeatedExportLimit(-1).setRegister(false);
    providerConfig2.export();
    ConsumerConfig<HelloService> consumerConfig = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setDirectUrl("bolt://127.0.0.1:22225;bolt://127.0.0.1:22226").setTimeout(1000).setCluster("failover").setLoadBalancer("random").setRegister(false);
    final HelloService helloService = consumerConfig.refer();
    int count2 = 0;
    for (int i = 0; i < 10; i++) {
        try {
            RpcInvokeContext.getContext().setTargetURL("127.0.0.1:22225");
            Assert.assertEquals("55", helloService.sayHello("xxx", 22));
            count2++;
        } catch (Exception ignore) {
        }
    }
    Assert.assertEquals(count2, 10);
    count2 = 0;
    for (int i = 0; i < 10; i++) {
        try {
            RpcInvokeContext.getContext().setTargetURL("127.0.0.1:22226");
            Assert.assertEquals("66", helloService.sayHello("xxx", 22));
            count2++;
        } catch (Exception ignore) {
        }
    }
    Assert.assertEquals(count2, 10);
    boolean error = false;
    try {
        RpcInvokeContext.getContext().setTargetURL("127.0.0.1:22227");
        Assert.assertEquals("66", helloService.sayHello("xxx", 22));
    } catch (Exception e) {
        error = true;
    }
    Assert.assertTrue(error);
}
Also used : ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) HelloService(com.alipay.sofa.rpc.test.HelloService) HelloServiceImpl(com.alipay.sofa.rpc.test.HelloServiceImpl) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) SofaRouteException(com.alipay.sofa.rpc.core.exception.SofaRouteException) SofaTimeOutException(com.alipay.sofa.rpc.core.exception.SofaTimeOutException) Test(org.junit.Test) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest)

Example 14 with ConsumerConfig

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

the class FailoverClusterTest method testSingleServer.

@Test
public void testSingleServer() {
    // 只有2个线程 执行
    ServerConfig serverConfig = new ServerConfig().setStopTimeout(0).setPort(22222).setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT).setQueues(100).setCoreThreads(5).setMaxThreads(5);
    // 发布一个服务,每个请求要执行1秒
    ProviderConfig<HelloService> providerConfig = new ProviderConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl() {

        AtomicInteger cnt = new AtomicInteger();

        @Override
        public String sayHello(String name, int age) {
            if (cnt.getAndIncrement() % 3 != 0) {
                try {
                    Thread.sleep(2000);
                } catch (Exception ignore) {
                }
            }
            LOGGER.info("xxxxxxxxxxxxxxxxx" + age);
            return "hello " + name + " from server! age: " + age;
        }
    }).setServer(serverConfig).setRegister(false);
    providerConfig.export();
    ConsumerConfig<HelloService> consumerConfig = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setDirectUrl("bolt://127.0.0.1:22222").setCluster("failover").setTimeout(1000).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(2, count1);
    ConsumerConfig<HelloService> consumerConfig2 = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setDirectUrl("bolt://127.0.0.1:22222").setTimeout(1000).setCluster("failover").setRetries(// 失败后自动重试2次
    2).setRegister(false);
    final HelloService helloService2 = consumerConfig2.refer();
    int count2 = 0;
    for (int i = 0; i < 4; i++) {
        try {
            helloService2.sayHello("xxx", 22);
            count2++;
        } catch (Exception ignore) {
            ignore.printStackTrace();
        }
    }
    Assert.assertEquals(4, count2);
    Cluster cluster = consumerConfig2.getConsumerBootstrap().getCluster();
    Assert.assertTrue(cluster.isAvailable());
}
Also used : 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) SofaRouteException(com.alipay.sofa.rpc.core.exception.SofaRouteException) SofaTimeOutException(com.alipay.sofa.rpc.core.exception.SofaTimeOutException) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) Test(org.junit.Test) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest)

Example 15 with ConsumerConfig

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

the class FailoverClusterTest method testMultiServer.

@Test
public void testMultiServer() {
    // 发布一个服务,每个请求要执行2秒
    ServerConfig serverConfig = new ServerConfig().setStopTimeout(0).setPort(22223).setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT).setQueues(100).setCoreThreads(5).setMaxThreads(5);
    ProviderConfig<HelloService> providerConfig = new ProviderConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl(2000)).setServer(serverConfig).setRepeatedExportLimit(-1).setRegister(false);
    providerConfig.export();
    // 再发布一个服务,不等待
    ServerConfig serverConfig2 = new ServerConfig().setStopTimeout(0).setPort(22224).setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT).setQueues(100).setCoreThreads(5).setMaxThreads(5);
    ProviderConfig<HelloService> providerConfig2 = new ProviderConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl()).setServer(serverConfig2).setRepeatedExportLimit(-1).setRegister(false);
    providerConfig2.export();
    ConsumerConfig<HelloService> consumerConfig = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setDirectUrl("bolt://127.0.0.1:22223;bolt://127.0.0.1:22224").setTimeout(1000).setCluster("failover").setRetries(// 失败后重试一次
    1).setRegister(false);
    final HelloService helloService = consumerConfig.refer();
    int count2 = 0;
    for (int i = 0; i < 4; i++) {
        try {
            helloService.sayHello("xxx", 22);
            count2++;
        } catch (Exception ignore) {
            ignore.printStackTrace();
        }
    }
    Assert.assertEquals(4, count2);
}
Also used : ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) HelloService(com.alipay.sofa.rpc.test.HelloService) HelloServiceImpl(com.alipay.sofa.rpc.test.HelloServiceImpl) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) SofaRouteException(com.alipay.sofa.rpc.core.exception.SofaRouteException) SofaTimeOutException(com.alipay.sofa.rpc.core.exception.SofaTimeOutException) 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