Search in sources :

Example 26 with ServerConfig

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

the class BizThrowExceptionTest method testAll.

@Test
public void testAll() {
    // 只有2个线程 执行
    ServerConfig serverConfig = new ServerConfig().setStopTimeout(0).setPort(22222).setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT).setQueues(100).setCoreThreads(5).setMaxThreads(5);
    // 发布一个服务,每个请求要执行1秒
    ProviderConfig<TestExceptionService> providerConfig = new ProviderConfig<TestExceptionService>().setInterfaceId(TestExceptionService.class.getName()).setRef(new TestExceptionServiceImpl()).setServer(serverConfig).setRegister(false);
    providerConfig.export();
    for (String proxy : new String[] { "jdk", "javassist" }) {
        ConsumerConfig<TestExceptionService> consumerConfig = new ConsumerConfig<TestExceptionService>().setInterfaceId(TestExceptionService.class.getName()).setDirectUrl("bolt://127.0.0.1:22222").setTimeout(1000).setProxy(proxy).setRepeatedReferLimit(-1).setRegister(false);
        final TestExceptionService service = consumerConfig.refer();
        try {
            service.throwRuntimeException();
            Assert.fail();
        } catch (Throwable e) {
            Assert.assertTrue(e instanceof RuntimeException);
            Assert.assertEquals(e.getMessage(), "RuntimeException");
        }
        try {
            service.throwException();
            Assert.fail();
        } catch (Throwable e) {
            Assert.assertTrue(e instanceof Exception);
            Assert.assertEquals(e.getMessage(), "Exception");
        }
        try {
            service.throwSofaException();
            Assert.fail();
        } catch (Throwable e) {
            Assert.assertTrue(e instanceof SofaRpcException);
            Assert.assertEquals(e.getMessage(), "SofaRpcException");
        }
        try {
            service.throwDeclaredException();
            Assert.fail();
        } catch (Throwable e) {
            Assert.assertTrue(e instanceof TestException);
            Assert.assertEquals(e.getMessage(), "TestException");
        }
        try {
            service.throwDeclaredExceptionWithoutReturn();
            Assert.fail();
        } catch (Throwable e) {
            Assert.assertTrue(e instanceof TestException);
            Assert.assertEquals(e.getMessage(), "DeclaredExceptionWithoutReturn");
        }
    }
}
Also used : ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest) Test(org.junit.Test)

Example 27 with ServerConfig

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

the class GenericTest method testAll.

