mirror of
https://gitee.com/wujiawei1207537021/wu-lazy-cloud-network.git
synced 2025-06-16 18:35:05 +08:00
【fix】 so nice serve proxy client is easy
This commit is contained in:
@ -374,5 +374,13 @@ public class ClientAutoConfiguration {
|
||||
public ClientHandleHttpDistributeClientProxyClientTransferCLoseTypeAdvanced clientHandleHttpDistributeClientProxyClientTransferCLoseTypeAdvanced(){
|
||||
return new ClientHandleHttpDistributeClientProxyClientTransferCLoseTypeAdvanced();
|
||||
}
|
||||
@Bean
|
||||
public ClientHandleDistributeHttpServerProxyClientConnectionSuccessfulTypeAdvanced clientHandleDistributeHttpServerProxyClientConnectionSuccessfulTypeAdvanced(){
|
||||
return new ClientHandleDistributeHttpServerProxyClientConnectionSuccessfulTypeAdvanced();
|
||||
}
|
||||
@Bean
|
||||
public ClientHandleDistributeHttpServerProxyClientTransferRequestAdvanced clientHandleDistributeHttpServerProxyClientTransferRequestAdvanced(){
|
||||
return new ClientHandleDistributeHttpServerProxyClientTransferRequestAdvanced();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,53 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.advanced;
|
||||
|
||||
import io.netty.channel.Channel;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.config.NettyClientProperties;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.socket.NettyHttpServerProxyClientRealSocket;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.NettyProxyMsg;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.advanced.HandleChannelTypeAdvanced;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.client.AbstractHandleHttpDistributeServerProxyClientConnectionSuccessfulTypeAdvanced;
|
||||
import org.wu.framework.spring.utils.SpringContextHolder;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
public class ClientHandleDistributeHttpServerProxyClientConnectionSuccessfulTypeAdvanced extends
|
||||
AbstractHandleHttpDistributeServerProxyClientConnectionSuccessfulTypeAdvanced<NettyProxyMsg> {
|
||||
|
||||
/**
|
||||
* 处理当前数据
|
||||
*
|
||||
* @param channel 当前通道
|
||||
* @param nettyProxyMsg 通道数据
|
||||
*/
|
||||
@Override
|
||||
protected void doHandler(Channel channel, NettyProxyMsg nettyProxyMsg) {
|
||||
|
||||
|
||||
// 创建一个传输通道
|
||||
byte[] msgClientId = nettyProxyMsg.getClientId();
|
||||
byte[] msgClientTargetIp = nettyProxyMsg.getClientTargetIp();
|
||||
byte[] msgClientTargetPort = nettyProxyMsg.getClientTargetPort();
|
||||
byte[] msgVisitorId = nettyProxyMsg.getVisitorId();
|
||||
|
||||
String clientId = new String(msgClientId);
|
||||
String clientTargetIp = new String(msgClientTargetIp);
|
||||
Integer clientTargetPort = Integer.parseInt(new String(msgClientTargetPort));
|
||||
String visitorId = new String(msgVisitorId);
|
||||
|
||||
NettyClientProperties nettyClientProperties = SpringContextHolder.getBean(NettyClientProperties.class);
|
||||
List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList = new ArrayList<>(SpringContextHolder.getApplicationContext().getBeansOfType(HandleChannelTypeAdvanced.class).values());
|
||||
|
||||
NettyHttpServerProxyClientRealSocket.buildRealServer(
|
||||
clientId,
|
||||
clientTargetIp,
|
||||
clientTargetPort,
|
||||
visitorId,
|
||||
nettyClientProperties,
|
||||
handleChannelTypeAdvancedList
|
||||
);
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.advanced;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.Channel;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.handler.NettyHttpServerProxyClientRealHandler;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.NettyProxyMsg;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.advanced.proxy.http.client.AbstractHandleHttpDistributeServerProxyClientTransferRequestTypeAdvanced;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
|
||||
import org.wu.framework.core.utils.ObjectUtils;
|
||||
|
||||
/**
|
||||
* @see NettyHttpServerProxyClientRealHandler
|
||||
*/
|
||||
@Slf4j
|
||||
public class ClientHandleDistributeHttpServerProxyClientTransferRequestAdvanced extends
|
||||
AbstractHandleHttpDistributeServerProxyClientTransferRequestTypeAdvanced<NettyProxyMsg> {
|
||||
|
||||
|
||||
/**
|
||||
* 处理当前数据
|
||||
*
|
||||
* @param channel 当前通道
|
||||
* @param nettyProxyMsg 通道数据
|
||||
*/
|
||||
@Override
|
||||
protected void doHandler(Channel channel, NettyProxyMsg nettyProxyMsg) {
|
||||
|
||||
// 数据发送
|
||||
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(channel);
|
||||
if (ObjectUtils.isNotEmpty(nextChannel)) {
|
||||
ByteBuf buf = channel.config().getAllocator().buffer(nettyProxyMsg.getData().length);
|
||||
buf.writeBytes(nettyProxyMsg.getData());
|
||||
nextChannel.writeAndFlush(buf);
|
||||
} else {
|
||||
log.error("客户端代理到另一个客户端,发送请求到目标客户端但是未找到真实通道");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.filter;
|
||||
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelPipeline;
|
||||
import io.netty.channel.socket.SocketChannel;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.handler.NettyHttpServerProxyClientRealHandler;
|
||||
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.filter.DebugChannelInitializer;
|
||||
|
||||
public class NettyHttpServerProxyClientRealFilter extends DebugChannelInitializer<SocketChannel> {
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
@Override
|
||||
protected void initChannel0(SocketChannel ch) {
|
||||
ChannelPipeline pipeline = ch.pipeline();
|
||||
// 解码、编码
|
||||
pipeline.addLast(new TransferDecoder(Integer.MAX_VALUE, 1024 * 1024 * 10));
|
||||
pipeline.addLast(new TransferEncoder());
|
||||
pipeline.addLast(new NettyHttpServerProxyClientRealHandler());
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.filter;
|
||||
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.ChannelPipeline;
|
||||
import io.netty.channel.socket.SocketChannel;
|
||||
import io.netty.handler.timeout.IdleStateHandler;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.handler.NettyHttpClientProxyClientTransferRealHandler;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.handler.NettyHttpServerProxyClientTransferRealHandler;
|
||||
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.encoder.NettyProxyMsgEncoder;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.filter.DebugChannelInitializer;
|
||||
|
||||
/**
|
||||
* netty 客户端连接真实服服务端访客拦截器
|
||||
*/
|
||||
public class NettyHttpServerProxyClientTransferRealFilter extends DebugChannelInitializer<SocketChannel> {
|
||||
private final ChannelTypeAdapter channelTypeAdapter;
|
||||
|
||||
public NettyHttpServerProxyClientTransferRealFilter(ChannelTypeAdapter channelTypeAdapter) {
|
||||
this.channelTypeAdapter = channelTypeAdapter;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 connectionClose
|
||||
* the {@link Channel}.
|
||||
*/
|
||||
@Override
|
||||
protected void initChannel0(SocketChannel ch) throws Exception {
|
||||
ChannelPipeline pipeline = ch.pipeline();
|
||||
// // 解码、编码
|
||||
// pipeline.addLast(new NettyProxyMsgDecoder(Integer.MAX_VALUE, 0, 4, -4, 0));
|
||||
// pipeline.addLast(new NettMsgEncoder());
|
||||
|
||||
pipeline.addLast(new IdleStateHandler(0, 4, 0));
|
||||
|
||||
pipeline.addLast(new NettyProxyMsgDecoder(Integer.MAX_VALUE, 0, 4, -4, 0));
|
||||
pipeline.addLast(new NettyProxyMsgEncoder());
|
||||
pipeline.addLast(new NettyHttpServerProxyClientTransferRealHandler(channelTypeAdapter));
|
||||
}
|
||||
}
|
@ -0,0 +1,79 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.handler;
|
||||
|
||||
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.SimpleChannelInboundHandler;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.NettyByteBuf;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.NettyProxyMsg;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.constant.TcpMessageType;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
|
||||
|
||||
/**
|
||||
* 来自客户端 真实服务器返回的数据请求
|
||||
*/
|
||||
@Slf4j
|
||||
public class NettyHttpServerProxyClientRealHandler extends SimpleChannelInboundHandler<NettyByteBuf> {
|
||||
|
||||
|
||||
@Override
|
||||
public void channelRead0(ChannelHandlerContext ctx,NettyByteBuf nettyByteBuf) {
|
||||
|
||||
byte[] bytes = nettyByteBuf.getData();
|
||||
log.debug("bytes.length:{}",bytes.length);
|
||||
log.debug("客户端代理客户端,接收目标客户端真实服务数据:{}", new String(bytes));
|
||||
String visitorId = ChannelAttributeKeyUtils.getVisitorId(ctx.channel());
|
||||
String clientId = ChannelAttributeKeyUtils.getClientId(ctx.channel());
|
||||
// 访客通信通道 上报服务端代理完成
|
||||
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(ctx.channel());
|
||||
NettyProxyMsg returnMessage = new NettyProxyMsg();
|
||||
returnMessage.setType(TcpMessageType.HTTP_REPORT_SERVER_PROXY_CLIENT_TRANSFER_RESPONSE_);
|
||||
returnMessage.setVisitorId(visitorId);
|
||||
returnMessage.setClientId(clientId);
|
||||
returnMessage.setData(bytes);
|
||||
|
||||
nextChannel.writeAndFlush(returnMessage);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
||||
super.channelActive(ctx);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
||||
String visitorId = ChannelAttributeKeyUtils.getVisitorId(ctx.channel());
|
||||
// 客户端真实通信通道
|
||||
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(ctx.channel());
|
||||
if (nextChannel != null) {
|
||||
// 上报关闭这个客户端的访客通道
|
||||
NettyProxyMsg closeVisitorMsg = new NettyProxyMsg();
|
||||
closeVisitorMsg.setType(TcpMessageType.HTTP_REPORT_SERVER_PROXY_CLIENT_TRANSFER_CLOSE_);
|
||||
closeVisitorMsg.setVisitorId(visitorId);
|
||||
nextChannel.writeAndFlush(closeVisitorMsg);
|
||||
}
|
||||
|
||||
super.channelInactive(ctx);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
|
||||
// 获取访客的传输通道
|
||||
if (ctx.channel().isWritable()) {
|
||||
log.debug("Channel is writable again");
|
||||
// 恢复之前暂停的操作,如写入数据
|
||||
} else {
|
||||
log.debug("Channel is not writable");
|
||||
// 暂停写入操作,等待可写状态
|
||||
}
|
||||
log.info("channelWritabilityChanged!");
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
||||
super.exceptionCaught(ctx, cause);
|
||||
}
|
||||
}
|
@ -0,0 +1,72 @@
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.handler;
|
||||
|
||||
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.SimpleChannelInboundHandler;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
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.constant.TcpMessageType;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
|
||||
|
||||
/**
|
||||
* 客户端访客通信通道 处理器
|
||||
*/
|
||||
@Slf4j
|
||||
public class NettyHttpServerProxyClientTransferRealHandler extends SimpleChannelInboundHandler<NettyProxyMsg> {
|
||||
private final ChannelTypeAdapter channelTypeAdapter;
|
||||
|
||||
public NettyHttpServerProxyClientTransferRealHandler(ChannelTypeAdapter channelTypeAdapter) {
|
||||
this.channelTypeAdapter = channelTypeAdapter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
||||
super.channelActive(ctx);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelRead0(ChannelHandlerContext ctx, NettyProxyMsg nettyProxyMsg) throws Exception {
|
||||
Channel channel = ctx.channel();
|
||||
channelTypeAdapter.handler(channel, nettyProxyMsg);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
||||
|
||||
String clientId = ChannelAttributeKeyUtils.getClientId(ctx.channel());
|
||||
String visitorId = ChannelAttributeKeyUtils.getVisitorId(ctx.channel());
|
||||
Channel nextChannel = ChannelAttributeKeyUtils.getNextChannel(ctx.channel());
|
||||
log.warn("close client proxy client transfer real clientId:{} visitorId:{}", clientId, visitorId);
|
||||
// 关闭访客
|
||||
if (nextChannel != null) {
|
||||
// 上报关闭这个客户端的访客通道
|
||||
NettyProxyMsg closeVisitorMsg = new NettyProxyMsg();
|
||||
closeVisitorMsg.setType(TcpMessageType.HTTP_REPORT_SERVER_PROXY_CLIENT_TRANSFER_CLOSE_);
|
||||
closeVisitorMsg.setVisitorId(visitorId);
|
||||
nextChannel.writeAndFlush(closeVisitorMsg);
|
||||
}
|
||||
|
||||
super.channelInactive(ctx);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
|
||||
// 处理客户端本地真实通道问题
|
||||
if (ctx.channel().isWritable()) {
|
||||
log.debug("Channel is writable again");
|
||||
// 恢复之前暂停的操作,如写入数据
|
||||
} else {
|
||||
log.debug("Channel is not writable");
|
||||
// 暂停写入操作,等待可写状态
|
||||
}
|
||||
log.info("channelWritabilityChanged!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
||||
super.exceptionCaught(ctx, cause);
|
||||
}
|
||||
}
|
@ -0,0 +1,170 @@
|
||||
|
||||
package org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.socket;
|
||||
|
||||
|
||||
import io.netty.bootstrap.Bootstrap;
|
||||
import io.netty.channel.*;
|
||||
import io.netty.channel.nio.NioEventLoopGroup;
|
||||
import io.netty.channel.socket.nio.NioSocketChannel;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.config.NettyClientProperties;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.filter.NettyHttpServerProxyClientRealFilter;
|
||||
import org.framework.lazy.cloud.network.heartbeat.client.netty.proxy.http.filter.NettyHttpServerProxyClientTransferRealFilter;
|
||||
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.adapter.ChannelTypeAdapter;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.advanced.HandleChannelTypeAdvanced;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.constant.TcpMessageType;
|
||||
import org.framework.lazy.cloud.network.heartbeat.common.utils.ChannelAttributeKeyUtils;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 服务端代理客户端真实通道创建
|
||||
*/
|
||||
@Slf4j
|
||||
public class NettyHttpServerProxyClientRealSocket {
|
||||
static EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
|
||||
|
||||
|
||||
public static void buildRealServer(String clientId,
|
||||
String clientTargetIp,
|
||||
Integer clientTargetPort,
|
||||
String visitorId,
|
||||
NettyClientProperties nettyClientProperties,
|
||||
List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList) {
|
||||
|
||||
try {
|
||||
|
||||
Bootstrap bootstrap = new Bootstrap();
|
||||
bootstrap.group(eventLoopGroup).channel(NioSocketChannel.class)
|
||||
// 设置读缓冲区为2M
|
||||
.option(ChannelOption.SO_RCVBUF, 2048 * 1024)
|
||||
// 设置写缓冲区为1M
|
||||
.option(ChannelOption.SO_SNDBUF, 1024 * 1024)
|
||||
// .option(ChannelOption.TCP_NODELAY, false)
|
||||
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000 * 60)//连接超时时间设置为 60 秒
|
||||
// .option(ChannelOption.SO_BACKLOG, 128)//务端接受连接的队列长度 默认128
|
||||
// .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 NettyHttpServerProxyClientRealFilter())
|
||||
|
||||
;
|
||||
|
||||
|
||||
bootstrap.connect(clientTargetIp, clientTargetPort).addListener((ChannelFutureListener) future -> {
|
||||
if (future.isSuccess()) {
|
||||
// 客户端链接真实服务成功 设置自动读写false 等待访客连接成功后设置成true
|
||||
Channel realChannel = future.channel();
|
||||
// realChannel.config().setOption(ChannelOption.AUTO_READ, false);
|
||||
|
||||
log.info("访客通过 客户端:【{}】,visitorId:{},绑定本地服务,IP:{},端口:{} 新建通道成功", clientId, visitorId, clientTargetIp, clientTargetPort);
|
||||
// 客户端真实通道
|
||||
NettyRealIdContext.pushReal(realChannel, visitorId);
|
||||
// 绑定访客ID到当前真实通道属性
|
||||
ChannelAttributeKeyUtils.buildVisitorId(realChannel, visitorId);
|
||||
ChannelAttributeKeyUtils.buildClientId(realChannel, clientId);
|
||||
|
||||
// 连接服务端 然后绑定通道
|
||||
// 新建一个通道处理
|
||||
newVisitorConnect2Server(
|
||||
clientId,
|
||||
clientTargetIp,
|
||||
clientTargetPort,
|
||||
visitorId,
|
||||
realChannel,
|
||||
nettyClientProperties,
|
||||
handleChannelTypeAdvancedList
|
||||
);
|
||||
|
||||
|
||||
} else {
|
||||
log.error("客户:【{}】,无法连接当前网络内的目标IP:【{}】,目标端口:【{}】", clientId, clientTargetIp, clientTargetPort);
|
||||
}
|
||||
});
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建访客连接服务端
|
||||
*
|
||||
* @param nettyClientProperties 服务端配置信息
|
||||
* @param handleChannelTypeAdvancedList 处理器适配器
|
||||
* @throws InterruptedException 异常
|
||||
*/
|
||||
protected static void newVisitorConnect2Server(String clientId,
|
||||
String clientTargetIp,
|
||||
Integer clientTargetPort,
|
||||
String visitorId,
|
||||
Channel realChannel,
|
||||
NettyClientProperties nettyClientProperties,
|
||||
List<HandleChannelTypeAdvanced> handleChannelTypeAdvancedList) throws InterruptedException {
|
||||
Bootstrap bootstrap = new Bootstrap();
|
||||
bootstrap.group(eventLoopGroup)
|
||||
.channel(NioSocketChannel.class)
|
||||
.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.CONNECT_TIMEOUT_MILLIS, 1000 * 60)//连接超时时间设置为 60 秒
|
||||
// .option(ChannelOption.SO_BACKLOG, 256)//务端接受连接的队列长度 默认128
|
||||
// .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 NettyHttpServerProxyClientTransferRealFilter(new ChannelTypeAdapter(handleChannelTypeAdvancedList)))
|
||||
;
|
||||
|
||||
String inetHost = nettyClientProperties.getInetHost();
|
||||
int inetPort = nettyClientProperties.getInetPort();
|
||||
// local client id
|
||||
|
||||
// String clientId = nettyClientProperties.getClientId();
|
||||
|
||||
|
||||
// 客户端新建访客通道 连接服务端IP:{},连接服务端端口:{}
|
||||
log.info("client creates a new visitor channel to connect to server IP: {}, connecting to server port: {} with visitorId:{} & clientId:{}", inetHost, inetPort, visitorId, clientId);
|
||||
ChannelFuture future = bootstrap.connect(inetHost, inetPort);
|
||||
|
||||
future.addListener((ChannelFutureListener) futureListener -> {
|
||||
Channel transferChannel = futureListener.channel();
|
||||
if (futureListener.isSuccess()) {
|
||||
realChannel.config().setOption(ChannelOption.AUTO_READ, true);
|
||||
// 通知服务端访客连接成功
|
||||
NettyProxyMsg nettyProxyMsg = new NettyProxyMsg();
|
||||
nettyProxyMsg.setVisitorId(visitorId);
|
||||
nettyProxyMsg.setClientId(clientId);
|
||||
nettyProxyMsg.setClientTargetIp(clientTargetIp);
|
||||
nettyProxyMsg.setClientTargetPort(clientTargetPort);
|
||||
nettyProxyMsg.setType(TcpMessageType.HTTP_REPORT_SERVER_PROXY_CLIENT_TRANSFER_CHANNEL_INIT_SUCCESSFUL_);
|
||||
transferChannel.writeAndFlush(nettyProxyMsg);
|
||||
|
||||
ChannelAttributeKeyUtils.buildNextChannel(transferChannel, realChannel);
|
||||
ChannelAttributeKeyUtils.buildNextChannel(realChannel, transferChannel);
|
||||
|
||||
// 绑定客户端真实通信通道
|
||||
ChannelAttributeKeyUtils.buildVisitorId(transferChannel, visitorId);
|
||||
ChannelAttributeKeyUtils.buildClientId(transferChannel, clientId);
|
||||
|
||||
} else {
|
||||
log.info("无法连接到服务端....");
|
||||
eventLoopGroup.schedule(() -> {
|
||||
try {
|
||||
newVisitorConnect2Server(clientId,
|
||||
clientTargetIp,
|
||||
clientTargetPort,
|
||||
visitorId, realChannel, nettyClientProperties, handleChannelTypeAdvancedList);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}, 2, TimeUnit.SECONDS);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user