博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springboot整合netty
阅读量:4042 次
发布时间:2019-05-24

本文共 3041 字,大约阅读时间需要 10 分钟。

0、前言

在《》中,实现了一个最简单的Netty 整合webSocket 的Demo。

1、先上代码目录截图

在这里插入图片描述

2、pom文件

引入springboot依赖,父工程com.ct.netty的pom文件中增加:

org.springframework.boot
spring-boot-starter-parent
2.5.0

引入springboot依赖,子模块工程com.ct.netty.http的pom中增加:

org.springframework.boot
spring-boot-starter-web

3、springboot配置文件

server:  port: 8089

4、springboot启动类

package com.ct.netty.http;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;/** * @Author LaoHa * @Date 2021/6/8 */@SpringBootApplicationpublic class AppServer {    public static void main(String[] args) {        SpringApplication.run(AppServer.class, args);    }}

5、Server启动类

注意:@Component

package com.ct.netty.http.IIIspringboot;import com.ct.netty.http.IIwebsocket.WsServerInitializer;import io.netty.bootstrap.ServerBootstrap;import io.netty.channel.ChannelFuture;import io.netty.channel.EventLoopGroup;import io.netty.channel.nio.NioEventLoopGroup;import io.netty.channel.socket.nio.NioServerSocketChannel;import org.springframework.stereotype.Component;/** * 服务端基本配置,通过一个静态单例类,保证启动时候只被加载一次 * * @Author LaoHa * @Date 2021/6/8 */@Componentpublic class WsServer {    /**     * 单例静态内部类     *     */    public static class SingletonWsServer {        static final WsServer instance = new WsServer();    }    public static WsServer getInstance() {        return SingletonWsServer.instance;    }    private final ServerBootstrap server;    public WsServer() {        EventLoopGroup mainGroup = new NioEventLoopGroup();        EventLoopGroup subGroup = new NioEventLoopGroup();        server = new ServerBootstrap();        server.group(mainGroup, subGroup)                .channel(NioServerSocketChannel.class)                //添加自定义初始化处理器                .childHandler(new WsServerInitializer());    }    public void start() {        ChannelFuture future = this.server.bind(8087);        System.err.println("netty 服务端启动成功 .....");    }}

6、初始化配置器

同《》

7、自定义助手句柄处理类

同《》

8、spring启动自动加载“netty服务端启动”配置

package com.ct.netty.http.IIIspringboot;import org.springframework.context.ApplicationListener;import org.springframework.context.event.ContextRefreshedEvent;import org.springframework.stereotype.Component;/** * netty服务端启动加载配置 * 实现了ApplicationListener这个接口,这样springboot容器启动完毕就可以加载netty的相关配置信息 * * @Author LaoHa * @Date 2021/6/8 */@Componentpublic class StartNettyServerConfig implements ApplicationListener
{ @Override public void onApplicationEvent(ContextRefreshedEvent event) { if (event.getApplicationContext().getParent() == null) { WsServer.getInstance().start(); } }}

9、运行测试

运行AppServer,启动服务端。其他

同《》

10、附录

  • 如果不引入spring-boot-starter-web
    log.info 会一直报错。
    因为子模块中 lombok的版本在spring-boot-starter-web

附录

idea提交git

  • 1、在csdn创建项目
  • 2、在idea中git-commit Dictionary-Commit and Push
  • 3、Define Remote
  • 4、输入从csdn项目中“克隆“-“通过 HTTPS Clone 项目”中获取的https的git url
  • 5、在idea中,输入csdn用户名和密码
  • 6、Push-查看

在这里插入图片描述

转载地址:http://olmdi.baihongyu.com/

你可能感兴趣的文章
Android 跨应用程序访问窗口知识点总结
查看>>
各种排序算法的分析及java实现
查看>>
SSH框架总结(框架分析+环境搭建+实例源码下载)
查看>>
js弹窗插件
查看>>
自定义 select 下拉框 多选插件
查看>>
js判断数组内是否有重复值
查看>>
js获取url链接携带的参数值
查看>>
gdb 调试core dump
查看>>
gdb debug tips
查看>>
arm linux 生成火焰图
查看>>
linux和windows内存布局验证
查看>>
linux insmod error -1 required key invalid
查看>>
linux kconfig配置
查看>>
linux不同模块completion通信
查看>>
linux printf获得时间戳
查看>>
C语言位扩展
查看>>
linux dump_backtrace
查看>>
linux irqdebug
查看>>
git 常用命令
查看>>
linux位操作API
查看>>