网站logo的颜色与网页的颜色,旅游网站建设调研,乐云seo模板网站建设,西安单位网站建设文章目录 RPC项目
配置中心项目
网关项目
在前面的文章了解完毕之后#xff0c;我们以及设计了一个基本的RPC协议#xff0c;之后#xff0c;我们要做的就是基于这个RPC协议#xff0c;去实现一个能解析这个RPC协议的编解码器。
这里编解码器比较容易写#xff0c;按照… 文章目录 RPC项目
配置中心项目
网关项目
在前面的文章了解完毕之后我们以及设计了一个基本的RPC协议之后我们要做的就是基于这个RPC协议去实现一个能解析这个RPC协议的编解码器。
这里编解码器比较容易写按照前面几篇文章中讲解的方式来编写即可。
Slf4j
public class RpcEncode extends MessageToByteEncoderRpcDtoObject {Overrideprotected void encode(ChannelHandlerContext ctx, RpcDtoObject msg, ByteBuf out) throws Exception {log.info(Start encoding the data);//判断请求头合法性if (Objects.isNull(msg)) {log.warn(the RpcDto msg is Null!!!);return;}RpcHeader RpcHeadermsg.getHeader();out.writeByte(RpcHeader.getVersionId());out.writeByte(RpcHeader.getAlgorithmType());out.writeByte(RpcHeader.getReqType());out.writeLong(RpcHeader.getReqId());//设定序列化算法Serializer serializer SerializerStrategy.getSerializer(RpcHeader.getAlgorithmType());byte[] dataserializer.serialize(msg.getData());//设定数据长度out.writeInt(data.length);//设定数据out.writeBytes(data);}
}解码器的代码也并不难写我们不断的从bytebuf中读取数据即可然后最后还原为RpcDto对象即可。
package blossom.project.rpc.core.netty.codec;import blossom.project.rpc.common.constants.RpcCommonConstants;
import blossom.project.rpc.common.enums.ReqTypeEnum;
import blossom.project.rpc.core.entity.RpcDto;
import blossom.project.rpc.core.entity.RpcHeader;
import blossom.project.rpc.core.entity.RpcRequest;
import blossom.project.rpc.core.entity.RpcResponse;
import blossom.project.rpc.core.serialize.Serializer;
import blossom.project.rpc.core.serialize.SerializerStrategy;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import lombok.extern.slf4j.Slf4j;import java.util.List;
import java.util.Objects;/*** author: ZhangBlossom* date: 2023/12/16 18:58* contact: QQ:4602197553* contact: WX:qczjhczs0114* blog: https://blog.csdn.net/Zhangsama1* github: https://github.com/ZhangBlossom* RpcDecode类* 平平无奇RPC自定义协议的解码器*/
Slf4j
public class RpcDecode extends ByteToMessageDecoder {Overrideprotected void decode(ChannelHandlerContext ctx, ByteBuf in, ListObject out) throws Exception {log.info(Start decoding the data);//判断请求头合法性if (Objects.isNull(in)) {log.warn(the data is Null!!!);return;}//因为我们的数据至少包含请求头请求体 因此长度小于此肯定有问题if (in.readableBytes() RpcCommonConstants.HEADER_LENGTH) {log.warn(The length of the request header does not meet the requirements!!!);return;}//读取版本号byte versionId in.readByte();if (versionId ! RpcCommonConstants.VERSION_ID) {throw new IllegalArgumentException(Illegal versionId!!!);}//继续读取请求头信息byte algorithmType in.readByte();byte reqType in.readByte();long reqId in.readLong();int length in.readInt();//判断可读长度是否小于请求头中设定的请求体长度if (in.readableBytes() length) {//数据长度不对劲 先丢掉log.info(the readable bytess length is less!!!);return;}RpcHeader header new RpcHeader(versionId, algorithmType, reqType, reqId, length);//获得反序列化器Serializer serializer SerializerStrategy.getSerializer(algorithmType);//获得请求类型ReqTypeEnum reqTypeEnum ReqTypeEnum.getReqTypeByCode(reqType);//得到实际传输的数据byte[] data new byte[length];in.readBytes(data);RpcDto dto null;switch (reqTypeEnum) {case REQUEST:// 处理 GET 请求逻辑log.info(Handling REQUEST request);RpcRequest request serializer.deserialize(data, RpcRequest.class);dto new RpcDtoRpcRequest();dto.setHeader(header);dto.setData(request);//设定到out中 会自动被handler处理out.add(dto);break;case RESPONSE:// 处理 RESPONSE 请求逻辑log.info(Handling RESPONSE request);RpcResponse response serializer.deserialize(data, RpcResponse.class);dto new RpcDtoRpcResponse();dto.setHeader(header);dto.setData(response);//设定到out中 会自动被handler处理out.add(dto);break;case HEARTBEAT:// 处理心跳检测逻辑log.info(Handling HEARTBEAT request);break;default:log.warn(Unsupported request type: reqTypeEnum);}}
}编解码器我们就写完了接下来我们先不编写Handler因为其实从上到下Handler最终的设计思路还没办法那么快确定。 我们先去编写一个Server端和Client端。 然后设计一下我们希望的RPC调用方式。 这里我们参考dubbo的实现。