Java Code Examples for com.alipay.remoting.Connection#getRemoteIP()

The following examples show how to use com.alipay.remoting.Connection#getRemoteIP() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DataServerConnectionFactory.java    From sofa-registry with Apache License 2.0 4 votes vote down vote up
private String getConnectId(Connection connection) {
    return connection.getRemoteIP() + ":" + connection.getRemotePort();
}
 
Example 2
Source File: DefaultMetaServiceImpl.java    From sofa-registry with Apache License 2.0 4 votes vote down vote up
@Override
public Map<String, Set<String>> getMetaServerMap() {
    HashMap<String, Set<String>> map = new HashMap<>();
    Set<String> set = dataServerConfig.getMetaServerIpAddresses();

    Map<String, Connection> connectionMap = metaServerConnectionFactory
        .getConnections(dataServerConfig.getLocalDataCenter());
    Connection connection = null;
    try {
        if (connectionMap.isEmpty()) {
            List<String> list = new ArrayList(set);
            Collections.shuffle(list);
            connection = ((BoltChannel) metaNodeExchanger.connect(new URL(list.iterator()
                .next(), dataServerConfig.getMetaServerPort()))).getConnection();
        } else {
            List<Connection> connections = new ArrayList<>(connectionMap.values());
            Collections.shuffle(connections);
            connection = connections.iterator().next();
            if (!connection.isFine()) {
                connection = ((BoltChannel) metaNodeExchanger.connect(new URL(connection
                    .getRemoteIP(), dataServerConfig.getMetaServerPort()))).getConnection();
            }
        }

        GetNodesRequest request = new GetNodesRequest(NodeType.META);
        final Connection finalConnection = connection;
        Object obj = metaNodeExchanger.request(new Request() {
            @Override
            public Object getRequestBody() {
                return request;
            }

            @Override
            public URL getRequestUrl() {
                return new URL(finalConnection.getRemoteIP(), finalConnection.getRemotePort());
            }
        }).getResult();
        if (obj instanceof NodeChangeResult) {
            NodeChangeResult<MetaNode> result = (NodeChangeResult<MetaNode>) obj;

            Map<String, Map<String, MetaNode>> metaNodesMap = result.getNodes();
            if (metaNodesMap != null && !metaNodesMap.isEmpty()) {
                Map<String, MetaNode> metaNodeMap = metaNodesMap.get(dataServerConfig
                    .getLocalDataCenter());
                if (metaNodeMap != null && !metaNodeMap.isEmpty()) {
                    map.put(dataServerConfig.getLocalDataCenter(), metaNodeMap.keySet());
                } else {
                    LOGGER
                        .error(
                            "[DefaultMetaServiceImpl] refresh connections from metaServer {} has no result!",
                            connection.getRemoteIP());
                }
            }
        }
    } catch (Exception e) {
        String con = connection != null ? connection.getRemoteIP() : "null";
        LOGGER.error("[DefaultMetaServiceImpl] refresh connections from metaServer error : {}",
            con, e);
        LOGGER
            .warn(
                "[DefaultMetaServiceImpl] refresh connections from metaServer error,refresh leader : {}",
                con);
        throw new RuntimeException(
            "[DefaultMetaServiceImpl] refresh connections from metaServer error!", e);
    }
    return map;
}
 
Example 3
Source File: DefaultMetaServiceImpl.java    From sofa-registry with Apache License 2.0 4 votes vote down vote up
@Override
public ProvideData fetchData(String dataInfoId) {

    Map<String, Connection> connectionMap = metaServerConnectionFactory
        .getConnections(dataServerConfig.getLocalDataCenter());
    String leader = getLeader().getIp();
    if (connectionMap.containsKey(leader)) {
        Connection connection = connectionMap.get(leader);
        if (connection.isFine()) {
            try {
                Request<FetchProvideDataRequest> request = new Request<FetchProvideDataRequest>() {

                    @Override
                    public FetchProvideDataRequest getRequestBody() {

                        return new FetchProvideDataRequest(dataInfoId);
                    }

                    @Override
                    public URL getRequestUrl() {
                        return new URL(connection.getRemoteIP(), connection.getRemotePort());
                    }
                };

                Response response = metaNodeExchanger.request(request);

                Object result = response.getResult();
                if (result instanceof ProvideData) {
                    return (ProvideData) result;
                } else {
                    LOGGER.error("fetch null provider data!");
                    throw new RuntimeException("MetaNodeService fetch null provider data!");
                }
            } catch (Exception e) {
                LOGGER.error("fetch provider data error! " + e.getMessage(), e);
                throw new RuntimeException("fetch provider data error! " + e.getMessage(), e);
            }
        }
    }
    String newip = refreshLeader().getIp();
    LOGGER.warn(
        "[ConnectionRefreshTask] refresh connections metaServer not fine,refresh leader : {}",
        newip);
    return null;

}