diff --git a/zyt-rpc-common/src/main/java/serialization/kryo/KryoUtils.java b/zyt-rpc-common/src/main/java/serialization/kryo/KryoUtils.java index 46a5cef..90922fc 100644 --- a/zyt-rpc-common/src/main/java/serialization/kryo/KryoUtils.java +++ b/zyt-rpc-common/src/main/java/serialization/kryo/KryoUtils.java @@ -19,8 +19,9 @@ public class KryoUtils implements Serializer { */ private final ThreadLocal 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; }); diff --git a/zyt-rpc-consumer/src/main/java/consumer/netty_client_handler/NettyClientHandler22.java b/zyt-rpc-consumer/src/main/java/consumer/netty_client_handler/NettyClientHandler22.java index b879e2f..fb8bfb4 100644 --- a/zyt-rpc-consumer/src/main/java/consumer/netty_client_handler/NettyClientHandler22.java +++ b/zyt-rpc-consumer/src/main/java/consumer/netty_client_handler/NettyClientHandler22.java @@ -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; @@ -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(); //反序列化的模板 是根据我传进来的参数进行改变的 @@ -57,12 +60,12 @@ public synchronized Object call() throws Exception { //这个变量的目的就是保留原来的param实际参数类型,当返回的时候 可以当作反序列化的模板 Object request = param; //判断是否需要protostuff进行序列化 因为使用这个进行序列话 是我没有相应的解码器 - if (Serialization.class.getAnnotation(CodecSelector.class).Codec().equals("protostuff")&¶m.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")&¶m.getClass()!=String.class) + if (isKryo && param.getClass()!=String.class) { KryoUtils kryoUtils = new KryoUtils(); request = kryoUtils.serialize(param); diff --git a/zyt-rpc-provider/src/main/java/provider/netty_server_handler/NettyServerHandler22.java b/zyt-rpc-provider/src/main/java/provider/netty_server_handler/NettyServerHandler22.java index 720ec86..6868677 100644 --- a/zyt-rpc-provider/src/main/java/provider/netty_server_handler/NettyServerHandler22.java +++ b/zyt-rpc-provider/src/main/java/provider/netty_server_handler/NettyServerHandler22.java @@ -13,7 +13,8 @@ //实现简单的服务注册和回写 public class NettyServerHandler22 extends ChannelInboundHandlerAdapter { private String methodName; - + private boolean isProtostuff; + private boolean isKryo; //要传入对应的方法名 否则不知道 netty服务器能执行什么方法 public NettyServerHandler22(String methodName) { @@ -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()+"的信息"); @@ -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]); @@ -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); diff --git "a/\346\211\213\345\206\231RPC\346\241\206\346\236\266.pdf" "b/\346\211\213\345\206\231RPC\346\241\206\346\236\266.pdf" index 4914ff2..c41bd2c 100644 Binary files "a/\346\211\213\345\206\231RPC\346\241\206\346\236\266.pdf" and "b/\346\211\213\345\206\231RPC\346\241\206\346\236\266.pdf" differ