【fix】 优化读写缓冲区设置为度缓冲区2m 写1m 低水位1m 高水位2m

This commit is contained in:
wujiawei 2024-09-09 15:51:01 +08:00
parent 4ec1401712
commit 10d2d74ca3
18 changed files with 208 additions and 63 deletions

View File

@ -1,6 +1,5 @@
package org.framework.lazy.cloud.network.heartbeat.client.netty.filter; package org.framework.lazy.cloud.network.heartbeat.client.netty.filter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline; import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.string.StringDecoder; import io.netty.handler.codec.string.StringDecoder;
@ -11,8 +10,9 @@ import org.framework.lazy.cloud.network.heartbeat.client.netty.socket.NettyClien
import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelTypeAdapter; import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelTypeAdapter;
import org.framework.lazy.cloud.network.heartbeat.common.decoder.NettyProxyMsgDecoder; import org.framework.lazy.cloud.network.heartbeat.common.decoder.NettyProxyMsgDecoder;
import org.framework.lazy.cloud.network.heartbeat.common.encoder.NettyProxyMsgEncoder; import org.framework.lazy.cloud.network.heartbeat.common.encoder.NettyProxyMsgEncoder;
import org.framework.lazy.cloud.network.heartbeat.common.filter.DebugChannelInitializer;
public class NettyClientFilter extends ChannelInitializer<SocketChannel> { public class NettyClientFilter extends DebugChannelInitializer<SocketChannel> {
private final ChannelTypeAdapter channelTypeAdapter; private final ChannelTypeAdapter channelTypeAdapter;
@ -24,7 +24,7 @@ public class NettyClientFilter extends ChannelInitializer<SocketChannel> {
} }
@Override @Override
protected void initChannel(SocketChannel ch) throws Exception { protected void initChannel0(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline(); ChannelPipeline pipeline = ch.pipeline();
/* * 解码和编码,应和服务端一致 * */ /* * 解码和编码,应和服务端一致 * */

View File

@ -9,8 +9,9 @@ import io.netty.handler.codec.compression.JdkZlibEncoder;
import org.framework.lazy.cloud.network.heartbeat.client.netty.handler.NettyClientRealHandler; import org.framework.lazy.cloud.network.heartbeat.client.netty.handler.NettyClientRealHandler;
import org.framework.lazy.cloud.network.heartbeat.common.decoder.TransferDecoder; import org.framework.lazy.cloud.network.heartbeat.common.decoder.TransferDecoder;
import org.framework.lazy.cloud.network.heartbeat.common.encoder.TransferEncoder; import org.framework.lazy.cloud.network.heartbeat.common.encoder.TransferEncoder;
import org.framework.lazy.cloud.network.heartbeat.common.filter.DebugChannelInitializer;
public class NettyClientRealFilter extends ChannelInitializer<SocketChannel> { public class NettyClientRealFilter extends DebugChannelInitializer<SocketChannel> {
/** /**
* This method will be called once the {@link Channel} was registered. After the method returns this instance * This method will be called once the {@link Channel} was registered. After the method returns this instance
* will be removed from the {@link ChannelPipeline} of the {@link Channel}. * will be removed from the {@link ChannelPipeline} of the {@link Channel}.
@ -18,7 +19,7 @@ public class NettyClientRealFilter extends ChannelInitializer<SocketChannel> {
* @param ch the {@link Channel} which was registered. * @param ch the {@link Channel} which was registered.
*/ */
@Override @Override
protected void initChannel(SocketChannel ch) { protected void initChannel0(SocketChannel ch) {
ChannelPipeline pipeline = ch.pipeline(); ChannelPipeline pipeline = ch.pipeline();
// 解码编码 // 解码编码
pipeline.addLast(new TransferDecoder(Integer.MAX_VALUE, 1024 * 1024*10)); pipeline.addLast(new TransferDecoder(Integer.MAX_VALUE, 1024 * 1024*10));

View File

@ -9,11 +9,12 @@ import org.framework.lazy.cloud.network.heartbeat.client.netty.handler.NettyClie
import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelTypeAdapter; import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelTypeAdapter;
import org.framework.lazy.cloud.network.heartbeat.common.decoder.NettyProxyMsgDecoder; import org.framework.lazy.cloud.network.heartbeat.common.decoder.NettyProxyMsgDecoder;
import org.framework.lazy.cloud.network.heartbeat.common.encoder.NettyProxyMsgEncoder; import org.framework.lazy.cloud.network.heartbeat.common.encoder.NettyProxyMsgEncoder;
import org.framework.lazy.cloud.network.heartbeat.common.filter.DebugChannelInitializer;
/** /**
* netty 客户端连接真实服服务端访客拦截器 * netty 客户端连接真实服服务端访客拦截器
*/ */
public class NettyClientVisitorRealFilter extends ChannelInitializer<SocketChannel> { public class NettyClientVisitorRealFilter extends DebugChannelInitializer<SocketChannel> {
private final ChannelTypeAdapter channelTypeAdapter; private final ChannelTypeAdapter channelTypeAdapter;
public NettyClientVisitorRealFilter(ChannelTypeAdapter channelTypeAdapter) { public NettyClientVisitorRealFilter(ChannelTypeAdapter channelTypeAdapter) {
@ -30,7 +31,7 @@ public class NettyClientVisitorRealFilter extends ChannelInitializer<SocketChann
* the {@link Channel}. * the {@link Channel}.
*/ */
@Override @Override
protected void initChannel(SocketChannel ch) throws Exception { protected void initChannel0(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline(); ChannelPipeline pipeline = ch.pipeline();
// // 解码编码 // // 解码编码
// pipeline.addLast(new NettyProxyMsgDecoder(Integer.MAX_VALUE, 0, 4, -4, 0)); // pipeline.addLast(new NettyProxyMsgDecoder(Integer.MAX_VALUE, 0, 4, -4, 0));

View File

@ -4,6 +4,7 @@ package org.framework.lazy.cloud.network.heartbeat.client.netty.handler;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel; import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOption;
import io.netty.channel.SimpleChannelInboundHandler; import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.common.MessageType; import org.framework.lazy.cloud.network.heartbeat.common.MessageType;
@ -11,6 +12,7 @@ import org.framework.lazy.cloud.network.heartbeat.common.NettyByteBuf;
import org.framework.lazy.cloud.network.heartbeat.common.NettyCommunicationIdContext; import org.framework.lazy.cloud.network.heartbeat.common.NettyCommunicationIdContext;
import org.framework.lazy.cloud.network.heartbeat.common.NettyProxyMsg; import org.framework.lazy.cloud.network.heartbeat.common.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils; import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
import org.wu.framework.core.utils.ObjectUtils;
/** /**
* 来自客户端 真实服务器返回的数据请求 * 来自客户端 真实服务器返回的数据请求
@ -65,6 +67,7 @@ public class NettyClientRealHandler extends SimpleChannelInboundHandler<NettyByt
@Override @Override
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception { public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
// String vid = ctx.channel().attr(Constant.VID).get(); // String vid = ctx.channel().attr(Constant.VID).get();
// if (StringUtil.isNullOrEmpty(vid)) { // if (StringUtil.isNullOrEmpty(vid)) {
// super.channelWritabilityChanged(ctx); // super.channelWritabilityChanged(ctx);
@ -75,7 +78,18 @@ public class NettyClientRealHandler extends SimpleChannelInboundHandler<NettyByt
// proxyChannel.config().setOption(ChannelOption.AUTO_READ, ctx.channel().isWritable()); // proxyChannel.config().setOption(ChannelOption.AUTO_READ, ctx.channel().isWritable());
// } // }
super.channelWritabilityChanged(ctx); // 获取访客的传输通道
String visitorId = ChannelAttributeKeyUtils.getVisitorId(ctx.channel());
if(ObjectUtils.isEmpty(visitorId)) {
super.channelWritabilityChanged(ctx);
return;
}
Channel visitorCommunicationChannel = NettyCommunicationIdContext.getVisitor(visitorId);
if (visitorCommunicationChannel != null) {
log.debug("visitorId:{} transfer AUTO_READ:{} ",visitorId,ctx.channel().isWritable());
visitorCommunicationChannel.config().setOption(ChannelOption.AUTO_READ, ctx.channel().isWritable());
}
} }
@Override @Override

View File

@ -3,13 +3,13 @@ package org.framework.lazy.cloud.network.heartbeat.client.netty.handler;
import io.netty.channel.Channel; import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOption;
import io.netty.channel.SimpleChannelInboundHandler; import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.framework.lazy.cloud.network.heartbeat.common.ChannelContext; import org.framework.lazy.cloud.network.heartbeat.common.*;
import org.framework.lazy.cloud.network.heartbeat.common.MessageType;
import org.framework.lazy.cloud.network.heartbeat.common.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelTypeAdapter; import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelTypeAdapter;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils; import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
import org.wu.framework.core.utils.ObjectUtils;
/** /**
* 客户端访客通信通道 处理器 * 客户端访客通信通道 处理器
@ -55,8 +55,18 @@ public class NettyClientVisitorRealHandler extends SimpleChannelInboundHandler<N
@Override @Override
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception { public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
// 处理客户端本地真实通道问题
String visitorId = ChannelAttributeKeyUtils.getVisitorId(ctx.channel());
if(ObjectUtils.isEmpty(visitorId)) {
super.channelWritabilityChanged(ctx);
return;
}
super.channelWritabilityChanged(ctx); Channel realChannel = NettyRealIdContext.getReal(visitorId);
if (realChannel != null) {
log.debug("visitorId:{} transfer AUTO_READ:{} ",visitorId,ctx.channel().isWritable());
realChannel.config().setOption(ChannelOption.AUTO_READ, ctx.channel().isWritable());
}
} }
@Override @Override

View File

@ -53,14 +53,15 @@ public class NettyClientRealSocket {
String visitorId = internalNetworkPenetrationRealClient.getVisitorId(); String visitorId = internalNetworkPenetrationRealClient.getVisitorId();
Bootstrap bootstrap = new Bootstrap(); Bootstrap bootstrap = new Bootstrap();
bootstrap.group(eventLoopGroup).channel(NioSocketChannel.class) bootstrap.group(eventLoopGroup).channel(NioSocketChannel.class)
// 设置读缓冲区为2M // 设置读缓冲区为2M
// .option(ChannelOption.SO_RCVBUF, 2048 * 1024) .option(ChannelOption.SO_RCVBUF, 2048 * 1024)
// 设置写缓冲区为1M // 设置写缓冲区为1M
// .option(ChannelOption.SO_SNDBUF, 1024 * 1024) .option(ChannelOption.SO_SNDBUF, 1024 * 1024)
// .option(ChannelOption.TCP_NODELAY, false) // .option(ChannelOption.TCP_NODELAY, false)
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000 * 60)//连接超时时间设置为 60 .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000 * 60)//连接超时时间设置为 60
// .option(ChannelOption.SO_BACKLOG, 128)//务端接受连接的队列长度 默认128 // .option(ChannelOption.SO_BACKLOG, 128)//务端接受连接的队列长度 默认128
// .option(ChannelOption.RCVBUF_ALLOCATOR, new NettyRecvByteBufAllocator(1024 * 1024))//用于Channel分配接受Buffer的分配器 默认AdaptiveRecvByteBufAllocator.DEFAULT // .option(ChannelOption.RCVBUF_ALLOCATOR, new NettyRecvByteBufAllocator(1024 * 1024))//用于Channel分配接受Buffer的分配器 默认AdaptiveRecvByteBufAllocator.DEFAULT
.option(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(1024 * 1024, 1024 * 1024 * 2))
.handler(new NettyClientRealFilter()) .handler(new NettyClientRealFilter())
; ;
@ -136,12 +137,16 @@ public class NettyClientRealSocket {
bootstrap.group(eventLoopGroup) bootstrap.group(eventLoopGroup)
.channel(NioSocketChannel.class) .channel(NioSocketChannel.class)
.option(ChannelOption.SO_KEEPALIVE, true) .option(ChannelOption.SO_KEEPALIVE, true)
// 设置读缓冲区为2M
.option(ChannelOption.SO_RCVBUF, 2048 * 1024)
// 设置写缓冲区为1M
.option(ChannelOption.SO_SNDBUF, 1024 * 1024)
// .option(ChannelOption.TCP_NODELAY, false) // .option(ChannelOption.TCP_NODELAY, false)
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000 * 60)//连接超时时间设置为 60 .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000 * 60)//连接超时时间设置为 60
// .option(ChannelOption.SO_BACKLOG, 256)//务端接受连接的队列长度 默认128 // .option(ChannelOption.SO_BACKLOG, 256)//务端接受连接的队列长度 默认128
// .option(ChannelOption.RCVBUF_ALLOCATOR, new NettyRecvByteBufAllocator(1024 * 1024))//用于Channel分配接受Buffer的分配器 默认AdaptiveRecvByteBufAllocator.DEFAULT // .option(ChannelOption.RCVBUF_ALLOCATOR, new NettyRecvByteBufAllocator(1024 * 1024))//用于Channel分配接受Buffer的分配器 默认AdaptiveRecvByteBufAllocator.DEFAULT
.option(ChannelOption.WRITE_BUFFER_WATER_MARK, WriteBufferWaterMark.DEFAULT) .option(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(1024 * 1024, 1024 * 1024 * 2))
.handler(new NettyClientVisitorRealFilter(new ChannelTypeAdapter(handleChannelTypeAdvancedList))) .handler(new NettyClientVisitorRealFilter(new ChannelTypeAdapter(handleChannelTypeAdvancedList)))
; ;
@ -186,6 +191,9 @@ public class NettyClientRealSocket {
Channel visitor = NettyRealIdContext.getReal(visitorId); Channel visitor = NettyRealIdContext.getReal(visitorId);
visitor.config().setOption(ChannelOption.AUTO_READ, true); visitor.config().setOption(ChannelOption.AUTO_READ, true);
ChannelAttributeKeyUtils.buildNextChannel(visitor, channel);
ChannelAttributeKeyUtils.buildNextChannel(channel, visitor);

View File

@ -2,10 +2,7 @@ package org.framework.lazy.cloud.network.heartbeat.client.netty.socket;
import io.netty.bootstrap.Bootstrap; import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel; import io.netty.channel.*;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.channel.socket.nio.NioSocketChannel;
import lombok.Getter; import lombok.Getter;
@ -70,6 +67,15 @@ public class NettyClientSocket {
Bootstrap bootstrap = new Bootstrap(); Bootstrap bootstrap = new Bootstrap();
bootstrap.group(eventLoopGroup) bootstrap.group(eventLoopGroup)
.channel(NioSocketChannel.class) .channel(NioSocketChannel.class)
.option(ChannelOption.SO_RCVBUF, 2048 * 1024)
// 设置写缓冲区为1M
.option(ChannelOption.SO_SNDBUF, 1024 * 1024)
.option(ChannelOption.SO_KEEPALIVE, true)
// .childOption(ChannelOption.TCP_NODELAY, false)
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000 * 60)//连接超时时间设置为 60
// .childOption(ChannelOption.RCVBUF_ALLOCATOR, new NettyRecvByteBufAllocator(1024 * 1024))//用于Channel分配接受Buffer的分配器 默认AdaptiveRecvByteBufAllocator.DEFAULT
.option(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(1024 * 1024, 1024 * 1024 * 2))
.handler(new NettyClientFilter(new ChannelTypeAdapter(handleChannelTypeAdvancedList), this)) .handler(new NettyClientFilter(new ChannelTypeAdapter(handleChannelTypeAdvancedList), this))
; ;

View File

@ -0,0 +1,46 @@
package org.framework.lazy.cloud.network.heartbeat.common.filter;
import io.netty.channel.*;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public abstract class DebugChannelInitializer<C extends Channel> extends ChannelInitializer<C> {
protected abstract void initChannel0(C ch) throws Exception;
/**
* This method will be called once the {@link Channel} was registered. After the method returns this instance
* will be removed from the {@link ChannelPipeline} of the {@link Channel}.
*
* @param ch the {@link Channel} which was registered.
* @throws Exception is thrown if an error occurs. In that case it will be handled by
* {@link #exceptionCaught(ChannelHandlerContext, Throwable)} which will by default close
* the {@link Channel}.
*/
@Override
protected void initChannel(C ch) throws Exception {
initChannel0(ch);
ChannelConfig config = ch.config();
Class<?> filterClass = this.getClass();
// 获取默认配置
Integer defaultRcvBufSize = config.getOption(ChannelOption.SO_RCVBUF);//读缓冲区为
log.debug("filter:{},defaultRcvBufSize:{}", filterClass, defaultRcvBufSize);
Integer defaultSndBufSize = config.getOption(ChannelOption.SO_SNDBUF);//写缓冲区
log.debug("filter:{},defaultSndBufSize:{}", filterClass, defaultSndBufSize);
Integer defaultConnectTimeoutMillis = config.getOption(ChannelOption.CONNECT_TIMEOUT_MILLIS);// 连接时常
log.debug("filter:{},defaultConnectTimeoutMillis:{}", filterClass, defaultConnectTimeoutMillis);
RecvByteBufAllocator recvByteBufAllocator = config.getOption(ChannelOption.RCVBUF_ALLOCATOR);// 获取读缓冲区大小
log.debug("filter:{},recvByteBufAllocator:{}", filterClass, recvByteBufAllocator);
int writeBufferLowWaterMark = config.getWriteBufferLowWaterMark();
int writeBufferHighWaterMark = config.getWriteBufferHighWaterMark();
log.debug("filter:{}, high water mark: {}", filterClass,writeBufferHighWaterMark);
log.debug("filter:{}, low water mark: {}", filterClass,writeBufferLowWaterMark);
}
}

View File

@ -14,6 +14,8 @@ public class ChannelAttributeKeyUtils {
private static final AttributeKey<Integer> OUT_FLOW = AttributeKey.newInstance("outFlow"); private static final AttributeKey<Integer> OUT_FLOW = AttributeKey.newInstance("outFlow");
private static final AttributeKey<Integer> IN_FLOW = AttributeKey.newInstance("inFlow"); private static final AttributeKey<Integer> IN_FLOW = AttributeKey.newInstance("inFlow");
private static final AttributeKey<Channel> NEXT_CHANNEL = AttributeKey.newInstance("nextChannel");
/** /**
* 为通道绑定 访客属性 * 为通道绑定 访客属性
@ -131,4 +133,25 @@ public class ChannelAttributeKeyUtils {
public static Integer getVisitorPort(Channel channel) { public static Integer getVisitorPort(Channel channel) {
return channel.attr(VISITOR_PORT).get(); return channel.attr(VISITOR_PORT).get();
} }
/**
* 为通道绑定 下一个通道
*
* @param channel 通道
* @param nextChannel 下一个通道
*/
public static void buildNextChannel(Channel channel, Channel nextChannel) {
channel.attr(NEXT_CHANNEL).set(nextChannel);
}
/**
* 获取 通道中下一个通道
*
* @param channel 通道
*/
public static Channel getNextChannel(Channel channel) {
return channel.attr(NEXT_CHANNEL).get();
}
} }

View File

@ -3,14 +3,17 @@ package org.framework.lazy.cloud.network.heartbeat.server.netty.advanced;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel; import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.framework.lazy.cloud.network.heartbeat.common.NettyProxyMsg; import org.framework.lazy.cloud.network.heartbeat.common.NettyProxyMsg;
import org.framework.lazy.cloud.network.heartbeat.common.NettyRealIdContext; import org.framework.lazy.cloud.network.heartbeat.common.NettyRealIdContext;
import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelFlowAdapter; import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelFlowAdapter;
import org.framework.lazy.cloud.network.heartbeat.common.advanced.server.AbstractHandleReportHandleChannelTransferTypeAdvanced; import org.framework.lazy.cloud.network.heartbeat.common.advanced.server.AbstractHandleReportHandleChannelTransferTypeAdvanced;
import org.framework.lazy.cloud.network.heartbeat.common.enums.ChannelFlowEnum; import org.framework.lazy.cloud.network.heartbeat.common.enums.ChannelFlowEnum;
import org.framework.lazy.cloud.network.heartbeat.server.netty.flow.ServerChannelFlow; import org.framework.lazy.cloud.network.heartbeat.server.netty.flow.ServerChannelFlow;
import org.springframework.stereotype.Component;
import java.nio.charset.StandardCharsets;
/** /**
@ -37,15 +40,17 @@ public class ServerHandleReportHandleChannelTransferTypeAdvanced extends Abstrac
public void doHandler(Channel channel, NettyProxyMsg msg) { public void doHandler(Channel channel, NettyProxyMsg msg) {
String clientId = new String(msg.getClientId()); String clientId = new String(msg.getClientId());
Integer visitorPort = Integer.valueOf(new String(msg.getVisitorPort())); Integer visitorPort = Integer.valueOf(new String(msg.getVisitorPort()));
log.info("访客端口:[{}] 接收到客户端:[{}]",visitorPort, clientId);
log.debug("接收到客户端:[{}]内网穿透返回的数据:[{}]", clientId, new String(msg.getData()));
// 将数据转发访客通道
byte[] visitorId = msg.getVisitorId(); byte[] visitorId = msg.getVisitorId();
// log.info("访客ID:【{}】 访客端口:[{}] 接收到客户端:[{}]", new String(visitorId), visitorPort, clientId);
// log.debug("访客ID:【{}】接收到客户端:[{}] 返回数据大小:[{}] 内网穿透返回的数据:[{}]", new String(visitorId), clientId, msg.getData().length, new String(msg.getData()));
// 将数据转发访客通道
Channel visitor = NettyRealIdContext.getReal(visitorId); Channel visitor = NettyRealIdContext.getReal(visitorId);
if (visitor != null) { if (visitor != null) {
ByteBuf buf = visitor.config().getAllocator().buffer(msg.getData().length); ByteBuf buf = visitor.config().getAllocator().buffer(msg.getData().length);
buf.writeBytes(msg.getData()); buf.writeBytes(msg.getData());
visitor.writeAndFlush(buf); ChannelFuture channelFuture = visitor.writeAndFlush(buf);
boolean success = channelFuture.isSuccess();
log.debug("visitor writerAndFlush status: {}", success);
// 记录出口数据 // 记录出口数据
ServerChannelFlow serverChannelFlow = ServerChannelFlow ServerChannelFlow serverChannelFlow = ServerChannelFlow
.builder() .builder()
@ -56,7 +61,7 @@ public class ServerHandleReportHandleChannelTransferTypeAdvanced extends Abstrac
.build(); .build();
channelFlowAdapter.asyncHandler(channel, serverChannelFlow); channelFlowAdapter.asyncHandler(channel, serverChannelFlow);
} }
log.debug("访客ID:【{}】接收到客户端:[{}] 发送真实数据成功", new String(visitorId), clientId);
} }
} }

View File

@ -10,6 +10,7 @@ import io.netty.handler.codec.protobuf.ProtobufEncoder;
import io.netty.handler.codec.string.StringDecoder; import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder; import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.timeout.IdleStateHandler; import io.netty.handler.timeout.IdleStateHandler;
import org.framework.lazy.cloud.network.heartbeat.common.filter.DebugChannelInitializer;
import org.framework.lazy.cloud.network.heartbeat.server.netty.handler.NettyServerHandler; import org.framework.lazy.cloud.network.heartbeat.server.netty.handler.NettyServerHandler;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelTypeAdapter; import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelTypeAdapter;
@ -27,7 +28,7 @@ import java.util.concurrent.TimeUnit;
* @date 2023/09/13 10:26 * @date 2023/09/13 10:26
*/ */
@Component @Component
public class NettyServerFilter extends ChannelInitializer<SocketChannel> { public class NettyServerFilter extends DebugChannelInitializer<SocketChannel> {
private final List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList; private final List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList;
public NettyServerFilter(List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList) { public NettyServerFilter(List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList) {
@ -35,7 +36,7 @@ public class NettyServerFilter extends ChannelInitializer<SocketChannel> {
} }
@Override @Override
protected void initChannel(SocketChannel ch) throws Exception { protected void initChannel0(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline(); ChannelPipeline pipeline = ch.pipeline();
// ("\n")为结尾分割的 解码器 // ("\n")为结尾分割的 解码器
// 解码编码 // 解码编码

View File

@ -1,12 +1,16 @@
package org.framework.lazy.cloud.network.heartbeat.server.netty.filter; package org.framework.lazy.cloud.network.heartbeat.server.netty.filter;
import io.netty.channel.*; import io.netty.channel.Channel;
import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.SocketChannel;
import org.framework.lazy.cloud.network.heartbeat.common.InternalNetworkPenetrationRealClient; import org.framework.lazy.cloud.network.heartbeat.common.InternalNetworkPenetrationRealClient;
import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelFlowAdapter; import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelFlowAdapter;
import org.framework.lazy.cloud.network.heartbeat.common.filter.DebugChannelInitializer;
import org.framework.lazy.cloud.network.heartbeat.server.netty.handler.VisitorHandler; import org.framework.lazy.cloud.network.heartbeat.server.netty.handler.VisitorHandler;
public class VisitorFilter extends ChannelInitializer<SocketChannel> { public class VisitorFilter extends DebugChannelInitializer<SocketChannel> {
private final InternalNetworkPenetrationRealClient internalNetworkPenetrationRealClient; private final InternalNetworkPenetrationRealClient internalNetworkPenetrationRealClient;
private final ChannelFlowAdapter channelFlowAdapter; private final ChannelFlowAdapter channelFlowAdapter;
@ -25,10 +29,11 @@ public class VisitorFilter extends ChannelInitializer<SocketChannel> {
* the {@link Channel}. * the {@link Channel}.
*/ */
@Override @Override
protected void initChannel(SocketChannel ch) throws Exception { protected void initChannel0(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline(); ChannelPipeline pipeline = ch.pipeline();
ch.config().setReceiveBufferSize(1024 * 1024);
ch.config().setSendBufferSize(1024 * 1024);
pipeline.addLast(new ChannelDuplexHandler()); pipeline.addLast(new ChannelDuplexHandler());
pipeline.addLast(new VisitorHandler(internalNetworkPenetrationRealClient, channelFlowAdapter)); pipeline.addLast(new VisitorHandler(internalNetworkPenetrationRealClient, channelFlowAdapter));
} }
} }

View File

@ -130,4 +130,19 @@ public class NettyServerHandler extends SimpleChannelInboundHandler<NettyProxyMs
// //
if (channel.isActive()) ctx.close(); if (channel.isActive()) ctx.close();
} }
/**
* Calls {@link ChannelHandlerContext#fireChannelWritabilityChanged()} to forward
* to the next {@link ChannelInboundHandler} in the {@link ChannelPipeline}.
* <p>
* Sub-classes may override this method to change behavior.
*
* @param ctx
*/
@Override
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
log.info("netty server handler channel writability changed: {}", ctx.channel());
super.channelWritabilityChanged(ctx);
}
} }

View File

@ -13,6 +13,7 @@ import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelFlowAdap
import org.framework.lazy.cloud.network.heartbeat.common.enums.ChannelFlowEnum; import org.framework.lazy.cloud.network.heartbeat.common.enums.ChannelFlowEnum;
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils; import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
import org.framework.lazy.cloud.network.heartbeat.server.netty.flow.ServerChannelFlow; import org.framework.lazy.cloud.network.heartbeat.server.netty.flow.ServerChannelFlow;
import org.wu.framework.core.utils.ObjectUtils;
import java.util.UUID; import java.util.UUID;
@ -149,25 +150,36 @@ public class VisitorHandler extends SimpleChannelInboundHandler<ByteBuf> {
@Override @Override
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception { public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
// Channel visitorChannel = ctx.channel(); // 获取访客的传输通道
// String vid = visitorChannel.attr(Constant.VID).get(); String visitorId = ChannelAttributeKeyUtils.getVisitorId(ctx.channel());
// if (StringUtil.isNullOrEmpty(vid)) { if(ObjectUtils.isEmpty(visitorId)) {
// super.channelWritabilityChanged(ctx); super.channelWritabilityChanged(ctx);
// return; return;
// } }
// Channel clientChannel = Constant.vcc.get(vid);
// if (clientChannel != null) { Channel visitorCommunicationChannel = NettyCommunicationIdContext.getVisitor(visitorId);
// clientChannel.config().setOption(ChannelOption.AUTO_READ, visitorChannel.isWritable()); if (visitorCommunicationChannel != null) {
// } log.debug("visitorId:{} transfer AUTO_READ:{} ",visitorId,ctx.channel().isWritable());
visitorCommunicationChannel.config().setOption(ChannelOption.AUTO_READ, ctx.channel().isWritable());
}
// Channel visitorChannel = ctx.channel();
// String vid = visitorChannel.attr(Constant.VID).get();
// if (StringUtil.isNullOrEmpty(vid)) {
// super.channelWritabilityChanged(ctx);
// return;
// }
// Channel clientChannel = Constant.vcc.get(vid);
// if (clientChannel != null) {
// clientChannel.config().setOption(ChannelOption.AUTO_READ, visitorChannel.isWritable());
// }
if (ctx.channel().isWritable()) { if (ctx.channel().isWritable()) {
System.out.println("Channel is writable again"); log.debug("Channel is writable again");
// 恢复之前暂停的操作如写入数据 // 恢复之前暂停的操作如写入数据
} else { } else {
System.out.println("Channel is not writable"); log.debug("Channel is not writable");
// 暂停写入操作等待可写状态 // 暂停写入操作等待可写状态
} }
log.info("channelWritabilityChanged"); log.info("visitorId:{} channelWritabilityChanged!",visitorId);
super.channelWritabilityChanged(ctx);
} }
@Override @Override

View File

@ -2,10 +2,7 @@ package org.framework.lazy.cloud.network.heartbeat.server.netty.socket;
import io.netty.bootstrap.ServerBootstrap; import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture; import io.netty.channel.*;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel;
import org.framework.lazy.cloud.network.heartbeat.server.netty.filter.NettyServerFilter; import org.framework.lazy.cloud.network.heartbeat.server.netty.filter.NettyServerFilter;
@ -31,8 +28,6 @@ public class NettyOnCloudNettyServerSocket {
ServerBootstrap b = new ServerBootstrap(); ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
// 给服务端channel设置属性 // 给服务端channel设置属性
.childOption(ChannelOption.SO_KEEPALIVE, true)
// 设置读缓冲区为2M // 设置读缓冲区为2M
.childOption(ChannelOption.SO_RCVBUF, 2048 * 1024) .childOption(ChannelOption.SO_RCVBUF, 2048 * 1024)
// 设置写缓冲区为1M // 设置写缓冲区为1M
@ -42,7 +37,7 @@ public class NettyOnCloudNettyServerSocket {
// .childOption(ChannelOption.TCP_NODELAY, false) // .childOption(ChannelOption.TCP_NODELAY, false)
.childOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000 * 60)//连接超时时间设置为 60 .childOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000 * 60)//连接超时时间设置为 60
// .childOption(ChannelOption.RCVBUF_ALLOCATOR, new NettyRecvByteBufAllocator(1024 * 1024))//用于Channel分配接受Buffer的分配器 默认AdaptiveRecvByteBufAllocator.DEFAULT // .childOption(ChannelOption.RCVBUF_ALLOCATOR, new NettyRecvByteBufAllocator(1024 * 1024))//用于Channel分配接受Buffer的分配器 默认AdaptiveRecvByteBufAllocator.DEFAULT
.childOption(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(1024 * 1024, 1024 * 1024 * 2))
.childHandler(nettyServerFilter); .childHandler(nettyServerFilter);
channelFuture = b.bind(serverPort).sync(); channelFuture = b.bind(serverPort).sync();

View File

@ -10,6 +10,7 @@ import org.framework.lazy.cloud.network.heartbeat.common.InternalNetworkPenetrat
import org.framework.lazy.cloud.network.heartbeat.common.NettyClientVisitorContext; import org.framework.lazy.cloud.network.heartbeat.common.NettyClientVisitorContext;
import org.framework.lazy.cloud.network.heartbeat.common.NettyVisitorPortContext; import org.framework.lazy.cloud.network.heartbeat.common.NettyVisitorPortContext;
import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelFlowAdapter; import org.framework.lazy.cloud.network.heartbeat.common.adapter.ChannelFlowAdapter;
import org.framework.lazy.cloud.network.heartbeat.common.allocator.NettyRecvByteBufAllocator;
import org.framework.lazy.cloud.network.heartbeat.server.netty.filter.VisitorFilter; import org.framework.lazy.cloud.network.heartbeat.server.netty.filter.VisitorFilter;
import java.io.IOException; import java.io.IOException;
@ -41,12 +42,12 @@ public class NettyVisitorSocket {
* *
* @throws Exception * @throws Exception
*/ */
public void startServer() throws Exception { public void startVisitorServer() throws Exception {
Channel visitor = NettyVisitorPortContext.getVisitor(visitorPort); Channel visitor = NettyVisitorPortContext.getVisitor(visitorPort);
if (visitor == null) { if (visitor == null) {
ServerBootstrap b = new ServerBootstrap(); ServerBootstrap bootstrap = new ServerBootstrap();
b bootstrap
.group(bossGroup, workerGroup) .group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class) .channel(NioServerSocketChannel.class)
@ -60,11 +61,12 @@ public class NettyVisitorSocket {
.childOption(ChannelOption.SO_KEEPALIVE, true) .childOption(ChannelOption.SO_KEEPALIVE, true)
// .childOption(ChannelOption.TCP_NODELAY, false) // .childOption(ChannelOption.TCP_NODELAY, false)
.childOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000 * 60)//连接超时时间设置为 60 .childOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000 * 60)//连接超时时间设置为 60
// .childOption(ChannelOption.RCVBUF_ALLOCATOR, new NettyRecvByteBufAllocator(1024 * 1024))//用于Channel分配接受Buffer的分配器 默认AdaptiveRecvByteBufAllocator.DEFAULT // .childOption(ChannelOption.RCVBUF_ALLOCATOR, new NettyRecvByteBufAllocator(1024 * 1024))//用于Channel分配接受Buffer的分配器 默认 AdaptiveRecvByteBufAllocator.DEFAULT
.childOption(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(1024 * 1024, 1024 * 1024 * 2))
.childHandler(visitorFilter); .childHandler(visitorFilter);
ChannelFuture sync = b.bind(visitorPort).sync(); ChannelFuture sync = bootstrap.bind(visitorPort).sync();
sync.addListener((ChannelFutureListener) future -> { sync.addListener((ChannelFutureListener) future -> {
if (future.isSuccess()) { if (future.isSuccess()) {
// 这里时异步处理 // 这里时异步处理

View File

@ -8,7 +8,6 @@ import org.framework.lazy.cloud.network.heartbeat.server.standalone.application.
import org.framework.lazy.cloud.network.heartbeat.server.standalone.application.command.internal.network.penetration.mapping.*; import org.framework.lazy.cloud.network.heartbeat.server.standalone.application.command.internal.network.penetration.mapping.*;
import org.framework.lazy.cloud.network.heartbeat.server.standalone.domain.model.internal.network.penetration.mapping.LazyInternalNetworkPenetrationMapping; import org.framework.lazy.cloud.network.heartbeat.server.standalone.domain.model.internal.network.penetration.mapping.LazyInternalNetworkPenetrationMapping;
import org.framework.lazy.cloud.network.heartbeat.server.standalone.domain.model.internal.network.penetration.mapping.LazyInternalNetworkPenetrationMappingRepository; import org.framework.lazy.cloud.network.heartbeat.server.standalone.domain.model.internal.network.penetration.mapping.LazyInternalNetworkPenetrationMappingRepository;
import org.framework.lazy.cloud.network.heartbeat.server.standalone.application.command.internal.network.penetration.mapping.*;
import org.framework.lazy.cloud.network.heartbeat.server.standalone.application.dto.LazyInternalNetworkPenetrationMappingDTO; import org.framework.lazy.cloud.network.heartbeat.server.standalone.application.dto.LazyInternalNetworkPenetrationMappingDTO;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.wu.framework.core.utils.ObjectUtils; import org.wu.framework.core.utils.ObjectUtils;
@ -292,7 +291,7 @@ public class LazyInternalNetworkPenetrationMappingApplicationImpl implements Laz
.builderChannelFlowAdapter(channelFlowAdapter) .builderChannelFlowAdapter(channelFlowAdapter)
.build(); .build();
try { try {
nettyVisitorSocket.startServer(); nettyVisitorSocket.startVisitorServer();
} catch (Exception e) { } catch (Exception e) {
log.error("客户端:{},网络端口:{},开放失败", clientId, visitorPort); log.error("客户端:{},网络端口:{},开放失败", clientId, visitorPort);
throw new RuntimeException(e); throw new RuntimeException(e);

View File

@ -2,8 +2,10 @@ spring:
lazy: lazy:
netty: netty:
client: client:
inet-host: 124.222.48.62 # inet-host: 124.222.48.62
inet-port: 32647 # inet-port: 32647
inet-host: 127.0.0.1
inet-port: 7001
inet-path: wu-lazy-cloud-heartbeat-server inet-path: wu-lazy-cloud-heartbeat-server
client-id: wujiawei # 客户端ID client-id: wujiawei # 客户端ID
# inet-host: 124.222.48.62 # 服务端地址 # inet-host: 124.222.48.62 # 服务端地址