use of com.alipay.sofa.rpc.server.bolt.pb.EchoResponse 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.server.bolt.pb.EchoResponse in project sofa-rpc by sofastack.
the class BAsyncChainSampleServiceImpl method echoObj.
@Override
public EchoResponse echoObj(final EchoRequest req) {
RpcInvokeContext context = RpcInvokeContext.getContext();
LOGGER.info("--b1---:" + context);
// 读取一定要在这里读取
reqBaggage = context.getRequestBaggage("reqBaggageB");
context.putResponseBaggage("respBaggageB_useful1", "在返A之前写入有用");
final CountDownLatch latch = new CountDownLatch(1);
try {
RpcInvokeContext.getContext().setResponseCallback(new BoltSendableResponseCallback() {
@Override
public void onAppResponse(Object appResponse, String methodName, RequestBase request) {
// 返回一定要写在这里
RpcInvokeContext context = RpcInvokeContext.getContext();
LOGGER.info("--b3---:" + context);
if (reqBaggage != null) {
context.putResponseBaggage("respBaggageB", "b2aaa");
} else {
context.putResponseBaggage("respBaggageB_force", "b2aaaff");
}
EchoResponse s1 = (EchoResponse) appResponse;
// 这里已经取不到值了
String reqBaggageD = context.getRequestBaggage("reqBaggageD");
LOGGER.info("----reqBaggageD---:" + reqBaggageD);
EchoResponse s2 = sampleServiceD.echoObj(req);
sendAppResponse(EchoResponse.newBuilder().setCode(200).setMessage(s1.getMessage() + s2.getMessage()).build());
LOGGER.info("--b4---:" + RpcInvokeContext.getContext());
// 返回写在这里可能没用
context.putResponseBaggage("respBaggageB_useless2", "在返A之前写后没用");
latch.countDown();
}
});
sampleServiceC.echoObj(req);
context.putResponseBaggage("respBaggageB_useful2", "在返A之前写入有用");
LOGGER.info("--b2---:" + RpcInvokeContext.getContext());
// 模拟Callback更早回来的行为
latch.await(5000, TimeUnit.MILLISECONDS);
// 返回写在这里可能没用
context.putResponseBaggage("respBaggageB_useless2", "在返A之前写后没用");
LOGGER.info("--b3---:" + RpcInvokeContext.getContext());
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
use of com.alipay.sofa.rpc.server.bolt.pb.EchoResponse in project sofa-rpc by sofastack.
the class BCallbackSampleServiceImpl method echoObj.
@Override
public EchoResponse echoObj(EchoRequest req) {
RpcInvokeContext context = RpcInvokeContext.getContext();
LOGGER.info("--b1-----:" + context);
reqBaggage = context.getRequestBaggage("reqBaggageB");
if (reqBaggage != null) {
context.putResponseBaggage("respBaggageB", "b2aaa");
} else {
context.putResponseBaggage("respBaggageB_force", "b2aaaff");
}
final EchoResponse[] str = new EchoResponse[2];
final CountDownLatch latch = new CountDownLatch(2);
try {
RpcInvokeContext.getContext().setResponseCallback(new SofaResponseCallback() {
@Override
public void onAppResponse(Object appResponse, String methodName, RequestBase request) {
str[0] = (EchoResponse) 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();
}
});
sampleServiceC.hello();
RpcInvokeContext.getContext().setResponseCallback(new SofaResponseCallback() {
@Override
public void onAppResponse(Object appResponse, String methodName, RequestBase request) {
str[1] = (EchoResponse) 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();
}
});
sampleServiceD.hello();
latch.await(2000, TimeUnit.MILLISECONDS);
} catch (Exception e) {
e.printStackTrace();
}
EchoResponse s1 = str[0];
EchoResponse s2 = str[1];
return EchoResponse.newBuilder().setCode(200).setMessage(s1.getMessage() + s2.getMessage()).build();
}
use of com.alipay.sofa.rpc.server.bolt.pb.EchoResponse in project sofa-rpc by sofastack.
the class BSampleServiceImpl method echoObj.
@Override
public EchoResponse echoObj(EchoRequest req) {
RpcInvokeContext context = RpcInvokeContext.getContext();
LOGGER.info("-----b-----:" + context);
reqBaggage = context.getRequestBaggage("reqBaggageB");
if (reqBaggage != null) {
context.putResponseBaggage("respBaggageB", "b2aaa");
} else {
context.putResponseBaggage("respBaggageB_force", "b2aaaff");
}
EchoResponse s1 = sampleServiceC.echoObj(req);
EchoResponse s2 = sampleServiceD.echoObj(req);
return EchoResponse.newBuilder().setCode(200).setMessage(s1.getMessage() + s2.getMessage()).build();
}
use of com.alipay.sofa.rpc.server.bolt.pb.EchoResponse in project sofa-rpc by sofastack.
the class Http2ClearTextPriorKnowledgeTest method testProtobuf.
@Test
public void testProtobuf() {
// 只有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()).setSerialization(RpcConstants.SERIALIZE_PROTOBUF).setDirectUrl("h2c://127.0.0.1:12300").setApplication(new ApplicationConfig().setAppName("clientApp")).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C);
HttpService httpService = consumerConfig.refer();
EchoRequest request = EchoRequest.newBuilder().setGroup(Group.A).setName("xxx").build();
EchoResponse response = httpService.echoPb(request);
Assert.assertEquals("helloxxx", response.getMessage());
Assert.assertEquals(200, response.getCode());
}
{
ConsumerConfig<HttpService> consumerConfig2 = new ConsumerConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setSerialization(RpcConstants.SERIALIZE_PROTOBUF).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()).setSerialization(RpcConstants.SERIALIZE_PROTOBUF).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();
EchoRequest request = EchoRequest.newBuilder().setGroup(Group.A).setName("xxx").build();
EchoResponse response = httpService3.echoPb(request);
Assert.assertNull(response);
ResponseFuture future = RpcInvokeContext.getContext().getFuture();
try {
response = (EchoResponse) future.get();
Assert.assertEquals("helloxxx", response.getMessage());
Assert.assertEquals(200, response.getCode());
} catch (Exception e) {
e.printStackTrace();
Assert.fail();
}
}
{
final EchoResponse[] result = new EchoResponse[1];
final CountDownLatch latch = new CountDownLatch(1);
ConsumerConfig<HttpService> consumerConfig4 = new ConsumerConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setSerialization(RpcConstants.SERIALIZE_PROTOBUF).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] = (EchoResponse) 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();
EchoRequest request = EchoRequest.newBuilder().setGroup(Group.A).setName("xxx").build();
EchoResponse response = httpService4.echoPb(request);
Assert.assertNull(response);
try {
latch.await(2000, TimeUnit.MILLISECONDS);
response = result[0];
Assert.assertNotNull(response);
Assert.assertEquals("helloxxx", response.getMessage());
Assert.assertEquals(200, response.getCode());
} catch (Exception e) {
e.printStackTrace();
Assert.fail();
}
}
}
Aggregations