Search in sources :

Example 91 with ProviderConfig

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

the class FailoverClusterTest method testRetryLogicThrowException.

@Test(expected = SofaTimeOutException.class)
public void testRetryLogicThrowException() {
    // one provider and retry twice
    ServerConfig serverConfig = new ServerConfig().setStopTimeout(0).setPort(22222).setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT).setQueues(100).setCoreThreads(5).setMaxThreads(5);
    // 发布一个服务,每个请求要执行2秒
    ProviderConfig<HelloService> providerConfig = new ProviderConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl() {

        @Override
        public String sayHello(String name, int age) {
            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).setRetries(// 失败后自动重试2次
    2).setRegister(false);
    final HelloService helloService = consumerConfig.refer();
    helloService.sayHello("xxx", 20);
}
Also used : ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) 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) 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 92 with ProviderConfig

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

the class Http2ClearTextBadRequestTest method testAll.

@Test
public void testAll() throws Exception {
    // 只有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()).setApplication(new ApplicationConfig().setAppName("serverApp")).setServer(serverConfig).setUniqueId("uuu").setRegister(false);
    providerConfig.export();
    ClientTransportConfig clientTransportConfig = new ClientTransportConfig();
    clientTransportConfig.setProviderInfo(ProviderHelper.toProviderInfo("h2c://127.0.0.1:12333"));
    Http2ClientTransport clientTransport = new Http2ClientTransport(clientTransportConfig);
    clientTransport.connect();
    {
        // GET 图标
        String url = "/favicon.ico";
        FullHttpRequest httpRequest = buildHttpRequest(url);
        MyHandler handler = sendHttpRequest(clientTransport, httpRequest);
        Assert.assertEquals(200, handler.response.status().code());
        Assert.assertTrue(handler.content.length == 0);
    }
    {
        // 其它未知命令
        String url = "/com.alipay.sofa.rpc.server.http.HttpService/add";
        FullHttpRequest httpRequest = buildHttpRequest(url);
        httpRequest.setMethod(HttpMethod.OPTIONS);
        MyHandler handler = sendHttpRequest(clientTransport, httpRequest);
        Assert.assertEquals(400, handler.response.status().code());
        Assert.assertTrue(StringUtils.isNotEmpty(getStringContent(handler)));
    }
    {
        // HEAD 不存在的服务
        String url = "/com.alipay.sofa.rpc.server.http.HttpService12313/add";
        FullHttpRequest httpRequest = buildHttpRequest(url);
        httpRequest.setMethod(HttpMethod.HEAD);
        MyHandler handler = sendHttpRequest(clientTransport, httpRequest);
        Assert.assertEquals(404, handler.response.status().code());
    }
    {
        // HEAD 不存在的方法
        String url = "/com.alipay.sofa.rpc.server.http.HttpService:uuu/xasdasdasd";
        FullHttpRequest httpRequest = buildHttpRequest(url);
        httpRequest.setMethod(HttpMethod.HEAD);
        MyHandler handler = sendHttpRequest(clientTransport, httpRequest);
        Assert.assertEquals(404, handler.response.status().code());
    }
    {
        // HEAD 存在的方法
        String url = "/com.alipay.sofa.rpc.server.http.HttpService:uuu/add";
        FullHttpRequest httpRequest = buildHttpRequest(url);
        httpRequest.setMethod(HttpMethod.HEAD);
        MyHandler handler = sendHttpRequest(clientTransport, httpRequest);
        Assert.assertEquals(200, handler.response.status().code());
    }
    {
        // POST 异常:地址不对
        String url = "/com.alipay.sofa";
        FullHttpRequest httpRequest = buildHttpRequest(url);
        MyHandler handler = sendHttpRequest(clientTransport, httpRequest);
        Assert.assertEquals(400, handler.response.status().code());
        Assert.assertTrue(getStringContent(handler).contains("ip:port"));
    }
    {
        // POST 未知序列化
        String url = "http://127.0.0.1:12300/com.alipay.sofa.rpc.server.http.HttpService/asdasdas?code=xxx";
        FullHttpRequest httpRequest = buildHttpRequest(url);
        MyHandler handler = sendHttpRequest(clientTransport, httpRequest);
        Assert.assertEquals(400, handler.response.status().code());
        Assert.assertNotNull(getStringContent(handler));
    }
    {
        // POST 不存在的接口
        String url = "http://127.0.0.1:12300/com.alipay.sofa.rpc.server.http.HttpService/echoPb";
        FullHttpRequest httpRequest = buildHttpRequest(url);
        httpRequest.headers().add(RemotingConstants.HEAD_SERIALIZE_TYPE, "protobuf");
        MyHandler handler = sendHttpRequest(clientTransport, httpRequest);
        Assert.assertEquals(404, handler.response.status().code());
        Assert.assertNotNull(getStringContent(handler));
    }
    {
        // POST 不存在的方法
        String url = "http://127.0.0.1:12300/com.alipay.sofa.rpc.server.http.HttpService:uuu/adasdada";
        FullHttpRequest httpRequest = buildHttpRequest(url);
        httpRequest.headers().add(RemotingConstants.HEAD_SERIALIZE_TYPE, "protobuf");
        MyHandler handler = sendHttpRequest(clientTransport, httpRequest);
        Assert.assertEquals(404, handler.response.status().code());
        Assert.assertNotNull(getStringContent(handler));
    }
    {
        // POST 不传 HEAD_SERIALIZE_TYPE
        String url = "http://127.0.0.1:12300/com.alipay.sofa.rpc.server.http.HttpService:uuu/echoPb";
        FullHttpRequest httpRequest = buildHttpRequest(url);
        httpRequest.headers().add(HttpHeaderNames.CONTENT_TYPE, "application/x-protobuf");
        MyHandler handler = sendHttpRequest(clientTransport, httpRequest);
        Assert.assertEquals(200, handler.response.status().code());
        Assert.assertNotNull(getStringContent(handler));
        EchoResponse response = EchoResponse.parseFrom(handler.content);
        Assert.assertEquals("helloxxx", response.getMessage());
    }
    {
        // POST 正常
        String url = "http://127.0.0.1:12300/com.alipay.sofa.rpc.server.http.HttpService:uuu/echoPb";
        FullHttpRequest httpRequest = buildHttpRequest(url);
        httpRequest.headers().add(RemotingConstants.HEAD_SERIALIZE_TYPE, "protobuf");
        httpRequest.headers().add(RemotingConstants.HEAD_TARGET_APP, "serverApp1");
        MyHandler handler = sendHttpRequest(clientTransport, httpRequest);
        Assert.assertEquals(200, handler.response.status().code());
        EchoResponse response = EchoResponse.parseFrom(handler.content);
        Assert.assertEquals("helloxxx", response.getMessage());
    }
}
Also used : EchoResponse(com.alipay.sofa.rpc.server.bolt.pb.EchoResponse) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) HttpService(com.alipay.sofa.rpc.server.http.HttpService) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) ClientTransportConfig(com.alipay.sofa.rpc.transport.ClientTransportConfig) HttpServiceImpl(com.alipay.sofa.rpc.server.http.HttpServiceImpl) Test(org.junit.Test) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest)

