Search in sources :

Example 1 with ApplicationInstance

use of info.xiancloud.core.distribution.service_discovery.ApplicationInstance in project xian by happyyangyuan.

the class ApplicationRegistrationBridge method applicationInstance.

static ApplicationInstance applicationInstance(NodeStatus nodeStatus) {
    ApplicationInstance applicationInstance = new ApplicationInstance();
    applicationInstance.setRegistrationTimestamp(nodeStatus.getInitTime());
    applicationInstance.setPort(nodeStatus.getPort());
    applicationInstance.setPayload(nodeStatus);
    applicationInstance.setName(NodeIdBean.parse(nodeStatus.getNodeId()).getApplication());
    applicationInstance.setId(nodeStatus.getNodeId());
    applicationInstance.setEnabled(true);
    applicationInstance.setAddress(nodeStatus.getHost());
    return applicationInstance;
}
Also used : ApplicationInstance(info.xiancloud.core.distribution.service_discovery.ApplicationInstance)

Example 2 with ApplicationInstance

use of info.xiancloud.core.distribution.service_discovery.ApplicationInstance in project xian by happyyangyuan.

the class RpcNettyClient method lazyInit.

/**
 * @param nodeId The node's id to which you want to initialize the connection. This method is thread-safe because it is synchronized.
 * @throws info.xiancloud.plugin.distribution.exception.ApplicationInstanceOfflineException Because the destination node is offline, of cause you cannot initialize the connection.
 * @throws Exception                                                                        Other unknown exceptions.
 */
private static void lazyInit(String nodeId) throws Exception {
    lock.lock();
    String host = null;
    int port = -1;
    try {
        if (channelAvailable(nodeId)) {
            LOG.debug(String.format("RpcClient:已经存在一个与%s的长连接,不再新建连接.", nodeId));
            return;
        }
        LOG.info(String.format("RpcClient:开始新建与%s的长连接...", nodeId));
        ApplicationInstance node = ApplicationRouter.singleton.getInstance(nodeId);
        // 如果是在同一台主机内部部署的两个节点,那么避免走交换机、路由器了
        host = Objects.equals(node.getAddress(), EnvUtil.getLocalIp()) ? "127.0.0.1" : node.getAddress();
        port = node.getPort();
        final SslContext sslCtx;
        if (SSL) {
            sslCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build();
        } else {
            sslCtx = null;
        }
        EventLoopGroup group = new NioEventLoopGroup(1);
        Bootstrap b = new Bootstrap();
        b.group(group).option(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(// 10m
        10 * 1024 * 1024, // 20m
        20 * 1024 * 1024)).channel(NioSocketChannel.class).handler(new RpcNettyClientInitializer(sslCtx, nodeId)).option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 100);
        Channel connectedChannel = b.connect(host, port).sync().channel();
        connectedChannel.closeFuture().addListener(future -> {
            group.shutdownGracefully();
            LOG.info("The EventLoopGroup has been terminated completely and all Channels that belong to the group have been closed.");
        });
        nodeId_to_connectedChannel_map.put(nodeId, connectedChannel);
        LOG.info(new JSONObject() {

            {
                put("toNodeId", nodeId);
                put("rpcRemoteAddress", connectedChannel.remoteAddress().toString());
                put("type", "rpcChannelConnected");
                put("description", String.format("RpcClient:与%s的长连接建立完毕, remoteAddress=%s", nodeId, connectedChannel.remoteAddress()));
            }
        }.toJSONString());
    } catch (Throwable e) {
        throw new Exception(String.format("与远程节点%s建立长连接失败:host=%s,port=%s", nodeId, host, port), e);
    } finally {
        lock.unlock();
    }
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Channel(io.netty.channel.Channel) ApplicationInstanceOfflineException(info.xiancloud.core.distribution.exception.ApplicationInstanceOfflineException) ApplicationInstance(info.xiancloud.core.distribution.service_discovery.ApplicationInstance) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) JSONObject(com.alibaba.fastjson.JSONObject) Bootstrap(io.netty.bootstrap.Bootstrap) WriteBufferWaterMark(io.netty.channel.WriteBufferWaterMark) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) SslContext(io.netty.handler.ssl.SslContext)

Example 3 with ApplicationInstance

use of info.xiancloud.core.distribution.service_discovery.ApplicationInstance in project xian by happyyangyuan.

the class ApplicationRouter method localInstance.

