>>分享流行的Java框架以及开源软件,对孙卫琴的《精通Spring》、《Spring Cloud Alibaba微服务开发零基础入门到实操》提供技术支持 书籍支持  卫琴直播  品书摘要  在线测试  资源下载  联系我们
发表一个新主题 开启一个新投票 回复文章 您是本文章第 22853 个阅读者 刷新本主题
 * 贴子主题:  Marshalling在Netty中的使用 回复文章 点赞(0)  收藏  
作者:mary    发表时间:2021-06-24 03:45:15     消息  查看  搜索  好友  邮件  复制  引用

1. 引入jar包

  <dependency>

<groupId>org.jboss.marshalling</groupId>

<artifactId>jboss-marshalling</artifactId>

<version>1.4.10.Final</version>

</dependency>

<dependency>

<groupId>org.jboss.marshalling</groupId>

<artifactId>jboss-marshalling-serial</artifactId>

<version>1.4.10.Final</version>

</dependency>

2.编写编码解码程序

public final class MarshallingCodeCFactory {

    /**
     * 创建Jboss Marshalling解码器MarshallingDecoder
     * @return MarshallingDecoder
     */

    public static MarshallingDecoder buildMarshallingDecoder() {
        //首先通过Marshalling工具类的精通方法获取Marshalling实例对象 参数serial标识创建的是java序列化工厂对象。
        final MarshallerFactory marshallerFactory = Marshalling.getProvidedMarshallerFactory("serial");
        //创建了MarshallingConfiguration对象,配置了版本号为5
        final MarshallingConfiguration configuration = new MarshallingConfiguration();
        configuration.setVersion(5);
        //根据marshallerFactory和configuration创建provider
        UnmarshallerProvider provider = new DefaultUnmarshallerProvider(marshallerFactory, configuration);
        //构建Netty的MarshallingDecoder对象,俩个参数分别为provider和单个消息序列化后的最大长度
        MarshallingDecoder decoder = new MarshallingDecoder(provider, 1024 * 1024 * 1);
        return decoder;
    }

    /**
     * 创建Jboss Marshalling编码器MarshallingEncoder
     * @return MarshallingEncoder
     */

    public static MarshallingEncoder buildMarshallingEncoder() {
        final MarshallerFactory marshallerFactory = Marshalling.getProvidedMarshallerFactory("serial");
        final MarshallingConfiguration configuration = new MarshallingConfiguration();
        configuration.setVersion(5);
        MarshallerProvider provider = new DefaultMarshallerProvider(marshallerFactory, configuration);
        //构建Netty的MarshallingEncoder对象,MarshallingEncoder用于实现序列化接口的POJO对象序列化为二进制数组
        MarshallingEncoder encoder = new MarshallingEncoder(provider);
        return encoder;
    }

}

3.使用

socketChannel.pipeline().addLast(MarshallingCodeCFactory.buildMarshallingDecoder());

socketChannel.pipeline().addLast(MarshallingCodeCFactory.buildMarshallingEncoder());

4.注意事项

  要传输的对象必须实现原生的序列化
public class RpcRequest  implements Serializable { }

  ----------------------------
原文链接:https://www.jianshu.com/p/ab9fb15800a5

程序猿的技术大观园:www.javathinker.net



[这个贴子最后由 flybird 在 2021-06-24 11:08:27 重新编辑]
  Java面向对象编程-->Swing组件(下)
  JavaWeb开发-->异常处理
  JSP与Hibernate开发-->使用Session(Ⅱ)
  Java网络编程-->Web运作原理(Ⅲ)
  精通Spring-->
  Vue3开发-->立即检索和延迟检索策略
  超全Redis命令总结,墙裂建议收藏,说不定就用上了呢
  从零开始手写 spring ioc 框架,深入学习 spring 源码
  阿里巴巴为什么能抗住90秒100亿?看完这篇你就明白了!
  微服务的拆分方式
  Spring MVC服务器端推送的两种方式
  回字有四种写法,那你知道单例有五种写法吗
  阿里JAVA面试题剖析:Redis 和 memcached 有什么区别?
  Spring Boot和Feign中使用Java 8时间日期API(LocalDate等)...
  Spring Cloud与Dubbo的完美融合之手「Spring Cloud Alibaba
  Spring Cloud 技术栈及架构一览
  Spring Boot集成EHCache实现缓存机制
  Spring+JPA+ehcache开启二级本地缓存
  网红框架SpringBoot2.x之定制参数浅析
  mybatis-plus 注解实现多表关联查询的最佳实践
  Hibernate拦截器与监听器
  更多...
 IPIP: 已设置保密
楼主      
1页 0条记录 当前第1
发表一个新主题 开启一个新投票 回复文章


中文版权所有: JavaThinker技术网站 Copyright 2016-2026 沪ICP备16029593号-2
荟萃Java程序员智慧的结晶,分享交流Java前沿技术。  联系我们
如有技术文章涉及侵权,请与本站管理员联系。