Netty 实现简单 HTTP 服务

阳光穿透心脏的1/2处 2023-10-01 16:38 78阅读 0赞

Netty 实现简单 HTTP 服务

  • 一、服务端代码
    • 1.服务启动类
    • 2.管道初始化类
    • 3.处理器类
    • 4.启动服务,调用演示
    • 5.如果出现6668不安全提示,可以将端口号修改为 > 8000 或修改浏览器配置
    • 6.一次调用打印两次日志
    • 7.通道分析(服务端如何分配管道)

一、服务端代码

1.服务启动类

  1. package com.example.netty.server;
  2. import com.example.netty.server.init.NettyServerHttpInitializer;
  3. import io.netty.bootstrap.ServerBootstrap;
  4. import io.netty.channel.ChannelFuture;
  5. import io.netty.channel.ChannelOption;
  6. import io.netty.channel.EventLoopGroup;
  7. import io.netty.channel.nio.NioEventLoopGroup;
  8. import io.netty.channel.socket.nio.NioServerSocketChannel;
  9. /**
  10. * Description: Netty服务端
  11. *
  12. * @Author: zhx & moon hongxu_1234@163.com
  13. * @Date: 2021-11-14 23:31
  14. * @version: V1.0.0
  15. */
  16. public class NettyServer {
  17. public static void main(String[] args) throws InterruptedException {
  18. //创建BossGroup 和 WorkerGroup
  19. /**
  20. * 1.创建两个线程组
  21. * 2.BossGroup 处理连接请求
  22. * WorkerGroup 处理业务逻辑
  23. * 3.两个都是无限循环
  24. */
  25. EventLoopGroup bossGroup = new NioEventLoopGroup(1);
  26. EventLoopGroup workerGroup = new NioEventLoopGroup();
  27. try {
  28. //创建服务端的启动对象 配置参数
  29. ServerBootstrap bootstrap = new ServerBootstrap();
  30. //使用链式编程进行设置
  31. /**
  32. * 1.设置两个线程组
  33. * 2.使用 NioSocketChannel 作为服务器的通道实现
  34. * 3.设置线程队列得到的连接数
  35. * 4.设置保持活动连接状态
  36. * 5.给我们的 WorkerGroup 的 EventLoop 对应的管道设置处理器
  37. */
  38. bootstrap.group(bossGroup,workerGroup)
  39. .channel(NioServerSocketChannel.class)
  40. .option(ChannelOption.SO_BACKLOG,128)
  41. .childOption(ChannelOption.SO_KEEPALIVE,true)
  42. .childHandler(new NettyServerHttpInitializer());
  43. System.out.println("Server is Ready");
  44. //绑定一个端口并且同步 生成了一个 ChannelFuture 对象 启动服务
  45. ChannelFuture cf = bootstrap.bind(6668).sync();
  46. //对关闭通道进行侦听
  47. cf.channel().closeFuture().sync();
  48. }finally {
  49. bossGroup.shutdownGracefully();
  50. workerGroup.shutdownGracefully();
  51. }
  52. }
  53. }

2.管道初始化类

  1. package com.example.netty.server.init;
  2. import com.example.netty.server.handler.NettyServerHttpHandler;
  3. import io.netty.channel.ChannelInitializer;
  4. import io.netty.channel.ChannelPipeline;
  5. import io.netty.channel.socket.SocketChannel;
  6. import io.netty.handler.codec.http.HttpServerCodec;
  7. /**
  8. * Description: Netty Server 处理器初始化
  9. *
  10. * @Author: zhx & moon hongxu_1234@163.com
  11. * @Date: 2022-01-27 1:40
  12. * @version: V1.0.0
  13. */
  14. public class NettyServerHttpInitializer extends ChannelInitializer<SocketChannel> {
  15. /**
  16. * 使用Netty自带的HTTP编解码器
  17. * @param ch
  18. * @throws Exception
  19. */
  20. @Override
  21. protected void initChannel(SocketChannel ch) throws Exception {
  22. ChannelPipeline pipeline = ch.pipeline();
  23. pipeline.addLast("httpDec",new HttpServerCodec()).addLast("httpHan",new NettyServerHttpHandler());
  24. }
  25. }

3.处理器类

  1. package com.example.netty.server.handler;
  2. import io.netty.buffer.ByteBuf;
  3. import io.netty.buffer.Unpooled;
  4. import io.netty.channel.ChannelHandlerContext;
  5. import io.netty.channel.SimpleChannelInboundHandler;
  6. import io.netty.handler.codec.http.*;
  7. import io.netty.util.CharsetUtil;
  8. /**
  9. * Description: Netty Server 处理器
  10. *
  11. * @Author: zhx & moon hongxu_1234@163.com
  12. * @Date: 2022-01-27 1:27
  13. * @version: V1.0.0
  14. */
  15. public class NettyServerHttpHandler extends SimpleChannelInboundHandler<HttpObject> {
  16. /**
  17. * 读取客户端数据
  18. * @param ctx
  19. * @param msg
  20. * @throws Exception
  21. */
  22. @Override
  23. protected void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception {
  24. //判断 msg 是否为 http request 请求
  25. if (msg instanceof HttpRequest){
  26. System.out.println("MSG Class " + msg.getClass());
  27. System.out.println("Client IP " + ctx.channel().remoteAddress());
  28. //回复信息给Http
  29. ByteBuf content = Unpooled.copiedBuffer("Hello,server is busing", CharsetUtil.UTF_8);
  30. //构造Http响应 http response
  31. FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,HttpResponseStatus.OK,content);
  32. response.headers().set(HttpHeaderNames.CONTENT_TYPE,"text/plain");
  33. response.headers().set(HttpHeaderNames.CONTENT_LENGTH,content.readableBytes());
  34. //返回
  35. ctx.writeAndFlush(response);
  36. }
  37. }
  38. }

4.启动服务,调用演示

在这里插入图片描述

5.如果出现6668不安全提示,可以将端口号修改为 > 8000 或修改浏览器配置

6.一次调用打印两次日志

查看浏览器控制台发现,回车发送请求,会默认查询站点图标
在这里插入图片描述

尝试在服务端过滤该请求,不做响应;处理器中插入以下代码段

  1. String web_pic = "/favicon.ico";
  2. HttpRequest httpRequest = (HttpRequest)msg;
  3. URI uri = new URI(httpRequest.uri());
  4. if (web_pic.equals(uri.getPath())){
  5. System.out.println("Request Ico,do not response...");
  6. return;
  7. }

重启服务后,在次请求,查看结果如下
在这里插入图片描述

7.通道分析(服务端如何分配管道)

http 协议为无状态协议,通信完成即会断开连接,重复请求会重新分配管道
在这里插入图片描述

发表评论

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

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

相关阅读