Search in sources :

Example 1 with HelloServiceImpl

use of com.alipay.sofa.rpc.test.HelloServiceImpl in project sofa-rpc by sofastack.

the class AllConnectConnectionHolderTest method startServer.

@BeforeClass
public static void startServer() throws Exception {
    RpcRunningState.setUnitTestMode(true);
    // 发布一个服务,每个请求要执行2秒
    serverConfig1 = 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(serverConfig1).setRepeatedExportLimit(-1).setRegister(false);
    providerConfig.export();
    // 再发布一个服务,不等待
    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();
}
Also used : ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) HelloService(com.alipay.sofa.rpc.test.HelloService) HelloServiceImpl(com.alipay.sofa.rpc.test.HelloServiceImpl) BeforeClass(org.junit.BeforeClass)

Example 2 with HelloServiceImpl

use of com.alipay.sofa.rpc.test.HelloServiceImpl 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 3 with HelloServiceImpl

use of com.alipay.sofa.rpc.test.HelloServiceImpl 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 4 with HelloServiceImpl

use of com.alipay.sofa.rpc.test.HelloServiceImpl in project sofa-rpc by sofastack.

the class FailoverClusterTest method testStick.

@Test
public void testStick() {
    // 发布一个服务,每个请求要执行2秒
    ServerConfig serverConfig = new ServerConfig().setStopTimeout(0).setPort(22227).setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT).setQueues(100).setCoreThreads(5).setMaxThreads(5);
    ProviderConfig<HelloService> providerConfig = new ProviderConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl("77")).setServer(serverConfig).setRepeatedExportLimit(-1).setRegister(false);
    providerConfig.export();
    // 再发布一个服务,不等待
    ServerConfig serverConfig2 = new ServerConfig().setStopTimeout(0).setPort(22228).setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT).setQueues(100).setCoreThreads(5).setMaxThreads(5);
    ProviderConfig<HelloService> providerConfig2 = new ProviderConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl("88")).setServer(serverConfig2).setRepeatedExportLimit(-1).setRegister(false);
    providerConfig2.export();
    ConsumerConfig<HelloService> consumerConfig = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setDirectUrl("bolt://127.0.0.1:22227;bolt://127.0.0.1:22228").setTimeout(1000).setCluster("failover").setLoadBalancer("random").setSticky(true).setRegister(false);
    final HelloService helloService = consumerConfig.refer();
    int count2 = 0;
    String result = helloService.sayHello("xxx", 22);
    for (int i = 0; i < 10; i++) {
        try {
            Assert.assertEquals(result, helloService.sayHello("xxx", 22));
            count2++;
        } catch (Exception ignore) {
        }
    }
    Assert.assertEquals(count2, 10);
    String nextResult;
    if ("77".equals(result)) {
        serverConfig.destroy();
        nextResult = "88";
    } else {
        serverConfig2.destroy();
        nextResult = "77";
    }
    try {
        Assert.assertEquals(nextResult, helloService.sayHello("xxx", 22));
        count2++;
    } catch (Exception e) {
    }
    Assert.assertEquals(count2, 10);
}
Also used : ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) HelloService(com.alipay.sofa.rpc.test.HelloService) HelloServiceImpl(com.alipay.sofa.rpc.test.HelloServiceImpl) 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 5 with HelloServiceImpl

use of com.alipay.sofa.rpc.test.HelloServiceImpl 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

ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)51 HelloService (com.alipay.sofa.rpc.test.HelloService)51 HelloServiceImpl (com.alipay.sofa.rpc.test.HelloServiceImpl)51 ProviderConfig (com.alipay.sofa.rpc.config.ProviderConfig)33 Test (org.junit.Test)32 ActivelyDestroyTest (com.alipay.sofa.rpc.test.ActivelyDestroyTest)29 ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)21 ApplicationConfig (com.alipay.sofa.rpc.config.ApplicationConfig)15 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)11 EchoService (com.alipay.sofa.rpc.test.EchoService)10 CountDownLatch (java.util.concurrent.CountDownLatch)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)10 RegistryConfig (com.alipay.sofa.rpc.config.RegistryConfig)9 SofaTimeOutException (com.alipay.sofa.rpc.core.exception.SofaTimeOutException)9 Filter (com.alipay.sofa.rpc.filter.Filter)9 EchoServiceImpl (com.alipay.sofa.rpc.test.EchoServiceImpl)9 SofaRouteException (com.alipay.sofa.rpc.core.exception.SofaRouteException)5 SofaResponseCallback (com.alipay.sofa.rpc.core.invoke.SofaResponseCallback)5 RequestBase (com.alipay.sofa.rpc.core.request.RequestBase)5 SofaRpcRuntimeException (com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException)4