当前位置:   article > 正文

Netty之Marshalling编解码_netty 无法解析符号 'io

netty 无法解析符号 'io

Netty主流的编解码器:

①JBoss的Marshalling包

②google的Protobuf

③基于Protobuf的Kyro

Apache的Thrift

 本篇博客主要介绍Netty基于Marshalling的最简单的代码实践

实现引入依赖

  1. <dependency>
  2. <groupId>io.netty</groupId>
  3. <artifactId>netty-all</artifactId>
  4. <version>4.1.12.Final</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.jboss.marshalling</groupId>
  8. <artifactId>jboss-marshalling-serial</artifactId>
  9. <version>2.0.0.Beta2</version>
  10. </dependency>

MarshallingCodeFactory

  1. package com.yj.service.codec;
  2. import org.jboss.marshalling.MarshallerFactory;
  3. import org.jboss.marshalling.Marshalling;
  4. import org.jboss.marshalling.MarshallingConfiguration;
  5. import io.netty.handler.codec.marshalling.DefaultMarshallerProvider;
  6. import io.netty.handler.codec.marshalling.DefaultUnmarshallerProvider;
  7. import io.netty.handler.codec.marshalling.MarshallerProvider;
  8. import io.netty.handler.codec.marshalling.MarshallingDecoder;
  9. import io.netty.handler.codec.marshalling.MarshallingEncoder;
  10. import io.netty.handler.codec.marshalling.UnmarshallerProvider;
  11. public class MarshallingCodeFactory {
  12. /**
  13. * 创建Marshalling解码器MarshallingDecoder
  14. *
  15. * @return
  16. */
  17. public static MarshallingDecoder buildMarshallingDecoder() {
  18. MarshallerFactory factory = Marshalling.getProvidedMarshallerFactory("serial");
  19. MarshallingConfiguration configuration = new MarshallingConfiguration();
  20. configuration.setVersion(5);
  21. UnmarshallerProvider provider = new DefaultUnmarshallerProvider(factory, configuration);
  22. MarshallingDecoder decoder = new MarshallingDecoder(provider);
  23. return decoder;
  24. }
  25. /**
  26. * 创建MarshallingEncoder
  27. *
  28. * @return
  29. */
  30. public static MarshallingEncoder buildMarshallingEncoder() {
  31. // 这里表示的是支持java serial对象的序列化。所以我们传输的对象要实现Serializable接口
  32. MarshallerFactory factory = Marshalling.getProvidedMarshallerFactory("serial");
  33. MarshallingConfiguration configuration = new MarshallingConfiguration();
  34. configuration.setVersion(5);
  35. MarshallerProvider provider = new DefaultMarshallerProvider(factory, configuration);
  36. MarshallingEncoder encoder = new MarshallingEncoder(provider);
  37. return encoder;
  38. }
  39. }

客户端,服务端分别添加编解码器

  1. pipeline.addLast(MarshallingCodeFactory.buildMarshallingEncoder());
  2. pipeline.addLast(MarshallingCodeFactory.buildMarshallingDecoder());

 请求,响应实体都需要实现Serializable接口进行序列化

Req,Resp

  1. package com.yj.entity;
  2. import java.io.Serializable;
  3. import lombok.Data;
  4. import lombok.ToString;
  5. @Data
  6. @ToString
  7. public class Req implements Serializable {
  8. private static final long serialVersionUID = -1465519266979187295L;
  9. private String id;
  10. private String name;
  11. }
  1. package com.yj.entity;
  2. import java.io.Serializable;
  3. import lombok.Data;
  4. import lombok.ToString;
  5. @Data
  6. @ToString
  7. public class Resp implements Serializable{
  8. private static final long serialVersionUID = -1450604945238500417L;
  9. private String id;
  10. private String name;
  11. }

客户端handler

  1. package com.yj.service.codec;
  2. import com.yj.entity.Req;
  3. import com.yj.entity.Resp;
  4. import io.netty.channel.ChannelFuture;
  5. import io.netty.channel.ChannelFutureListener;
  6. import io.netty.channel.ChannelHandlerContext;
  7. import io.netty.channel.ChannelInboundHandlerAdapter;
  8. public class CodecClientHandler extends ChannelInboundHandlerAdapter {
  9. @Override
  10. public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
  11. super.exceptionCaught(ctx, cause);
  12. }
  13. @Override
  14. public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
  15. Resp resp = (Resp) msg;
  16. System.out.println("接收到服务端的响应:" + resp);
  17. }
  18. @Override
  19. public void channelActive(ChannelHandlerContext ctx) throws Exception {
  20. Req req = new Req();
  21. req.setId("1");
  22. req.setName("我的请求");
  23. ctx.writeAndFlush(req).addListener(new ChannelFutureListener() {
  24. public void operationComplete(ChannelFuture future) throws Exception {
  25. if (future.isSuccess()) {
  26. System.out.println("成功发送到服务端消息");
  27. } else {
  28. System.out.println("失败服务端消息失败:" + future.cause().getMessage());
  29. future.cause().printStackTrace();
  30. }
  31. }
  32. });
  33. }
  34. }

服务端handler

  1. package com.yj.service.codec;
  2. import com.yj.entity.Req;
  3. import com.yj.entity.Resp;
  4. import io.netty.channel.ChannelHandlerContext;
  5. import io.netty.channel.ChannelInboundHandlerAdapter;
  6. public class CodecServerHandler extends ChannelInboundHandlerAdapter {
  7. @Override
  8. public void channelRead(ChannelHandlerContext ctx, Object msg) {
  9. Req req = (Req) msg;
  10. System.out.println("接收到客户端的请求:" + req);
  11. }
  12. @Override
  13. public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
  14. Resp resp = new Resp();
  15. resp.setId("1");
  16. resp.setName("我的响应");
  17. ctx.writeAndFlush(resp);
  18. }
  19. }

分别启动服务端,客户端,观察输出结果

  1. 客户端输出:
  2. 接收到服务端的响应:Resp(id=1, name=我的响应)
  3. 服务端输出:
  4. 接收到客户端的请求:Req(id=1, name=我的请求)

服务端,客户端的Req和Resp实体似乎要位于相同的package下,否则解析会有异常

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/252803?site
推荐阅读
相关标签
  

闽ICP备14008679号