当前位置:   article > 正文

SpringBoot集成Netty_springboot集成netty网络编程

springboot集成netty网络编程

SpringBoot集成netty

基础类实现 CommandLineRunner

也可以将netty用多线程启动

package cn.wxt.test;

import cn.wxt.test.test2.Test2Server;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;

@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class})
@SpringBootApplication
public class TestApplication implements CommandLineRunner {
    @Autowired
    private Test2Server server;

    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        server.startup();
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

netty Server端

package cn.wxt.test.test2;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.FixedLengthFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class Test2Server {
    private final EventLoopGroup boss = new NioEventLoopGroup();
    private final EventLoopGroup worker = new NioEventLoopGroup();
    //
    private static final Logger logger = LoggerFactory.getLogger(Test2Server.class);

    public void startup(){
        try {
            ServerBootstrap server = new ServerBootstrap();//启动类
            server.group(boss, worker)
                    .channel(NioServerSocketChannel.class)
                    //BACKLOG用于构造服务端套接字ServerSocket对象,标识当服务器请求处理线程全满时,用于临时存放已完成三次握手的请求的队列的最大长度
                    .option(ChannelOption.SO_BACKLOG, 1024)
                    .option(ChannelOption.TCP_NODELAY, true)
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel sc) throws Exception {
                            sc.pipeline().addLast(new FixedLengthFrameDecoder(10));
                            sc.pipeline().addLast(new StringDecoder());
                            sc.pipeline().addLast(new Test2ServerHandler());
                        }
                    });
            ChannelFuture cf = server.bind(9092).sync();
            logger.info("server start");
            cf.channel().closeFuture().sync();
        }
        catch(Exception e) {
            e.printStackTrace();
        }finally {
            logger.info("server shutdown.");
            boss.shutdownGracefully();
            worker.shutdownGracefully();
        }
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50

http传输过程
在这里插入图片描述

相关问题

关于 LEAK: ByteBuf.release() was not called before it’s garbage-collected

netty默认的分配bytebuff的方式是PooledByteBufAllocator,所以要手动回收,要不然会造成内存泄漏

关于Netty的ByteBuff内存泄漏问题
https://www.jianshu.com/p/b9241e7a9eda

Netty内存分配ByteBuf
https://blog.csdn.net/qq_35729915/article/details/90761381

Netty之内存分配器ByteBufAllocator
https://blog.csdn.net/u011212394/article/details/103984870

Netty之Unpooled_Bytebuf
https://www.cnblogs.com/Courage129/p/14276926.html

Epoll原理解析
https://blog.csdn.net/armlinuxww/article/details/92803381

Netty如何发送消息(writeAndFlush)https://www.jianshu.com/p/d1005f5375ea

Netty检查连接断开的几种方法 https://www.cnblogs.com/alan6/p/11715722.html

彻底搞懂 netty 线程模型 https://www.cnblogs.com/luoxn28/p/11875340.html

Netty时间轮 https://blog.csdn.net/bz120413/article/details/122107790

Netty教程-ServerBootstrap https://blog.csdn.net/woaixiaopangniu521/article/details/70256018

netty的option(ChannelOption.SO_BACKLOG, backLog) https://blog.csdn.net/zhousenshan/article/details/72859923

Netty系列之Netty百万级推送服务设计要点
https://www.infoq.cn/article/netty-million-level-push-service-design-points/

https://tianyalei.blog.csdn.net/article/details/107920192?utm_medium=distribute.pc_relevant_ask_down.none-task-blog-baidujs-4.nonecase&depth_1-utm_source=distribute.pc_relevant_ask_down.none-task-blog-baidujs-4.nonecase

使用 Netty 手写一个 http 服务器
https://mp.weixin.qq.com/s/kpLmjXrBMBrBQ0iAgOnoMw

Netty如何发送消息(writeAndFlush)https://www.jianshu.com/p/d1005f5375ea
Netty 启动流程解析
https://mp.weixin.qq.com/s?__biz=MzIwNTI2ODY5OA==&mid=2649938646&idx=1&sn=c78446f9593a045e018c361a3ba5f185&chksm=8f350963b842807574df5ba837a908319e60248cbed057ed2bbfc1b668673e516c59cdf2fcf8&scene=21#wechat_redirect

