Netty框架快速入门

NIO 客户端服务器框架,快速简单开发网络应用程序,如协议服务器和客户端。自定义设计和实现一个全新的协议。文章参考Nettty官网

快速入门:最简单的通信协议服务 -- Discard Server

  • Discard:丢弃任何接收到的数据
  • 项目准备:maven依赖
        
            io.netty
            netty-all
            4.1.50.Final
        
  • 信道编写:用于处理接收数据后的业务
package com.ht.server;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;
import io.netty.util.ReferenceCountUtil;

/**
 * Discard 协议服务信道
 *
 * @author: liht
 * @date: 2021/7/8-10:26
 */
public class DiscardServerHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        // todo: 打印丢弃接收到的数据
        ByteBuf in = (ByteBuf) msg;
        while (in.isReadable()){
            System.out.println(in.toString(CharsetUtil.US_ASCII));
        }
        ReferenceCountUtil.release(msg);
/*        // todo: 将接收到的数据发回
        ctx.writeAndFlush(msg);*/
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        // 程序异常时,关闭信道
        cause.printStackTrace();
        ctx.close();
    }
}
  • 启动服务类
package com.ht.server;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * Discard 服务启动类
 *
 * @author: liht
 * @date: 2021/7/8-10:48
 */
public class DiscardServer {
    /**
     * 端口
     */
    private int port;
    /**
     * Ip地址
     */
    private String ip;

    public DiscardServer(int port) {
        this.port = port;
    }

    public DiscardServer(int port, String ip) {
        this.port = port;
        this.ip = ip;
    }

    private void run() throws Exception{
        // 多线程循环,接收传入的连接
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();

        // 设置服务助手类
        ServerBootstrap bootstrap = new ServerBootstrap();
        bootstrap.group(bossGroup, workerGroup)
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer() {
                    @Override
                    protected void initChannel(SocketChannel ch) {
                        ch.pipeline().addLast(new DiscardServerHandler());
                    }
                })
                .option(ChannelOption.SO_BACKLOG, 128)
                .childOption(ChannelOption.SO_KEEPALIVE, true);

        // 绑定端口,启动连接
        ChannelFuture cf = bootstrap.bind(port).sync();
        // 关闭服务
        cf.channel().closeFuture().sync();

        workerGroup.shutdownGracefully();
        bossGroup.shutdownGracefully();
    }


    public static void main(String[] args) throws Exception {
        int port = 8080;
        new DiscardServer(port).run();
    }
}

  • 使用postman 推送数据测试...

你可能感兴趣的:(Netty框架快速入门)