Java netty服务器接收两个http内容,但它们应该合并为一个
上面是日志。从日志中,我们可以看到客户端发送的seqNo都是497,并且它只发送了一次。但是从用netty构建的服务器上,我们收到了两个http内容。因此,它们的长度与标题中的内容长度不一致。但两个内容长度9+20=29,应合并为一个 下面是我的服务器处理程序代码,有人能帮我吗Java netty服务器接收两个http内容,但它们应该合并为一个,java,netty,Java,Netty,上面是日志。从日志中,我们可以看到客户端发送的seqNo都是497,并且它只发送了一次。但是从用netty构建的服务器上,我们收到了两个http内容。因此,它们的长度与标题中的内容长度不一致。但两个内容长度9+20=29,应合并为一个 下面是我的服务器处理程序代码,有人能帮我吗 12:19:22.737 [nioEventLoopGroup-3-10] ERROR ********************************* - get http content failed, expec
12:19:22.737 [nioEventLoopGroup-3-10] ERROR ********************************* - get http content failed, expect 29, actual 9
12:19:22.737 [nioEventLoopGroup-3-10] ERROR ********************************* - converty request DefaultHttpRequest(decodeResult: success, version: HTTP/1.1)
POST /rpc/gdt.marvel.MarvelIncreService.FetchData HTTP/1.1
Content-Type: application/x-protobuf
Rpc-SeqNo: 497
Content-Length: 29 failed
12:19:22.738 [nioEventLoopGroup-3-10] ERROR ********************************* - get http content failed, expect 29, actual 20
12:19:22.738 [nioEventLoopGroup-3-10] ERROR ********************************* - converty request DefaultHttpRequest(decodeResult: success, version: HTTP/1.1)
POST /rpc/gdt.marvel.MarvelIncreService.FetchData HTTP/1.1
Content-Type: application/x-protobuf
Rpc-SeqNo: 497
Content-Length: 29 failed
基本上,我们使用nettydo实现了一个httprpc服务器,实现非常简单,但是从日志中,我们看到了有线的东西。谢谢你的帮助
如下所示,我已经使用http编解码器来处理http协议,它在netty.*.http中提供
60 public class RpcNettyServerHandler extends ChannelInboundHandlerAdapter {
61
-----
92 @Override
93 public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
94 LOG.error("Exception happend on netty http handler {}", cause.toString());
95 cause.printStackTrace();
96 ctx.close();
97 }
98
99 @Override
100 public void channelReadComplete(ChannelHandlerContext ctx) {
101 ctx.flush();
102 }
103
104 @Override
105 public void channelRead(ChannelHandlerContext ctx, Object msg) {
106 if (msg instanceof HttpRequest) {
---
140 if (msg instanceof HttpContent) {
141 HttpContent httpContent = (HttpContent) msg;
142 if (this.httpMethod == POST) {
143 handleRpc(ctx, httpRequest, httpContent);
144 } else if (this.httpMethod == GET) {
145 handleForm(httpRequest, httpContent);
146 }
147 }
148 }
8导入io.netty.channel.ChannelInitializer;
9导入io.netty.channel.channel管道;
10导入io.netty.channel.socket.SocketChannel;
11导入io.netty.handler.codec.http.HttpServerCodec;
12导入io.netty.handler.ssl.SslContext;
13
14公共类RpcNettyServerInitializer扩展
15信道初始值设定项{
16
17私人RPChandlerRegistry MPL handlerRegistry;
18
19公共RpcNettyServerInitializer(RpcHandlerRegistryImpl handlerRegistry){
20 this.handlerRegistry=handlerRegistry;
21 }
22
23@Override
24公共频道(SocketChannel ch){
25通道管道p=通道管道();
26 p.addLast(新的HttpServerCodec());
27 p.addLast(新的RpcNettyServerHandler(this.handlerRegistry));
28 }
29
30 }
您应该使用HttpObjectAggregator将多个内容(区块)放入一个响应或请求中。当然,您也应该使用http编解码器、客户机或服务器,具体取决于您的方
编辑:如果不使用聚合器,则必须自己处理多个块(httpContent)
在您的情况下,可能有2个块,但在加载所有块之前尝试处理请求
管道示例:
8 import io.netty.channel.ChannelInitializer;
9 import io.netty.channel.ChannelPipeline;
10 import io.netty.channel.socket.SocketChannel;
11 import io.netty.handler.codec.http.HttpServerCodec;
12 import io.netty.handler.ssl.SslContext;
13
14 public class RpcNettyServerInitializer extends
15 ChannelInitializer<SocketChannel> {
16
17 private RpcHandlerRegistryImpl handlerRegistry;
18
19 public RpcNettyServerInitializer(RpcHandlerRegistryImpl handlerRegistry) {
20 this.handlerRegistry = handlerRegistry;
21 }
22
23 @Override
24 public void initChannel(SocketChannel ch) {
25 ChannelPipeline p = ch.pipeline();
26 p.addLast(new HttpServerCodec());
27 p.addLast(new RpcNettyServerHandler(this.handlerRegistry));
28 }
29
30 }
你的假设是错误的。没有什么规定应该一次交付。我该如何处理这种情况?请帮助在您的管道中添加聚合器:请参阅我编辑的答案中的示例…您并没有显示您的管道。。。那么聚合器呢?如果您没有,您必须在任何handleRpc或handleForm之前处理多个httpContent。很抱歉,没有看到您的管道。。。你只是错过了聚合器。。。添加它,它将解决您的问题。
p.addLast(new HttpServerCodec());
p.addLast("aggregator", new HttpObjectAggregator(1048576));
p.addLast(new YourHandler());