Netty(一):server启动流程解析 https://blog.csdn.net/lanzhupi/article/details/111935934

Netty事件监听和处理 https://blog.51cto.com/13714880/2114345

Netty中的Channel之数据冲刷与线程安全(writeAndFlush)
https://www.cnblogs.com/UncleCatMySelf/p/10769206.html

Netty入门(九)空闲连接以及超时
https://www.cnblogs.com/coderJiebao/p/Netty09.html
Netty入门-ByteBuf https://blog.csdn.net/qq_28497823/article/details/106137656

Netty 客户端发送自定义心跳包和掉线重连的实现
https://blog.csdn.net/li_c_yang/article/details/104841022

Netty(六):Netty中的连接管理(心跳机制和定时断线重连)
https://www.cnblogs.com/shamo89/p/8556185.html

Springboot+Netty搭建基于MQTT协议的服务端(五)_蚂蚁舞-CSDN博客
https://blog.csdn.net/myyhtw/article/details/114041042

网络编程Netty IoT百万长连接优化_狐言-CSDN博客_netty保持10万个长连接
https://blog.csdn.net/qq_34365173/article/details/106311934

ctx.close() 和 ctx.channel().close() 到底有何区别
https://www.cnblogs.com/kendoziyu/p/14787189.html

Netty之Channel(四)close操作 https://blog.csdn.net/weixin_43257196/article/details/104865942
ctx.write()和channel().write()的区别 https://blog.csdn.net/lalalahaitang/article/details/81563830
Netty中ctx.writeAndFlush与ctx.channel().writeAndFlush的区别
https://blog.csdn.net/fishseeker/article/details/78447684

Netty系列之Netty百万级推送服务设计要点 https://www.cnblogs.com/ruixueyan/p/6382770.html
Netty实现高性能IOT服务器(Groza)之手撕MQTT协议篇上
https://blog.csdn.net/weixin_33733810/article/details/88008379

基于Netty实现高性能弹幕系统 https://blog.csdn.net/u010152183/article/details/98770366
Netty 缓冲buffer介绍及使用 https://blog.csdn.net/cuiyaoqiang/article/details/51655712

Netty中使用WebSocket实现服务端与客户端的长连接通信发送消息
https://www.cnblogs.com/badaoliumangqizhi/p/13686101.html

Netty的ChannelOption使用 https://blog.csdn.net/hbtj_1216/article/details/74199162
Netty Buffer(缓冲)https://blog.csdn.net/sdgihshdv/article/details/78777082
fireChannelRead用法 https://www.cnblogs.com/Shining-stars/p/13091314.html
Netty自定义协议半包编解码 + 心跳处理 https://www.cnblogs.com/sidesky/p/12726599.html
Netty中的缓冲区原理及API概览 https://zhuanlan.zhihu.com/p/104357529
Netty 粘包/拆包问题与使用LineBasedFrameDecoder的解决 https://zhuanlan.zhihu.com/p/71722002
netty ChannelPipeline的事件传输机制 https://www.imooc.com/article/272419
Netty之Channel https://www.cnblogs.com/krcys/p/9297092.html#3278483607
Netty(六)ChannelHandlerContext https://blog.csdn.net/qq_42605968/article/details/105578540
Netty博客 https://blog.csdn.net/qq_42605968/category_9804622.html
netty ByteBuf与String相互转换 https://www.cnblogs.com/deltadeblog/p/11464948.html
Netty(五)之 初识ByteBuf和ByteBuf的常用API https://blog.csdn.net/linuu/article/details/51322224
基于netty框架的Socket传输 https://www.cnblogs.com/10158wsj/p/8428347.html
Netty实现心跳机制 https://www.cnblogs.com/demingblog/p/9957143.html
Netty面试题和解答(一) https://blog.csdn.net/weixin_30606461/article/details/102361252
netty—sync,await https://www.cnblogs.com/heroinss/p/9990445.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/385486
推荐阅读
相关标签
  

闽ICP备14008679号