Search in sources :

Example 31 with ProviderConfig

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

the class RestDirectUrlTest method testAll.

@Test
public void testAll() {
    // 只有1个线程 执行
    ServerConfig serverConfig = new ServerConfig().setPort(12300).setProtocol(RpcConstants.PROTOCOL_TYPE_REST).setDaemon(true);
    // 发布一个服务,每个请求要执行1秒
    ProviderConfig<RestService> providerConfig = new ProviderConfig<RestService>().setInterfaceId(RestService.class.getName()).setRef(new RestServiceImpl()).setBootstrap("rest").setApplication(new ApplicationConfig().setAppName("serverApp")).setServer(serverConfig).setRegister(false);
    providerConfig.export();
    final ConsumerConfig<RestService> consumerConfig = new ConsumerConfig<RestService>().setInterfaceId(RestService.class.getName()).setDirectUrl("rest://127.0.0.1:12300").setProtocol(RpcConstants.PROTOCOL_TYPE_REST).setBootstrap("rest").setApplication(new ApplicationConfig().setAppName("clientApp")).setReconnectPeriod(1000);
    RestService restService = consumerConfig.refer();
    Assert.assertEquals(restService.query(11), "hello world !null");
    serverConfig.getServer().stop();
    // 关闭后再调用一个抛异常
    try {
        restService.query(11);
    } 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));
    Assert.assertEquals(restService.query(11), "hello world !null");
}
Also used : ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) RestServiceImpl(com.alipay.sofa.rpc.server.rest.RestServiceImpl) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) RestService(com.alipay.sofa.rpc.server.rest.RestService) 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 32 with ProviderConfig

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

the class NotFoundInvokerTest method testAll.

@Test
public void testAll() {
    ServerConfig serverConfig = new ServerConfig().setStopTimeout(0).setPort(22222).setQueues(100).setCoreThreads(10).setMaxThreads(10);
    // 发布一个服务,每个请求要执行1秒
    ProviderConfig<HelloService> providerConfig = new ProviderConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl(1500)).setServer(serverConfig).setRegister(false);
    providerConfig.export();
    ConsumerConfig<HelloService> consumerConfig = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setDirectUrl("bolt://127.0.0.1:22222").setTimeout(30000).setFilterRef(Collections.<Filter>singletonList(new Filter() {

        @Override
        public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException {
            // 造一个假方法
            request.setMethodName(request.getMethodName() + "_unknown");
            return invoker.invoke(request);
        }
    })).setRegister(false);
    HelloService helloService = consumerConfig.refer();
    boolean ok = true;
    try {
        // 找不到方法
        helloService.sayHello("xxx", 22);
    } catch (Exception e) {
        if (e instanceof SofaRpcException) {
            Assert.assertEquals(((SofaRpcException) e).getErrorType(), RpcErrorType.SERVER_UNDECLARED_ERROR);
        } else if (e instanceof UndeclaredThrowableException) {
            Assert.assertEquals(((SofaRpcException) e.getCause()).getErrorType(), RpcErrorType.SERVER_UNDECLARED_ERROR);
        }
        ok = false;
    }
    Assert.assertFalse(ok);
    ConsumerConfig<EchoService> consumerConfig2 = new ConsumerConfig<EchoService>().setInterfaceId(EchoService.class.getName()).setDirectUrl("bolt://127.0.0.1:22222").setTimeout(30000).setRegister(false);
    EchoService echoService = consumerConfig2.refer();
    ok = true;
    try {
        echoService.echoStr("xx");
    } catch (Exception e) {
        if (e instanceof SofaRpcException) {
            Assert.assertEquals(((SofaRpcException) e).getErrorType(), RpcErrorType.SERVER_UNDECLARED_ERROR);
        } else if (e instanceof UndeclaredThrowableException) {
            Assert.assertEquals(((SofaRpcException) e.getCause()).getErrorType(), RpcErrorType.SERVER_UNDECLARED_ERROR);
        }
        ok = false;
    }
    Assert.assertFalse(ok);
}
Also used : SofaRequest(com.alipay.sofa.rpc.core.request.SofaRequest) FilterInvoker(com.alipay.sofa.rpc.filter.FilterInvoker) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) EchoService(com.alipay.sofa.rpc.test.EchoService) HelloService(com.alipay.sofa.rpc.test.HelloService) HelloServiceImpl(com.alipay.sofa.rpc.test.HelloServiceImpl) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) Filter(com.alipay.sofa.rpc.filter.Filter) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) Test(org.junit.Test) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest)

