在用netty进行pojo的传输,如果是两个不同的程序进程,如何做到更好的编码解码?
现在
//server
ServerBootstrap sb = new ServerBootstrap();
sb.group(group) // 绑定线程池
.channel(NioServerSocketChannel.class) // 指定使用的channel
.localAddress(this.port)// 绑定监听端口
.childHandler(new ChannelInitializer<SocketChannel>() { // 绑定客户端连接时候触发操作
@Override
protected void initChannel(SocketChannel ch) throws Exception {
System.out.println("connected...; Client:" + ch.remoteAddress());
ch.pipeline().addLast(
new ObjectEncoder(),
new ObjectDecoder(1024 * 1024, ClassResolvers.weakCachingConcurrentResolver(this.getClass().getClassLoader())),
new EchoServerHandler()
); // 客户端触发操作
}
});
//client
ch.pipeline().addLast(
new ObjectEncoder(),
new ObjectDecoder(1024*1024,ClassResolvers.weakCachingConcurrentResolver(this.getClass().getClassLoader())),
new EchoClientHandler()
);
在同一个程序里是可以进行pojo传输的,因为依赖共同的class
编码解码也都正常
this.getClass().getClassLoader()
但当server和client分别在不同的项目里,就会出现:
Caused by: java.lang.ClassNotFoundException
client 发送过去的数据,server找不到类进行解码?
这个如何解决?
server端需要包含client端使用到的类,俗称二方包