Springboot 整合 Netty服务的用法

一、什么是Netty

1、Netty是一个Java的网络通信框架,用于快速开发可扩展的高性能网络服务器和客户端。它提供了简单而强大的抽象,使开发人员能够轻松地构建各种应用程序,包括实时通信、实时监测和大规模分布式系统。Netty的核心特点包括:异步的、事件驱动的网络编程模型、高性能、高度可定制、协议灵活、简单易用等。
2、WebSocket 的底层通常使用 Netty、Node.js、Tornado 等框架和技术实现。Netty 是一个基于 Java NIO 的异步、事件驱动的网络编程框架,被广泛用于构建高性能的网络应用程序,包括 WebSocket 服务器。在 Netty 中,可以方便地实现 WebSocket 的握手、数据传输等功能。

二、整合springboot

导入pom坐标

<dependencies>
    <!-- Spring Boot 相关依赖 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- Netty 相关依赖 -->
    <dependency>
        <groupId>io.netty</groupId>
        <artifactId>netty-all</artifactId>
        <version>4.1.66.Final</version>
    </dependency>
</dependencies>



导入配置
public class NettyConfig {
    /**
     * 定义一个channel组,管理所有的channel
     * GlobalEventExecutor.INSTANCE 是全局的事件执行器,是一个单例
     */
    private static ChannelGroup channelGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);

    /**
     * 存放用户与Chanel的对应信息,用于给指定用户发送消息
     */
    private static ConcurrentHashMap<String,Channel> userChannelMap = new ConcurrentHashMap<>();

    private NettyConfig() {}

    /**
     * 获取channel组
     * @return
     */
    public static ChannelGroup getChannelGroup() {
        return channelGroup;
    }

    /**
     * 获取用户channel map
     * @return
     */
    public static ConcurrentHashMap<String,Channel> getUserChannelMap(){
        return userChannelMap;
    }
}

netty server 服务的创建
@Component
public class NettyServer {

    private static final Logger log = LoggerFactory.getLogger(NettyServer.class);

    /**
     * webSocket协议名
     */
    private static final String WEBSOCKET_PROTOCOL = "WebSocket";

    /**
     * 端口号
     */
    @Value("${webSocket.netty.port}")
    private int port;

    /**
     * webSocket路径
     */
    @Value("${webSocket.netty.path}")
    private String webSocketPath;

    /**
     * 在Netty心跳检测中配置 - 读空闲超时时间设置
     */
    @Value("${webSocket.netty.readerIdleTime}")
    private long readerIdleTime;

    /**
     * 在Netty心跳检测中配置 - 写空闲超时时间设置
     */
    @Value("${webSocket.netty.writerIdleTime}")
    private long writerIdleTime;

    /**
     * 在Netty心跳检测中配置 - 读写空闲超时时间设置
     */
    @Value("${webSocket.netty.allIdleTime}")
    private long allIdleTime;

    @Autowired
    private WebSocketHandler webSocketHandler;

    private EventLoopGroup bossGroup;
    private EventLoopGroup workGroup;

