首页 Netty 学习与使用
文章
取消

Netty 学习与使用

概述

netty是一个高性能的socket框架。

源码

https://github.com/netty/netty

快速入门

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
    <build>
        <plugins>
            <!--【必用插件】用于设置项目jdk版本-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>


    <dependencies>
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
            <version>4.1.20.Final</version>
        </dependency>
        <dependency>
            <groupId>com.google.protobuf</groupId>
            <artifactId>protobuf-java</artifactId>
            <version>3.2.0</version>
        </dependency>
    </dependencies>

客户端

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
    public void connect(String ip, int port) {
        NioEventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(workerGroup)
                    .channel(NioSocketChannel.class)
                    .option(ChannelOption.SO_KEEPALIVE, true)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel socketChannel) throws Exception {
                            ChannelPipeline pipeline = socketChannel.pipeline();
                            pipeline.addLast("intDecoder", new ProtobufVarint32FrameDecoder());
                            pipeline.addLast("intEncoder", new ProtobufVarint32LengthFieldPrepender());
                            pipeline.addLast("protobufDecoder", new ProtobufDecoder(MessagePOJO.Message.getDefaultInstance()));
                            pipeline.addLast("protobufEncoder", new ProtobufEncoder());
                            pipeline.addLast(new SimpleClientHandler());
                        }
                    });
            ChannelFuture channelFuture = bootstrap.connect(ip, port).sync();
            if (channelFuture.isSuccess()) System.out.println("连接成功!!!!");
            else System.out.println("连接失败");
            channelFuture.channel().closeFuture().sync();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            workerGroup.shutdownGracefully();
        }
    }

———-另一种———————–

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
    public void connect(String ip, int port) {
        EventLoopGroup group = new NioEventLoopGroup();// 开启工作线程组
        try {
            Bootstrap bootstrap = new Bootstrap(); //创建一个和服务端相对应的server
            bootstrap.group(group) //设置线程组
                    .channel(NioSocketChannel.class) //使用NioSocketChannel作为客户端的通道实现
                    .handler(new ChannelInitializer<SocketChannel>() {//设置回调函数
                        @Override
                        protected void initChannel(SocketChannel ch) {
                            ch.pipeline().addLast(new NettyClientHandler());
                        }
                    });
            System.out.println("netty client start。。");
            ChannelFuture cf = bootstrap.connect("127.0.0.1", 9000).sync();//启动客户端去连接服务器端
            //对通道关闭进行监听
            cf.channel().closeFuture().sync();
        } finally {
            group.shutdownGracefully();//关闭线程组
        }
    }

服务端

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public void startServer() {
        NioEventLoopGroup bossGroup = new NioEventLoopGroup();
        NioEventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            serverBootstrap.group(bossGroup, workerGroup)
                    .option(ChannelOption.SO_BACKLOG, 128)
                    .childOption(ChannelOption.SO_KEEPALIVE, true)
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel socketChannel) throws Exception {
                            ChannelPipeline pipeline = socketChannel.pipeline();
                            pipeline.addLast("intDecoder", new ProtobufVarint32FrameDecoder());
                            pipeline.addLast("intEncoder", new ProtobufVarint32LengthFieldPrepender());
                            pipeline.addLast("protobufDecoder", new ProtobufDecoder(MessagePOJO.Message.getDefaultInstance()));
                            pipeline.addLast(new SimpleServerHandler());
                        }
                    });
            ChannelFuture channelFuture = serverBootstrap.bind(port).sync();
            if (channelFuture.isSuccess()) System.out.println("服务器开启成功!!!!");
            else System.out.println("服务器开启失败");
            channelFuture.channel().closeFuture().sync();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            workerGroup.shutdownGracefully();
            bossGroup.shutdownGracefully();
        }
    }

———-另一种———————–

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
    public void startServer() {
        EventLoopGroup bossGroup = new NioEventLoopGroup(1);//新建线程组,用于处理请求
        EventLoopGroup workerGroup = new NioEventLoopGroup();//真正工作的线程组
        try {
            ServerBootstrap bootstrap = new ServerBootstrap();//创建一个server,相当于NIO的server
            bootstrap.group(bossGroup, workerGroup) //采用链式编程,将两个线程组加入到server中
                    .channel(NioServerSocketChannel.class) //设置一个信道,相当于NIO中的ServerSocketChannel
                    .option(ChannelOption.SO_BACKLOG, 1024) //对服务端给予一些设置
                    .childHandler(new ChannelInitializer<SocketChannel>() {//给予初始化,并加入回调函数
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            ch.pipeline().addLast(new NettyServerHandler());
                        }
                    });
            System.out.println("netty server start。。");
            ChannelFuture cf = bootstrap.bind(9000).sync();//绑定端口,sync方法是等待异步操作执行完毕
            cf.channel().closeFuture().sync();//对通道关闭进行监听,closeFuture是异步操作,监听通道关闭
                                        // 通过sync方法同步等待通道关闭处理完毕,这里会阻塞等待通道关闭完成
        } finally {
            bossGroup.shutdownGracefully(); //关闭处理请求的线程组
            workerGroup.shutdownGracefully();//关闭真正工作的线程组
        }
    }

回调

服务端回调

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class NettyServerHandler extends ChannelInboundHandlerAdapter {

    //当客户端连接服务器完成就会触发该方法
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        ByteBuf buf = Unpooled.copiedBuffer("HelloServer".getBytes(CharsetUtil.UTF_8));
        ctx.writeAndFlush(buf);
    }

    //当通道有读取事件时会触发,即客户端发送数据给服务端
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        ByteBuf buf = (ByteBuf) msg;
        System.out.println("收到客户端的消息:" + buf.toString(CharsetUtil.UTF_8));
        System.out.println("客户端的地址: " + ctx.channel().remoteAddress());
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        System.out.println("我收到了异常");
        cause.printStackTrace();
        ctx.close();
    }
}

客户端回调

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class NettyClientHandler extends ChannelInboundHandlerAdapter {

    /**
     * 当客户端连接服务器完成就会触发该方法
     *
     * @param ctx 设置好的信道,相当于上下文
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) {
        ByteBuf buf = Unpooled.copiedBuffer("HelloServer".getBytes(CharsetUtil.UTF_8));
        ctx.writeAndFlush(buf);
    }

    //当通道有读取事件时会触发,即服务端发送数据给客户端
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        ByteBuf buf = (ByteBuf) msg;
        System.out.println("收到服务端的消息:" + buf.toString(CharsetUtil.UTF_8));
        System.out.println("服务端的地址: " + ctx.channel().remoteAddress());
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        cause.printStackTrace();
        ctx.close();
    }
}

参考文档

本文由作者按照 CC BY 4.0 进行授权

从vuepress迁移至jekyll

深入浅出 Netty

载入天数...载入时分秒...