diff --git a/src/main/java/com/lion/lionwebsite/Service/RemoteService.java b/src/main/java/com/lion/lionwebsite/Service/RemoteService.java index 30ddaa5..7817ce3 100644 --- a/src/main/java/com/lion/lionwebsite/Service/RemoteService.java +++ b/src/main/java/com/lion/lionwebsite/Service/RemoteService.java @@ -5,6 +5,7 @@ import com.lion.lionwebsite.Domain.Gallery; import com.lion.lionwebsite.Domain.GalleryTask; import com.lion.lionwebsite.Message.*; +import com.lion.lionwebsite.Util.CustomUtil; import io.netty.bootstrap.Bootstrap; import io.netty.channel.*; import io.netty.channel.nio.NioEventLoopGroup; @@ -18,6 +19,7 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import java.io.IOException; +import java.io.OutputStream; import java.net.*; import java.util.Arrays; import java.util.HashMap; @@ -72,18 +74,32 @@ public class RemoteService { public boolean initChannel(){ try { - channelFuture = new Bootstrap() - .channel(NioSocketChannel.class) - .group(new NioEventLoopGroup()) - .handler(new ChannelInitializer() { - @Override - protected void initChannel(NioSocketChannel channel){ - channel.pipeline().addLast(new LengthFieldBasedFrameDecoder(100000000, 1, 4)); - channel.pipeline().addLast(new MessageCodec()); - channel.pipeline().addLast(new LoggingHandler()); - channel.pipeline().addLast(new MyChannelInboundHandlerAdapter()); - } - }).connect(new InetSocketAddress(ip, port)).sync(); + int i; + for(i=0; i<20; i++) { + try { + channelFuture = new Bootstrap() + .channel(NioSocketChannel.class) + .group(new NioEventLoopGroup()) + .handler(new ChannelInitializer() { + @Override + protected void initChannel(NioSocketChannel channel) { + channel.pipeline().addLast(new LengthFieldBasedFrameDecoder(100000000, 1, 4)); + channel.pipeline().addLast(new MessageCodec()); + channel.pipeline().addLast(new LoggingHandler()); + channel.pipeline().addLast(new MyChannelInboundHandlerAdapter()); + } + }).connect(new InetSocketAddress(ip, port + i)).sync(); + break; + }catch (Exception e){ + log.error("连接storageNode失败,端口偏移量(重试次数):{}", i); + } + } + + //超过二十次连不上,主动抛出错误,由下方catch + if(i==20) { + throw new Exception(); + } + log.info("connect success"); if(pushService != null) pushService.storageNodeOnline(); @@ -163,15 +179,22 @@ public class RemoteService { } public void monitorFunc(){ - System.out.println("监听端口: " + (port + 1) + " 等待节点上线"); - try(ServerSocket socket = new ServerSocket(port + 1)) { + int real_port = CustomUtil._findIdlePort(port + 1); + log.info("监听端口: {}等待节点上线", real_port); + try(ServerSocket socket = new ServerSocket(real_port)) { Socket client; while(true){ client = socket.accept(); - client.close(); + if(client.getInetAddress().getHostAddress().equals("5.255.110.45")){ - System.out.println("尝试连接"); + //连接之后发送lionwebsite,否则存储节点不能确认这个端口是否有效 + OutputStream outputStream = client.getOutputStream(); + outputStream.write("lionwebsite".getBytes()); + outputStream.flush(); + outputStream.close(); + log.info("尝试连接"); initChannel(); + client.close(); socket.close(); break; } diff --git a/src/main/java/com/lion/lionwebsite/Util/CustomUtil.java b/src/main/java/com/lion/lionwebsite/Util/CustomUtil.java index ba77ad4..b6124c9 100644 --- a/src/main/java/com/lion/lionwebsite/Util/CustomUtil.java +++ b/src/main/java/com/lion/lionwebsite/Util/CustomUtil.java @@ -80,11 +80,11 @@ public class CustomUtil { * * @return 可用端口的起始位置 -1为没有(几乎没有可能) */ - public static short _findIdlePort(){ - for(int i=20000; i<65535; i++){ + public static int _findIdlePort(int port) { + for(int i=port; i<65535; i++){ try(ServerSocket ignored = new ServerSocket(i)){ ignored.close(); - return (short) i; + return i; }catch (IOException ignored) { } }