Search in sources :

Example 1 with Cluster

use of com.alipay.sofa.rpc.client.Cluster 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 Cluster

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

use of com.alipay.sofa.rpc.client.Cluster in project sofa-rpc by sofastack.

the class BoltConsumerBootstrapTest method testAttrUpdate.

@Test
public void testAttrUpdate() {
    // 发布一个服务
    ServerConfig serverConfig0 = new ServerConfig().setStopTimeout(0).setPort(22224).setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT).setQueues(100).setCoreThreads(5).setMaxThreads(5);
    ProviderConfig<HelloService> providerConfig0 = new ProviderConfig<HelloService>().setId("p-0").setInterfaceId(HelloService.class.getName()).setUniqueId("attr").setRef(new HelloServiceImpl(1000)).setServer(serverConfig0).setRepeatedExportLimit(5);
    providerConfig0.export();
    ConsumerConfig<HelloService> consumerConfig0 = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setUniqueId("attr").setProxy("jdk").setDirectUrl("bolt://127.0.0.1:22224").setTimeout(500);
    HelloService proxy = consumerConfig0.refer();
    Invoker invoker = JDKProxy.parseInvoker(proxy);
    Cluster cluster = consumerConfig0.getConsumerBootstrap().getCluster();
    boolean error = false;
    try {
        proxy.sayHello("11", 11);
    } catch (Exception e) {
        LOGGER.info(e.getMessage());
        error = true;
    }
    Assert.assertTrue(error);
    // wrong key
    error = false;
    try {
        consumerConfig0.getConfigListener().attrUpdated(Collections.singletonMap("loadbalance", "asdasd"));
    } catch (Exception e) {
        error = true;
    }
    Assert.assertFalse(error);
    // wrong value
    error = false;
    try {
        consumerConfig0.getConfigListener().attrUpdated(Collections.singletonMap("loadBalancer", "asdasd"));
    } catch (Exception e) {
        error = true;
    }
    Assert.assertFalse(error);
    // 动态加大超时时间
    consumerConfig0.getConfigListener().attrUpdated(Collections.singletonMap("timeout", "2000"));
    Invoker invoker2 = JDKProxy.parseInvoker(proxy);
    Cluster cluster2 = consumerConfig0.getConsumerBootstrap().getCluster();
    error = false;
    try {
        proxy.sayHello("11", 11);
    } catch (Exception e) {
        e.printStackTrace();
        error = true;
    }
    Assert.assertFalse(error);
    Assert.assertTrue(invoker == invoker2);
    Assert.assertTrue(cluster != cluster2);
    // 切到一个没有的分组
    consumerConfig0.getConfigListener().attrUpdated(Collections.singletonMap("uniqueId", "attr2"));
    error = false;
    try {
        proxy.sayHello("11", 11);
    } catch (Exception e) {
        error = true;
    }
    Assert.assertTrue(error);
    // 切到一个有的分组
    consumerConfig0.getConfigListener().attrUpdated(Collections.singletonMap("uniqueId", "attr"));
    error = false;
    try {
        proxy.sayHello("11", 11);
    } catch (Exception e) {
        error = true;
    }
    Assert.assertFalse(error);
}
Also used : ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) Invoker(com.alipay.sofa.rpc.invoke.Invoker) 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) SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException) Test(org.junit.Test) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest)

Example 4 with Cluster

use of com.alipay.sofa.rpc.client.Cluster in project hugegraph-common by hugegraph.

the class RpcConsumerConfig method destroy.

public void destroy() {
    Set<Cluster> clusters = Sets.newHashSet();
    for (ConsumerBootstrap<?> bootstrap : this.bootstraps) {
        bootstrap.unRefer();
        clusters.add(bootstrap.getCluster());
    }
    for (Cluster cluster : clusters) {
        cluster.destroy();
    }
}
Also used : AbstractCluster(com.alipay.sofa.rpc.client.AbstractCluster) Cluster(com.alipay.sofa.rpc.client.Cluster)

Example 5 with Cluster

use of com.alipay.sofa.rpc.client.Cluster in project sofa-rpc by sofastack.

the class DefaultClientProxyInvoker method setCluster.

@Override
public Cluster setCluster(Cluster newCluster) {
    Cluster old = super.setCluster(newCluster);
    cacheCommonData();
    return old;
}
Also used : Cluster(com.alipay.sofa.rpc.client.Cluster)

Aggregations

Cluster (com.alipay.sofa.rpc.client.Cluster)9 ActivelyDestroyTest (com.alipay.sofa.rpc.test.ActivelyDestroyTest)7 HelloService (com.alipay.sofa.rpc.test.HelloService)7 Test (org.junit.Test)7 ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)5 AllConnectConnectionHolder (com.alipay.sofa.rpc.client.AllConnectConnectionHolder)4 ClientProxyInvoker (com.alipay.sofa.rpc.client.ClientProxyInvoker)4 ProviderConfig (com.alipay.sofa.rpc.config.ProviderConfig)3 ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)3 HelloServiceImpl (com.alipay.sofa.rpc.test.HelloServiceImpl)3 ProviderGroup (com.alipay.sofa.rpc.client.ProviderGroup)2 ProviderInfo (com.alipay.sofa.rpc.client.ProviderInfo)2 SofaRpcRuntimeException (com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException)2 Invoker (com.alipay.sofa.rpc.invoke.Invoker)2 AbstractCluster (com.alipay.sofa.rpc.client.AbstractCluster)1 SofaRouteException (com.alipay.sofa.rpc.core.exception.SofaRouteException)1 SofaTimeOutException (com.alipay.sofa.rpc.core.exception.SofaTimeOutException)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1