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();
}
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());
}
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);
}
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();
}
}
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;
}
Aggregations