Skip to content

Commit

Permalink
5.7 v2.3 实现protobuf/protostuff/kryo 通过编译编解码/序列化/序列化 完成对象的传输
Browse files Browse the repository at this point in the history
  • Loading branch information
zzzzzzzzyt committed May 7, 2022
1 parent de8487a commit e0ed119
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ public class KryoUtils implements Serializer {
*/
private final ThreadLocal<Kryo> kryoThreadLocal = ThreadLocal.withInitial(() -> {
Kryo kryo = new Kryo();
kryo.register(Person.class); //这里这个注册其实就有点类似于protostuff的scheme 就是一个模板
// kryo.register(Person.class); // 注册了能提高性能 减少了空间的 浪费 /但如果分布式系统中注册 不同的顺序可能导致错误
// kryo.register(PersonPOJO.Person.class); //不能使用这个
kryo.setRegistrationRequired(false); //显式的关闭了注册的行为
return kryo;
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public class NettyClientHandler22 extends ChannelInboundHandlerAdapter implement
private Object param;
private Object response;
private ChannelHandlerContext context;

private boolean isProtostuff;
private boolean isKryo;

public void setParam(Object param) {
this.param = param;
Expand All @@ -28,19 +29,21 @@ public void setParam(Object param) {
public void channelActive(ChannelHandlerContext ctx) throws Exception {
context = ctx;
System.out.println("U•ェ•*U 成功连接");

String codecType = Serialization.class.getAnnotation(CodecSelector.class).Codec();
if (codecType.equals("protostuff"))isProtostuff = true;
else if (codecType.equals("kryo"))isKryo = true;
}

@Override
public synchronized void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
//在这要进行解码 获得传回来的信息 如果是遇到下面的msg 那就代表传回来的肯定是个byte[] 根据我们要的方法进行解码
if (Serialization.class.getAnnotation(CodecSelector.class).Codec().equals("protostuff") &&msg.getClass()!=String.class)
if (isProtostuff && msg.getClass()!=String.class)
{
ProtostuffUtils protostuffUtils = new ProtostuffUtils();
//反序列化的模板 是根据我传进来的参数进行改变的
msg = protostuffUtils.deserialize((byte[]) msg,param.getClass());
}
if (Serialization.class.getAnnotation(CodecSelector.class).Codec().equals("kryo") &&msg.getClass()!=String.class)
if (isKryo && msg.getClass()!=String.class)
{
KryoUtils kryoUtils = new KryoUtils();
//反序列化的模板 是根据我传进来的参数进行改变的
Expand All @@ -57,12 +60,12 @@ public synchronized Object call() throws Exception {
//这个变量的目的就是保留原来的param实际参数类型,当返回的时候 可以当作反序列化的模板
Object request = param;
//判断是否需要protostuff进行序列化 因为使用这个进行序列话 是我没有相应的解码器
if (Serialization.class.getAnnotation(CodecSelector.class).Codec().equals("protostuff")&&param.getClass()!=String.class)
if (isProtostuff && param.getClass()!=String.class)
{
ProtostuffUtils protostuffUtils = new ProtostuffUtils();
request = protostuffUtils.serialize(param);
}
if (Serialization.class.getAnnotation(CodecSelector.class).Codec().equals("kryo")&&param.getClass()!=String.class)
if (isKryo && param.getClass()!=String.class)
{
KryoUtils kryoUtils = new KryoUtils();
request = kryoUtils.serialize(param);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
//实现简单的服务注册和回写
public class NettyServerHandler22 extends ChannelInboundHandlerAdapter {
private String methodName;

private boolean isProtostuff;
private boolean isKryo;
//要传入对应的方法名 否则不知道 netty服务器能执行什么方法
public NettyServerHandler22(String methodName)
{
Expand All @@ -22,6 +23,14 @@ public NettyServerHandler22(String methodName)

//实现对应的方法


@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
String codecType = Serialization.class.getAnnotation(CodecSelector.class).Codec();
if (codecType.equals("protostuff"))isProtostuff = true;
else if (codecType.equals("kryo"))isKryo = true;
}

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println("收到来自"+ctx.channel().remoteAddress()+"的信息");
Expand All @@ -31,12 +40,12 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
Method method = methods[0];

//如果我传入的是protostuff 传进来的是相应的byte数组 那我就找不到对应的方法 现在默认是第一个方法 还有就是判断不是字符串
if (Serialization.class.getAnnotation(CodecSelector.class).Codec().equals("protostuff")&&msg.getClass()!=String.class)
if (isProtostuff&&msg.getClass()!=String.class)
{
ProtostuffUtils protostuffUtils = new ProtostuffUtils();
msg = protostuffUtils.deserialize((byte[]) msg, method.getParameterTypes()[0]);
}
else if (Serialization.class.getAnnotation(CodecSelector.class).Codec().equals("kryo")&&msg.getClass()!=String.class)
else if (isKryo&&msg.getClass()!=String.class)
{
KryoUtils kryoUtils = new KryoUtils();
msg = kryoUtils.deserialize((byte[]) msg, method.getParameterTypes()[0]);
Expand All @@ -58,12 +67,12 @@ else if (Serialization.class.getAnnotation(CodecSelector.class).Codec().equals("
//获得对应信息并进行回传

//判断是否需要通过对应的方法进行序列化
if (Serialization.class.getAnnotation(CodecSelector.class).Codec().equals("protostuff")&&response.getClass()!=String.class)
if (isProtostuff&&response.getClass()!=String.class)
{
ProtostuffUtils protostuffUtils = new ProtostuffUtils();
response = protostuffUtils.serialize(response);
}
if (Serialization.class.getAnnotation(CodecSelector.class).Codec().equals("kryo")&&response.getClass()!=String.class)
if (isKryo&&response.getClass()!=String.class)
{
KryoUtils kryoUtils = new KryoUtils();
response = kryoUtils.serialize(response);
Expand Down
Binary file modified 手写RPC框架.pdf
Binary file not shown.

0 comments on commit e0ed119

Please sign in to comment.