Example 93 with ProviderConfig

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

the class RestServerMain method main.

public static void main(String[] args) {
    ApplicationConfig application = new ApplicationConfig().setAppName("test-server");
    /*
         访问地址:
         POST http://127.0.0.1:8888/rest/hello/code/name
         GET http://127.0.0.1:8888/rest/hello/code
         PUT http://127.0.0.1:8888/rest/hello/code/name
         DELETE http://127.0.0.1:8888/rest/hello/code
         GET http://127.0.0.1:8888/rest/get/1234567890
         POST http://127.0.0.1:8888/rest/post/1234567890 bodydddddd
         */
    ServerConfig serverConfig = new ServerConfig().setProtocol("rest").setPort(8888).setDaemon(false);
    ProviderConfig<RestService> providerConfig = new ProviderConfig<RestService>().setInterfaceId(RestService.class.getName()).setApplication(application).setRef(new RestServiceImpl()).setBootstrap("rest").setServer(serverConfig).setRegister(false);
    providerConfig.export();
    LOGGER.warn("started at pid {}", RpcRuntimeContext.PID);
}
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.rest.RestServiceImpl) RestService(com.alipay.sofa.rpc.rest.RestService)

Example 94 with ProviderConfig

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

the class SofaRegistryServer method main.

public static void main(String[] args) {
    /**
     * 运行时项目引入依赖
     *         <dependency>
     *             <groupId>com.alipay.sofa</groupId>
     *             <artifactId>registry-client-all</artifactId>
     *             <version>5.2.0</version>
     *         </dependency>
     */
    RegistryConfig registryConfig = new RegistryConfig().setProtocol(RpcConstants.REGISTRY_PROTOCOL_SOFA).setAddress("127.0.0.1:9603");
    ServerConfig serverConfig = new ServerConfig().setProtocol("bolt").setPort(12200).setDaemon(false);
    ProviderConfig<HelloService> providerConfig = new ProviderConfig<HelloService>().setRegistry(registryConfig).setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl()).setServer(serverConfig);
    providerConfig.export();
    LOGGER.warn("started at pid {}", RpcRuntimeContext.PID);
}
Also used : RegistryConfig(com.alipay.sofa.rpc.config.RegistryConfig) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) HelloService(com.alipay.sofa.rpc.quickstart.HelloService) HelloServiceImpl(com.alipay.sofa.rpc.quickstart.HelloServiceImpl)

Example 95 with ProviderConfig

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

the class LazyBoltServerMain method main.

public static void main(String[] args) {
    ServerConfig serverConfig = new ServerConfig().setPort(22100).setDaemon(false);
    ProviderConfig<HelloService> providerConfig = new ProviderConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl("result from 22100")).setServer(serverConfig).setRegister(false);
    ProviderConfig<EchoService> providerConfig2 = new ProviderConfig<EchoService>().setInterfaceId(EchoService.class.getName()).setRef(new EchoServiceImpl()).setServer(serverConfig).setRegister(false);
    providerConfig.export();
    providerConfig2.export();
    LOGGER.warn("started at pid {}", RpcRuntimeContext.PID);
}
Also used : ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) 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) EchoServiceImpl(com.alipay.sofa.rpc.test.EchoServiceImpl)

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