Search in sources :

Example 1 with RemoteException

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);
    }
}
Also used : App(rpc.turbo.transport.client.App) EventLoopGroup(io.netty.channel.EventLoopGroup) EventLoopGroupHolder(rpc.turbo.common.EventLoopGroupHolder) RemoteServiceFactory(rpc.turbo.remote.RemoteServiceFactory) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) IOException(java.io.IOException) Objects(java.util.Objects) InvokerUtils(rpc.turbo.invoke.InvokerUtils) App(rpc.turbo.transport.client.App) HostPort(rpc.turbo.config.HostPort) AppConfig(rpc.turbo.config.client.AppConfig) Closeable(java.io.Closeable) RemoteException(rpc.turbo.remote.RemoteException) RemoteInterface(rpc.turbo.remote.RemoteInterface) RpcClientFilter(rpc.turbo.filter.RpcClientFilter) Optional(java.util.Optional) ClientConfig(rpc.turbo.config.client.ClientConfig) Log(org.apache.commons.logging.Log) LogFactory(org.apache.commons.logging.LogFactory) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) FailoverInvokerFactory(rpc.turbo.invoke.FailoverInvokerFactory) RemoteException(rpc.turbo.remote.RemoteException) IOException(java.io.IOException) RemoteException(rpc.turbo.remote.RemoteException)

Example 2 with RemoteException

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);
}
Also used : RemoteException(rpc.turbo.remote.RemoteException) RemoteException(rpc.turbo.remote.RemoteException) IOException(java.io.IOException) InvokeException(rpc.turbo.invoke.InvokeException) ExecutionException(java.util.concurrent.ExecutionException)

Example 3 with RemoteException

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);
    }
}
Also used : Response(rpc.turbo.protocol.Response) CompletableFuture(java.util.concurrent.CompletableFuture) RequestWithFuture(rpc.turbo.transport.client.future.RequestWithFuture) EmptyMethodParam(rpc.turbo.param.EmptyMethodParam) Request(rpc.turbo.protocol.Request) RemoteException(rpc.turbo.remote.RemoteException) RemoteException(rpc.turbo.remote.RemoteException) IOException(java.io.IOException)

Example 4 with RemoteException

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);
    }
}
Also used : App(rpc.turbo.transport.client.App) EventLoopGroup(io.netty.channel.EventLoopGroup) EventLoopGroupHolder(rpc.turbo.common.EventLoopGroupHolder) RemoteServiceFactory(rpc.turbo.remote.RemoteServiceFactory) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) IOException(java.io.IOException) Objects(java.util.Objects) InvokerUtils(rpc.turbo.invoke.InvokerUtils) App(rpc.turbo.transport.client.App) HostPort(rpc.turbo.config.HostPort) AppConfig(rpc.turbo.config.client.AppConfig) Closeable(java.io.Closeable) RemoteException(rpc.turbo.remote.RemoteException) RemoteInterface(rpc.turbo.remote.RemoteInterface) RpcClientFilter(rpc.turbo.filter.RpcClientFilter) Optional(java.util.Optional) ClientConfig(rpc.turbo.config.client.ClientConfig) Log(org.apache.commons.logging.Log) LogFactory(org.apache.commons.logging.LogFactory) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) FailoverInvokerFactory(rpc.turbo.invoke.FailoverInvokerFactory) RemoteException(rpc.turbo.remote.RemoteException) IOException(java.io.IOException) RemoteException(rpc.turbo.remote.RemoteException)

Example 5 with RemoteException

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);
    }
}
Also used : App(rpc.turbo.transport.client.App) RemoteInterface(rpc.turbo.remote.RemoteInterface) RemoteException(rpc.turbo.remote.RemoteException) IOException(java.io.IOException) RemoteException(rpc.turbo.remote.RemoteException)

Aggregations

IOException (java.io.IOException)7 RemoteException (rpc.turbo.remote.RemoteException)7 App (rpc.turbo.transport.client.App)5 AppConfig (rpc.turbo.config.client.AppConfig)3 RemoteInterface (rpc.turbo.remote.RemoteInterface)3 EventLoopGroup (io.netty.channel.EventLoopGroup)2 Closeable (java.io.Closeable)2 Objects (java.util.Objects)2 Optional (java.util.Optional)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2 Log (org.apache.commons.logging.Log)2 LogFactory (org.apache.commons.logging.LogFactory)2 EventLoopGroupHolder (rpc.turbo.common.EventLoopGroupHolder)2 HostPort (rpc.turbo.config.HostPort)2 ClientConfig (rpc.turbo.config.client.ClientConfig)2 RpcClientFilter (rpc.turbo.filter.RpcClientFilter)2 FailoverInvokerFactory (rpc.turbo.invoke.FailoverInvokerFactory)2 InvokerUtils (rpc.turbo.invoke.InvokerUtils)2 RemoteServiceFactory (rpc.turbo.remote.RemoteServiceFactory)2