    /**
     * 启动
     *
     * @throws InterruptedException
     */
    private void start() throws InterruptedException {
        bossGroup = new NioEventLoopGroup();
        workGroup = new NioEventLoopGroup();
        ServerBootstrap bootstrap = new ServerBootstrap();
        // bossGroup辅助客户端的tcp连接请求, workGroup负责与客户端之前的读写操作
        bootstrap.group(bossGroup, workGroup);
        // 设置NIO类型的channel
        bootstrap.channel(NioServerSocketChannel.class);
        // 设置监听端口
        bootstrap.localAddress(new InetSocketAddress(port));
        // 连接到达时会创建一个通道
        bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {

            @Override
            protected void initChannel(SocketChannel ch) throws Exception {
                // 心跳检测(一般情况第一个设置,如果超时了,则会调用userEventTriggered方法,且会告诉你超时的类型)
                ch.pipeline().addLast(new IdleStateHandler(readerIdleTime, writerIdleTime, allIdleTime, TimeUnit.MINUTES));
                // 流水线管理通道中的处理程序(Handler),用来处理业务
                // webSocket协议本身是基于http协议的,所以这边也要使用http编解码器
                ch.pipeline().addLast(new HttpServerCodec());
                ch.pipeline().addLast(new ObjectEncoder());
                // 以块的方式来写的处理器
                ch.pipeline().addLast(new ChunkedWriteHandler());
                /*
                    说明:
                    1、http数据在传输过程中是分段的,HttpObjectAggregator可以将多个段聚合
                    2、这就是为什么,当浏览器发送大量数据时,就会发送多次http请求
                 */
                ch.pipeline().addLast(new HttpObjectAggregator(8192));
                /*
                    说明:
                    1、对应webSocket,它的数据是以帧(frame)的形式传递
                    2、浏览器请求时 ws://localhost:58080/xxx 表示请求的uri
                    3、核心功能是将http协议升级为ws协议,保持长连接
                */
                ch.pipeline().addLast(new WebSocketServerProtocolHandler(webSocketPath, WEBSOCKET_PROTOCOL, true, 65536 * 10));
                // 自定义的handler,处理业务逻辑
                ch.pipeline().addLast(webSocketHandler);
            }
        });
        // 配置完成,开始绑定server,通过调用sync同步方法阻塞直到绑定成功
        ChannelFuture channelFuture = bootstrap.bind().sync();
        log.info("Server started and listen on:{}", channelFuture.channel().localAddress());
        // 对关闭通道进行监听
        channelFuture.channel().closeFuture().sync();
    }

    /**
     * 释放资源
     *
     * @throws InterruptedException
     */
    @PreDestroy
    public void destroy() throws InterruptedException {
        if (bossGroup != null) {
            bossGroup.shutdownGracefully().sync();
        }
        if (workGroup != null) {
            workGroup.shutdownGracefully().sync();
        }
    }

    /**
     * 初始化(新线程开启)
     */
    @PostConstruct()
    public void init() {
        //需要开启一个新的线程来执行netty server 服务器
        new Thread(() -> {
            try {
                start();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
    }
}


webdocket服务的创建
@Component
@ChannelHandler.Sharable
public class WebSocketHandler extends SimpleChannelInboundHandler<TextWebSocketFrame> {

    private static final Logger log = LoggerFactory.getLogger(WebSocketHandler.class);

    /**
     * 一旦连接,第一个被执行
     *
     * @param ctx
     * @throws Exception
     */
    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
        log.info("handlerAdded 被调用" + ctx.channel().id().asLongText());
        // 添加到channelGroup 通道组
        NettyConfig.getChannelGroup().add(ctx.channel());
    }

    /**
     * 读取数据
     */
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, TextWebSocketFrame msg) throws Exception {
        // 获取用户ID,关联channel
        /*InetSocketAddress inSocket = (InetSocketAddress) ctx.channel().remoteAddress();
        String clientIp = inSocket.getAddress().getHostAddress();*/
        if (!StringUtils.isBlank(msg.text())) {
            try {

                ReceiveMsg receiveMsg = JSON.parseObject(msg.text(), ReceiveMsg.class);
                String uid = receiveMsg.getUserId();
                String command = receiveMsg.getCommand();
                String hyNbbh = receiveMsg.getHyNbbh();
                String ytNbbh = receiveMsg.getYtNbbh();
                String appId = receiveMsg.getAppId();
                if (StringUtils.isBlank(uid)) {
                    ReturnMsg returnMsg = new ReturnMsg();
                    returnMsg.setSuccess(false).setErrmsg("用户ID不能为空!");
                    ctx.channel().writeAndFlush(new TextWebSocketFrame(JSONObject.toJSONString(returnMsg)));
                    return;
                }
                // 当用户ID已存入通道内,则不进行写入,只有第一次建立连接时才会存入,其他情况发送uid则为心跳需求
                if (StringUtils.isBlank(hyNbbh)) {
                    //首页等
                    uid = ctx.channel().id().asLongText() + "_" + uid + "_Home";
                } else {
                    //会议讨论页面
                    uid = ctx.channel().id().asLongText() + "_" + uid + "_" + hyNbbh;
                }
                log.info("服务器收到消息:{}", msg.text());
                if (!NettyConfig.getUserChannelMap().containsKey(uid)) {
                    log.info("服务器收到消息:{}", msg.text());
                    NettyConfig.getUserChannelMap().put(uid, ctx.channel());//新用户上线

                    // 将用户ID作为自定义属性加入到channel中,方便随时channel中获取用户ID
                    // 回复消息
                    //ctx.channel().writeAndFlush(new TextWebSocketFrame("服务器连接成功!"));
                } else {
                    log.info("服务器收到消息:{}", msg.text());
                }
                if (StringUtils.isNotBlank(command)) {
                    if (command.equals(CommandEnum.HY_USERONLINE.getText())) {
                        pushService().updateSdry(receiveMsg, "1", false);//更新实到人员
                    }
                    //广播用户在线信息
                    if (command.equals(CommandEnum.HY_SMS.getText())) {
                        pushService().pushMsgToTlry(receiveMsg, false);
                    } else {
                        pushService().pushMsgToTlry(receiveMsg, true);
                    }
                }
            } catch (Exception e) {
                ReturnMsg returnMsg = new ReturnMsg();
                returnMsg.setSuccess(false).setErrmsg("解析收到数据时发生错误:" + e);
                ctx.channel().writeAndFlush(new TextWebSocketFrame(JSONObject.toJSONString(returnMsg)));
            }

        }

    }

    /**
     * 移除通道及关联用户
     *
     * @param ctx
     * @throws Exception
     */
    @Override
    public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
        log.info("handlerRemoved 被调用" + ctx.channel().id().asLongText());
        // 删除通道
        NettyConfig.getChannelGroup().remove(ctx.channel());
        removeUserId(ctx);
    }

    /**
     * 异常处理
     *
     * @param ctx
     * @param cause
     * @throws Exception
     */
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        log.info("异常:{}", cause.getMessage());
        // 删除通道
        NettyConfig.getChannelGroup().remove(ctx.channel());
        removeUserId(ctx);
        ctx.close();
    }

    /**
     * 心跳检测相关方法 - 会主动调用handlerRemoved
     *
     * @param ctx
     * @param evt
     * @throws Exception
     */
    @Override
    public void userEventTriggered(final ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt instanceof IdleStateEvent) {
            IdleStateEvent event = (IdleStateEvent) evt;
            if (event.state() == IdleState.ALL_IDLE) {
                //清除超时会话
                /*ChannelFuture writeAndFlush = ctx.writeAndFlush("you will close");
                writeAndFlush.addListener(new ChannelFutureListener() {

                    @Override
                    public void operationComplete(ChannelFuture future) throws Exception {
                       // ctx.channel().close();
                    }
                });*/
            }
        } else {
            super.userEventTriggered(ctx, evt);
        }
    }

    /**
     * channelInactive
     * channel 通道 Inactive 不活跃的
     * 当客户端主动断开服务端的链接后,这个通道就是不活跃的。也就是说客户端与服务端的关闭了通信通道并且不可以传输数据
     *
     * @param ctx
     */
    @Override
    public void channelInactive(ChannelHandlerContext ctx) {
        //getThis().pushMsgToAll("设备消息:----发现一个设备主动离线;设备的IP地址为"+clientIp);
        NettyConfig.getChannelGroup().remove(ctx.channel());
        removeUserId(ctx);
        ctx.close();
    }

    /**
     * 删除用户与channel的对应关系
     *
     * @param ctx
     */
    private void removeUserId(ChannelHandlerContext ctx) {
        /*ctx.channel().id().asLongText();
        AttributeKey<String> key = AttributeKey.valueOf("userId");*/
        String userId = ctx.channel().id().asLongText();

        if (!StringUtils.isBlank(userId) && NettyConfig.getUserChannelMap() != null && NettyConfig.getUserChannelMap().size() > 0) {
            NettyConfig.getUserChannelMap().forEach((key, v) -> {
                String id = v.id().asLongText();
                if (userId.equals(id)) {
                    NettyConfig.getUserChannelMap().remove(key);
                    log.info("删除用户与channel的对应关系,uid:{}", userId);
                    ReceiveMsg receiveMsg = new ReceiveMsg();
                    String[] s = key.split("_");
                    receiveMsg.setCommand(CommandEnum.HY_USEROFLINE.getText()).setHyNbbh(s[2]).setUserId(s[1]);
                    pushService().updateSdry(receiveMsg, "0", true);
                    pushService().pushMsgToTlry(receiveMsg, true);
                }
            });

        }
    }

    private PushService pushService() {
        PushService pushService = ApplicationContextUtil.getBean("pushService", PushService.class);
        return pushService;
    }

    private PushHomeService pushHomeService() {
        PushHomeService pushHomeService = ApplicationContextUtil.getBean("pushHomeService", PushHomeService.class);
        return pushHomeService;
    }
}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
使用 SpringBoot 整合 Netty 实现 TCP 服务器可以让我们更方便地管理和部署我们的应用程序。下面是一些基本的步骤: 1. 创建一个 SpringBoot 项目,并添加 Netty 和相应的依赖。 2. 创建一个 Netty 服务类,实现 ChannelInboundHandlerAdapter 接口。在这个类中,你可以实现接收、处理和发送 TCP 消息的逻辑。 3. 通过 SpringBoot 的配置文件,配置 Netty 服务器的端口和其他参数。 4. 在 SpringBoot 的启动类中,使用 @Bean 注解将 Netty 服务类注册为 bean。 5. 启动 SpringBoot 应用程序,Netty 服务器将开始监听传入的连接。 下面是一个简单的示例: ``` // 服务类 @Component public class MyNettyServer extends ChannelInboundHandlerAdapter { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { // 处理接收到的消息 ByteBuf buf = (ByteBuf) msg; String message = buf.toString(CharsetUtil.UTF_8); // 返回响应消息 String response = "Hello, " + message; ctx.writeAndFlush(Unpooled.copiedBuffer(response.getBytes())); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { // 处理异常 cause.printStackTrace(); ctx.close(); } } // 启动类 @SpringBootApplication public class MyApplication { @Autowired private MyNettyServer myNettyServer; @Value("${netty.port}") private int port; @PostConstruct public void start() throws Exception { // 创建 EventLoopGroup EventLoopGroup group = new NioEventLoopGroup(); try { // 创建 ServerBootstrap ServerBootstrap b = new ServerBootstrap(); b.group(group) .channel(NioServerSocketChannel.class) .localAddress(new InetSocketAddress(port)) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { // 添加处理器 ch.pipeline().addLast(myNettyServer); } }); // 启动服务器 ChannelFuture f = b.bind().sync(); f.channel().closeFuture().sync(); } finally { // 关闭 EventLoopGroup group.shutdownGracefully().sync(); } } public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } } // 配置文件 netty.port=8080 ``` 在这个例子中,我们创建了一个名为 `MyNettyServer` 的服务类,并实现了 `ChannelInboundHandlerAdapter` 接口。在 `channelRead` 方法中,我们处理接收到的消息,并返回响应消息。在 `exceptionCaught` 方法中,我们处理异常。 在启动类中,我们使用 `@Autowired` 注解将 `MyNettyServer` 注入到启动类中,并使用 `@Value` 注解获取配置文件中的端口号。在 `start` 方法中,我们创建了一个 `EventLoopGroup`,并使用 `ServerBootstrap` 创建了一个 Netty 服务器。然后,我们将 `MyNettyServer` 添加到 `SocketChannel` 的处理器中。最后,我们启动服务器,并在关闭服务器之前等待连接。 这只是一个简单的示例,你可以根据你的需求修改和扩展它。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

QD_IT伟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值