SpringBoot和netty实现udp server

1、pom

     
            io.netty
            netty-all
            4.1.75.Final
        

2、server

package com.example.demo.udp;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioDatagramChannel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class NettyUdpServer implements ApplicationRunner {

    private Bootstrap bootstrap;

    private NioEventLoopGroup group;

    private Channel channel;
    @Autowired
    NettyUdpServerHandler nettyUdpServerHandler;


    public void start() throws InterruptedException {
        bootstrap = new Bootstrap();
        group = new NioEventLoopGroup();
        bootstrap.group(group)
                //  // 主线程处理
                .channel(NioDatagramChannel.class)
                // 广播
                .option(ChannelOption.SO_BROADCAST, true)
                // 设置读缓冲区为 10M
                .option(ChannelOption.SO_RCVBUF, 1024 * 1024 * 10)
                // 设置写缓冲区为1M
                .option(ChannelOption.SO_SNDBUF, 1024 * 1024)
                .handler(new ChannelInitializer() {
                    @Override
                    protected void initChannel(Channel channel) throws Exception {
                        ChannelPipeline pipeline = channel.pipeline();
                        pipeline.addLast(nettyUdpServerHandler);
                    }
                });
        try {
            channel = bootstrap.bind("0.0.0.0", 8888).sync().channel();
            System.out.println("UdpServer start success");
            channel.closeFuture().await();
        } finally {
            group.shutdownGracefully();
        }
    }


    @Override
    public void run(ApplicationArguments args) throws Exception {
        this.start();
    }


}

3、

NettyUdpServerHandler
package com.example.demo.udp;

import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.DatagramPacket;
import io.netty.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;


@Slf4j
@Component
@ChannelHandler.Sharable
public class NettyUdpServerHandler extends SimpleChannelInboundHandler {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket datagramPacket) {
        // log.info("开始接收数据");
        String msgString = datagramPacket.content().toString(CharsetUtil.UTF_8);
        System.out.println(msgString);
        System.out.println(Thread.currentThread().getName());
        System.out.println();
        //收到udp消息后,可通过此方式原路返回的方式返回消息,例如返回时间戳
        ctx.writeAndFlush(new DatagramPacket(Unpooled.copiedBuffer("receice data", CharsetUtil.UTF_8),
                datagramPacket.sender()));


    }

}

你可能感兴趣的:(netty,spring,boot,eureka,java)