use of org.apache.dubbo.rpc.RpcException in project dubbo by alibaba.
the class GrpcInvoker method doInvoke.
@Override
protected Result doInvoke(Invocation invocation) throws Throwable {
try {
Result result = target.invoke(invocation);
// FIXME result is an AsyncRpcResult instance.
Throwable e = result.getException();
if (e != null) {
throw getRpcException(getInterface(), getUrl(), invocation, e);
}
return result;
} catch (RpcException e) {
if (e.getCode() == RpcException.UNKNOWN_EXCEPTION) {
e.setCode(getErrorCode(e.getCause()));
}
throw e;
} catch (Throwable e) {
throw getRpcException(getInterface(), getUrl(), invocation, e);
}
}
use of org.apache.dubbo.rpc.RpcException in project dubbo by alibaba.
the class GrpcInvoker method getRpcException.
private RpcException getRpcException(Class<?> type, URL url, Invocation invocation, Throwable e) {
RpcException re = new RpcException("Failed to invoke remote service: " + type + ", method: " + invocation.getMethodName() + ", cause: " + e.getMessage(), e);
re.setCode(getErrorCode(e));
return re;
}
use of org.apache.dubbo.rpc.RpcException in project dubbo by alibaba.
the class GrpcProtocol method doExport.
@Override
protected <T> Runnable doExport(T proxiedImpl, Class<T> type, URL url) throws RpcException {
String key = url.getAddress();
ProtocolServer protocolServer = serverMap.computeIfAbsent(key, k -> {
DubboHandlerRegistry registry = new DubboHandlerRegistry();
NettyServerBuilder builder = NettyServerBuilder.forPort(url.getPort()).fallbackHandlerRegistry(registry);
Server originalServer = GrpcOptionsUtils.buildServerBuilder(url, builder).build();
GrpcRemotingServer remotingServer = new GrpcRemotingServer(originalServer, registry);
return new ProxyProtocolServer(remotingServer);
});
GrpcRemotingServer grpcServer = (GrpcRemotingServer) protocolServer.getRemotingServer();
ServiceRepository serviceRepository = ApplicationModel.getServiceRepository();
ProviderModel providerModel = serviceRepository.lookupExportedService(url.getServiceKey());
if (providerModel == null) {
throw new IllegalStateException("Service " + url.getServiceKey() + "should have already been stored in service repository, " + "but failed to find it.");
}
Object originalImpl = providerModel.getServiceInstance();
Class<?> implClass = originalImpl.getClass();
try {
Method method = implClass.getMethod("setProxiedImpl", type);
method.invoke(originalImpl, proxiedImpl);
} catch (Exception e) {
throw new IllegalStateException("Failed to set dubbo proxied service impl to stub, please make sure your stub " + "was generated by the dubbo-protoc-compiler.", e);
}
grpcServer.getRegistry().addService((BindableService) originalImpl, url.getServiceKey());
if (!grpcServer.isStarted()) {
grpcServer.start();
}
return () -> grpcServer.getRegistry().removeService(url.getServiceKey());
}
use of org.apache.dubbo.rpc.RpcException in project dubbo by alibaba.
the class MemcachedProtocol method protocolBindingRefer.
@Override
public <T> Invoker<T> protocolBindingRefer(final Class<T> type, final URL url) throws RpcException {
try {
String address = url.getAddress();
String backup = url.getParameter(RemotingConstants.BACKUP_KEY);
if (backup != null && backup.length() > 0) {
address += "," + backup;
}
MemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil.getAddresses(address));
final MemcachedClient memcachedClient = builder.build();
final int expiry = url.getParameter("expiry", 0);
final String get = url.getParameter("get", "get");
final String set = url.getParameter("set", Map.class.equals(type) ? "put" : "set");
final String delete = url.getParameter("delete", Map.class.equals(type) ? "remove" : "delete");
return new AbstractInvoker<T>(type, url) {
@Override
protected Result doInvoke(Invocation invocation) throws Throwable {
try {
Object value = null;
if (get.equals(invocation.getMethodName())) {
if (invocation.getArguments().length != 1) {
throw new IllegalArgumentException("The memcached get method arguments mismatch, must only one arguments. interface: " + type.getName() + ", method: " + invocation.getMethodName() + ", url: " + url);
}
value = memcachedClient.get(String.valueOf(invocation.getArguments()[0]));
} else if (set.equals(invocation.getMethodName())) {
if (invocation.getArguments().length != 2) {
throw new IllegalArgumentException("The memcached set method arguments mismatch, must be two arguments. interface: " + type.getName() + ", method: " + invocation.getMethodName() + ", url: " + url);
}
memcachedClient.set(String.valueOf(invocation.getArguments()[0]), expiry, invocation.getArguments()[1]);
} else if (delete.equals(invocation.getMethodName())) {
if (invocation.getArguments().length != 1) {
throw new IllegalArgumentException("The memcached delete method arguments mismatch, must only one arguments. interface: " + type.getName() + ", method: " + invocation.getMethodName() + ", url: " + url);
}
memcachedClient.delete(String.valueOf(invocation.getArguments()[0]));
} else {
throw new UnsupportedOperationException("Unsupported method " + invocation.getMethodName() + " in memcached service.");
}
return AsyncRpcResult.newDefaultAsyncResult(value, invocation);
} catch (Throwable t) {
RpcException re = new RpcException("Failed to invoke memcached service method. interface: " + type.getName() + ", method: " + invocation.getMethodName() + ", url: " + url + ", cause: " + t.getMessage(), t);
if (t instanceof TimeoutException || t instanceof SocketTimeoutException) {
re.setCode(RpcException.TIMEOUT_EXCEPTION);
} else if (t instanceof MemcachedException || t instanceof IOException) {
re.setCode(RpcException.NETWORK_EXCEPTION);
}
throw re;
}
}
@Override
public void destroy() {
super.destroy();
try {
memcachedClient.shutdown();
} catch (Throwable e) {
logger.warn(e.getMessage(), e);
}
}
};
} catch (Throwable t) {
throw new RpcException("Failed to refer memcached service. interface: " + type.getName() + ", url: " + url + ", cause: " + t.getMessage(), t);
}
}
use of org.apache.dubbo.rpc.RpcException in project dubbo by alibaba.
the class ThriftProtocol method getTServer.
private <T> TServer getTServer(T impl, Class<T> type, URL url) {
TThreadedSelectorServer.Args tArgs = null;
String typeName = type.getName();
TServer tserver;
if (typeName.endsWith(THRIFT_IFACE)) {
String processorClsName = typeName.substring(0, typeName.indexOf(THRIFT_IFACE)) + THRIFT_PROCESSOR;
try {
Class<?> clazz = Class.forName(processorClsName);
Constructor constructor = clazz.getConstructor(type);
TProcessor tprocessor = (TProcessor) constructor.newInstance(impl);
processor.registerProcessor(typeName, tprocessor);
tserver = SERVER_MAP.get(url.getAddress());
if (tserver == null) {
/**
*Solve the problem of only 50 of the default number of concurrent connections
*/
TNonblockingServerSocket.NonblockingAbstractServerSocketArgs args = new TNonblockingServerSocket.NonblockingAbstractServerSocketArgs();
/**
*1000 connections
*/
args.backlog(1000);
String bindIp = url.getParameter(Constants.BIND_IP_KEY, url.getHost());
if (url.getParameter(ANYHOST_KEY, false)) {
bindIp = ANYHOST_VALUE;
}
int bindPort = url.getParameter(Constants.BIND_PORT_KEY, url.getPort());
args.bindAddr(new InetSocketAddress(bindIp, bindPort));
/**
*timeout: 10s
*/
args.clientTimeout(10000);
TNonblockingServerSocket transport = new TNonblockingServerSocket(args);
tArgs = new TThreadedSelectorServer.Args(transport);
tArgs.workerThreads(200);
tArgs.selectorThreads(4);
tArgs.acceptQueueSizePerThread(256);
tArgs.processor(processor);
tArgs.transportFactory(new TFramedTransport.Factory());
tArgs.protocolFactory(new TCompactProtocol.Factory());
} else {
// if server is starting, return and do nothing here
return null;
}
} catch (Exception e) {
logger.error(e.getMessage(), e);
throw new RpcException("Fail to create nativethrift server(" + url + ") : " + e.getMessage(), e);
}
}
if (tArgs == null) {
logger.error("Fail to create native thrift server(" + url + ") due to null args");
throw new RpcException("Fail to create nativethrift server(" + url + ") due to null args");
}
tserver = new TThreadedSelectorServer(tArgs);
return tserver;
}
Aggregations