Search in sources :

Example 1 with HttpService

use of com.alipay.sofa.rpc.server.http.HttpService in project sofa-rpc by sofastack.

the class Http2ClearTextExceptionTest method testProtobuf.

@Test
public void testProtobuf() throws InterruptedException {
    // 只有1个线程 执行
    ServerConfig serverConfig = new ServerConfig().setStopTimeout(60000).setPort(12333).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C).setDaemon(true);
    // 发布一个服务,每个请求要执行1秒
    ProviderConfig<HttpService> providerConfig = new ProviderConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setRef(new HttpServiceImpl()).setServer(serverConfig).setApplication(new ApplicationConfig().setAppName("serverApp")).setRegister(false);
    providerConfig.export();
    {
        ConsumerConfig<HttpService> consumerConfig = new ConsumerConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setSerialization(RpcConstants.SERIALIZE_PROTOBUF).setDirectUrl("h2c://127.0.0.1:12333").setApplication(new ApplicationConfig().setAppName("clientApp")).setTimeout(500).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C);
        HttpService httpService = consumerConfig.refer();
        EchoRequest request = EchoRequest.newBuilder().setGroup(Group.B).setName("xxx").build();
        try {
            EchoResponse response = httpService.echoPb(request);
            Assert.fail();
        } catch (SofaRpcException e) {
            Assert.assertTrue(e.getErrorType() == RpcErrorType.SERVER_BIZ);
        }
    }
    {
        ConsumerConfig<HttpService> consumerConfig2 = new ConsumerConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setSerialization(RpcConstants.SERIALIZE_PROTOBUF).setDirectUrl("h2c://127.0.0.1:12333").setApplication(new ApplicationConfig().setAppName("clientApp")).setTimeout(500).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C).setInvokeType(RpcConstants.INVOKER_TYPE_ONEWAY).setRepeatedReferLimit(-1);
        HttpService httpService2 = consumerConfig2.refer();
        EchoRequest request = EchoRequest.newBuilder().setGroup(Group.B).setName("xxx").build();
        try {
            httpService2.echoPb(request);
            // NOT SUPPORTED NOW, If want support this, need add key to head.
            Assert.fail();
        } catch (Exception e) {
            Assert.assertTrue(e instanceof SofaRpcException);
        }
    }
    {
        ConsumerConfig<HttpService> consumerConfig3 = new ConsumerConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setSerialization(RpcConstants.SERIALIZE_PROTOBUF).setDirectUrl("h2c://127.0.0.1:12333").setApplication(new ApplicationConfig().setAppName("clientApp")).setTimeout(500).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C).setInvokeType(RpcConstants.INVOKER_TYPE_FUTURE).setRepeatedReferLimit(-1);
        HttpService httpService3 = consumerConfig3.refer();
        EchoRequest request = EchoRequest.newBuilder().setGroup(Group.B).setName("xxx").build();
        EchoResponse response = httpService3.echoPb(request);
        Assert.assertNull(response);
        ResponseFuture future = RpcInvokeContext.getContext().getFuture();
        try {
            future.get();
            Assert.fail();
        } catch (ExecutionException e) {
            SofaRpcException re = (SofaRpcException) e.getCause();
            Assert.assertTrue(re.getErrorType() == RpcErrorType.SERVER_BIZ);
        }
    }
    {
        final Object[] result = new Object[1];
        final CountDownLatch latch = new CountDownLatch(1);
        ConsumerConfig<HttpService> consumerConfig4 = new ConsumerConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setSerialization(RpcConstants.SERIALIZE_PROTOBUF).setTimeout(500).setDirectUrl("h2c://127.0.0.1:12333").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] = appResponse;
                latch.countDown();
            }

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

            @Override
            public void onSofaException(SofaRpcException exception, String methodName, RequestBase request) {
                result[0] = exception;
                latch.countDown();
            }
        }).setRepeatedReferLimit(-1);
        HttpService httpService4 = consumerConfig4.refer();
        EchoRequest request = EchoRequest.newBuilder().setGroup(Group.B).setName("xxx").build();
        EchoResponse response = httpService4.echoPb(request);
        Assert.assertNull(response);
        latch.await(2000, TimeUnit.MILLISECONDS);
        Throwable e = (Throwable) result[0];
        Assert.assertTrue(e instanceof SofaRpcException);
        Assert.assertTrue(((SofaRpcException) e).getErrorType() == RpcErrorType.SERVER_BIZ);
    }
}
Also used : EchoResponse(com.alipay.sofa.rpc.server.bolt.pb.EchoResponse) 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) ExecutionException(java.util.concurrent.ExecutionException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) RequestBase(com.alipay.sofa.rpc.core.request.RequestBase) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) HttpService(com.alipay.sofa.rpc.server.http.HttpService) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest)