Example 33 with ProviderConfig

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

the class RejectedTest method testAll.

@Test
public void testAll() {
    ServerConfig serverConfig = new ServerConfig().setStopTimeout(0).setPort(22222).setQueues(0).setCoreThreads(1).setMaxThreads(2);
    // 发布一个服务,每个请求要执行1秒
    ProviderConfig<HelloService> providerConfig = new ProviderConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl(1000)).setServer(serverConfig).setRegister(false);
    providerConfig.export();
    ConsumerConfig<HelloService> consumerConfig = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setTimeout(3000).setDirectUrl("bolt://127.0.0.1:22222").setRegister(false);
    final HelloService helloService = consumerConfig.refer();
    final AtomicInteger success = new AtomicInteger();
    final AtomicInteger failure = new AtomicInteger();
    int times = 3;
    final CountDownLatch latch = new CountDownLatch(times);
    for (int i = 0; i < times; i++) {
        Thread thread1 = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    helloService.sayHello("xxx", 22);
                    success.incrementAndGet();
                } catch (Exception e) {
                    if (e instanceof SofaRpcException) {
                        Assert.assertEquals(((SofaRpcException) e).getErrorType(), RpcErrorType.SERVER_BUSY);
                    }
                    failure.incrementAndGet();
                } finally {
                    latch.countDown();
                }
            }
        }, "T1");
        thread1.start();
    }
    try {
        latch.await(10000, TimeUnit.MILLISECONDS);
    } catch (InterruptedException ignore) {
    }
    Assert.assertEquals(success.get(), 2);
    Assert.assertEquals(failure.get(), 1);
}
Also used : ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) HelloService(com.alipay.sofa.rpc.test.HelloService) HelloServiceImpl(com.alipay.sofa.rpc.test.HelloServiceImpl) CountDownLatch(java.util.concurrent.CountDownLatch) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) 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 34 with ProviderConfig

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

the class Http1ServerTest method testHttp1Json.

