use of com.alipay.sofa.rpc.config.ConsumerConfig in project sofa-rpc by sofastack.
the class Http2ClearTextHessianTest method testHessian.
@Test
public void testHessian() {
// 只有1个线程 执行
ServerConfig serverConfig = new ServerConfig().setStopTimeout(60000).setPort(12300).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C).setDaemon(true);
// 发布一个服务,每个请求要执行1秒
ProviderConfig<HttpService> providerConfig = new ProviderConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setRef(new HttpServiceImpl()).setApplication(new ApplicationConfig().setAppName("serverApp")).setServer(serverConfig).setRegister(false);
providerConfig.export();
{
ConsumerConfig<HttpService> consumerConfig = new ConsumerConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setDirectUrl("h2c://127.0.0.1:12300").setApplication(new ApplicationConfig().setAppName("clientApp")).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C);
HttpService httpService = consumerConfig.refer();
ExampleObj request = new ExampleObj();
request.setId(200);
request.setName("yyy");
ExampleObj response = httpService.object(request);
Assert.assertEquals(200, response.getId());
Assert.assertEquals("yyyxx", response.getName());
}
{
ConsumerConfig<HttpService> consumerConfig2 = new ConsumerConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setDirectUrl("h2c://127.0.0.1:12300").setApplication(new ApplicationConfig().setAppName("clientApp")).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C).setInvokeType(RpcConstants.INVOKER_TYPE_ONEWAY).setRepeatedReferLimit(-1);
HttpService httpService2 = consumerConfig2.refer();
EchoRequest request = EchoRequest.newBuilder().setGroup(Group.A).setName("xxx").build();
try {
httpService2.echoPb(request);
// NOT SUPPORTED NOW, If want support this, need add key to head.
Assert.fail();
} catch (Exception e) {
}
}
{
ConsumerConfig<HttpService> consumerConfig3 = new ConsumerConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setDirectUrl("h2c://127.0.0.1:12300").setApplication(new ApplicationConfig().setAppName("clientApp")).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C).setInvokeType(RpcConstants.INVOKER_TYPE_FUTURE).setRepeatedReferLimit(-1);
HttpService httpService3 = consumerConfig3.refer();
ExampleObj request = new ExampleObj();
request.setId(200);
request.setName("yyy");
ExampleObj response = httpService3.object(request);
Assert.assertNull(response);
ResponseFuture<ExampleObj> future = RpcInvokeContext.getContext().getFuture();
try {
response = future.get();
Assert.assertEquals(200, response.getId());
Assert.assertEquals("yyyxx", response.getName());
} catch (Exception e) {
e.printStackTrace();
Assert.fail();
}
}
{
final ExampleObj[] result = new ExampleObj[1];
final CountDownLatch latch = new CountDownLatch(1);
ConsumerConfig<HttpService> consumerConfig4 = new ConsumerConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setDirectUrl("h2c://127.0.0.1:12300").setApplication(new ApplicationConfig().setAppName("clientApp")).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C).setInvokeType(RpcConstants.INVOKER_TYPE_CALLBACK).setOnReturn(new SofaResponseCallback() {
@Override
public void onAppResponse(Object appResponse, String methodName, RequestBase request) {
result[0] = (ExampleObj) appResponse;
latch.countDown();
}
@Override
public void onAppException(Throwable throwable, String methodName, RequestBase request) {
latch.countDown();
}
@Override
public void onSofaException(SofaRpcException sofaException, String methodName, RequestBase request) {
latch.countDown();
}
}).setRepeatedReferLimit(-1);
HttpService httpService4 = consumerConfig4.refer();
ExampleObj request = new ExampleObj();
request.setId(200);
request.setName("yyy");
ExampleObj response = httpService4.object(request);
Assert.assertNull(response);
try {
latch.await(2000, TimeUnit.MILLISECONDS);
response = result[0];
Assert.assertEquals(200, response.getId());
Assert.assertEquals("yyyxx", response.getName());
} catch (Exception e) {
e.printStackTrace();
Assert.fail();
}
}
}
use of com.alipay.sofa.rpc.config.ConsumerConfig 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.config.ConsumerConfig 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.config.ConsumerConfig 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.config.ConsumerConfig 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