netty客户端在请求永远不会完成的情况下重用服务器端引导结果

时间:2018-06-14 04:16:44

标签: netty

Netty服务器为其收到的每个请求请求另一台服务器作为客户端。几乎像代理一样,除了它没有按原样转发收到的消息。我尝试使用服务器ChannelHandlerContext来创建客户端,但请求永远不会完成。

在最后一个双工处理程序中,如果test,客户端会收到响应,否则请求就会出现问题。

服务器最后一个处理程序:

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    log.info("receive: {}", msg);
    if (!test) {
        HttpClient.clientSend(ctx);
    } else {
        FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1,
            io.netty.handler.codec.http.HttpResponseStatus.OK,
            Unpooled.wrappedBuffer(CONTENT));
        response.headers().set(CONTENT_TYPE, "text/plain");
        response.headers().setInt(CONTENT_LENGTH, response.content().readableBytes());
        ctx.channel().writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
    }
}

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
    log.info("write: {}", msg);
    ctx.write(msg, promise); // checked, it is success. but client never receives.
}

客户端:

public static void clientSend(ChannelHandlerContext ctx) {
    final Channel inboundChannel = ctx.channel();
    try {
        Bootstrap b = new Bootstrap();
        b.group(inboundChannel.eventLoop())
            .channel(inboundChannel.getClass())
            .handler(new ClientChannelInitializer(inboundChannel));

        // Make the connection attempt.
        ChannelFuture f = b.connect(host,port);
        Channel outboundChannel = f.channel();
        f.addListener(new ChannelFutureListener() {
              @Override
              public void operationComplete(ChannelFuture future) throws Exception {
                  if (future.isSuccess()) {
                      // Prepare the HTTP request.
                      HttpRequest request =
                          new DefaultFullHttpRequest(
                              HttpVersion.HTTP_1_1, HttpMethod.POST, "targets", Unpooled.copiedBuffer(requestBody,
                              CharsetUtil.UTF_8));
                      // Send the HTTP request.
                      ChannelFuture wf = outboundChannel.writeAndFlush(request);
                      wf.addListener(new ChannelFutureListener() {
                          @Override
                          public void operationComplete(ChannelFuture future) throws Exception {
                              if (future.isSuccess()) {
                                  log.info("write success");
                              } else {
                                  log.info("write failed ", future.cause());
                              }
                          }
                      });
                      log.info("channel listener connected");
                  } else {
                      log.info("channel listener not connected");
                  }
              }
          }
        );
    } 

调用write后,我可以看到msg是正确的,ctx.write成功了,但邮递员给出了: postman

1 个答案:

答案 0 :(得分:0)

对我来说,您似乎错过了设置与您的有效负载的字节数相匹配的“Content-Length”标头。添加它,它应该没问题。