NettyConfig.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package com.zhentao.information.config;
  2. import com.zhentao.information.handler.WebSocketHandler;
  3. import io.netty.bootstrap.ServerBootstrap;
  4. import io.netty.channel.ChannelInitializer;
  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.SocketChannel;
  9. import io.netty.channel.socket.nio.NioServerSocketChannel;
  10. import io.netty.handler.codec.http.HttpObjectAggregator;
  11. import io.netty.handler.codec.http.HttpServerCodec;
  12. import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
  13. import io.netty.handler.stream.ChunkedWriteHandler;
  14. import io.netty.handler.timeout.IdleStateHandler;
  15. import lombok.extern.slf4j.Slf4j;
  16. import org.springframework.beans.factory.annotation.Value;
  17. import org.springframework.context.annotation.Bean;
  18. import org.springframework.context.annotation.Configuration;
  19. import javax.annotation.Resource;
  20. import java.util.concurrent.TimeUnit;
  21. /**
  22. * Netty服务器配置类
  23. * 配置WebSocket服务器的启动参数和处理器链
  24. */
  25. @Slf4j
  26. @Configuration
  27. public class NettyConfig {
  28. /**
  29. * WebSocket服务器端口
  30. */
  31. @Value("${netty.websocket.port}")
  32. private int port;
  33. /**
  34. * WebSocket消息处理器
  35. */
  36. @Resource
  37. private WebSocketHandler webSocketHandler;
  38. /**
  39. * 配置并启动Netty服务器
  40. * @return ServerBootstrap实例
  41. */
  42. @Bean
  43. public ServerBootstrap serverBootstrap() {
  44. // 创建主从线程组
  45. // bossGroup用于接收客户端连接
  46. EventLoopGroup bossGroup = new NioEventLoopGroup(1);
  47. // workerGroup用于处理客户端数据
  48. EventLoopGroup workerGroup = new NioEventLoopGroup();
  49. // 创建服务器启动对象
  50. ServerBootstrap bootstrap = new ServerBootstrap();
  51. bootstrap.group(bossGroup, workerGroup)
  52. // 设置服务器通道实现
  53. .channel(NioServerSocketChannel.class)
  54. // 设置线程队列等待连接个数
  55. .option(ChannelOption.SO_BACKLOG, 128)
  56. // 设置保持活动连接状态
  57. .childOption(ChannelOption.SO_KEEPALIVE, true)
  58. // 禁用Nagle算法,减少延迟
  59. .childOption(ChannelOption.TCP_NODELAY, true)
  60. // 设置处理器
  61. .childHandler(new ChannelInitializer<SocketChannel>() {
  62. @Override
  63. protected void initChannel(SocketChannel ch) {
  64. // 获取管道
  65. ch.pipeline()
  66. // HTTP编解码器
  67. .addLast(new HttpServerCodec())
  68. // 支持大数据流
  69. .addLast(new ChunkedWriteHandler())
  70. // HTTP消息聚合器
  71. .addLast(new HttpObjectAggregator(65536))
  72. // 调整心跳检测时间:30秒没有收到消息就触发
  73. .addLast(new IdleStateHandler(30, 0, 0, TimeUnit.SECONDS))
  74. // WebSocket协议处理器
  75. .addLast(new WebSocketServerProtocolHandler("/ws", null, true, 65536))
  76. // 自定义消息处理器
  77. .addLast(webSocketHandler);
  78. }
  79. });
  80. try {
  81. // 绑定端口并启动服务器
  82. bootstrap.bind(port).sync();
  83. log.info("Netty WebSocket服务器启动成功,端口:{}", port);
  84. } catch (InterruptedException e) {
  85. log.error("Netty WebSocket服务器启动失败", e);
  86. Thread.currentThread().interrupt();
  87. }
  88. return bootstrap;
  89. }
  90. }