Netty剖析之Http服务案例

怼烎@ 2023-07-03 06:15 114阅读 0赞

前言

前面我们讲了Netty的线程模型,还有一些Netty的特性,以及使用Netty编写过一个Tcp服务案例,本次呢,我们将结合前面所学的知识,来编写一个Http服务案例

Http服务案例

需求:使用Netty编写一个Http服务端,可以让浏览器正常访问,并且服务端可以返回信息给浏览器。

服务端代码基本与前面的TCP服务一致,只是在pipeline管道里额外加入了处理http的编码、解码器(HttpServerCodec),自定义的handler不在继承ChannelInboundHandlerAdapter,而是继承SimpleChannelInboundHandler,详见如下代码:

  1. public class HttpServer {
  2. public static void main(String[] args) throws Exception{
  3. EventLoopGroup bossGroup = new NioEventLoopGroup();
  4. EventLoopGroup workerGroup = new NioEventLoopGroup();
  5. try {
  6. ServerBootstrap serverBootstrap = new ServerBootstrap();
  7. serverBootstrap.group(bossGroup,workerGroup)
  8. .channel(NioServerSocketChannel.class)
  9. .childHandler(new ChannelInitializer<SocketChannel>() {
  10. @Override
  11. protected void initChannel(SocketChannel socketChannel) throws Exception {
  12. // 获取管道对象
  13. ChannelPipeline pipeline = socketChannel.pipeline();
  14. // 添加netty提供的处理http的编码、解码器
  15. pipeline.addLast(new HttpServerCodec());
  16. // 加入自定义的handler
  17. pipeline.addLast(new HttpServerHandler());
  18. }
  19. });
  20. ChannelFuture channelFuture = serverBootstrap.bind(8899).sync();
  21. channelFuture.channel().closeFuture().sync();
  22. } finally {
  23. bossGroup.shutdownGracefully();
  24. workerGroup.shutdownGracefully();
  25. }
  26. }
  27. }
  28. public class HttpServerHandler extends SimpleChannelInboundHandler<HttpObject> {
  29. @Override
  30. protected void channelRead0(ChannelHandlerContext channelHandlerContext, HttpObject httpObject) throws Exception {
  31. // 判断是不是Http请求
  32. if(httpObject instanceof HttpRequest){
  33. // 得到http请求
  34. HttpRequest httpRequest = (HttpRequest) httpObject;
  35. // 请求地址信息
  36. URI uri = new URI(httpRequest.uri());
  37. System.out.println("request url :" + uri.getPath());
  38. ByteBuf content = Unpooled.copiedBuffer("hello client", CharsetUtil.UTF_8);
  39. // 创建http响应对象
  40. DefaultFullHttpResponse httpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, content);
  41. // 设置响应头
  42. httpResponse.headers().set(HttpHeaderNames.CONTENT_TYPE,"text/plan");
  43. httpResponse.headers().set(HttpHeaderNames.CONTENT_LENGTH, content.readableBytes());
  44. channelHandlerContext.writeAndFlush(httpResponse);
  45. }
  46. }
  47. }

浏览器访问测试:
在这里插入图片描述
可以看到,浏览器请求成功了,并且收到了服务端响应的信息。

发表评论

表情:
评论列表 (有 0 条评论,114人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Netty剖析Http服务案例

    前言 前面我们讲了Netty的线程模型,还有一些Netty的特性,以及使用Netty编写过一个Tcp服务案例,本次呢,我们将结合前面所学的知识,来编写一个Http服务案例

    相关 Netty剖析NIO

    什么是NIO? NIO(non blocking IO),是JDK提供的新API。从JDK1.4开始,Java提供了一系列改进的输入/输出的新特性,被统称为 NIO(即

    相关 Netty剖析BIO

    什么是BIO? BIO(blocking I/O),同步阻塞IO,服务器实现模式为一个线程处理一个连接,即每当有一个客户端连接时,就会启动一个独立的线程来进行处理,如果这

    相关 Netty剖析IO

    什么是IO? 所谓IO即`input`和`output`的缩写,是对数据流入和流出的抽象概念 IO简单的理解就是用什么样的通道进行数据的发送和接收,很大程度上

    相关 Netty剖析Netty介绍

    什么是Netty? Netty是由Jboss提供的一个Java开源框架,现为GitHub上的独立项目; Netty是一个异步的、基于事件驱动的网路应用框架,用