Search in sources :

Example 1 with RpcAction

use of com.duangframework.rpc.common.RpcAction in project duangframework by tcrct.

the class RpcUtils method getAssignRpcActionMap.

/**
 * 如果有指定rpc.endport的话,则根据指定的endpoint地址去取对应的节点数据返回,一般用于测试开发
 * @param actionMap
 * @return
 */
public static Map<String, List<RpcAction>> getAssignRpcActionMap(Map<String, List<RpcAction>> actionMap) {
    // endport的格式为 ip:port,
    String[] endPortArray = ConfigKit.duang().key("rpc.endport").asArray();
    if (ToolsKit.isEmpty(endPortArray)) {
        return null;
    }
    List endPortList = Arrays.asList(endPortArray);
    Map<String, List<RpcAction>> newActionMap = new HashMap<String, List<RpcAction>>();
    for (Iterator<Map.Entry<String, List<RpcAction>>> it = actionMap.entrySet().iterator(); it.hasNext(); ) {
        Map.Entry<String, List<RpcAction>> entry = it.next();
        List<RpcAction> actionList = entry.getValue();
        List<RpcAction> actionListNew = new ArrayList<>();
        if (ToolsKit.isNotEmpty(actionList)) {
            for (RpcAction action : actionList) {
                String publicEndPort = action.getRemoteip() + ":" + action.getPort();
                String intranetEndPort = action.getIntranetip() + ":" + action.getPort();
                if (endPortList.contains(publicEndPort) || endPortList.contains(intranetEndPort) || endPortList.contains(action.getRemoteip()) || endPortList.contains(action.getIntranetip())) {
                    actionListNew.add(action);
                }
            }
        }
        if (ToolsKit.isNotEmpty(actionListNew)) {
            newActionMap.put(entry.getKey(), actionListNew);
        }
    }
    return ToolsKit.isEmpty(newActionMap) ? null : newActionMap;
}
Also used : RpcAction(com.duangframework.rpc.common.RpcAction)

Example 2 with RpcAction

use of com.duangframework.rpc.common.RpcAction in project duangframework by tcrct.

the class RpcFactory method initService.

/**
 * 初始化RPC服务, 发布到注册中心
 * @param classSet
 */
public static void initService(Set<Class<?>> classSet) throws Exception {
    if (ToolsKit.isEmpty(classSet)) {
        throw new EmptyNullException("rpc server class set is null");
    }
    for (Class<?> rpcInterfaceClass : classSet) {
        Rpc rpcAnnotation = rpcInterfaceClass.getAnnotation(Rpc.class);
        if (!rpcInterfaceClass.isInterface() || ToolsKit.isEmpty(rpcAnnotation)) {
            continue;
        }
        String interfaceName = rpcInterfaceClass.getName();
        String serviceName = rpcAnnotation.service();
        if (ToolsKit.isEmpty(serviceName)) {
            throw new EmptyNullException("serviceFullPath is empty");
        }
        HANDLER_MAP.put(interfaceName, new RpcAction(ClassUtils.loadClass(serviceName), rpcInterfaceClass, IpUtils.getLocalHostIP(false), IpUtils.getLocalHostIP(true), RpcUtils.getPort()));
    }
    if (ToolsKit.isEmpty(HANDLER_MAP)) {
        logger.warn("Rpc service is null, exit initService...");
        return;
    }
    // 启动Netty服务
    if (startRpcServer()) {
        // 发布服务
        publish();
    }
}
Also used : Rpc(com.duangframework.core.annotation.rpc.Rpc) EmptyNullException(com.duangframework.core.exceptions.EmptyNullException) RpcAction(com.duangframework.rpc.common.RpcAction)

Example 3 with RpcAction

use of com.duangframework.rpc.common.RpcAction in project duangframework by tcrct.

the class RpcFactory method watchNode.

