• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Springboot+Netty搭建UDP协议的服务端三

武飞扬头像
阳光__小好
帮助1

  UDP是一个无连接协议,应用范围很大,对于一些低功耗的设备可以使用UDP方式向云端推送消息信息,也可以在推送消息时收到从云端原路返回的消息,使用Netty SpringBoot方式可以快速开发一套基于UDP协议的服务端程序。

1、 新建Springboot的maven项目,pom.xml文件导入依赖包

  1.  
  2.  
    <properties>
  3.  
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  4.  
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  5.  
    <java.version>1.8</java.version>
  6.  
    </properties>
  7.  
     
  8.  
    <parent>
  9.  
    <groupId>org.springframework.boot</groupId>
  10.  
    <artifactId>spring-boot-starter-parent</artifactId>
  11.  
    <version>2.1.6.RELEASE</version>
  12.  
    <relativePath /> <!-- lookup parent from repository -->
  13.  
    </parent>
  14.  
     
  15.  
    <dependencies>
  16.  
     
  17.  
    <!--web模块的启动器 -->
  18.  
    <dependency>
  19.  
    <groupId>org.springframework.boot</groupId>
  20.  
    <artifactId>spring-boot-starter-web</artifactId>
  21.  
    </dependency>
  22.  
    <!-- netty依赖 springboot2.x自动导入版本 -->
  23.  
    <dependency>
  24.  
    <groupId>io.netty</groupId>
  25.  
    <artifactId>netty-all</artifactId>
  26.  
    </dependency>
  27.  
     
  28.  
    </dependencies>
学新通

2、Springboot启动类,使用异步方式启动一个基于UDP协议的Netty应用(也包含web应用的)

  1.  
    package boot.netty.udp;
  2.  
     
  3.  
    import org.springframework.boot.CommandLineRunner;
  4.  
    import org.springframework.boot.SpringApplication;
  5.  
    import org.springframework.boot.autoconfigure.SpringBootApplication;
  6.  
    import org.springframework.scheduling.annotation.Async;
  7.  
    import org.springframework.scheduling.annotation.EnableAsync;
  8.  
     
  9.  
     
  10.  
    @SpringBootApplication
  11.  
    @EnableAsync
  12.  
    public class BootNettyUdpApplication implements CommandLineRunner{
  13.  
    public static void main( String[] args )
  14.  
    {
  15.  
    /**
  16.  
    * 启动springboot
  17.  
    */
  18.  
    SpringApplication app = new SpringApplication(BootNettyUdpApplication.class);
  19.  
    app.run(args);
  20.  
     
  21.  
    System.out.println( "Hello World!" );
  22.  
    }
  23.  
     
  24.  
    @Async
  25.  
    @Override
  26.  
    public void run(String... args) throws Exception {
  27.  
    /**
  28.  
    * 使用异步注解方式启动netty udp服务端服务
  29.  
    */
  30.  
    new BootNettyUdpServer().bind(9999);
  31.  
     
  32.  
    }
  33.  
    }
学新通

3、Netty的UDP启动类

  1.  
    package boot.netty.udp;
  2.  
     
  3.  
    import boot.netty.udp.adapter.BootNettyUdpSimpleChannelInboundHandler;
  4.  
    import io.netty.bootstrap.Bootstrap;
  5.  
    import io.netty.channel.ChannelFuture;
  6.  
    import io.netty.channel.ChannelOption;
  7.  
    import io.netty.channel.EventLoopGroup;
  8.  
    import io.netty.channel.nio.NioEventLoopGroup;
  9.  
    import io.netty.channel.socket.nio.NioDatagramChannel;
  10.  
     
  11.  
    public class BootNettyUdpServer {
  12.  
     
  13.  
    /**
  14.  
    * 启动服务
  15.  
    */
  16.  
    public void bind(int port) {
  17.  
     
  18.  
    EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
  19.  
    try {
  20.  
    //UDP方式使用Bootstrap
  21.  
    Bootstrap serverBootstrap = new Bootstrap();
  22.  
    serverBootstrap = serverBootstrap.group(eventLoopGroup);
  23.  
    serverBootstrap = serverBootstrap.channel(NioDatagramChannel.class);
  24.  
    serverBootstrap = serverBootstrap.option(ChannelOption.SO_BROADCAST, true);
  25.  
    //不需要太多其他的东西,直接这样就可以用
  26.  
    serverBootstrap = serverBootstrap.handler(new BootNettyUdpSimpleChannelInboundHandler());
  27.  
    System.out.println("netty udp start!");
  28.  
    ChannelFuture f = serverBootstrap.bind(port).sync();
  29.  
    f.channel().closeFuture().sync();
  30.  
    } catch (Exception e) {
  31.  
    // TODO: handle exception
  32.  
    } finally {
  33.  
    System.out.println("netty udp close!");
  34.  
    eventLoopGroup.shutdownGracefully();
  35.  
    }
  36.  
    }
  37.  
    }
学新通

4、服务端I/O数据读写处理类

  1.  
    package boot.netty.udp.adapter;
  2.  
     
  3.  
     
  4.  
    import io.netty.buffer.Unpooled;
  5.  
    import io.netty.channel.ChannelHandlerContext;
  6.  
    import io.netty.channel.SimpleChannelInboundHandler;
  7.  
    import io.netty.channel.socket.DatagramPacket;
  8.  
    import io.netty.util.CharsetUtil;
  9.  
     
  10.  
    public class BootNettyUdpSimpleChannelInboundHandler extends SimpleChannelInboundHandler<DatagramPacket> {
  11.  
     
  12.  
    @Override
  13.  
    protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception {
  14.  
    try {
  15.  
    String strdata = msg.content().toString(CharsetUtil.UTF_8);
  16.  
    //打印收到的消息
  17.  
    System.out.println("msg---" strdata);
  18.  
    //收到udp消息后,可通过此方式原路返回的方式返回消息,例如返回时间戳
  19.  
    ctx.writeAndFlush(new DatagramPacket(Unpooled.copiedBuffer(System.currentTimeMillis()/1000 "", CharsetUtil.UTF_8), msg.sender()));
  20.  
    } catch (Exception e) {
  21.  
     
  22.  
    }
  23.  
    }
  24.  
     
  25.  
    }
学新通

可以使用一些UDP客户端的软件来测试,这个Demo应用是基于Netty4.x的,对于Netty5.0有一些小的变化,在I/O数据处理类这里的变化需要注意,channelRead0(ChannelHandlerContext, I)方法改成了messageReceived(ChannelHandlerContext, I)},否则代码会报错然后找不到原因,Netty方法源码上是有注释信息的。

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfifhch
系列文章
更多 icon
同类精品
更多 icon
继续加载