gpt4 book ai didi

java - netty 服务器接收两个 http 内容,但它们应该合并为一个

转载 作者:行者123 更新时间:2023-12-02 04:28:39 26 4
gpt4 key购买 nike

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

以上是日志。从Log中我们可以看到客户端发送的seqNo都是497,而且确实发送过一次。但是从用netty搭建的Server中,我们收到了两个http内容。因此,它们的长度与 header 中的 Content-Length 不一致。但两个内容长度 9 + 20 = 29,应该合并为一个。

以下是我的服务器处理程序代码,有人可以帮助我吗?

 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 }

基本上,我们使用netty来做实现一个http rpc服务器,实现很简单,但是从日志中,我们看到了有线的东西。感谢您的帮助

如下,我已经使用http编解码器来处理http协议(protocol),这是在netty.*.http中提供的

 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 }

最佳答案

您应该使用 HttpObjectAggregator 以便将多个内容( block )放入一个响应或请求中。当然,您还应该使用 http 编解码器、客户端或服务器,具体取决于您这边...

编辑:如果您不使用聚合器,则必须自己处理多个 block (httpContent)。

就您的情况而言,您可能有 2 个 block ,但尝试在加载所有 block 之前处理请求...

管道示例:

p.addLast(new HttpServerCodec());
p.addLast("aggregator", new HttpObjectAggregator(1048576));
p.addLast(new YourHandler());

关于java - netty 服务器接收两个 http 内容,但它们应该合并为一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31823446/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com