分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 教程案例

netty4与netty5序列化问题记录

发布时间:2023-09-06 01:44责任编辑:胡小海关键词:暂无标签

两个版本,序列化问题疑惑

在netty4上,使用序列化

Netty4.x实战(二) 对象传输 - 程序园
http://www.voidcn.com/article/p-hwrhqscn-bau.html

源码下载:

stevenlii/Socket_Netty
https://github.com/stevenlii/Socket_Netty

其中关键代码:

server.java

package bhz.netty.serial;import io.netty.bootstrap.ServerBootstrap;import io.netty.channel.ChannelFuture;import io.netty.channel.ChannelInitializer;import io.netty.channel.ChannelOption;import io.netty.channel.EventLoopGroup;import io.netty.channel.nio.NioEventLoopGroup;import io.netty.channel.socket.SocketChannel;import io.netty.channel.socket.nio.NioServerSocketChannel;import io.netty.handler.codec.serialization.ClassResolvers;import io.netty.handler.codec.serialization.ObjectDecoder;import io.netty.handler.codec.serialization.ObjectEncoder;import io.netty.handler.logging.LogLevel;import io.netty.handler.logging.LoggingHandler;public class Server { ???public static void main(String[] args) throws Exception{ ???????????????EventLoopGroup pGroup = new NioEventLoopGroup(); ???????EventLoopGroup cGroup = new NioEventLoopGroup(); ???????????????ServerBootstrap b = new ServerBootstrap(); ???????b.group(pGroup, cGroup) ????????.channel(NioServerSocketChannel.class) ????????.option(ChannelOption.SO_BACKLOG, 1024) ????????//设置日志 ????????.handler(new LoggingHandler(LogLevel.INFO)) ????????.childHandler(new ChannelInitializer<SocketChannel>() { ???????????protected void initChannel(SocketChannel sc) throws Exception { ???????????????sc.pipeline().addLast(new ObjectDecoder(Integer.MAX_VALUE, ClassResolvers.cacheDisabled(null))); ???????????????sc.pipeline().addLast(new ObjectEncoder()); ???????????????sc.pipeline().addLast(new ServerHandler()); ???????????} ???????}); ???????????????ChannelFuture cf = b.bind(8765).sync(); ???????????????cf.channel().closeFuture().sync(); ???????pGroup.shutdownGracefully(); ???????cGroup.shutdownGracefully(); ???????????}}

使用如下方式序列化

sc.pipeline().addLast(new ObjectDecoder(Integer.MAX_VALUE, ClassResolvers.cacheDisabled(null))); ???????????????sc.pipeline().addLast(new ObjectEncoder());

而在netty5上,使用如下方式

sc.pipeline().addLast(MarshallingCodeCFactory.buildMarshallingDecoder()); ???????????????sc.pipeline().addLast(MarshallingCodeCFactory.buildMarshallingEncoder());

其中引用的是(https://github.com/stevenlii/Socket_Netty/blob/master/pom.xml)

<!-- https://mvnrepository.com/artifact/org.jboss.marshalling/jboss-marshalling --> ???????<dependency> ???????????<groupId>org.jboss.marshalling</groupId> ???????????<artifactId>jboss-marshalling</artifactId> ???????????<version>2.0.4.Final</version> ???????</dependency> ???????<!-- https://mvnrepository.com/artifact/org.jboss.marshalling/jboss-marshalling-serial --> ???????<dependency> ???????????<groupId>org.jboss.marshalling</groupId> ???????????<artifactId>jboss-marshalling-serial</artifactId> ???????????<version>2.0.4.Final</version> ???????????<scope>test</scope> ???????</dependency>

详细方式在

基于netty的Marshalling序列化框架简单实现 - 简书
https://www.jianshu.com/p/bacdc610f557

也可以找到

疑惑:

如果在4上使用5的配置方式,则无法收到消息,非常奇怪。

 


netty4与netty5序列化问题记录

原文地址:https://www.cnblogs.com/stevenlii/p/8523663.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved