1 方案一
1.1 niurenbang
1.1.1 http协议控制沾包拆包
pipeline.addLast(new HttpRequestDecoder()); // 客户端发送的是httpResponse,所以要使用HttpResponseEncoder进行编码 pipeline.addLast(new HttpResponseEncoder()); pipeline.addLast(new HttpObjectAggregator(65535)); pipeline.addLast(new HttpBodyDecoder()); pipeline.addLast(new HttpBodyEncoder()); // 业务处理Handler pipeline.addLast("httpServerHandler", handler);
1.1.2 最外层协议解析
public class HttpBodyDecoder extends SimpleChannelInboundHandler<FullHttpRequest> { private static final Logger logger = LoggerFactory.getLogger(HttpBodyDecoder.class); @Override protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception { if (HttpMethod.POST.equals(request.method())) { String uri = request.uri(); ByteBuf content = request.content(); try { byte[] bs = new byte[content.readableBytes()]; content.readBytes(bs); SimulateMsg simulateMsg = SimulateMsg.parseFrom(bs); ctx.pipeline().addBefore("httpServerHandler", "protobufEncoder", new ProtobufEncoder()); Attribute<String> attr = ctx.channel().attr(AttributeMapConstant.NETTY_CHANNEL_KEY_HTTP_REQ_URI); attr.setIfAbsent(uri); super.channelRead(ctx, simulateMsg);