public static void watchNode() {
    for (Iterator<String> iterator = watchNodePath.iterator(); iterator.hasNext(); ) {
        String path = iterator.next();
        if (ToolsKit.isEmpty(path)) {
            continue;
        }
        boolean isExists = ZooKit.duang().path(path).exists();
        if (!isExists) {
            throw new RpcException("zookeeper note[" + path + "]  is not exists");
        }
        List<String> nodeList = ZooKit.duang().path(path).children();
        if (ToolsKit.isEmpty(nodeList)) {
            throw new RpcException("zookeeper children note[" + path + "]  is empty");
        }
        for (String nodePath : nodeList) {
            String subPath = path + "/" + nodePath;
            String jsonText = ZooKit.duang().path(subPath).get();
            Map<String, Object> map = ToolsKit.jsonParseObject(jsonText, Map.class);
            if (ToolsKit.isNotEmpty(map)) {
                for (Iterator<Map.Entry<String, Object>> it = map.entrySet().iterator(); it.hasNext(); ) {
                    Map.Entry<String, Object> entry = it.next();
                    String key = entry.getKey();
                    String value = entry.getValue() + "";
                    RpcAction rpcAction = ToolsKit.jsonParseObject(value, RpcAction.class);
                    addRpcAction2List(key, rpcAction);
                }
            }
        }
    }
    // 如果有指定的调用服务器时 2017-7-13
    Map<String, List<RpcAction>> actionMapNew = RpcUtils.getAssignRpcActionMap(actionMap);
    if (ToolsKit.isNotEmpty(actionMapNew)) {
        actionMap.clear();
        actionMap.putAll(actionMapNew);
        logger.warn("AssignRpcActionEndport:  " + ToolsKit.toJsonString(actionMap));
    }
    // 监听该节点下的所有目录
    if (ToolsKit.isEmpty(zooKeeperListener)) {
        zooKeeperListener = new ZooKeeperListener(watchNodePath);
        zooKeeperListener.startListener();
    }
}
Also used : RpcException(com.duangframework.core.exceptions.RpcException) RpcAction(com.duangframework.rpc.common.RpcAction) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 4 with RpcAction

use of com.duangframework.rpc.common.RpcAction in project duangframework by tcrct.

the class RpcClientProxy method doProxy.

@Override
public Object doProxy(ProxyChain proxyChain) throws Exception {
    Method method = proxyChain.getTargetMethod();
    Class<?> targetClass = proxyChain.getTargetClass();
    // 发现服务
    RpcAction action = RpcFactory.discoverService(targetClass);
    if (ToolsKit.isEmpty(action)) {
        throw new RpcException("Can't Discover " + targetClass.getName() + " RpcAction! Please Check ZooKeep Server");
    }
    // 创建并初始化 RPC 请求
    RpcRequest request = new RpcRequest(System.currentTimeMillis(), RpcUtils.getRequestId());
    request.setIface(targetClass.getName());
    request.setMethodName(method.getName());
    request.setParameterTypes(method.getParameterTypes());
    request.setParameters(proxyChain.getMethodParams());
    request.setService(action.getService().getName());
    logger.warn("[" + RpcUtils.formatDate(request.getStartTime()) + "] request[" + request.getRequestId() + "] [" + action.getRemoteip() + "/" + action.getIntranetip() + ":" + action.getPort() + "] " + request.getIface() + "." + request.getMethodName());
    RpcResponse response = RpcClient.getInstance().call(request, action);
    if (response != null) {
        if (response.isError()) {
            logger.warn("response error:" + response.getError().getMessage());
            throw new RpcException(response.getError());
        } else {
            return response.getResult();
        }
    }
    return null;
}
Also used : RpcAction(com.duangframework.rpc.common.RpcAction) RpcException(com.duangframework.core.exceptions.RpcException) RpcRequest(com.duangframework.rpc.common.RpcRequest) Method(java.lang.reflect.Method) RpcResponse(com.duangframework.rpc.common.RpcResponse)

Aggregations

RpcAction (com.duangframework.rpc.common.RpcAction)4 RpcException (com.duangframework.core.exceptions.RpcException)2 Rpc (com.duangframework.core.annotation.rpc.Rpc)1 EmptyNullException (com.duangframework.core.exceptions.EmptyNullException)1 RpcRequest (com.duangframework.rpc.common.RpcRequest)1 RpcResponse (com.duangframework.rpc.common.RpcResponse)1 Method (java.lang.reflect.Method)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1