use of com.alipay.sofa.rpc.core.exception.SofaRpcException in project Sentinel by alibaba.
the class AbstractSofaRpcFilter method traceOtherException.
protected SofaRpcException traceOtherException(Throwable t, Entry interfaceEntry, Entry methodEntry) {
SofaRpcException rpcException;
if (t instanceof SofaRpcException) {
rpcException = (SofaRpcException) t;
} else {
rpcException = new SofaRpcException(RpcErrorType.SERVER_FILTER, t);
}
Tracer.traceEntry(rpcException, interfaceEntry);
Tracer.traceEntry(rpcException, methodEntry);
return rpcException;
}
use of com.alipay.sofa.rpc.core.exception.SofaRpcException in project incubator-shenyu by apache.
the class SofaProxyService method genericInvoker.
/**
* Generic invoker object.
*
* @param body the body
* @param metaData the meta data
* @param exchange the exchange
* @return the object
* @throws ShenyuException the shenyu exception
*/
public Mono<Object> genericInvoker(final String body, final MetaData metaData, final ServerWebExchange exchange) throws ShenyuException {
ConsumerConfig<GenericService> reference = ApplicationConfigCache.getInstance().get(metaData.getPath());
if (Objects.isNull(reference) || StringUtils.isEmpty(reference.getInterfaceId())) {
ApplicationConfigCache.getInstance().invalidate(metaData.getPath());
reference = ApplicationConfigCache.getInstance().initRef(metaData);
}
Pair<String[], Object[]> pair;
if (StringUtils.isBlank(metaData.getParameterTypes()) || parameterIsNone(body)) {
pair = new ImmutablePair<>(new String[] {}, new Object[] {});
} else {
pair = sofaParamResolveService.buildParameter(body, metaData.getParameterTypes());
}
CompletableFuture<Object> future = new CompletableFuture<>();
RpcInvokeContext.getContext().setResponseCallback(new SofaResponseCallback<Object>() {
@Override
public void onAppResponse(final Object o, final String s, final RequestBase requestBase) {
future.complete(o);
}
@Override
public void onAppException(final Throwable throwable, final String s, final RequestBase requestBase) {
future.completeExceptionally(throwable);
}
@Override
public void onSofaException(final SofaRpcException e, final String s, final RequestBase requestBase) {
future.completeExceptionally(e);
}
});
GenericService genericService = reference.refer();
genericService.$genericInvoke(metaData.getMethodName(), pair.getLeft(), pair.getRight());
return Mono.fromFuture(future.thenApply(ret -> {
if (Objects.isNull(ret)) {
ret = Constants.SOFA_RPC_RESULT_EMPTY;
}
GenericObject genericObject = (GenericObject) ret;
exchange.getAttributes().put(Constants.RPC_RESULT, genericObject.getFields());
exchange.getAttributes().put(Constants.CLIENT_RESPONSE_RESULT_TYPE, ResultEnum.SUCCESS.getName());
return ret;
})).onErrorMap(ShenyuException::new);
}
use of com.alipay.sofa.rpc.core.exception.SofaRpcException in project sofa-rpc by sofastack.
the class SofaRpcMetricsTest method buildRequest.
private SofaRequest buildRequest() throws NoSuchMethodException {
SofaRequest request = new SofaRequest();
request.setInterfaceName(TestService.class.getName());
request.setMethodName("echoStr");
request.setMethod(TestService.class.getMethod("func"));
request.setMethodArgs(new Object[] {});
request.setMethodArgSigs(new String[] {});
request.setTargetServiceUniqueName(TestService.class.getName() + ":1.0");
request.setTargetAppName("targetApp");
request.setSerializeType((byte) 11);
request.setTimeout(1024);
request.setInvokeType(RpcConstants.INVOKER_TYPE_SYNC);
request.addRequestProp(RemotingConstants.HEAD_APP_NAME, "app");
request.addRequestProp(RemotingConstants.HEAD_PROTOCOL, "bolt");
request.setSofaResponseCallback(new SofaResponseCallback<String>() {
@Override
public void onAppResponse(Object appResponse, String methodName, RequestBase request) {
}
@Override
public void onAppException(Throwable throwable, String methodName, RequestBase request) {
}
@Override
public void onSofaException(SofaRpcException sofaException, String methodName, RequestBase request) {
}
});
return request;
}
use of com.alipay.sofa.rpc.core.exception.SofaRpcException 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.core.exception.SofaRpcException 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();
}
}
}
Aggregations