@Test
public void testAll() throws SofaRpcException, InterruptedException {
    // 发布服务
    ServerConfig serverConfig2 = new ServerConfig().setPort(22222).setDaemon(false);
    List<MethodConfig> methodConfigs = new ArrayList<MethodConfig>();
    MethodConfig config1 = new MethodConfig().setName("helloFuture").setInvokeType("future");
    methodConfigs.add(config1);
    MethodConfig config2 = new MethodConfig().setName("helloCallback").setInvokeType("callback").setOnReturn(new TestCallback());
    methodConfigs.add(config2);
    MethodConfig config21 = new MethodConfig().setName("helloCallbackException").setInvokeType("callback").setOnReturn(new TestCallback());
    methodConfigs.add(config21);
    MethodConfig config3 = new MethodConfig().setName("helloOneway").setInvokeType("oneway");
    methodConfigs.add(config3);
    MethodConfig config4 = new MethodConfig().setName("helloTimeout").setInvokeType("sync");
    methodConfigs.add(config4);
    // C服务的服务端
    ProviderConfig<TestInterface> CProvider = new ProviderConfig<TestInterface>().setInterfaceId(TestInterface.class.getName()).setRef(new TestClass()).setServer(serverConfig2);
    CProvider.export();
    // 引用服务
    ConsumerConfig<GenericService> BConsumer = new ConsumerConfig<GenericService>().setInterfaceId(TestInterface.class.getName()).setGeneric(true).setMethods(methodConfigs).setDirectUrl("bolt://127.0.0.1:22222").setTimeout(3000).setRetries(2);
    GenericService proxy = BConsumer.refer();
    GenericObject genericObject = new GenericObject("com.alipay.sofa.rpc.test.generic.bean.People");
    genericObject.putField("name", "Lilei");
    genericObject.putField("job", new Job("coder"));
    People people = new People();
    people.setName("Lilei");
    people.setJob(new Job("coder"));
    // sync 调用
    assertEquals(proxy.$invoke("hello", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { people }), new TestClass().hello(people));
    People peopleResult = proxy.$genericInvoke("hello", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { genericObject }, People.class);
    assertEquals(peopleResult, new TestClass().hello(people));
    GenericObject result = (GenericObject) proxy.$genericInvoke("hello", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { genericObject });
    isCorrect(result);
    // future 调用
    proxy.$invoke("helloFuture", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { people });
    assertEquals(SofaResponseFuture.getResponse(1000, true), new TestClass().helloFuture(people));
    proxy.$genericInvoke("helloFuture", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { genericObject }, People.class);
    assertEquals(SofaResponseFuture.getResponse(1000, true), new TestClass().helloFuture(people));
    proxy.$genericInvoke("helloFuture", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { genericObject });
    result = (GenericObject) SofaResponseFuture.getResponse(1000, true);
    isCorrect(result);
    // callback调用
    proxy.$invoke("helloCallback", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { people });
    TestCallback.startLatach();
    assertEquals(TestCallback.result, new TestClass().helloCallback(people));
    proxy.$genericInvoke("helloCallback", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { genericObject }, People.class);
    TestCallback.startLatach();
    assertEquals(TestCallback.result, new TestClass().helloCallback(people));
    proxy.$genericInvoke("helloCallback", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { genericObject });
    TestCallback.startLatach();
    isCorrect((GenericObject) TestCallback.result);
    TestCallback.result = null;
    // oneway调用
    proxy.$invoke("helloOneway", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { people });
    proxy.$genericInvoke("helloOneway", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { genericObject }, People.class);
    proxy.$genericInvoke("helloOneway", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { genericObject });
    // callback出现异常
    proxy.$invoke("helloCallbackException", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { people });
    TestCallback.startLatach();
    Assert.assertEquals(((Throwable) TestCallback.result).getMessage(), "Hello~");
    proxy.$genericInvoke("helloCallbackException", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { genericObject }, People.class);
    TestCallback.startLatach();
    Assert.assertEquals(((Throwable) TestCallback.result).getMessage(), "Hello~");
    proxy.$genericInvoke("helloCallbackException", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { genericObject });
    TestCallback.startLatach();
    Assert.assertEquals(((Throwable) TestCallback.result).getMessage(), "Hello~");
    testTimeout(proxy, genericObject, people);
    testComplexBean(proxy);
    testBasicBean(proxy);
}
Also used : GenericService(com.alipay.sofa.rpc.api.GenericService) ArrayList(java.util.ArrayList) People(com.alipay.sofa.rpc.test.generic.bean.People) MethodConfig(com.alipay.sofa.rpc.config.MethodConfig) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) GenericObject(com.alipay.hessian.generic.model.GenericObject) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) GenericObject(com.alipay.hessian.generic.model.GenericObject) Job(com.alipay.sofa.rpc.test.generic.bean.Job) Test(org.junit.Test) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest)

Example 28 with ServerConfig

use of com.alipay.sofa.rpc.config.ServerConfig 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 29 with ServerConfig

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

the class TripleHessianInvokeTest method testInvoke.

@Test
public void testInvoke() throws InterruptedException {
    RpcRunningState.setDebugMode(true);
    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).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).setRegister(false).setApplication(clientApp);
    TripleHessianInterface helloService = consumerConfig.refer();
    LOGGER.info("Grpc stub bean successful: {}", helloService.getClass().getName());
    helloService.call();
    Assert.assertEquals("call", ref.getFlag());
    // test Pressure Mark
    boolean isLoadTest = helloService.testPressureMark("name");
    Assert.assertFalse(isLoadTest);
    SofaTracerSpan currentSpan = SofaTraceContextHolder.getSofaTraceContext().getCurrentSpan();
    Map<String, String> bizBaggage = currentSpan.getSofaTracerSpanContext().getBizBaggage();
    bizBaggage.put("mark", "T");
    Assert.assertTrue(TracerUtils.isLoadTest(currentSpan));
    isLoadTest = helloService.testPressureMark("name");
    Assert.assertTrue(isLoadTest);
    String s = helloService.call1();
    Assert.assertEquals("call1", ref.getFlag());
    Assert.assertEquals("call1", s);
    Request request = new Request();
    int age = RandomUtils.nextInt();
    request.setAge(age);
    String call2 = "call2";
    request.setFlag(call2);
    Response response = helloService.call2(request);
    Assert.assertEquals(age, response.getAge());
    Assert.assertEquals(call2, response.getFlag());
    Response response1 = helloService.call2(null);
    Assert.assertNull(response1);
    providerConfig.unExport();
    serverConfig.destroy();
}
Also used : ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) SofaTracerSpan(com.alipay.common.tracer.core.span.SofaTracerSpan) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) Test(org.junit.Test)

