监听唤醒端口允许漂移,防止唤醒端口被其他程序占用时无法实现唤醒

This commit is contained in:
chuzhongzai 2025-08-31 14:11:28 +08:00
parent 3bdaddaead
commit 8b99608193
2 changed files with 42 additions and 19 deletions

View File

@ -5,6 +5,7 @@ import com.lion.lionwebsite.Domain.Gallery;
import com.lion.lionwebsite.Domain.GalleryTask; import com.lion.lionwebsite.Domain.GalleryTask;
import com.lion.lionwebsite.Message.*; import com.lion.lionwebsite.Message.*;
import com.lion.lionwebsite.Util.CustomUtil;
import io.netty.bootstrap.Bootstrap; import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*; import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup;
@ -18,6 +19,7 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream;
import java.net.*; import java.net.*;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
@ -72,18 +74,32 @@ public class RemoteService {
public boolean initChannel(){ public boolean initChannel(){
try { try {
channelFuture = new Bootstrap() int i;
.channel(NioSocketChannel.class) for(i=0; i<20; i++) {
.group(new NioEventLoopGroup()) try {
.handler(new ChannelInitializer<NioSocketChannel>() { channelFuture = new Bootstrap()
@Override .channel(NioSocketChannel.class)
protected void initChannel(NioSocketChannel channel){ .group(new NioEventLoopGroup())
channel.pipeline().addLast(new LengthFieldBasedFrameDecoder(100000000, 1, 4)); .handler(new ChannelInitializer<NioSocketChannel>() {
channel.pipeline().addLast(new MessageCodec()); @Override
channel.pipeline().addLast(new LoggingHandler()); protected void initChannel(NioSocketChannel channel) {
channel.pipeline().addLast(new MyChannelInboundHandlerAdapter()); channel.pipeline().addLast(new LengthFieldBasedFrameDecoder(100000000, 1, 4));
} channel.pipeline().addLast(new MessageCodec());
}).connect(new InetSocketAddress(ip, port)).sync(); 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"); log.info("connect success");
if(pushService != null) if(pushService != null)
pushService.storageNodeOnline(); pushService.storageNodeOnline();
@ -163,15 +179,22 @@ public class RemoteService {
} }
public void monitorFunc(){ public void monitorFunc(){
System.out.println("监听端口: " + (port + 1) + " 等待节点上线"); int real_port = CustomUtil._findIdlePort(port + 1);
try(ServerSocket socket = new ServerSocket(port + 1)) { log.info("监听端口: {}等待节点上线", real_port);
try(ServerSocket socket = new ServerSocket(real_port)) {
Socket client; Socket client;
while(true){ while(true){
client = socket.accept(); client = socket.accept();
client.close();
if(client.getInetAddress().getHostAddress().equals("5.255.110.45")){ 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(); initChannel();
client.close();
socket.close(); socket.close();
break; break;
} }

View File

@ -80,11 +80,11 @@ public class CustomUtil {
* *
* @return 可用端口的起始位置 -1为没有几乎没有可能 * @return 可用端口的起始位置 -1为没有几乎没有可能
*/ */
public static short _findIdlePort(){ public static int _findIdlePort(int port) {
for(int i=20000; i<65535; i++){ for(int i=port; i<65535; i++){
try(ServerSocket ignored = new ServerSocket(i)){ try(ServerSocket ignored = new ServerSocket(i)){
ignored.close(); ignored.close();
return (short) i; return i;
}catch (IOException ignored) { }catch (IOException ignored) {
} }
} }