当前位置:   article > 正文

Netty与Springboot的整合_childchannelhandler

childchannelhandler

Netty简单介绍

Netty和Tomcat

协议:Netty通过编程自定义各种协议(netty能够通过codec自己来编码/解码字节流),Tomcat基于Http协议

性能:Tomcat从6.x开始支持nio模式,后续还有APR模式(一种通过jni调用apache网络库的模式,相比于旧的bio模式,并发性能得

           到了很大提高),而netty是否比tomcat性能更高,则要取决于netty程序作者的技术实力了。

Netty优势

1.并发高:基于NIO(非阻塞IO)

2.传输快:零拷贝,数据不需要经过经过Socket缓冲区,它在堆内存之外开辟一块内存,数据就直接从IO读到了那块内存中

3.封装好:受到大公司的青睐

 

springboot与netty整合

1.在springboot项目的pom文件中添加netty依赖

  1. <!--springboot 整合 netty-->
  2. <dependency>
  3. <groupId>io.netty</groupId>
  4. <artifactId>netty-all</artifactId>
  5. <version>5.0.0.Alpha1</version>
  6. </dependency>

2.实现流程说明:

    

    springboot 启动类中启动  DiscardServer(netty启动类),

    DiscardServer(netty启动类)再调用  ChildChannelHandler(初始化channel类),

    ChildChannelHandler(初始化channel类) 再调用  DiscardServerHandler 类;

    DiscardServerHandler 类 再调用service服务;

 

3.代码实现

    1.DiscardServer类:

  1. @Component
  2. public class DiscardServer {
  3. @Resource
  4. private ChildChannelHandler childChannelHandler;
  5. public void run(int port) throws Exception {
  6. EventLoopGroup bossGroup = new NioEventLoopGroup();
  7. EventLoopGroup workerGroup = new NioEventLoopGroup();
  8. System.out.println("准备运行端口:" + port);
  9. try {
  10. ServerBootstrap bootstrap = new ServerBootstrap();
  11. bootstrap.group(bossGroup, workerGroup)
  12. .channel(NioServerSocketChannel.class)
  13. .option(ChannelOption.SO_BACKLOG, 128)
  14. .childHandler(childChannelHandler);
  15. //绑定端口,同步等待成功
  16. ChannelFuture f = bootstrap.bind(port).sync();
  17. //等待服务监听端口关闭
  18. f.channel().closeFuture().sync();
  19. } finally {
  20. //退出,释放线程资源
  21. workerGroup.shutdownGracefully();
  22. bossGroup.shutdownGracefully();
  23. }
  24. }
  25. }

    2.ChildChannelHandler类

  1. @Component
  2. public class ChildChannelHandler extends ChannelInitializer<SocketChannel> {
  3. @Resource
  4. private DiscardServerHandler discardServerHandler;
  5. @Override
  6. public void initChannel(SocketChannel socketChannel) throws Exception {
  7. socketChannel.pipeline().addLast(discardServerHandler);
  8. }
  9. }

    3.DiscardServerHandler类

  1. @Component
  2. @ChannelHandler.Sharable
  3. public class DiscardServerHandler extends ChannelHandlerAdapter{
  4. @Resource
  5. private BaseService baseService;
  6. @Override
  7. public void channelRead(ChannelHandlerContext ctx, Object msg){
  8. try{
  9. ByteBuf in = (ByteBuf) msg;
  10. System.out.println("传输内容是");
  11. System.out.println(in.toString(CharsetUtil.UTF_8));
  12. //这里调用service服务
  13. baseService.baseTest();
  14. }finally {
  15. ReferenceCountUtil.release(msg);
  16. }
  17. }
  18. @Override
  19. public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
  20. // 出现异常就关闭
  21. cause.printStackTrace();
  22. ctx.close();
  23. }
  24. }

    4.BaseService接口

  1. public interface BaseService {
  2. /**
  3. * 测试接口
  4. */
  5. void baseTest();
  6. }

    5.接口实现类BaseServiceImpl

  1. @Service
  2. public class BaseServiceImpl implements BaseService {
  3. @Override
  4. public void baseTest() {
  5. System.out.println("成功调用service服务");
  6. }
  7. }

    6.springboot启动类

       实现CommandLineRunner接口的run方法,在run方法里启动netty服务

  1. @SpringBootApplication
  2. public class FundmodelApplication implements CommandLineRunner{
  3. @Resource
  4. private DiscardServer discardServer;
  5. public static void main(String[] args) {
  6. SpringApplication.run(FundmodelApplication.class, args);
  7. }
  8. @Override
  9. public void run(String... args) throws Exception {
  10. discardServer.run(8089);
  11. }
  12. }

3.测试

    在本地重启一个应用然后在里面写测试类,发送测试数据

  1. @RunWith(SpringRunner.class)
  2. @SpringBootTest
  3. public class DiscardServerTest {
  4. @Test
  5. public void testDiscardServer(){
  6. try {
  7. Socket socket=new Socket("localhost",8089);
  8. OutputStream outputStream = socket.getOutputStream();
  9. PrintWriter printWriter=new PrintWriter(outputStream);
  10. printWriter.write("$tmb00035ET3318/08/22 11:5804029.94,027.25,20.00,20.00$");
  11. printWriter.flush();
  12. socket.shutdownOutput();
  13. socket.close();
  14. } catch (IOException e) {
  15. e.printStackTrace();
  16. }
  17. }
  18. }

测试结果:

  1. 准备运行端口:8089
  2. 传输内容是
  3. $tmb00035ET3318/08/22 11:5804029.94,027.25,20.00,20.00$
  4. 成功调用service服务

netty与springboot的整合就完成。

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

闽ICP备14008679号