Example 30 with ServerConfig

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

the class TripleHessianInvokeTest method testInvokeWithUniqueId.

@Test
public void testInvokeWithUniqueId() throws InterruptedException {
    String uniqueId = "uniqueId1";
    RpcRunningState.setDebugMode(true);
    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).setPort(port);
    TripleHessianInterfaceImpl ref = new TripleHessianInterfaceImpl();
    ProviderConfig<TripleHessianInterface> providerConfig = getProviderConfig().setApplication(serverApp).setUniqueId(uniqueId).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).setUniqueId(uniqueId).setRegister(false).setApplication(clientApp);
    TripleHessianInterface helloService = consumerConfig.refer();
    LOGGER.info("Grpc stub bean successful: {}", helloService.getClass().getName());
    helloService.call();
    Assert.assertEquals("call", ref.getFlag());
    String s = helloService.call1();
    Assert.assertEquals("call1", ref.getFlag());
    Assert.assertEquals("call1", s);
    Request request = new Request();
    int age = RandomUtils.nextInt();
    request.setAge(age);
    String call2 = "call2";
    request.setFlag(call2);
    Response response = helloService.call2(request);
    Assert.assertEquals(age, response.getAge());
    Assert.assertEquals(call2, response.getFlag());
    Response response1 = helloService.call2(null);
    Assert.assertNull(response1);
    // 测试没有设置 uniqueId 的情况,也能访问
    consumerConfig = new ConsumerConfig<TripleHessianInterface>();
    consumerConfig.setInterfaceId(TripleHessianInterface.class.getName()).setProtocol(RpcConstants.PROTOCOL_TYPE_TRIPLE).setDirectUrl("localhost:" + port).setRegister(false).setApplication(clientApp);
    helloService = consumerConfig.refer();
    LOGGER.info("Grpc stub bean successful: {}", helloService.getClass().getName());
    helloService.call();
    Assert.assertEquals("call", ref.getFlag());
    s = helloService.call1();
    Assert.assertEquals("call1", ref.getFlag());
    Assert.assertEquals("call1", s);
    request = new Request();
    age = RandomUtils.nextInt();
    request.setAge(age);
    call2 = "call2";
    request.setFlag(call2);
    response = helloService.call2(request);
    Assert.assertEquals(age, response.getAge());
    Assert.assertEquals(call2, response.getFlag());
    response1 = helloService.call2(null);
    Assert.assertNull(response1);
    providerConfig.unExport();
    serverConfig.destroy();
}
Also used : 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)

Aggregations

ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)202 Test (org.junit.Test)117 ProviderConfig (com.alipay.sofa.rpc.config.ProviderConfig)88 ApplicationConfig (com.alipay.sofa.rpc.config.ApplicationConfig)77 ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)71 ActivelyDestroyTest (com.alipay.sofa.rpc.test.ActivelyDestroyTest)54 HelloService (com.alipay.sofa.rpc.test.HelloService)53 HelloServiceImpl (com.alipay.sofa.rpc.test.HelloServiceImpl)51 CountDownLatch (java.util.concurrent.CountDownLatch)30 RegistryConfig (com.alipay.sofa.rpc.config.RegistryConfig)25 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)24 ActivelyDestroyTest (com.alipay.sofa.rpc.boot.test.ActivelyDestroyTest)18 ArrayList (java.util.ArrayList)18 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)17 ProviderInfo (com.alipay.sofa.rpc.client.ProviderInfo)15 SofaResponseCallback (com.alipay.sofa.rpc.core.invoke.SofaResponseCallback)14 RequestBase (com.alipay.sofa.rpc.core.request.RequestBase)14 Filter (com.alipay.sofa.rpc.filter.Filter)13 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)13 ProviderGroup (com.alipay.sofa.rpc.client.ProviderGroup)12