Search in sources :

Example 1 with HelloService

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

the class AllConnectConnectionHolderTest method getAvailableClientTransport2.

@Test
public void getAvailableClientTransport2() throws Exception {
    ConsumerConfig<HelloService> consumerConfig = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setDirectUrl("bolt://127.0.0.1:22223,bolt://127.0.0.1:22224").setConnectionHolder("all").setRegister(false).setLazy(true).setTimeout(3000);
    HelloService helloService = consumerConfig.refer();
    ClientProxyInvoker invoker = (ClientProxyInvoker) ProxyFactory.getInvoker(helloService, consumerConfig.getProxy());
    Cluster cluster = invoker.getCluster();
    Assert.assertTrue(cluster.getConnectionHolder() instanceof AllConnectConnectionHolder);
    AllConnectConnectionHolder holder = (AllConnectConnectionHolder) cluster.getConnectionHolder();
    Assert.assertTrue(holder.isAvailableEmpty());
    Assert.assertNotNull(holder.getAvailableClientTransport(ProviderHelper.toProviderInfo("bolt://127.0.0.1:22223")));
    Assert.assertNotNull(holder.getAvailableClientTransport(ProviderHelper.toProviderInfo("bolt://127.0.0.1:22224")));
    consumerConfig.unRefer();
}
Also used : AllConnectConnectionHolder(com.alipay.sofa.rpc.client.AllConnectConnectionHolder) HelloService(com.alipay.sofa.rpc.test.HelloService) Cluster(com.alipay.sofa.rpc.client.Cluster) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) ClientProxyInvoker(com.alipay.sofa.rpc.client.ClientProxyInvoker) Test(org.junit.Test) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest)

Example 2 with HelloService

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

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

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

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

Aggregations

HelloService (com.alipay.sofa.rpc.test.HelloService)81 ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)53 HelloServiceImpl (com.alipay.sofa.rpc.test.HelloServiceImpl)51 Test (org.junit.Test)47 ActivelyDestroyTest (com.alipay.sofa.rpc.test.ActivelyDestroyTest)43 ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)38 ProviderConfig (com.alipay.sofa.rpc.config.ProviderConfig)33 ApplicationConfig (com.alipay.sofa.rpc.config.ApplicationConfig)30 EchoService (com.alipay.sofa.rpc.test.EchoService)19 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)16 CountDownLatch (java.util.concurrent.CountDownLatch)14 RegistryConfig (com.alipay.sofa.rpc.config.RegistryConfig)12 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)12 Filter (com.alipay.sofa.rpc.filter.Filter)11 SofaTimeOutException (com.alipay.sofa.rpc.core.exception.SofaTimeOutException)10 SofaResponseCallback (com.alipay.sofa.rpc.core.invoke.SofaResponseCallback)10 RequestBase (com.alipay.sofa.rpc.core.request.RequestBase)10 EchoServiceImpl (com.alipay.sofa.rpc.test.EchoServiceImpl)9 Cluster (com.alipay.sofa.rpc.client.Cluster)7 SofaRouteException (com.alipay.sofa.rpc.core.exception.SofaRouteException)6