From 8b99608193ceadbaafd395df4dd5dd750e727316 Mon Sep 17 00:00:00 2001 From: chuzhongzai Date: Sun, 31 Aug 2025 14:11:28 +0800 Subject: [PATCH] =?UTF-8?q?=E7=9B=91=E5=90=AC=E5=94=A4=E9=86=92=E7=AB=AF?= =?UTF-8?q?=E5=8F=A3=E5=85=81=E8=AE=B8=E6=BC=82=E7=A7=BB=EF=BC=8C=E9=98=B2?= =?UTF-8?q?=E6=AD=A2=E5=94=A4=E9=86=92=E7=AB=AF=E5=8F=A3=E8=A2=AB=E5=85=B6?= =?UTF-8?q?=E4=BB=96=E7=A8=8B=E5=BA=8F=E5=8D=A0=E7=94=A8=E6=97=B6=E6=97=A0?= =?UTF-8?q?=E6=B3=95=E5=AE=9E=E7=8E=B0=E5=94=A4=E9=86=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lionwebsite/Service/RemoteService.java | 55 +++++++++++++------ .../com/lion/lionwebsite/Util/CustomUtil.java | 6 +- 2 files changed, 42 insertions(+), 19 deletions(-) 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) { } }