Netty http客户端无法接收到服务器的响应信息

这是我的客户端的Handler代码

class RobotHandler : ChannelInboundHandlerAdapter() {

    private val logger = LoggerFactory.getLogger(this.javaClass)

    override fun channelRead(ctx: ChannelHandlerContext?, msg: Any?) {

        logger.info(msg?.toString())
    }

    override fun channelActive(ctx: ChannelHandlerContext?) {
        val request = DefaultHttpRequest(HttpVersion.HTTP_1_0, HttpMethod.GET, "/robot")

        ctx?.writeAndFlush(request)
    }

    override fun exceptionCaught(ctx: ChannelHandlerContext?, cause: Throwable?) {
        cause?.printStackTrace()
        ctx?.close()
    }
}

这是客户端的代码

@Component
@Order(1)
class RobotNettyClient : ApplicationRunner{

    private val logger = LoggerFactory.getLogger(this.javaClass)

    override fun run(args: ApplicationArguments?) {
        val group = NioEventLoopGroup()

        try {
            val bootstrap = Bootstrap()
            bootstrap.group(group)
                    .channel(NioSocketChannel::class.java)
                    .option(ChannelOption.SO_KEEPALIVE, true)
                    .handler(object: ChannelInitializer<SocketChannel>() {
                        override fun initChannel(ch: SocketChannel?) {
                            val pipeline = ch?.pipeline()
                            pipeline?.addLast(HttpClientCodec())
                            pipeline?.addLast(HttpContentCompressor())
                            pipeline?.addLast(RobotHandler())
                        }
                    })
            logger.info("Netty started")
            bootstrap.connect("127.0.0.1",8080).sync()
        } finally {
            group.shutdownGracefully()
        }
    }
}

我的服务器选用的是Spring boot,像在内部通过Netty实现数据不断读取和发送响应,然后获取到数据。可是我通过Netty向Spring boot发送响应过后,触发Controller但是无法接收到返回的消息,请问怎么解决

阅读 2.5k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