use of com.alipay.sofa.rpc.config.ApplicationConfig in project sofa-rpc by sofastack.
the class TripleServerTest method testSyncWithUniqueId.
@Test
public // 同步调用,直连 有uniqueId
void testSyncWithUniqueId() {
ApplicationConfig applicationConfig = new ApplicationConfig().setAppName("triple-server");
int port = 50052;
ServerConfig serverConfig = new ServerConfig().setProtocol(RpcConstants.PROTOCOL_TYPE_TRIPLE).setPort(port);
ProviderConfig<SofaGreeterTriple.IGreeter> providerConfig = new ProviderConfig<SofaGreeterTriple.IGreeter>().setApplication(applicationConfig).setUniqueId("abc").setBootstrap(RpcConstants.PROTOCOL_TYPE_TRIPLE).setInterfaceId(SofaGreeterTriple.IGreeter.class.getName()).setRef(new GreeterImpl()).setServer(serverConfig);
providerConfig.export();
ConsumerConfig<SofaGreeterTriple.IGreeter> consumerConfig = new ConsumerConfig<SofaGreeterTriple.IGreeter>();
consumerConfig.setInterfaceId(SofaGreeterTriple.IGreeter.class.getName()).setProtocol(RpcConstants.PROTOCOL_TYPE_TRIPLE).setUniqueId("abc").setDirectUrl("tri://127.0.0.1:" + port);
SofaGreeterTriple.IGreeter greeterBlockingStub = consumerConfig.refer();
HelloRequest.DateTime dateTime = HelloRequest.DateTime.newBuilder().setDate("2018-12-28").setTime("11:13:00").build();
HelloReply reply = null;
HelloRequest request = HelloRequest.newBuilder().setName("world").setDateTime(dateTime).build();
reply = greeterBlockingStub.sayHello(request);
Assert.assertNotNull(reply);
consumerConfig = new ConsumerConfig<SofaGreeterTriple.IGreeter>();
consumerConfig.setInterfaceId(SofaGreeterTriple.IGreeter.class.getName()).setProtocol(RpcConstants.PROTOCOL_TYPE_TRIPLE).setDirectUrl("tri://127.0.0.1:" + port);
greeterBlockingStub = consumerConfig.refer();
dateTime = HelloRequest.DateTime.newBuilder().setDate("2018-12-28").setTime("11:13:00").build();
request = HelloRequest.newBuilder().setName("world").setDateTime(dateTime).build();
reply = greeterBlockingStub.sayHello(request);
Assert.assertNotNull(reply);
}
use of com.alipay.sofa.rpc.config.ApplicationConfig in project sofa-rpc by sofastack.
the class Http2ClearTextExceptionTest method testProtobuf.
@Test
public void testProtobuf() throws InterruptedException {
// 只有1个线程 执行
ServerConfig serverConfig = new ServerConfig().setStopTimeout(60000).setPort(12333).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C).setDaemon(true);
// 发布一个服务,每个请求要执行1秒
ProviderConfig<HttpService> providerConfig = new ProviderConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setRef(new HttpServiceImpl()).setServer(serverConfig).setApplication(new ApplicationConfig().setAppName("serverApp")).setRegister(false);
providerConfig.export();
{
ConsumerConfig<HttpService> consumerConfig = new ConsumerConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setSerialization(RpcConstants.SERIALIZE_PROTOBUF).setDirectUrl("h2c://127.0.0.1:12333").setApplication(new ApplicationConfig().setAppName("clientApp")).setTimeout(500).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C);
HttpService httpService = consumerConfig.refer();
EchoRequest request = EchoRequest.newBuilder().setGroup(Group.B).setName("xxx").build();
try {
EchoResponse response = httpService.echoPb(request);
Assert.fail();
} catch (SofaRpcException e) {
Assert.assertTrue(e.getErrorType() == RpcErrorType.SERVER_BIZ);
}
}
{
ConsumerConfig<HttpService> consumerConfig2 = new ConsumerConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setSerialization(RpcConstants.SERIALIZE_PROTOBUF).setDirectUrl("h2c://127.0.0.1:12333").setApplication(new ApplicationConfig().setAppName("clientApp")).setTimeout(500).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C).setInvokeType(RpcConstants.INVOKER_TYPE_ONEWAY).setRepeatedReferLimit(-1);
HttpService httpService2 = consumerConfig2.refer();
EchoRequest request = EchoRequest.newBuilder().setGroup(Group.B).setName("xxx").build();
try {
httpService2.echoPb(request);
// NOT SUPPORTED NOW, If want support this, need add key to head.
Assert.fail();
} catch (Exception e) {
Assert.assertTrue(e instanceof SofaRpcException);
}
}
{
ConsumerConfig<HttpService> consumerConfig3 = new ConsumerConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setSerialization(RpcConstants.SERIALIZE_PROTOBUF).setDirectUrl("h2c://127.0.0.1:12333").setApplication(new ApplicationConfig().setAppName("clientApp")).setTimeout(500).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C).setInvokeType(RpcConstants.INVOKER_TYPE_FUTURE).setRepeatedReferLimit(-1);
HttpService httpService3 = consumerConfig3.refer();
EchoRequest request = EchoRequest.newBuilder().setGroup(Group.B).setName("xxx").build();
EchoResponse response = httpService3.echoPb(request);
Assert.assertNull(response);
ResponseFuture future = RpcInvokeContext.getContext().getFuture();
try {
future.get();
Assert.fail();
} catch (ExecutionException e) {
SofaRpcException re = (SofaRpcException) e.getCause();
Assert.assertTrue(re.getErrorType() == RpcErrorType.SERVER_BIZ);
}
}
{
final Object[] result = new Object[1];
final CountDownLatch latch = new CountDownLatch(1);
ConsumerConfig<HttpService> consumerConfig4 = new ConsumerConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setSerialization(RpcConstants.SERIALIZE_PROTOBUF).setTimeout(500).setDirectUrl("h2c://127.0.0.1:12333").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] = appResponse;
latch.countDown();
}
@Override
public void onAppException(Throwable throwable, String methodName, RequestBase request) {
result[0] = throwable;
latch.countDown();
}
@Override
public void onSofaException(SofaRpcException exception, String methodName, RequestBase request) {
result[0] = exception;
latch.countDown();
}
}).setRepeatedReferLimit(-1);
HttpService httpService4 = consumerConfig4.refer();
EchoRequest request = EchoRequest.newBuilder().setGroup(Group.B).setName("xxx").build();
EchoResponse response = httpService4.echoPb(request);
Assert.assertNull(response);
latch.await(2000, TimeUnit.MILLISECONDS);
Throwable e = (Throwable) result[0];
Assert.assertTrue(e instanceof SofaRpcException);
Assert.assertTrue(((SofaRpcException) e).getErrorType() == RpcErrorType.SERVER_BIZ);
}
}
use of com.alipay.sofa.rpc.config.ApplicationConfig 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.ApplicationConfig in project sofa-rpc by sofastack.
the class FailoverClusterTest method testRpcDirectInvokeFromContextWithAvailableProviders.
@Test
public void testRpcDirectInvokeFromContextWithAvailableProviders() {
ServerConfig serverConfig = new ServerConfig().setProtocol("bolt").setHost("0.0.0.0").setPort(13900);
ProviderConfig<HelloService> provider = new ProviderConfig();
provider.setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl("x-demo-invoke")).setApplication(new ApplicationConfig().setAppName("x-test-server")).setProxy("javassist").setSerialization("hessian2").setServer(serverConfig).setTimeout(3000);
provider.export();
ConsumerConfig<HelloService> consumer = new ConsumerConfig();
consumer.setInterfaceId(HelloService.class.getName()).setApplication(new ApplicationConfig().setAppName("x-test-client")).setDirectUrl("bolt://127.0.0.1:65534").setProxy("javassist");
HelloService proxy = consumer.refer();
for (int i = 0; i < 3; i++) {
RpcInvokeContext.getContext().setTargetURL("127.0.0.1:13900");
Assert.assertEquals("x-demo-invoke", proxy.sayHello("x-demo-invoke", 1));
}
provider.unExport();
consumer.unRefer();
}
use of com.alipay.sofa.rpc.config.ApplicationConfig in project sofa-rpc by sofastack.
the class RpcContextTest method testAll.
@Test
public void testAll() {
// 只有1个线程 执行
ServerConfig serverConfig = new ServerConfig().setPort(22222).setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT).setQueues(100).setCoreThreads(1).setMaxThreads(2);
// 发布一个服务,每个请求要执行1秒
CtxHelloServiceImpl helloServiceImpl = new CtxHelloServiceImpl();
ProviderConfig<HelloService> providerConfig = new ProviderConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setApplication(new ApplicationConfig().setAppName("test-server")).setRef(helloServiceImpl).setServer(serverConfig).setRegister(false);
providerConfig.export();
{
ConsumerConfig<HelloService> consumerConfig = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setApplication(new ApplicationConfig().setAppName("test-client")).setDirectUrl("bolt://127.0.0.1:22222?appName=test-server").setTimeout(30000).setRegister(false);
final HelloService helloService = consumerConfig.refer();
String str = helloService.sayHello("xxx", 123);
RpcServiceContext serviceContext = RpcContextManager.currentServiceContext(false);
RpcReferenceContext referenceContext = RpcContextManager.lastReferenceContext(false);
Assert.assertNull(serviceContext);
Assert.assertNotNull(referenceContext);
serviceContext = helloServiceImpl.serviceContext;
Assert.assertNotNull(serviceContext);
Assert.assertEquals(serviceContext.getCallerAppName(), "test-client");
Assert.assertEquals(referenceContext.getTargetAppName(), "test-server");
Assert.assertNotNull(referenceContext.getClientIP());
Assert.assertTrue(referenceContext.getClientPort() > 0);
}
{
final CountDownLatch latch = new CountDownLatch(1);
final String[] ret = { null };
ConsumerConfig<HelloService> consumerConfig2 = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setApplication(new ApplicationConfig().setAppName("test-client")).setDirectUrl("bolt://127.0.0.1:22222?appName=test-server").setTimeout(2000).setInvokeType("callback").setOnReturn(new SofaResponseCallback() {
@Override
public void onAppResponse(Object appResponse, String methodName, RequestBase request) {
ret[0] = (String) 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();
}
}).setRegister(false);
final HelloService helloServiceCallback = consumerConfig2.refer();
String ret0 = helloServiceCallback.sayHello("xxx", 22);
// 第一次返回null
Assert.assertNull(ret0);
RpcServiceContext serviceContext = RpcContextManager.currentServiceContext(false);
RpcReferenceContext referenceContext = RpcContextManager.lastReferenceContext(false);
Assert.assertNull(serviceContext);
Assert.assertNotNull(referenceContext);
serviceContext = helloServiceImpl.serviceContext;
Assert.assertNotNull(serviceContext);
Assert.assertEquals(serviceContext.getCallerAppName(), "test-client");
Assert.assertEquals(referenceContext.getTargetAppName(), "test-server");
Assert.assertNotNull(referenceContext.getClientIP());
Assert.assertTrue(referenceContext.getClientPort() > 0);
try {
latch.await(5000, TimeUnit.MILLISECONDS);
} catch (InterruptedException ignore) {
}
Assert.assertNotNull(ret[0]);
}
}
Aggregations