use of rpc.turbo.remote.RemoteException in project turbo-rpc by hank-whu.
the class TurboClient method register.
/**
* 注册一个远程服务
*
* @param clazz
*/
public <T> void register(Class<T> clazz) {
Objects.requireNonNull(clazz, "clazz");
T service = remoteServiceFactory.getService(clazz);
if (service != null) {
return;
}
Optional<App> appOptional = //
appMap.keySet().stream().filter(//
v -> v.isSupport(clazz)).findFirst();
if (!appOptional.isPresent()) {
throw new RemoteException("not support this service, " + clazz.getName());
}
App app = appOptional.get();
try {
remoteServiceFactory.register(app, clazz);
} catch (Exception e) {
throw new RemoteException(e);
}
}
use of rpc.turbo.remote.RemoteException in project turbo-rpc by hank-whu.
the class App method execute.
/**
* 推荐使用
*
* @param methodId
*
* @param timeout
* 超时时间,millseconds
*
* @param methodParam
*
* @return
*/
public CompletableFuture<?> execute(int methodId, long timeout, MethodParam methodParam, Invoker<CompletableFuture<?>> failoverInvoker) {
MethodRouter router;
if (//
methodId < 0 || methodId >= methodRouterMap.size() || (router = methodRouterMap.get(methodId)) == null) {
String msg = group + "#" + app + " " + "不支持的方法id, " + methodId;
logger.error(msg);
return CompletableFuture.failedFuture(new RemoteException(msg));
}
if (filters.size() > 0) {
RemoteContext.setRemoteMethod(router.getMethod());
RemoteContext.setServiceMethodName(router.getServiceMethodName());
}
ConnectorContext connectorContext = router.selectConnector();
for (int i = 0; i < MAX_CONNECTOR_SELECT_TIMES; i++) {
// 设定有限次数的尝试,防止发生死循环
if (connectorContext == null) {
break;
}
if (connectorContext.isClosed()) {
// 说明已经在其他地方关闭,实际不存在资源泄露的问题
try {
connectorContext.close();
} catch (Exception e) {
}
connectorContext = router.selectConnector();
} else if (//
ThreadLocalRandom.current().nextInt(100) < 20 && connectorContext.isZombie()) {
kill(connectorContext);
connectorContext = router.selectConnector();
} else {
break;
}
}
if (connectorContext == null) {
String msg = group + "#" + app + " " + "request error, 无可用连接 ";
if (logger.isWarnEnabled()) {
logger.warn(msg);
}
if (failoverInvoker == null) {
return CompletableFuture.failedFuture(new RemoteException(msg, false));
} else {
return failoverInvoker.invoke(methodParam);
}
}
int serviceId = getServiceId(connectorContext, methodId);
if (serviceId < 0) {
String msg = group + "#" + app + " " + "找不到对应的服务, methodId: " + methodId;
if (logger.isWarnEnabled()) {
logger.warn(msg);
}
return CompletableFuture.failedFuture(new RemoteException(msg, false));
}
return connectorContext.execute(serviceId, timeout, methodParam, failoverInvoker);
}
use of rpc.turbo.remote.RemoteException in project turbo-rpc by hank-whu.
the class ConnectorContext method execute.
/**
* 远程调用
*
* @param serviceId
* 远程serviceId
* @param timeout
* millseconds
* @param methodParam
* 方法参数对象,无参类型为null
* @param failoverInvoker
* 失败回退
* @return
*/
<T> CompletableFuture<T> execute(int serviceId, long timeout, MethodParam methodParam, Invoker<CompletableFuture<?>> failoverInvoker) {
if (isClosed) {
throw new RemoteException("已关闭的连接!");
}
int requestId = sequencer.next();
for (int i = 0; i < connectCount; i++) {
// 最多循环一遍
if (isZombie(channelIndex(requestId))) {
requestId = sequencer.next();
continue;
}
break;
}
Request request = new Request();
request.setServiceId(serviceId);
request.setRequestId(requestId);
if (methodParam instanceof EmptyMethodParam) {
request.setMethodParam(null);
} else {
request.setMethodParam(methodParam);
}
if (globalTimeout > 0) {
timeout = globalTimeout;
}
CompletableFuture<Response> future = new CompletableFuture<>();
try {
if (requestWaitSemaphore != null) {
requestWaitSemaphore.acquire();
}
boolean allowSend = doRequestFilter(request);
if (allowSend) {
long expireTime = SystemClock.fast().mills() + timeout;
//
connector.send(//
channelIndex(request), new RequestWithFuture(request, future, expireTime));
} else {
future.completeExceptionally(new RemoteException(RpcClientFilter.CLIENT_FILTER_DENY, false));
}
} catch (Exception e) {
future.completeExceptionally(e);
}
if (failoverInvoker == null) {
return handleResult(request, future);
} else {
return handleResult(request, future, failoverInvoker, methodParam);
}
}
use of rpc.turbo.remote.RemoteException in project turbo-rpc by hank-whu.
the class TurboClient method register.
/**
* 注册一个远程服务
*
* @param group
* @param app
* @param clazz
* @param failover
*/
public <T> void register(String group, String app, Class<T> clazz, Object failover) {
T service = remoteServiceFactory.getService(clazz);
if (service != null) {
return;
}
Objects.requireNonNull(group, "group");
Objects.requireNonNull(app, "app");
Optional<App> appOptional = //
appMap.keySet().stream().filter(//
v -> v.group.equals(group)).filter(//
v -> v.app.equals(app)).filter(//
v -> v.isSupport(clazz)).findFirst();
if (!appOptional.isPresent()) {
throw new RemoteException("not support this service, " + InvokerUtils.getServiceClassName(group, app, clazz));
}
App application = appOptional.get();
try {
remoteServiceFactory.register(application, clazz);
remoteServiceFactory.setFailover(application, clazz, service, failover);
} catch (Exception e) {
throw new RemoteException(e);
}
}
use of rpc.turbo.remote.RemoteException in project turbo-rpc by hank-whu.
the class TurboClient method setFailover.
/**
* 设置失败回退方法
*
* @param clazz
* @param failover
*/
public <T> void setFailover(Class<T> clazz, Object failover) {
Objects.requireNonNull(clazz, "clazz");
T service = remoteServiceFactory.getService(clazz);
if (service == null) {
throw new RemoteException("not register this service, " + clazz.getName());
}
try {
App app = ((RemoteInterface) service).getApp();
remoteServiceFactory.setFailover(app, clazz, service, failover);
} catch (Exception e) {
throw new RemoteException(e);
}
}
Aggregations