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