@Override
public ApplicationInstance localInstance(String application) {
    if (EnvUtil.getApplication().equals(application)) {
        // 本地节点直接返回不需要从服务注册器内寻找 todo 请封装成统一代码,然后使用adapter与curator serviceInstance互转
        ApplicationInstance applicationInstance = new ApplicationInstance();
        applicationInstance.setRegistrationTimestamp(LocalNodeManager.singleton.getFullStatus().getInitTime());
        applicationInstance.setPort(Node.RPC_PORT);
        applicationInstance.setPayload(LocalNodeManager.singleton.getFullStatus());
        applicationInstance.setName(application);
        applicationInstance.setId(LocalNodeManager.LOCAL_NODE_ID);
        applicationInstance.setEnabled(true);
        applicationInstance.setAddress(EnvUtil.getLocalIp());
        return applicationInstance;
    }
    return null;
}
Also used : ApplicationInstance(info.xiancloud.core.distribution.service_discovery.ApplicationInstance)

Example 4 with ApplicationInstance

use of info.xiancloud.core.distribution.service_discovery.ApplicationInstance in project xian by happyyangyuan.

the class GetNodeInfoUnit method execute.

@Override
public UnitResponse execute(UnitRequest msg) {
    if (StringUtil.isEmpty(msg.get("application"))) {
        JSONArray nodeInfoArray = new JSONArray();
        for (String application : ApplicationDiscovery.singleton.queryForNames()) {
            JSONObject nodeInfo;
            try {
                nodeInfo = nodeInfo(ApplicationRouter.singleton.newestDefinition(application));
            } catch (ApplicationUndefinedException e) {
                LOG.info("忽略未定义的application:" + application);
                continue;
            }
            nodeInfoArray.add(nodeInfo);
        }
        return UnitResponse.success(nodeInfoArray);
    } else
        try {
            JSONArray nodeInfoArray = new JSONArray();
            List<ApplicationInstance> clients = ApplicationRouter.singleton.allInstances(msg.get("application"));
            for (ApplicationInstance instance : clients) {
                nodeInfoArray.add(nodeInfo(instance.getPayload()));
            }
            return UnitResponse.success(nodeInfoArray);
        } catch (ApplicationOfflineException | ApplicationUndefinedException e) {
            return e.toUnitResponse();
        }
}
Also used : ApplicationInstance(info.xiancloud.core.distribution.service_discovery.ApplicationInstance) JSONObject(com.alibaba.fastjson.JSONObject) JSONArray(com.alibaba.fastjson.JSONArray) ApplicationUndefinedException(info.xiancloud.core.distribution.exception.ApplicationUndefinedException) List(java.util.List)

Example 5 with ApplicationInstance

use of info.xiancloud.core.distribution.service_discovery.ApplicationInstance in project xian by happyyangyuan.

the class ApplicationRouter method firstInstance.

@Override
public ApplicationInstance firstInstance(String application) throws ApplicationOfflineException, ApplicationUndefinedException {
    ApplicationInstance instance = localInstance(application);
    if (instance != null)
        return instance;
    newestDefinition(application);
    if (ApplicationDiscovery.singleton != null)
        instance = ApplicationDiscovery.singleton.firstInstance(application);
    if (instance != null)
        return instance;
    throw new ApplicationOfflineException(application);
}
Also used : ApplicationInstance(info.xiancloud.core.distribution.service_discovery.ApplicationInstance) ApplicationOfflineException(info.xiancloud.core.distribution.exception.ApplicationOfflineException)

Aggregations

ApplicationInstance (info.xiancloud.core.distribution.service_discovery.ApplicationInstance)7 ApplicationOfflineException (info.xiancloud.core.distribution.exception.ApplicationOfflineException)3 JSONObject (com.alibaba.fastjson.JSONObject)2 JSONArray (com.alibaba.fastjson.JSONArray)1 ApplicationInstanceOfflineException (info.xiancloud.core.distribution.exception.ApplicationInstanceOfflineException)1 ApplicationUndefinedException (info.xiancloud.core.distribution.exception.ApplicationUndefinedException)1 Bootstrap (io.netty.bootstrap.Bootstrap)1 Channel (io.netty.channel.Channel)1 EventLoopGroup (io.netty.channel.EventLoopGroup)1 WriteBufferWaterMark (io.netty.channel.WriteBufferWaterMark)1 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)1 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)1 SslContext (io.netty.handler.ssl.SslContext)1 List (java.util.List)1