SpringBoot整合Netty

前言

上一篇<<初识Netty>>, 我们讲解了如何用Netty实现一个WebSocket服务端,作为实时聊天室的服务器.

本文的场景是, 我们已经有一个SpringBoot的Web服务了, 希望将WebSocket的能力整合到SpringBoot开发的Web服务中,也就是随着Web服务的启动而启动, 随着Web服务的关闭而关闭.

Demo

监听 ContextRefreshedEvent

ContextRefreshedEvent : 上下文刷新事件是在 Spring 应用上下文(ApplicationContext)刷新之后发送。

Spring Boot 启动事件顺序

  • ApplicationStartingEvent : 这个事件在 Spring Boot 应用运行开始时,且进行任何处理之前发送(除了监听器和初始化器注册之外)。
  • ApplicationEnvironmentPreparedEvent : 这个事件在当已知要在上下文中使用 Spring 环境(Environment)时,在 Spring 上下文(context)创建之前发送。
  • ApplicationContextInitializedEvent : 这个事件在当 Spring 应用上下文(ApplicationContext)准备好了,并且应用初始化器(ApplicationContextInitializers)已经被调用,在 bean 的定义(bean definitions)被加载之前发送。
  • ApplicationPreparedEvent : 这个事件是在 Spring 上下文(context)刷新之前,且在 bean 的定义(bean definitions)被加载之后发送。
  • ApplicationStartedEvent : 这个事件是在 Spring 上下文(context)刷新之后,且在 application/ command-line runners 被调用之前发送。
  • AvailabilityChangeEvent : 这个事件紧随上个事件之后发送,状态:ReadinessState.CORRECT,表示应用已处于活动状态。
  • ApplicationReadyEvent : 这个事件在任何 application/ command-line runners 调用之后发送。
  • AvailabilityChangeEvent : 这个事件紧随上个事件之后发送,状态:ReadinessState.ACCEPTING_TRAFFIC,表示应用可以开始准备接收请求了。
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import Javax.annotation.Resource;

@Component
public class NettyStartListener implements ApplicationListener<ContextRefreshedEvent{

    @Resource
    private WebSocketNettyServer webSocketNettyServer;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        if (event.getApplicationContext().getParent() == null) {
            try {
                webSocketNettyServer.start();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

Netty 业务处理实现

除了 WebSocketNettyServer, 还需要 ChatHandler / WebSocketChannelInitializer, 这两个跟上一篇文章内容的编码完全一样, 懒得搬了, 有需要自己去找下吧. <<Netty基于WebSocket实现的简单聊天室>>

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import org.springframework.stereotype.Component;

@Component
public class WebSocketNettyServer {

    // Netty服务器启动对象
    private final ServerBootstrap serverBootstrap;

    public WebSocketNettyServer() {
        serverBootstrap = new ServerBootstrap();
        // 初始化服务器启动对象
        // 主线程池
        NioEventLoopGroup mainGrp = new NioEventLoopGroup();
        // 从线程池
        NioEventLoopGroup subGrp = new NioEventLoopGroup();
        serverBootstrap
                // 指定使用上面创建的两个线程池
                .group(mainGrp, subGrp)
                // 指定Netty通道类型
                .channel(NioServerSocketChannel.class)
                // 指定通道初始化器用来加载当Channel收到事件消息后,
                // 如何进行业务处理
                .childHandler(new WebSocketChannelInitializer())
;
    }

    public void start() {
        // 绑定服务器端口,以异步的方式启动服务器
        ChannelFuture future = serverBootstrap.bind(9090);
    }
}

测试一下

客户端1:

SpringBoot整合Netty

客户端2:

SpringBoot整合Netty

服务端运行日志:

SpringBoot整合Netty


原文始发于微信公众号(灰气球):SpringBoot整合Netty

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/134430.html

(0)
小半的头像小半

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!