From 47b20dbb8f81f2a3ac113a7e209a8b4d1cb0146d Mon Sep 17 00:00:00 2001 From: huanghongfa <huanghongfa123456> Date: 星期一, 13 九月 2021 14:35:44 +0800 Subject: [PATCH] Merge remote-tracking branch 'origin/test' into test --- springcloud_k8s_panzhihuazhihuishequ/service_property/src/main/java/com/panzhihua/service_property/netty/NettyServer.java | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 46 insertions(+), 0 deletions(-) diff --git a/springcloud_k8s_panzhihuazhihuishequ/service_property/src/main/java/com/panzhihua/service_property/netty/NettyServer.java b/springcloud_k8s_panzhihuazhihuishequ/service_property/src/main/java/com/panzhihua/service_property/netty/NettyServer.java new file mode 100644 index 0000000..6b51b5f --- /dev/null +++ b/springcloud_k8s_panzhihuazhihuishequ/service_property/src/main/java/com/panzhihua/service_property/netty/NettyServer.java @@ -0,0 +1,46 @@ +package com.panzhihua.service_property.netty; + +import io.netty.bootstrap.ServerBootstrap; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelOption; +import io.netty.channel.EventLoopGroup; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.nio.NioServerSocketChannel; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +import java.net.InetSocketAddress; + +@Slf4j +public class NettyServer { + private static final Integer port=20012; + private static final String host="47.104.148.185"; + public void start() { + //new 一个主线程组 + EventLoopGroup bossGroup = new NioEventLoopGroup(1); + //new 一个工作线程组 + EventLoopGroup workGroup = new NioEventLoopGroup(200); + ServerBootstrap bootstrap = new ServerBootstrap() + .group(bossGroup, workGroup) + .channel(NioServerSocketChannel.class) + .childHandler(new ServerChannelInitializer()) + //设置队列大小 + .option(ChannelOption.SO_BACKLOG, 1024) + // 两小时内没有数据的通信时,TCP会自动发送一个活动探测数据报文 + .childOption(ChannelOption.SO_KEEPALIVE, true); + //绑定端口,开始接收进来的连接 + try { + ChannelFuture future = bootstrap.bind(host,port).sync(); + log.info("服务器启动开始监听端口: {}", port); + future.channel().closeFuture().sync(); + } catch (InterruptedException e) { + e.printStackTrace(); + } finally { + //关闭主线程组 + bossGroup.shutdownGracefully(); + //关闭工作线程组 + workGroup.shutdownGracefully(); + } + } +} -- Gitblit v1.7.1