Netty4.0入门案例

目录

    • 1.导入netty坐标(pom)
    • 2.编写入口程序(HelloServers)
    • 3.编写子处理器(HelloServerInit)
    • 4.编写自定义助手类(CustomHandler)
    • 5.效果
      • B/S交互
      • postman

项目结构
Netty4.0入门案例_第1张图片

1.导入netty坐标(pom)

    <dependency>
        <groupId>io.nettygroupId>
        <artifactId>netty-allartifactId>
        <version>4.1.48.Finalversion>
    dependency>

2.编写入口程序(HelloServers)

package netty;

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

public class HelloServers {
    public static void main(String[] args) throws Exception {
        /*定义一对组从线程组*/
        /*主线程组,只接收客户端的连接,不做事*/
        EventLoopGroup bossEvent = new NioEventLoopGroup();
        /*从线程组,从组线程组获取任务,来做任务*/
        EventLoopGroup workEvent = new NioEventLoopGroup();

        try {
            /*新建一个服务器启动*/
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            serverBootstrap.group(bossEvent, workEvent)/*设置主从线程组*/
                    .channel(NioServerSocketChannel.class) /*设置NIO的双向通道*/
                    .childHandler(new HelloServerInit());  /*设置子处理器*/

            /*启动server,并且设置端口号,为同步启动*/
            ChannelFuture channelfuture = serverBootstrap.bind(8088).sync();

            /*监听关闭的channel,设置为同步方式*/
            channelfuture.channel().closeFuture().sync();
        } finally {
            /*以优雅的方式关闭*/
            bossEvent.shutdownGracefully();
            workEvent.shutdownGracefully();
        }


    }
}

3.编写子处理器(HelloServerInit)

package netty;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpServerCodec;

/**
 * 初始化器,channel注册后会执行下面的初始化方法
 */
public class HelloServerInit extends ChannelInitializer<SocketChannel> {

    protected void initChannel(SocketChannel socketChannel) throws Exception {
        /*获取对应的管道*/
        ChannelPipeline pipeline = socketChannel.pipeline();

        /*通过管道添加handler*/
        /*HttpServerCodec是netty提供的助手类,类似拦截器(编码/解码)*/
        pipeline.addLast("httpServerCodec", new HttpServerCodec());
        /*添加自定义助手类*/
        pipeline.addLast("customHandler",new CustomHandler());
    }
}

4.编写自定义助手类(CustomHandler)

package netty;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.*;
import io.netty.util.CharsetUtil;

/**
 * 创建自定义助手类,SimpleChannelInboundHandler相当入站
 */
public class CustomHandler extends SimpleChannelInboundHandler<HttpObject> {
    /*从缓冲区读数据*/
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, HttpObject httpObject)
            throws Exception {
        /*获取channel*/
        Channel channel = channelHandlerContext.channel();
        /*筛选请求*/
        if (httpObject instanceof HttpRequest) {
            /*打印客户端地址*/
            System.out.println(channel.remoteAddress());

            /*定义发送的消息以及编码方式*/
            ByteBuf content = Unpooled.copiedBuffer("hello netty", CharsetUtil.UTF_8);
            /*新建一个response*/
            FullHttpResponse response =
                    new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
                            HttpResponseStatus.OK, content);
            /*设置响应头——数据类型和长度*/
            response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain");
            response.headers().set(HttpHeaderNames.CONTENT_LENGTH, content.readableBytes());
            /*把消息刷到客户端*/
            channelHandlerContext.writeAndFlush(response);
        }

    }

    @Override
    public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
        System.out.println("Channel注册");
        super.channelRegistered(ctx);
    }

    @Override
    public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
        System.out.println("Channel解注册");
        super.channelUnregistered(ctx);
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("Channel活动");
        super.channelActive(ctx);
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("Channel不活动");
        super.channelInactive(ctx);
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        System.out.println("Channel读取完毕");
        super.channelReadComplete(ctx);
    }

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        System.out.println("用户事件触发");
        super.userEventTriggered(ctx, evt);
    }

    @Override
    public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
        System.out.println("Channel可写更改");
        super.channelWritabilityChanged(ctx);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        System.out.println("捕获到异常");
        super.exceptionCaught(ctx, cause);
    }

    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
        System.out.println("助手类添加");
        super.handlerAdded(ctx);
    }

    @Override
    public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
        System.out.println("助手类移除");
        super.handlerRemoved(ctx);
    }
}

5.效果

B/S交互

Netty4.0入门案例_第2张图片
在这里插入图片描述
Netty4.0入门案例_第3张图片

关闭浏览器时
Netty4.0入门案例_第4张图片

postman

Netty4.0入门案例_第5张图片
Netty4.0入门案例_第6张图片

关闭postman时
Netty4.0入门案例_第7张图片

你可能感兴趣的:(技术笔记,netty)