Example 2 with HttpService

use of com.alipay.sofa.rpc.server.http.HttpService 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 3 with HttpService

use of com.alipay.sofa.rpc.server.http.HttpService in project sofa-rpc by sofastack.

the class Http2ClearTextPriorKnowledgeTest method testProtobuf.

@Test
public void testProtobuf() {
    // 只有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()).setSerialization(RpcConstants.SERIALIZE_PROTOBUF).setDirectUrl("h2c://127.0.0.1:12300").setApplication(new ApplicationConfig().setAppName("clientApp")).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C);
        HttpService httpService = consumerConfig.refer();
        EchoRequest request = EchoRequest.newBuilder().setGroup(Group.A).setName("xxx").build();
        EchoResponse response = httpService.echoPb(request);
        Assert.assertEquals("helloxxx", response.getMessage());
        Assert.assertEquals(200, response.getCode());
    }
    {
        ConsumerConfig<HttpService> consumerConfig2 = new ConsumerConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setSerialization(RpcConstants.SERIALIZE_PROTOBUF).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()).setSerialization(RpcConstants.SERIALIZE_PROTOBUF).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();
        EchoRequest request = EchoRequest.newBuilder().setGroup(Group.A).setName("xxx").build();
        EchoResponse response = httpService3.echoPb(request);
        Assert.assertNull(response);
        ResponseFuture future = RpcInvokeContext.getContext().getFuture();
        try {
            response = (EchoResponse) future.get();
            Assert.assertEquals("helloxxx", response.getMessage());
            Assert.assertEquals(200, response.getCode());
        } catch (Exception e) {
            e.printStackTrace();
            Assert.fail();
        }
    }
    {
        final EchoResponse[] result = new EchoResponse[1];
        final CountDownLatch latch = new CountDownLatch(1);
        ConsumerConfig<HttpService> consumerConfig4 = new ConsumerConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setSerialization(RpcConstants.SERIALIZE_PROTOBUF).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] = (EchoResponse) 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();
        EchoRequest request = EchoRequest.newBuilder().setGroup(Group.A).setName("xxx").build();
        EchoResponse response = httpService4.echoPb(request);
        Assert.assertNull(response);
        try {
            latch.await(2000, TimeUnit.MILLISECONDS);
            response = result[0];
            Assert.assertNotNull(response);
            Assert.assertEquals("helloxxx", response.getMessage());
            Assert.assertEquals(200, response.getCode());
        } catch (Exception e) {
            e.printStackTrace();
            Assert.fail();
        }
    }
}
Also used : EchoResponse(com.alipay.sofa.rpc.server.bolt.pb.EchoResponse) 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) 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 4 with HttpService

use of com.alipay.sofa.rpc.server.http.HttpService in project sofa-rpc by sofastack.

the class H2cDirectUrlTest method testAll.

@Test
public void testAll() throws InterruptedException {
    // 只有1个线程 执行
    ServerConfig serverConfig = new ServerConfig().setPort(12300).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C).setDaemon(true);
    // 发布一个服务,每个请求要执行1秒
    ProviderConfig<HttpService> providerConfig = new ProviderConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setRef(new HttpServiceImpl()).setBootstrap("h2c").setApplication(new ApplicationConfig().setAppName("serverApp")).setServer(serverConfig).setRegister(false);
    providerConfig.export();
    final ConsumerConfig<HttpService> consumerConfig = new ConsumerConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setDirectUrl("h2c://127.0.0.1:12300").setProtocol(RpcConstants.PROTOCOL_TYPE_H2C).setBootstrap("h2c").setApplication(new ApplicationConfig().setAppName("clientApp")).setReconnectPeriod(1000);
    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());
    serverConfig.getServer().stop();
    // 关闭后再调用一个抛异常
    try {
        httpService.object(request);
    } catch (Exception e) {
        // 应该抛出异常
        Assert.assertTrue(e instanceof SofaRpcException);
    }
    Assert.assertTrue(TestUtils.delayGet(new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            return CommonUtils.isEmpty(consumerConfig.getConsumerBootstrap().getCluster().getConnectionHolder().getAvailableConnections());
        }
    }, true, 50, 40));
    serverConfig.getServer().start();
    // 等待客户端重连服务端
    Assert.assertTrue(TestUtils.delayGet(new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            return CommonUtils.isNotEmpty(consumerConfig.getConsumerBootstrap().getCluster().getConnectionHolder().getAvailableConnections());
        }
    }, true, 50, 60));
    response = httpService.object(request);
    Assert.assertEquals(200, response.getId());
    Assert.assertEquals("yyyxx", response.getName());
}
Also used : 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) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) HttpServiceImpl(com.alipay.sofa.rpc.server.http.HttpServiceImpl) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) Callable(java.util.concurrent.Callable) Test(org.junit.Test) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest)