@Test
public void testHttp1Json() throws Exception {
    // 只有1个线程 执行
    ServerConfig serverConfig = new ServerConfig().setStopTimeout(60000).setPort(12300).setProtocol(RpcConstants.PROTOCOL_TYPE_HTTP).setDaemon(true);
    // 发布一个服务,每个请求要执行1秒
    ProviderConfig<HttpService> providerConfig = new ProviderConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setRef(new HttpServiceImpl()).setApplication(new ApplicationConfig().setAppName("serverApp")).setServer(serverConfig).setUniqueId("uuu").setRegister(false);
    providerConfig.export();
    HttpClient httpclient = HttpClientBuilder.create().build();
    {
        // POST jackson不存在的接口
        String url = "http://127.0.0.1:12300/com.alipay.sofa.rpc.server.http.HttpService/adasdad";
        HttpPost httpPost = new HttpPost(url);
        httpPost.setHeader(RemotingConstants.HEAD_SERIALIZE_TYPE, "json");
        ExampleObj obj = new ExampleObj();
        obj.setId(1);
        obj.setName("xxx");
        byte[] bytes = mapper.writeValueAsBytes(obj);
        ByteArrayEntity entity = new ByteArrayEntity(bytes, ContentType.create("application/json"));
        httpPost.setEntity(entity);
        HttpResponse httpResponse = httpclient.execute(httpPost);
        Assert.assertEquals(404, httpResponse.getStatusLine().getStatusCode());
        Assert.assertNotNull(getStringContent(httpResponse));
    }
    {
        // POST 不存在的方法
        String url = "http://127.0.0.1:12300/com.alipay.sofa.rpc.server.http.HttpService:uuu/adasdad";
        HttpPost httpPost = new HttpPost(url);
        httpPost.setHeader(RemotingConstants.HEAD_SERIALIZE_TYPE, "json");
        ExampleObj obj = new ExampleObj();
        obj.setId(1);
        obj.setName("xxx");
        byte[] bytes = mapper.writeValueAsBytes(obj);
        ByteArrayEntity entity = new ByteArrayEntity(bytes, ContentType.create("application/json"));
        httpPost.setEntity(entity);
        HttpResponse httpResponse = httpclient.execute(httpPost);
        Assert.assertEquals(404, httpResponse.getStatusLine().getStatusCode());
        Assert.assertNotNull(getStringContent(httpResponse));
    }
    {
        // POST 不传 HEAD_SERIALIZE_TYPE
        String url = "http://127.0.0.1:12300/com.alipay.sofa.rpc.server.http.HttpService:uuu/object";
        HttpPost httpPost = new HttpPost(url);
        ExampleObj obj = new ExampleObj();
        obj.setId(1);
        obj.setName("xxx");
        byte[] bytes = mapper.writeValueAsBytes(obj);
        ByteArrayEntity entity = new ByteArrayEntity(bytes, ContentType.create("application/json"));
        httpPost.setEntity(entity);
        HttpResponse httpResponse = httpclient.execute(httpPost);
        Assert.assertEquals(200, httpResponse.getStatusLine().getStatusCode());
        byte[] data = EntityUtils.toByteArray(httpResponse.getEntity());
        ExampleObj result = mapper.readValue(data, ExampleObj.class);
        Assert.assertEquals("xxxxx", result.getName());
    }
    {
        // POST 正常请求
        String url = "http://127.0.0.1:12300/com.alipay.sofa.rpc.server.http.HttpService:uuu/object";
        HttpPost httpPost = new HttpPost(url);
        httpPost.setHeader(RemotingConstants.HEAD_SERIALIZE_TYPE, "json");
        ExampleObj obj = new ExampleObj();
        obj.setId(1);
        obj.setName("xxx");
        byte[] bytes = mapper.writeValueAsBytes(obj);
        ByteArrayEntity entity = new ByteArrayEntity(bytes, ContentType.create("application/json"));
        httpPost.setEntity(entity);
        HttpResponse httpResponse = httpclient.execute(httpPost);
        Assert.assertEquals(200, httpResponse.getStatusLine().getStatusCode());
        byte[] data = EntityUtils.toByteArray(httpResponse.getEntity());
        ExampleObj result = mapper.readValue(data, ExampleObj.class);
        Assert.assertEquals("xxxxx", result.getName());
    }
}
Also used : ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) HttpPost(org.apache.http.client.methods.HttpPost) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) HttpClient(org.apache.http.client.HttpClient) HttpResponse(org.apache.http.HttpResponse) Test(org.junit.Test) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest)

Example 35 with ProviderConfig

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

the class BootstrapsTest method from.

@Test
public void from() throws Exception {
    ProviderConfig providerConfig = new ProviderConfig().setBootstrap("test");
    ProviderBootstrap bootstrap = Bootstraps.from(providerConfig);
    Assert.assertEquals(TestProviderBootstrap.class, bootstrap.getClass());
    Assert.assertEquals(providerConfig, bootstrap.getProviderConfig());
    // if not set bootstrap
    providerConfig = new ProviderConfig();
    bootstrap = Bootstraps.from(providerConfig);
    Assert.assertEquals(TestProviderBootstrap.class, bootstrap.getClass());
    Assert.assertEquals(providerConfig, bootstrap.getProviderConfig());
}
Also used : ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) Test(org.junit.Test)

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