diff --git a/easy-rpc-core/pom.xml b/easy-rpc-core/pom.xml
index 1e523d5..69e77cd 100644
--- a/easy-rpc-core/pom.xml
+++ b/easy-rpc-core/pom.xml
@@ -21,6 +21,8 @@
3.4.14
0.7
2.12.0
+ 4.0.62
+ 3.0.3
UTF-8
1.8
1.8
@@ -46,6 +48,20 @@
${slf4j-api.version}
+
+
+ com.esotericsoftware
+ kryo-shaded
+ ${kryo.version}
+
+
+
+
+ com.caucho
+ hessian
+ ${hessian.version}
+
+
com.alibaba
diff --git a/easy-rpc-core/src/main/java/com/shaogezhu/easy/rpc/core/client/Client.java b/easy-rpc-core/src/main/java/com/shaogezhu/easy/rpc/core/client/Client.java
index 88ae7a5..9573b40 100644
--- a/easy-rpc-core/src/main/java/com/shaogezhu/easy/rpc/core/client/Client.java
+++ b/easy-rpc-core/src/main/java/com/shaogezhu/easy/rpc/core/client/Client.java
@@ -15,6 +15,10 @@
import com.shaogezhu.easy.rpc.core.registy.zookeeper.ZookeeperRegister;
import com.shaogezhu.easy.rpc.core.router.RandomRouterImpl;
import com.shaogezhu.easy.rpc.core.router.RotateRouterImpl;
+import com.shaogezhu.easy.rpc.core.serialize.fastjson.FastJsonSerializeFactory;
+import com.shaogezhu.easy.rpc.core.serialize.hessian.HessianSerializeFactory;
+import com.shaogezhu.easy.rpc.core.serialize.jdk.JdkSerializeFactory;
+import com.shaogezhu.easy.rpc.core.serialize.kryo.KryoSerializeFactory;
import com.shaogezhu.easy.rpc.interfaces.DataService;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
@@ -75,6 +79,25 @@ protected void initChannel(SocketChannel ch) throws Exception {
RpcListenerLoader rpcListenerLoader = new RpcListenerLoader();
rpcListenerLoader.init();
+ //初始化序列化器
+ String clientSerialize = clientConfig.getClientSerialize();
+ switch (clientSerialize) {
+ case JDK_SERIALIZE_TYPE:
+ CLIENT_SERIALIZE_FACTORY = new JdkSerializeFactory();
+ break;
+ case FAST_JSON_SERIALIZE_TYPE:
+ CLIENT_SERIALIZE_FACTORY = new FastJsonSerializeFactory();
+ break;
+ case HESSIAN2_SERIALIZE_TYPE:
+ CLIENT_SERIALIZE_FACTORY = new HessianSerializeFactory();
+ break;
+ case KRYO_SERIALIZE_TYPE:
+ CLIENT_SERIALIZE_FACTORY = new KryoSerializeFactory();
+ break;
+ default:
+ throw new RuntimeException("no match serialize type for" + clientSerialize);
+ }
+
//初始化代理工厂
RpcReference rpcReference;
if (JAVASSIST_PROXY_TYPE.equals(clientConfig.getProxyType())) {
@@ -92,6 +115,7 @@ public void initClientConfig() {
clientConfig.setApplicationName("easy-rpc-client");
clientConfig.setProxyType("JDK");
clientConfig.setRouterStrategy("random");
+ clientConfig.setClientSerialize("kryo");
this.setClientConfig(clientConfig);
}
@@ -162,9 +186,10 @@ public void run() {
try {
//阻塞模式
RpcInvocation data = SEND_QUEUE.take();
- String json = JSON.toJSONString(data);
+ //进行序列化
+ byte[] serialize = CLIENT_SERIALIZE_FACTORY.serialize(data);
//将RpcInvocation封装到RpcProtocol对象中,然后发送给服务端
- RpcProtocol rpcProtocol = new RpcProtocol(json.getBytes());
+ RpcProtocol rpcProtocol = new RpcProtocol(serialize);
//获取netty通道
ChannelFuture channelFuture = ConnectionHandler.getChannelFuture(data.getTargetServiceName());
//netty的通道负责发送数据给服务端
diff --git a/easy-rpc-core/src/main/java/com/shaogezhu/easy/rpc/core/client/ClientHandler.java b/easy-rpc-core/src/main/java/com/shaogezhu/easy/rpc/core/client/ClientHandler.java
index 5bfb2ec..40eea37 100644
--- a/easy-rpc-core/src/main/java/com/shaogezhu/easy/rpc/core/client/ClientHandler.java
+++ b/easy-rpc-core/src/main/java/com/shaogezhu/easy/rpc/core/client/ClientHandler.java
@@ -1,6 +1,5 @@
package com.shaogezhu.easy.rpc.core.client;
-import com.alibaba.fastjson.JSON;
import com.shaogezhu.easy.rpc.core.common.RpcInvocation;
import com.shaogezhu.easy.rpc.core.common.RpcProtocol;
import io.netty.channel.Channel;
@@ -8,6 +7,7 @@
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.ReferenceCountUtil;
+import static com.shaogezhu.easy.rpc.core.common.cache.CommonClientCache.CLIENT_SERIALIZE_FACTORY;
import static com.shaogezhu.easy.rpc.core.common.cache.CommonClientCache.RESP_MAP;
/**
@@ -18,9 +18,7 @@ public class ClientHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
RpcProtocol rpcProtocol = (RpcProtocol) msg;
- byte[] reqContent = rpcProtocol.getContent();
- String json = new String(reqContent,0,reqContent.length);
- RpcInvocation rpcInvocation = JSON.parseObject(json, RpcInvocation.class);
+ RpcInvocation rpcInvocation = CLIENT_SERIALIZE_FACTORY.deserialize(rpcProtocol.getContent(), RpcInvocation.class);
//通过之前发送的uuid来注入匹配的响应数值
if(!RESP_MAP.containsKey(rpcInvocation.getUuid())){
throw new IllegalArgumentException("server response is error!");
diff --git a/easy-rpc-core/src/main/java/com/shaogezhu/easy/rpc/core/common/RpcInvocation.java b/easy-rpc-core/src/main/java/com/shaogezhu/easy/rpc/core/common/RpcInvocation.java
index a8754e5..347dd3c 100644
--- a/easy-rpc-core/src/main/java/com/shaogezhu/easy/rpc/core/common/RpcInvocation.java
+++ b/easy-rpc-core/src/main/java/com/shaogezhu/easy/rpc/core/common/RpcInvocation.java
@@ -1,5 +1,6 @@
package com.shaogezhu.easy.rpc.core.common;
+import java.io.Serializable;
import java.util.Arrays;
/**
@@ -7,7 +8,10 @@
* @Date 2023/2/24
* @description:
*/
-public class RpcInvocation {
+public class RpcInvocation implements Serializable {
+
+ private static final long serialVersionUID = 2951293262547830249L;
+
/**
* 请求的目标方法, 例如sendData
*/
diff --git a/easy-rpc-core/src/main/java/com/shaogezhu/easy/rpc/core/common/cache/CommonClientCache.java b/easy-rpc-core/src/main/java/com/shaogezhu/easy/rpc/core/common/cache/CommonClientCache.java
index c2e3236..de7a580 100644
--- a/easy-rpc-core/src/main/java/com/shaogezhu/easy/rpc/core/common/cache/CommonClientCache.java
+++ b/easy-rpc-core/src/main/java/com/shaogezhu/easy/rpc/core/common/cache/CommonClientCache.java
@@ -5,6 +5,7 @@
import com.shaogezhu.easy.rpc.core.common.RpcInvocation;
import com.shaogezhu.easy.rpc.core.registy.URL;
import com.shaogezhu.easy.rpc.core.router.Router;
+import com.shaogezhu.easy.rpc.core.serialize.SerializeFactory;
import java.util.*;
import java.util.concurrent.ArrayBlockingQueue;
@@ -38,6 +39,8 @@ public class CommonClientCache {
//每次进行远程调用的时候都是从这里面去选择服务提供者
public static Map SERVICE_ROUTER_MAP = new ConcurrentHashMap<>();
public static ChannelFuturePollingRef CHANNEL_FUTURE_POLLING_REF = new ChannelFuturePollingRef();
-
+ //路由组件
public static Router ROUTER;
+ //客户端序列化工厂
+ public static SerializeFactory CLIENT_SERIALIZE_FACTORY;
}
diff --git a/easy-rpc-core/src/main/java/com/shaogezhu/easy/rpc/core/common/cache/CommonServerCache.java b/easy-rpc-core/src/main/java/com/shaogezhu/easy/rpc/core/common/cache/CommonServerCache.java
index 1659ce5..d5a2c07 100644
--- a/easy-rpc-core/src/main/java/com/shaogezhu/easy/rpc/core/common/cache/CommonServerCache.java
+++ b/easy-rpc-core/src/main/java/com/shaogezhu/easy/rpc/core/common/cache/CommonServerCache.java
@@ -2,6 +2,7 @@
import com.shaogezhu.easy.rpc.core.registy.RegistryService;
import com.shaogezhu.easy.rpc.core.registy.URL;
+import com.shaogezhu.easy.rpc.core.serialize.SerializeFactory;
import java.util.HashMap;
import java.util.HashSet;
@@ -26,4 +27,8 @@ public class CommonServerCache {
* 注册中心:用于服务端 服务的注册url和下线
*/
public static RegistryService REGISTRY_SERVICE;
+ /**
+ * 服务端序列化工厂
+ */
+ public static SerializeFactory SERVER_SERIALIZE_FACTORY;
}
diff --git a/easy-rpc-core/src/main/java/com/shaogezhu/easy/rpc/core/common/config/ClientConfig.java b/easy-rpc-core/src/main/java/com/shaogezhu/easy/rpc/core/common/config/ClientConfig.java
index 60f8840..9283adb 100644
--- a/easy-rpc-core/src/main/java/com/shaogezhu/easy/rpc/core/common/config/ClientConfig.java
+++ b/easy-rpc-core/src/main/java/com/shaogezhu/easy/rpc/core/common/config/ClientConfig.java
@@ -21,6 +21,11 @@ public class ClientConfig {
*/
private String routerStrategy;
+ /**
+ * 客户端序列化方式 example: hessian2,kryo,jdk,fastjson
+ */
+ private String clientSerialize;
+
public String getRegisterAddr() {
return registerAddr;
}
@@ -52,4 +57,12 @@ public String getRouterStrategy() {
public void setRouterStrategy(String routerStrategy) {
this.routerStrategy = routerStrategy;
}
+
+ public String getClientSerialize() {
+ return clientSerialize;
+ }
+
+ public void setClientSerialize(String clientSerialize) {
+ this.clientSerialize = clientSerialize;
+ }
}
diff --git a/easy-rpc-core/src/main/java/com/shaogezhu/easy/rpc/core/common/config/ServerConfig.java b/easy-rpc-core/src/main/java/com/shaogezhu/easy/rpc/core/common/config/ServerConfig.java
index 760e703..3ab8ddf 100644
--- a/easy-rpc-core/src/main/java/com/shaogezhu/easy/rpc/core/common/config/ServerConfig.java
+++ b/easy-rpc-core/src/main/java/com/shaogezhu/easy/rpc/core/common/config/ServerConfig.java
@@ -13,6 +13,11 @@ public class ServerConfig {
private String applicationName;
+ /**
+ * 服务端序列化方式 example: hessian2,kryo,jdk,fastjson
+ */
+ private String serverSerialize;
+
public Integer getPort() {
return port;
}
@@ -36,4 +41,12 @@ public String getApplicationName() {
public void setApplicationName(String applicationName) {
this.applicationName = applicationName;
}
+
+ public String getServerSerialize() {
+ return serverSerialize;
+ }
+
+ public void setServerSerialize(String serverSerialize) {
+ this.serverSerialize = serverSerialize;
+ }
}
diff --git a/easy-rpc-core/src/main/java/com/shaogezhu/easy/rpc/core/common/constants/RpcConstants.java b/easy-rpc-core/src/main/java/com/shaogezhu/easy/rpc/core/common/constants/RpcConstants.java
index 1771e02..d58ff08 100644
--- a/easy-rpc-core/src/main/java/com/shaogezhu/easy/rpc/core/common/constants/RpcConstants.java
+++ b/easy-rpc-core/src/main/java/com/shaogezhu/easy/rpc/core/common/constants/RpcConstants.java
@@ -16,4 +16,12 @@ public class RpcConstants {
public static final String RANDOM_ROUTER_TYPE = "random";
public static final String ROTATE_ROUTER_TYPE = "rotate";
+
+ public static final String JDK_SERIALIZE_TYPE = "jdk";
+
+ public static final String FAST_JSON_SERIALIZE_TYPE = "fastJson";
+
+ public static final String HESSIAN2_SERIALIZE_TYPE = "hessian2";
+
+ public static final String KRYO_SERIALIZE_TYPE = "kryo";
}
diff --git a/easy-rpc-core/src/main/java/com/shaogezhu/easy/rpc/core/serialize/SerializeFactory.java b/easy-rpc-core/src/main/java/com/shaogezhu/easy/rpc/core/serialize/SerializeFactory.java
new file mode 100644
index 0000000..ecd231c
--- /dev/null
+++ b/easy-rpc-core/src/main/java/com/shaogezhu/easy/rpc/core/serialize/SerializeFactory.java
@@ -0,0 +1,29 @@
+package com.shaogezhu.easy.rpc.core.serialize;
+
+/**
+ * @Author peng
+ * @Date 2023/3/4
+ * @description: 序列化的接口
+ */
+public interface SerializeFactory {
+
+
+ /**
+ * 序列化
+ *
+ * @param t
+ * @param
+ * @return
+ */
+ byte[] serialize(T t);
+
+ /**
+ * 反序列化
+ *
+ * @param data
+ * @param clazz
+ * @param
+ * @return
+ */
+ T deserialize(byte[] data, Class clazz);
+}
diff --git a/easy-rpc-core/src/main/java/com/shaogezhu/easy/rpc/core/serialize/fastjson/FastJsonSerializeFactory.java b/easy-rpc-core/src/main/java/com/shaogezhu/easy/rpc/core/serialize/fastjson/FastJsonSerializeFactory.java
new file mode 100644
index 0000000..1cd5353
--- /dev/null
+++ b/easy-rpc-core/src/main/java/com/shaogezhu/easy/rpc/core/serialize/fastjson/FastJsonSerializeFactory.java
@@ -0,0 +1,24 @@
+package com.shaogezhu.easy.rpc.core.serialize.fastjson;
+
+import com.alibaba.fastjson.JSON;
+import com.shaogezhu.easy.rpc.core.serialize.SerializeFactory;
+
+/**
+ * @Author peng
+ * @Date 2023/3/4
+ * @description: FastJson序列化工厂
+ */
+public class FastJsonSerializeFactory implements SerializeFactory {
+
+ @Override
+ public byte[] serialize(T t) {
+ String jsonStr = JSON.toJSONString(t);
+ return jsonStr.getBytes();
+ }
+
+ @Override
+ public T deserialize(byte[] data, Class clazz) {
+ return JSON.parseObject(new String(data),clazz);
+ }
+
+}
diff --git a/easy-rpc-core/src/main/java/com/shaogezhu/easy/rpc/core/serialize/hessian/HessianSerializeFactory.java b/easy-rpc-core/src/main/java/com/shaogezhu/easy/rpc/core/serialize/hessian/HessianSerializeFactory.java
new file mode 100644
index 0000000..5f07f3d
--- /dev/null
+++ b/easy-rpc-core/src/main/java/com/shaogezhu/easy/rpc/core/serialize/hessian/HessianSerializeFactory.java
@@ -0,0 +1,50 @@
+package com.shaogezhu.easy.rpc.core.serialize.hessian;
+
+import com.caucho.hessian.io.Hessian2Input;
+import com.caucho.hessian.io.Hessian2Output;
+import com.shaogezhu.easy.rpc.core.serialize.SerializeFactory;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+
+/**
+ * @Author peng
+ * @Date 2023/3/4
+ * @description: Hessian序列化工厂
+ */
+public class HessianSerializeFactory implements SerializeFactory {
+
+ @Override
+ public byte[] serialize(T t) {
+ byte[] data = null;
+ try {
+ ByteArrayOutputStream os = new ByteArrayOutputStream();
+ Hessian2Output output = new Hessian2Output(os);
+ output.writeObject(t);
+ output.getBytesOutputStream().flush();
+ output.completeMessage();
+ output.close();
+ data = os.toByteArray();
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ return data;
+ }
+
+ @Override
+ public T deserialize(byte[] data, Class clazz) {
+ if (data == null) {
+ return null;
+ }
+ Object result = null;
+ try {
+ ByteArrayInputStream is = new ByteArrayInputStream(data);
+ Hessian2Input input = new Hessian2Input(is);
+ result = input.readObject();
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ return (T) result;
+ }
+
+}
diff --git a/easy-rpc-core/src/main/java/com/shaogezhu/easy/rpc/core/serialize/jdk/JdkSerializeFactory.java b/easy-rpc-core/src/main/java/com/shaogezhu/easy/rpc/core/serialize/jdk/JdkSerializeFactory.java
new file mode 100644
index 0000000..70ade84
--- /dev/null
+++ b/easy-rpc-core/src/main/java/com/shaogezhu/easy/rpc/core/serialize/jdk/JdkSerializeFactory.java
@@ -0,0 +1,43 @@
+package com.shaogezhu.easy.rpc.core.serialize.jdk;
+
+import com.shaogezhu.easy.rpc.core.serialize.SerializeFactory;
+
+import java.io.*;
+
+/**
+ * @Author peng
+ * @Date 2023/3/4
+ * @description: JDK序列化工厂
+ */
+public class JdkSerializeFactory implements SerializeFactory {
+
+
+ @Override
+ public byte[] serialize(T t) {
+ byte[] data = null;
+ try {
+ ByteArrayOutputStream os = new ByteArrayOutputStream();
+ ObjectOutputStream output = new ObjectOutputStream(os);
+ output.writeObject(t);
+ output.flush();
+ output.close();
+ data = os.toByteArray();
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ return data;
+ }
+
+ @Override
+ public T deserialize(byte[] data, Class clazz) {
+ ByteArrayInputStream is = new ByteArrayInputStream(data);
+ try {
+ ObjectInputStream input = new ObjectInputStream(is);
+ Object result = input.readObject();
+ return ((T) result);
+ } catch (IOException | ClassNotFoundException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+}
diff --git a/easy-rpc-core/src/main/java/com/shaogezhu/easy/rpc/core/serialize/kryo/KryoSerializeFactory.java b/easy-rpc-core/src/main/java/com/shaogezhu/easy/rpc/core/serialize/kryo/KryoSerializeFactory.java
new file mode 100644
index 0000000..ba25e5e
--- /dev/null
+++ b/easy-rpc-core/src/main/java/com/shaogezhu/easy/rpc/core/serialize/kryo/KryoSerializeFactory.java
@@ -0,0 +1,55 @@
+package com.shaogezhu.easy.rpc.core.serialize.kryo;
+
+import com.esotericsoftware.kryo.Kryo;
+import com.esotericsoftware.kryo.io.Input;
+import com.esotericsoftware.kryo.io.Output;
+import com.shaogezhu.easy.rpc.core.serialize.SerializeFactory;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+
+/**
+ * @Author peng
+ * @Date 2023/3/4
+ * @description: kryo序列化工厂
+ */
+public class KryoSerializeFactory implements SerializeFactory {
+
+ private final static ThreadLocal kryos = ThreadLocal.withInitial(() -> new Kryo());
+
+ @Override
+ public byte[] serialize(T t) {
+ Output output = null;
+ try {
+ Kryo kryo = kryos.get();
+ ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
+ output = new Output(byteArrayOutputStream);
+ kryo.writeClassAndObject(output, t);
+ return output.toBytes();
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ } finally {
+ if (output != null) {
+ output.close();
+ }
+ }
+ }
+
+ @Override
+ public T deserialize(byte[] data, Class clazz) {
+ Input input = null;
+ try {
+ Kryo kryo = kryos.get();
+ ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(data);
+ input = new Input(byteArrayInputStream);
+ return (T) kryo.readClassAndObject(input);
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ } finally {
+ if (input != null) {
+ input.close();
+ }
+ }
+ }
+
+}
\ No newline at end of file
diff --git a/easy-rpc-core/src/main/java/com/shaogezhu/easy/rpc/core/server/Server.java b/easy-rpc-core/src/main/java/com/shaogezhu/easy/rpc/core/server/Server.java
index 35c71ca..d5a45e1 100644
--- a/easy-rpc-core/src/main/java/com/shaogezhu/easy/rpc/core/server/Server.java
+++ b/easy-rpc-core/src/main/java/com/shaogezhu/easy/rpc/core/server/Server.java
@@ -7,6 +7,10 @@
import com.shaogezhu.easy.rpc.core.common.utils.CommonUtil;
import com.shaogezhu.easy.rpc.core.registy.URL;
import com.shaogezhu.easy.rpc.core.registy.zookeeper.ZookeeperRegister;
+import com.shaogezhu.easy.rpc.core.serialize.fastjson.FastJsonSerializeFactory;
+import com.shaogezhu.easy.rpc.core.serialize.hessian.HessianSerializeFactory;
+import com.shaogezhu.easy.rpc.core.serialize.jdk.JdkSerializeFactory;
+import com.shaogezhu.easy.rpc.core.serialize.kryo.KryoSerializeFactory;
import com.shaogezhu.easy.rpc.core.server.impl.DataServiceImpl;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelInitializer;
@@ -16,6 +20,7 @@
import io.netty.channel.socket.nio.NioServerSocketChannel;
import static com.shaogezhu.easy.rpc.core.common.cache.CommonServerCache.*;
+import static com.shaogezhu.easy.rpc.core.common.constants.RpcConstants.*;
/**
* @Author peng
@@ -54,6 +59,26 @@ protected void initChannel(SocketChannel ch) throws Exception {
}
});
+
+ //初始化序列化器
+ String serverSerialize = serverConfig.getServerSerialize();
+ switch (serverSerialize) {
+ case JDK_SERIALIZE_TYPE:
+ SERVER_SERIALIZE_FACTORY = new JdkSerializeFactory();
+ break;
+ case FAST_JSON_SERIALIZE_TYPE:
+ SERVER_SERIALIZE_FACTORY = new FastJsonSerializeFactory();
+ break;
+ case HESSIAN2_SERIALIZE_TYPE:
+ SERVER_SERIALIZE_FACTORY = new HessianSerializeFactory();
+ break;
+ case KRYO_SERIALIZE_TYPE:
+ SERVER_SERIALIZE_FACTORY = new KryoSerializeFactory();
+ break;
+ default:
+ throw new RuntimeException("no match serialize type for" + serverSerialize);
+ }
+
this.batchExportUrl();
bootstrap.bind(serverConfig.getPort()).sync();
System.out.println("========== Server start success ==========");
@@ -64,6 +89,7 @@ public void initServerConfig() {
serverConfig.setPort(8010);
serverConfig.setRegisterAddr("localhost:2181");
serverConfig.setApplicationName("easy-rpc-server");
+ serverConfig.setServerSerialize("kryo");
this.setServerConfig(serverConfig);
}
diff --git a/easy-rpc-core/src/main/java/com/shaogezhu/easy/rpc/core/server/ServerHandler.java b/easy-rpc-core/src/main/java/com/shaogezhu/easy/rpc/core/server/ServerHandler.java
index 2242fe7..d025455 100644
--- a/easy-rpc-core/src/main/java/com/shaogezhu/easy/rpc/core/server/ServerHandler.java
+++ b/easy-rpc-core/src/main/java/com/shaogezhu/easy/rpc/core/server/ServerHandler.java
@@ -1,6 +1,5 @@
package com.shaogezhu.easy.rpc.core.server;
-import com.alibaba.fastjson.JSON;
import com.shaogezhu.easy.rpc.core.common.RpcInvocation;
import com.shaogezhu.easy.rpc.core.common.RpcProtocol;
import io.netty.channel.Channel;
@@ -10,6 +9,7 @@
import java.lang.reflect.Method;
import static com.shaogezhu.easy.rpc.core.common.cache.CommonServerCache.PROVIDER_CLASS_MAP;
+import static com.shaogezhu.easy.rpc.core.common.cache.CommonServerCache.SERVER_SERIALIZE_FACTORY;
/**
* @Author peng
@@ -20,8 +20,7 @@ public class ServerHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
RpcProtocol rpcProtocol = (RpcProtocol) msg;
- String json = new String(rpcProtocol.getContent(), 0, rpcProtocol.getContentLength());
- RpcInvocation rpcInvocation = JSON.parseObject(json, RpcInvocation.class);
+ RpcInvocation rpcInvocation = SERVER_SERIALIZE_FACTORY.deserialize(rpcProtocol.getContent(), RpcInvocation.class);
Object aimObject = PROVIDER_CLASS_MAP.get(rpcInvocation.getTargetServiceName());
Method[] methods = aimObject.getClass().getDeclaredMethods();
Object result = null;
@@ -37,7 +36,8 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
}
}
rpcInvocation.setResponse(result);
- RpcProtocol respRpcProtocol = new RpcProtocol(JSON.toJSONString(rpcInvocation).getBytes());
+ byte[] serialize = SERVER_SERIALIZE_FACTORY.serialize(rpcInvocation);
+ RpcProtocol respRpcProtocol = new RpcProtocol(serialize);
ctx.writeAndFlush(respRpcProtocol);
}