out.add(request = new Request(requestId, requestType,
new String(serviceData), new String(paramsData)));
request.setContentLength(contentLength); //这里,其实上层应用不需要知道请求包的长度,这里属于多于的操作
LogUtils.log(“RequestFrameDecoder 成功从服务端响应中解析出业务包,requestId:%d”, requestId);
} else if (requestType == 2) { // 此处不对请求类型进行校验
in.skipBytes(HEAD_LENGTH);
out.add(request = new Request(requestId, requestType));
request.setContentLength(contentLength); //这里,其实上层应用不需要知道请求包的长度,这里属于多于的操作
LogUtils.log(“RequestFrameDecoder 成功从服务端响应中解析出心跳包,requestId:%d”, requestId);
}
}
}
package persistent.prestige.demo.netty.dubbo.net;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import persistent.prestige.demo.netty.dubbo.utils.LogUtils;
/**
-
请求消息编码器,实现自定义协议,用于客户端
-
自定义协议如下
-
4字节的请求序列号 2字节的请求类型 2字节数据长度 2字节服务调用长度 N字节的服务调用名 2字节请求参数长度 请求参数
-
其中请求类型为1:服务调用2:心跳包,如果是心跳包的话,只有协议头,也就是【4字节请求序列号,2字节请求类型,2字节数据长度】
-
@author prestigeding@126.com
*/
public class RequestFrameEncoder extends MessageToByteEncoder {
@Override
protected void encode(ChannelHandlerContext ctx, Request msg, ByteBuf out)
throws Exception {
LogUtils.log(“RequestFrameEncoder begin calling…requestId:%d,requestType:%d.”,msg.getRequestId(), msg.getRequestType());
out.writeInt(msg.getRequestId()); // requestId
out.writeShort(msg.getRequestType()); // requestType
if(msg.getRequestType() == 1 ) { //服务调用
byte[] serviceData = msg.getService().getBytes();
byte[] paramsData = msg.getParams().getBytes();
int slen, plen;
out.writeShort((
2 + (slen = serviceData.length) +
2 + (plen = paramsData.length)));
out.writeShort(slen);
out.writeBytes(serviceData);
out.writeShort(plen);
out.writeBytes(paramsData);
} else if(msg.getRequestType() == 2 ) { //心跳包
out.writeShort(0);
}
LogUtils.log(“RequestFrameEncoder end call…”);
}
}
package persistent.prestige.demo.netty.dubbo.net;
import java.util.List;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import persistent.prestige.demo.netty.dubbo.utils.LogUtils;
/**
-
响应信息解码器 用于客户端
-
@author dingwei2
-
响应报文格式
-
4字节的请求序列号 2字节的执行状态码 2字节数据长度 多字节的响应数据信息(data)
*/
public class ResponseFrameDecoder extends ByteToMessageDecoder {
private static final int HEAD_LENGTH = 8;
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) throws Exception {
LogUtils.log(“ResponseFrameDecoder start calling…”);
// TODO Auto-generated method stub
int r = in.readableBytes();
LogUtils.log(“ResponseFrameDecoder 累积缓存区中可读长度:%d”, r);
if(r < HEAD_LENGTH ) {
LogUtils.log(“ResponseFrameDecoder 当前累积缓存区可读数据长度为%d,小于协议头的长度%d”, r, HEAD_LENGTH);
return;
}
int currentReaderIndex = in.readerIndex();
int requestId = in.getInt(currentReaderIndex); // 0-3 4个字节
short code = in.getShort(currentReaderIndex + 4); // 4,5
short contentLength = in.getShort(currentReaderIndex + 6); //响应数据长度
LogUtils.log(“ResponseFrameDecoder requestId:%d,code:%d,contentLength:%d”, requestId, code, contentLength);
int packetLen;
if( r < (packetLen = HEAD_LENGTH + contentLength)) {
LogUtils.log(“ResponseFrameDecoder 可读长度:%d,需要包长度:%d”, r, packetLen);
return;
}
in.skipBytes( HEAD_LENGTH );
String data;
if(contentLength > 0 ) {
byte[] dst = new byte[contentLength];
in.readBytes(dst, 0, contentLength);
data = new String(dst);
} else {
data = “”;
}
out.add(new Response(requestId, code, new String(data)));
LogUtils.log(“ResponseFrameDecoder 成功解析服务端响应信息并准备给下游处理器处理”);
}
}
package persistent.prestige.demo.netty.dubbo.net;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import persistent.prestige.demo.netty.dubbo.utils.LogUtils;
/**
-
响应信息 编码器,,用于服务器
-
@author dingwei2
-
响应报文格式
-
4字节的请求序列号 2字节的执行状态码 2字节数据长度 多字节的响应数据信息(data)
*/
public class ResponseFrameEncoder extends MessageToByteEncoder {
@Override
protected void encode(ChannelHandlerContext ctx, Response msg, ByteBuf out) throws Exception {
// TODO Auto-generated method stub
LogUtils.log(“ResponseFrameEncoder:服务端编码响应,requestId:%d,code:%d,data:%s”, msg.getRequestId(), msg.getCode(), msg.getData());
out.writeInt(msg.getRequestId());
out.writeShort(msg.getCode());
byte[] data = msg.getData().getBytes();
out.writeShort(data.length);
out.writeBytes(data);
LogUtils.log(“ResponseFrameEncoder:服务端编码响应成功”);
}
}
package persistent.prestige.demo.netty.dubbo.net;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import persistent.prestige.demo.netty.dubbo.net.heart.HeartExecutorThreadPool;
import persistent.prestige.demo.netty.dubbo.net.heart.HeartTask;
import persistent.prestige.demo.netty.dubbo.utils.LogUtils;
/**
-
netty 客户端 connection 一个NettyConnection 就是一条长连接
-
@author prestigeding@126.com
*/
public class NettyClientConnection {
private String serverIp;
private int serverPort;
private boolean connect = false;
private Bootstrap bootstrap;
private NioEventLoopGroup workGroup;
private ChannelFuture channelFuture;
//客户端连接请求
private static final ConcurrentHashMap<Integer, ResponseFuture> requestMap = new ConcurrentHashMap<Integer, ResponseFuture>();
private static final AtomicInteger seq = new AtomicInteger(0);
private HeartExecutorThreadPool heartExecutor = null;
/**
-
@param serverIp
-
@param serverPort
*/
public NettyClientConnection(String serverIp, int serverPort) {
this.serverIp = serverIp;
this.serverPort = serverPort;
workGroup = new NioEventLoopGroup();
bootstrap = new Bootstrap();
bootstrap.group(workGroup)
.channel(NioSocketChannel.class)
小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Java工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年最新Java开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Java)
,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。**
深知大多数初中级Java工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年最新Java开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
[外链图片转存中…(img-yAbfQKPS-1710836580779)]
[外链图片转存中…(img-j3q1uKNv-1710836580780)]
[外链图片转存中…(img-GZsoBOxL-1710836580780)]
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Java)
[外链图片转存中…(img-gp0KoH7c-1710836580781)]