Example 5 with HttpService

use of com.alipay.sofa.rpc.server.http.HttpService in project sofa-rpc by sofastack.

the class Http2ClearTextHttpUpgradeTest method testProtobuf.

@Test
public void testProtobuf() {
    // 只有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()).setSerialization(RpcConstants.SERIALIZE_PROTOBUF).setDirectUrl("h2c://127.0.0.1:12300").setApplication(new ApplicationConfig().setAppName("clientApp")).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C);
        HttpService httpService = consumerConfig.refer();
        EchoRequest request = EchoRequest.newBuilder().setGroup(Group.A).setName("xxx").build();
        EchoResponse response = httpService.echoPb(request);
        Assert.assertEquals("helloxxx", response.getMessage());
        Assert.assertEquals(200, response.getCode());
    }
    {
        ConsumerConfig<HttpService> consumerConfig2 = new ConsumerConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setSerialization(RpcConstants.SERIALIZE_PROTOBUF).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()).setSerialization(RpcConstants.SERIALIZE_PROTOBUF).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();
        EchoRequest request = EchoRequest.newBuilder().setGroup(Group.A).setName("xxx").build();
        EchoResponse response = httpService3.echoPb(request);
        Assert.assertNull(response);
        ResponseFuture future = RpcInvokeContext.getContext().getFuture();
        try {
            response = (EchoResponse) future.get();
            Assert.assertEquals("helloxxx", response.getMessage());
            Assert.assertEquals(200, response.getCode());
        } catch (Exception e) {
            e.printStackTrace();
            Assert.fail();
        }
    }
    {
        final EchoResponse[] result = new EchoResponse[1];
        final CountDownLatch latch = new CountDownLatch(1);
        ConsumerConfig<HttpService> consumerConfig4 = new ConsumerConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setSerialization(RpcConstants.SERIALIZE_PROTOBUF).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] = (EchoResponse) 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();
        EchoRequest request = EchoRequest.newBuilder().setGroup(Group.A).setName("xxx").build();
        EchoResponse response = httpService4.echoPb(request);
        Assert.assertNull(response);
        try {
            latch.await(2000, TimeUnit.MILLISECONDS);
            response = result[0];
            Assert.assertNotNull(response);
            Assert.assertEquals("helloxxx", response.getMessage());
            Assert.assertEquals(200, response.getCode());
        } catch (Exception e) {
            e.printStackTrace();
            Assert.fail();
        }
    }
}
Also used : EchoResponse(com.alipay.sofa.rpc.server.bolt.pb.EchoResponse) 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) 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)

Aggregations

ApplicationConfig (com.alipay.sofa.rpc.config.ApplicationConfig)7 ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)7 HttpService (com.alipay.sofa.rpc.server.http.HttpService)7 HttpServiceImpl (com.alipay.sofa.rpc.server.http.HttpServiceImpl)7 ActivelyDestroyTest (com.alipay.sofa.rpc.test.ActivelyDestroyTest)7 Test (org.junit.Test)7 ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)6 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)6 SofaResponseCallback (com.alipay.sofa.rpc.core.invoke.SofaResponseCallback)5 RequestBase (com.alipay.sofa.rpc.core.request.RequestBase)5 ResponseFuture (com.alipay.sofa.rpc.message.ResponseFuture)5 EchoRequest (com.alipay.sofa.rpc.server.bolt.pb.EchoRequest)5 EchoResponse (com.alipay.sofa.rpc.server.bolt.pb.EchoResponse)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 ProviderConfig (com.alipay.sofa.rpc.config.ProviderConfig)2 ExampleObj (com.alipay.sofa.rpc.server.http.ExampleObj)2 SofaTimeOutException (com.alipay.sofa.rpc.core.exception.SofaTimeOutException)1 ClientTransportConfig (com.alipay.sofa.rpc.transport.ClientTransportConfig)1 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)1 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)1