Search in sources :

Example 1 with HostAndPort

use of io.transport.sdk.Configuration.HostAndPort in project transporter by wang4ever.

the class TransportConnector method connect.

/**
 * 连接到服务器
 *
 * @param sync
 * @param callback
 */
public void connect(final boolean sync) {
    try {
        if (this.bootstrap == null)
            this.configure();
        // Get the current load balancing node.
        HostAndPort hap = this.config.getRoutingBalancer().determineCurrentLookupNode();
        config.getLogger().info("Connect to " + hap + " failed.");
        // Configuration completion, starting connection with server,
        // blocking by calling the sync synchronization method until the
        // connection is successful.
        ChannelFuture cf = this.bootstrap.connect(hap.getHost(), hap.getPort());
        // Reconnect listener.
        cf.addListener((ChannelFuture f) -> {
            if (!f.isSuccess()) {
                f.channel().eventLoop().schedule(() -> {
                    // Update connection failure (counter, for load balance
                    // calculation).
                    config.getRoutingBalancer().onConnectFailed(hap);
                    // Retry connection.
                    this.connect(sync);
                }, config.getReconnectDelay(), TimeUnit.SECONDS);
            } else {
                this.config.getLogger().info("Connected to " + hap);
                // Log on after the connection is successful.
                this.client.login();
            }
        });
        if (sync)
            this.channel = cf.sync().channel();
        else
            this.channel = cf.channel();
    } catch (Exception e) {
        throw new TransportException("Connection failed.", e);
    } finally {
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            try {
                this.close();
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }));
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) HostAndPort(io.transport.sdk.Configuration.HostAndPort) TransportException(io.transport.sdk.exception.TransportException) IOException(java.io.IOException) TransportException(io.transport.sdk.exception.TransportException)

Example 2 with HostAndPort

use of io.transport.sdk.Configuration.HostAndPort in project transporter by wang4ever.

the class HashRoutingLoadBalancer method getPreselectedHap.

/**
 * 统计获取预选节点
 */
private HostAndPort getPreselectedHap() {
    List<HostAndPort> preHaps = new ArrayList<>();
    for (HostAndPort hap0 : this.config.getHostAndPorts()) {
        if (// 是否可作为预选节点
        this.isRetryNode(hap0))
            preHaps.add(hap0);
    }
    int crc16 = CRC16.crc16Modbus(Constants.processSerial.getBytes());
    int size = preHaps.size();
    if (size == 0)
        throw new TransportException("All cluster nodes of the server failed.");
    int nodeIndex = crc16 % size & (size - 1);
    return preHaps.get(nodeIndex);
}
Also used : HostAndPort(io.transport.sdk.Configuration.HostAndPort) ArrayList(java.util.ArrayList) TransportException(io.transport.sdk.exception.TransportException)

Example 3 with HostAndPort

use of io.transport.sdk.Configuration.HostAndPort in project transporter by wang4ever.

the class TransportChannel method connect.

public void connect(final boolean sync) {
    try {
        if (this.bootstrap == null)
            this.configure();
        // 获取当前负载均衡节点
        HostAndPort hap = this.config.getRoutingBalancer().determineCurrentLookupNode();
        // 配置完成,开始连接server, 通过调用sync同步方法阻塞直到连接成功
        ChannelFuture cf = this.bootstrap.connect(hap.getHost(), hap.getPort());
        // Reconnect listener.
        cf.addListener((ChannelFuture f) -> {
            if (!f.isSuccess()) {
                f.channel().eventLoop().schedule(() -> {
                    config.getLogger().info("Reconnect to " + hap + " failed.");
                    // 更新连接失败(计数器,用于负载均衡计算)
                    config.getRoutingBalancer().onConnectFailed(hap);
                    // 重试连接
                    connect(sync);
                }, config.getReconnectDelay(), TimeUnit.SECONDS);
            } else {
                this.config.getLogger().info("Connected to " + hap);
                // 连接成功后重新登录
                TransportClients.getInstance().login();
            }
        });
        if (sync)
            this.channel = cf.sync().channel();
        else
            this.channel = cf.channel();
    } catch (Exception e) {
        throw new TransportException("Connection failed.", e);
    } finally {
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            try {
                this.close();
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }));
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) HostAndPort(io.transport.sdk.Configuration.HostAndPort) TransportException(io.transport.sdk.exception.TransportException) IOException(java.io.IOException) TransportException(io.transport.sdk.exception.TransportException)

Example 4 with HostAndPort

use of io.transport.sdk.Configuration.HostAndPort in project transporter by wang4ever.

the class HashRoutingLoadBalancer method main.

public static void main(String[] args) {
    // 初始化
    Configuration config = new Configuration(null, null, null, null);
    List<HostAndPort> hostAndPorts = new ArrayList<>();
    HostAndPort hap0 = new HostAndPort("192.168.1.100", 10030);
    HostAndPort hap1 = new HostAndPort("192.168.1.101", 10030);
    HostAndPort hap2 = new HostAndPort("192.168.1.102", 10030);
    HostAndPort hap3 = new HostAndPort("192.168.1.103", 10030);
    HostAndPort hap4 = new HostAndPort("192.168.1.104", 10030);
    HostAndPort hap5 = new HostAndPort("192.168.1.105", 10030);
    HostAndPort hap6 = new HostAndPort("192.168.1.106", 10030);
    hostAndPorts.add(hap0);
    hostAndPorts.add(hap1);
    hostAndPorts.add(hap2);
    hostAndPorts.add(hap3);
    hostAndPorts.add(hap4);
    hostAndPorts.add(hap5);
    hostAndPorts.add(hap6);
    config.setHostAndPorts(hostAndPorts);
    HashRoutingLoadBalancer routingBalancer = new HashRoutingLoadBalancer(config);
    // 模拟失败
    routingBalancer.onConnectFailed(hap0);
    routingBalancer.onConnectFailed(hap1);
    routingBalancer.onConnectFailed(hap2);
    routingBalancer.onConnectFailed(hap3);
    routingBalancer.onConnectFailed(hap5);
    routingBalancer.onConnectFailed(hap4);
    routingBalancer.onConnectFailed(hap6);
    System.out.println(routingBalancer.isRetryNode(hap0));
    System.out.println(routingBalancer.determineCurrentLookupNode());
    // 模拟重连
    int crc16 = 23, nodes = 10;
    int mod = crc16 % nodes;
    int index = mod & (nodes - 1);
    System.out.println(index);
}
Also used : HostAndPort(io.transport.sdk.Configuration.HostAndPort) Configuration(io.transport.sdk.Configuration) ArrayList(java.util.ArrayList)

Aggregations

HostAndPort (io.transport.sdk.Configuration.HostAndPort)4 TransportException (io.transport.sdk.exception.TransportException)3 ChannelFuture (io.netty.channel.ChannelFuture)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Configuration (io.transport.sdk.Configuration)1