netty权威指南学习笔记二——netty入门应用

  经过了前面的NIO基础知识准备,我们已经对NIO有了较大了解,现在就进入netty的实际应用中来看看吧。重点体会整个过程。

按照权威指南写程序的过程中,发现一些问题:当我们在定义handler继承ChannelHanderAdapter时候,发现在其接口中没有可以实现的channelRead方法和channelReadComplete方法,然后查阅官网才发现netty5.0不见了,网上说是舍弃了,然后再看官网的一些例子,发现官网上继承的是ChannelInboundHandlerAdapter,当我将代码中handler的继承类更改为这个的时候发现前面的方法可以实现了。(同时发现ByteBuffer的一些方法调用不能调出书上的方法如readableBytes(),这个是我弄错了,ByteBuffer是jdk的包装类不是netty的,netty的应该用ByteBuf)为此将netty版本变更为

一、首先我们需要一个netty的环境,netty的环境搭建只需要一个netty的jar包即可。

  在maven工程中导入netty的jar资源

1 <dependency>
2             <groupId>io.netty</groupId>
3             <artifactId>netty-all</artifactId>
4             <!--<version>5.0.0.Alpha1</version>-->
5             <version>4.1.25.Final</version>
6         </dependency>

二、netty 服务端代码

TimeServer

 1 package com.netty.example;
 2 
 3 import io.netty.bootstrap.ServerBootstrap;
 4 import io.netty.channel.ChannelFuture;
 5 import io.netty.channel.ChannelInitializer;
 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.SocketChannel;
10 import io.netty.channel.socket.nio.NioServerSocketChannel;
11 
12 public class TimeServer {
13     public void bind(int port){
14 //        创建接收任务的线程组和处理IO事件的线程组
15         EventLoopGroup boosGroup = new NioEventLoopGroup();
16         EventLoopGroup workGroup = new NioEventLoopGroup();
17         try {
18             ServerBootstrap bs = new ServerBootstrap();
19             bs.group(boosGroup,workGroup)
20                     .channel(NioServerSocketChannel.class)
21                     .option(ChannelOption.SO_BACKLOG,1024)
22                     .childHandler(new ChildChannelHandler());
23 //        绑定端口号,同步等待成功
24             ChannelFuture f = bs.bind(port).sync();
25 //        等待服务器监听端口关闭
26             f.channel().closeFuture().sync();
27         } catch (InterruptedException e) {
28             e.printStackTrace();
29         }finally {
30 //          优雅退出释放线程池资源
31             boosGroup.shutdownGracefully();
32             workGroup.shutdownGracefully();
33         }
34     }
35     public static void main(String[] args){
36         int port = 8080;
37         if(args!=null&&args.length>0){
38             port = Integer.valueOf(args[0]);
39         }
40         new TimeServer().bind(port);
41     }
42 
43 private  class ChildChannelHandler extends ChannelInitializer<SocketChannel>{
44     @Override
45     protected void initChannel(SocketChannel socketChannel) throws Exception {
46         socketChannel.pipeline().addLast(new TimeServerHandler());
47     }
48 
49 }
50 
51 }

TimeServerHandler

 1 package com.netty.example;
 2 
 3 import io.netty.buffer.ByteBuf;
 4 import io.netty.buffer.Unpooled;
 5 import io.netty.channel.ChannelHandlerContext;
 6 import io.netty.channel.ChannelInboundHandlerAdapter;
 7 
 8 import java.util.Date;
 9 
10 public class TimeServerHandler extends ChannelInboundHandlerAdapter {
11 
12 
13     @Override
14     public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
15         ByteBuf buf = (ByteBuf) msg;//将接收到的msg转换为netty的BytyBuf对象,注意区分jdk中的ByteBuffer
16         byte[] req = new byte[buf.readableBytes()];//创建接收缓冲区数据的字节数组
17         buf.readBytes(req);//将缓冲区的数据读取到字节数组中
18         String body = new String(req,"utf-8");//获取请求消息
19         System.out.println("The time server receive order:"+body);
20         String currentTime = "QUERY TIME ORDER".equalsIgnoreCase(body)?new Date(System.currentTimeMillis()).toString():"BAD ORDER";
21         ByteBuf resp = Unpooled.copiedBuffer(currentTime.getBytes());//将响应信息封装为netty 的ByteBuf对象,
22         ctx.write(resp);//异步发送应答消息给客户端
23     }
24 
25     @Override
26     public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
27         /*将消息发送队列中的消息写道SocketChannel中发送给对方。从性能上考虑,为了防止频繁唤醒Selector进行消息发送
28         * netty的write方法并不直接将消息写入socketChannel中,调用write方法只是把待发送的消息发送到数据缓冲数组中,
29         * 再通过调用flush()方法,将发送缓冲区中的消息全部写道SocketChannel中*/
30         ctx.flush();
31     }
32 
33     @Override
34     public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
35         ctx.close();//发生异常时,关闭ChannelHandlerContext,释放ChannelHandlerContext相关联的句柄资源
36     }
37 }

三、netty 客户端代码

TimeClient

 1 package com.netty.example;
 2 
 3 import io.netty.bootstrap.Bootstrap;
 4 import io.netty.channel.ChannelFuture;
 5 import io.netty.channel.ChannelInitializer;
 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.SocketChannel;
10 import io.netty.channel.socket.nio.NioSocketChannel;
11 
12 /*
13 * 客户端代码
14 * */
15 public class TimeClient {
16 //    配置客户端NIO线程
17     public void connection(String host,int port) {
18         EventLoopGroup group = new NioEventLoopGroup();
19         try {
20         Bootstrap bootstrap = new Bootstrap();
21         bootstrap.group(group)
22                 .channel(NioSocketChannel.class)
23                 .option(ChannelOption.TCP_NODELAY,true)
24                 .handler(new ChannelInitializer<SocketChannel>() {
25                     @Override
26                     protected void initChannel(SocketChannel socketChannel) throws Exception {
27                         socketChannel.pipeline().addLast(new TimeClientHandler());
28                     }
29                 });
30 //        发起异步连接操作
31         ChannelFuture f = bootstrap.connect(host,port).sync();
32 //        等待客户端链路关闭
33             f.channel().closeFuture().sync();
34         } catch (InterruptedException e) {
35             e.printStackTrace();
36         }finally{
37             group.shutdownGracefully();
38         }
39 
40     }
41     public static void main(String[] args){
42         int port = 8080;
43         if(args.length>0&&args!=null){
44            port = Integer.valueOf(args[0]);
45         }
46         new TimeClient().connection("127.0.0.1",port);
47     }
48 }

TimeClientHandler

 1 package com.netty.example;
 2 
 3 import io.netty.buffer.ByteBuf;
 4 import io.netty.buffer.Unpooled;
 5 import io.netty.channel.ChannelHandlerContext;
 6 import io.netty.channel.ChannelInboundHandlerAdapter;
 7 
 8 public class TimeClientHandler extends ChannelInboundHandlerAdapter {
 9     private final ByteBuf firstMsg;
10 
11     public TimeClientHandler() {
12         byte[] req = "QUERY TIME ORDER".getBytes();
13         firstMsg = Unpooled.buffer(req.length);
14         firstMsg.writeBytes(req);
15     }
16 
17     @Override
18     public void channelActive(ChannelHandlerContext ctx) throws Exception {
19     /*    当客户端和服务端TCP链路建立成功之后,Netty的NIO线程会调用channelActive方法,发送查询时间的指令给服务端,
20         调用ChannelHandlerContext的writeAndFlush方法将请求消息发送给服务端
21            当服务端返回应答消息时,channelRead()方法被调用*/
22         ctx.writeAndFlush(firstMsg);
23     }
24 
25     @Override
26     public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
27         ByteBuf buf = (ByteBuf) msg;
28         byte[] req = new byte[buf.readableBytes()];
29         buf.readBytes(req);
30         String body = new String(req,"utf-8");
31         System.out.println("Now is"+body);
32     }
33 
34     @Override
35     public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
36         ctx.close();
37     }
38 }

四、启动运行

开启服务端

 1 D:javajava1.8jdk1.8injava.exe "-javaagent:D:我的软件itSoftIntelliJ IDEAIntelliJ IDEA 2018.1.5libidea_rt.jar=59255:D:我的软件itSoftIntelliJ IDEAIntelliJ IDEA 2018.1.5in" -Dfile.encoding=UTF-8 -classpath D:javajava1.8jdk1.8jrelibcharsets.jar;D:javajava1.8jdk1.8jrelibdeploy.jar;D:javajava1.8jdk1.8jrelibextaccess-bridge-64.jar;D:javajava1.8jdk1.8jrelibextcldrdata.jar;D:javajava1.8jdk1.8jrelibextdnsns.jar;D:javajava1.8jdk1.8jrelibextjaccess.jar;D:javajava1.8jdk1.8jrelibextjfxrt.jar;D:javajava1.8jdk1.8jrelibextlocaledata.jar;D:javajava1.8jdk1.8jrelibext
ashorn.jar;D:javajava1.8jdk1.8jrelibextsunec.jar;D:javajava1.8jdk1.8jrelibextsunjce_provider.jar;D:javajava1.8jdk1.8jrelibextsunmscapi.jar;D:javajava1.8jdk1.8jrelibextsunpkcs11.jar;D:javajava1.8jdk1.8jrelibextzipfs.jar;D:javajava1.8jdk1.8jrelibjavaws.jar;D:javajava1.8jdk1.8jrelibjce.jar;D:javajava1.8jdk1.8jrelibjfr.jar;D:javajava1.8jdk1.8jrelibjfxswt.jar;D:javajava1.8jdk1.8jrelibjsse.jar;D:javajava1.8jdk1.8jrelibmanagement-agent.jar;D:javajava1.8jdk1.8jrelibplugin.jar;D:javajava1.8jdk1.8jrelib
esources.jar;D:javajava1.8jdk1.8jrelib
t.jar;D:
etty	argetclasses;C:Userslitan.m2
epositoryorgspringframeworkootspring-boot-starter-data-redis2.0.3.RELEASEspring-boot-starter-data-redis-2.0.3.RELEASE.jar;C:Userslitan.m2
epositoryorgspringframeworkootspring-boot-starter2.0.3.RELEASEspring-boot-starter-2.0.3.RELEASE.jar;C:Userslitan.m2
epositoryorgspringframeworkootspring-boot2.0.3.RELEASEspring-boot-2.0.3.RELEASE.jar;C:Userslitan.m2
epositoryorgspringframeworkootspring-boot-autoconfigure2.0.3.RELEASEspring-boot-autoconfigure-2.0.3.RELEASE.jar;C:Userslitan.m2
epositoryorgspringframeworkootspring-boot-starter-logging2.0.3.RELEASEspring-boot-starter-logging-2.0.3.RELEASE.jar;C:Userslitan.m2
epositorychqoslogbacklogback-classic1.2.3logback-classic-1.2.3.jar;C:Userslitan.m2
epositorychqoslogbacklogback-core1.2.3logback-core-1.2.3.jar;C:Userslitan.m2
epositoryorgapachelogginglog4jlog4j-to-slf4j2.10.0log4j-to-slf4j-2.10.0.jar;C:Userslitan.m2
epositoryorgapachelogginglog4jlog4j-api2.10.0log4j-api-2.10.0.jar;C:Userslitan.m2
epositoryorgslf4jjul-to-slf4j1.7.25jul-to-slf4j-1.7.25.jar;C:Userslitan.m2
epositoryjavaxannotationjavax.annotation-api1.3.2javax.annotation-api-1.3.2.jar;C:Userslitan.m2
epositoryorgyamlsnakeyaml1.19snakeyaml-1.19.jar;C:Userslitan.m2
epositoryorgspringframeworkdataspring-data-redis2.0.8.RELEASEspring-data-redis-2.0.8.RELEASE.jar;C:Userslitan.m2
epositoryorgspringframeworkdataspring-data-keyvalue2.0.8.RELEASEspring-data-keyvalue-2.0.8.RELEASE.jar;C:Userslitan.m2
epositoryorgspringframeworkdataspring-data-commons2.0.8.RELEASEspring-data-commons-2.0.8.RELEASE.jar;C:Userslitan.m2
epositoryorgspringframeworkspring-tx5.0.7.RELEASEspring-tx-5.0.7.RELEASE.jar;C:Userslitan.m2
epositoryorgspringframeworkspring-oxm5.0.7.RELEASEspring-oxm-5.0.7.RELEASE.jar;C:Userslitan.m2
epositoryorgspringframeworkspring-aop5.0.7.RELEASEspring-aop-5.0.7.RELEASE.jar;C:Userslitan.m2
epositoryorgspringframeworkspring-context-support5.0.7.RELEASEspring-context-support-5.0.7.RELEASE.jar;C:Userslitan.m2
epositoryorgslf4jslf4j-api1.7.25slf4j-api-1.7.25.jar;C:Userslitan.m2
epositoryiolettucelettuce-core5.0.4.RELEASElettuce-core-5.0.4.RELEASE.jar;C:Userslitan.m2
epositoryioprojectreactor
eactor-core3.1.8.RELEASE
eactor-core-3.1.8.RELEASE.jar;C:Userslitan.m2
epositoryorg
eactivestreams
eactive-streams1.0.2
eactive-streams-1.0.2.jar;C:Userslitan.m2
epositoryio
etty
etty-common4.1.25.Final
etty-common-4.1.25.Final.jar;C:Userslitan.m2
epositoryio
etty
etty-transport4.1.25.Final
etty-transport-4.1.25.Final.jar;C:Userslitan.m2
epositoryio
etty
etty-buffer4.1.25.Final
etty-buffer-4.1.25.Final.jar;C:Userslitan.m2
epositoryio
etty
etty-resolver4.1.25.Final
etty-resolver-4.1.25.Final.jar;C:Userslitan.m2
epositoryio
etty
etty-handler4.1.25.Final
etty-handler-4.1.25.Final.jar;C:Userslitan.m2
epositoryio
etty
etty-codec4.1.25.Final
etty-codec-4.1.25.Final.jar;C:Userslitan.m2
epositoryorgspringframeworkootspring-boot-starter-web2.0.3.RELEASEspring-boot-starter-web-2.0.3.RELEASE.jar;C:Userslitan.m2
epositoryorgspringframeworkootspring-boot-starter-json2.0.3.RELEASEspring-boot-starter-json-2.0.3.RELEASE.jar;C:Userslitan.m2
epositorycomfasterxmljacksoncorejackson-databind2.9.6jackson-databind-2.9.6.jar;C:Userslitan.m2
epositorycomfasterxmljacksoncorejackson-annotations2.9.0jackson-annotations-2.9.0.jar;C:Userslitan.m2
epositorycomfasterxmljacksoncorejackson-core2.9.6jackson-core-2.9.6.jar;C:Userslitan.m2
epositorycomfasterxmljacksondatatypejackson-datatype-jdk82.9.6jackson-datatype-jdk8-2.9.6.jar;C:Userslitan.m2
epositorycomfasterxmljacksondatatypejackson-datatype-jsr3102.9.6jackson-datatype-jsr310-2.9.6.jar;C:Userslitan.m2
epositorycomfasterxmljacksonmodulejackson-module-parameter-names2.9.6jackson-module-parameter-names-2.9.6.jar;C:Userslitan.m2
epositoryorgspringframeworkootspring-boot-starter-tomcat2.0.3.RELEASEspring-boot-starter-tomcat-2.0.3.RELEASE.jar;C:Userslitan.m2
epositoryorgapache	omcatembed	omcat-embed-core8.5.31	omcat-embed-core-8.5.31.jar;C:Userslitan.m2
epositoryorgapache	omcatembed	omcat-embed-el8.5.31	omcat-embed-el-8.5.31.jar;C:Userslitan.m2
epositoryorgapache	omcatembed	omcat-embed-websocket8.5.31	omcat-embed-websocket-8.5.31.jar;C:Userslitan.m2
epositoryorghibernatevalidatorhibernate-validator6.0.10.Finalhibernate-validator-6.0.10.Final.jar;C:Userslitan.m2
epositoryjavaxvalidationvalidation-api2.0.1.Finalvalidation-api-2.0.1.Final.jar;C:Userslitan.m2
epositoryorgjbossloggingjboss-logging3.3.2.Finaljboss-logging-3.3.2.Final.jar;C:Userslitan.m2
epositorycomfasterxmlclassmate1.3.4classmate-1.3.4.jar;C:Userslitan.m2
epositoryorgspringframeworkspring-web5.0.7.RELEASEspring-web-5.0.7.RELEASE.jar;C:Userslitan.m2
epositoryorgspringframeworkspring-beans5.0.7.RELEASEspring-beans-5.0.7.RELEASE.jar;C:Userslitan.m2
epositoryorgspringframeworkspring-webmvc5.0.7.RELEASEspring-webmvc-5.0.7.RELEASE.jar;C:Userslitan.m2
epositoryorgspringframeworkspring-context5.0.7.RELEASEspring-context-5.0.7.RELEASE.jar;C:Userslitan.m2
epositoryorgspringframeworkspring-expression5.0.7.RELEASEspring-expression-5.0.7.RELEASE.jar;C:Userslitan.m2
epositorymysqlmysql-connector-java5.1.46mysql-connector-java-5.1.46.jar;C:Userslitan.m2
epositoryorgspringframeworkspring-core5.0.7.RELEASEspring-core-5.0.7.RELEASE.jar;C:Userslitan.m2
epositoryorgspringframeworkspring-jcl5.0.7.RELEASEspring-jcl-5.0.7.RELEASE.jar;C:Userslitan.m2
epositoryio
etty
etty-all4.1.25.Final
etty-all-4.1.25.Final.jar com.netty.example.TimeServer
 2 22:06:36.427 [main] DEBUG io.netty.util.internal.logging.InternalLoggerFactory - Using SLF4J as the default logging framework
 3 22:06:36.462 [main] DEBUG io.netty.channel.MultithreadEventLoopGroup - -Dio.netty.eventLoopThreads: 8
 4 22:06:36.509 [main] DEBUG io.netty.channel.nio.NioEventLoop - -Dio.netty.noKeySetOptimization: false
 5 22:06:36.509 [main] DEBUG io.netty.channel.nio.NioEventLoop - -Dio.netty.selectorAutoRebuildThreshold: 512
 6 22:06:36.556 [main] DEBUG io.netty.util.internal.PlatformDependent - Platform: Windows
 7 22:06:36.562 [main] DEBUG io.netty.util.internal.PlatformDependent0 - -Dio.netty.noUnsafe: false
 8 22:06:36.563 [main] DEBUG io.netty.util.internal.PlatformDependent0 - Java version: 8
 9 22:06:36.567 [main] DEBUG io.netty.util.internal.PlatformDependent0 - sun.misc.Unsafe.theUnsafe: available
10 22:06:36.568 [main] DEBUG io.netty.util.internal.PlatformDependent0 - sun.misc.Unsafe.copyMemory: available
11 22:06:36.569 [main] DEBUG io.netty.util.internal.PlatformDependent0 - java.nio.Buffer.address: available
12 22:06:36.570 [main] DEBUG io.netty.util.internal.PlatformDependent0 - direct buffer constructor: available
13 22:06:36.571 [main] DEBUG io.netty.util.internal.PlatformDependent0 - java.nio.Bits.unaligned: available, true
14 22:06:36.571 [main] DEBUG io.netty.util.internal.PlatformDependent0 - jdk.internal.misc.Unsafe.allocateUninitializedArray(int): unavailable prior to Java9
15 22:06:36.571 [main] DEBUG io.netty.util.internal.PlatformDependent0 - java.nio.DirectByteBuffer.<init>(long, int): available
16 22:06:36.571 [main] DEBUG io.netty.util.internal.PlatformDependent - sun.misc.Unsafe: available
17 22:06:36.573 [main] DEBUG io.netty.util.internal.PlatformDependent - -Dio.netty.tmpdir: C:UserslitanAppDataLocalTemp (java.io.tmpdir)
18 22:06:36.575 [main] DEBUG io.netty.util.internal.PlatformDependent - -Dio.netty.bitMode: 64 (sun.arch.data.model)
19 22:06:36.577 [main] DEBUG io.netty.util.internal.PlatformDependent - -Dio.netty.noPreferDirect: false
20 22:06:36.577 [main] DEBUG io.netty.util.internal.PlatformDependent - -Dio.netty.maxDirectMemory: 1890582528 bytes
21 22:06:36.577 [main] DEBUG io.netty.util.internal.PlatformDependent - -Dio.netty.uninitializedArrayAllocationThreshold: -1
22 22:06:36.581 [main] DEBUG io.netty.util.internal.CleanerJava6 - java.nio.ByteBuffer.cleaner(): available
23 22:06:36.595 [main] DEBUG io.netty.util.internal.PlatformDependent - org.jctools-core.MpscChunkedArrayQueue: available
24 22:06:37.179 [main] DEBUG io.netty.channel.DefaultChannelId - -Dio.netty.processId: 14124 (auto-detected)
25 22:06:37.186 [main] DEBUG io.netty.util.NetUtil - -Djava.net.preferIPv4Stack: false
26 22:06:37.186 [main] DEBUG io.netty.util.NetUtil - -Djava.net.preferIPv6Addresses: false
27 22:06:37.538 [main] DEBUG io.netty.util.NetUtil - Loopback interface: lo (Software Loopback Interface 1, 127.0.0.1)
28 22:06:37.540 [main] DEBUG io.netty.util.NetUtil - Failed to get SOMAXCONN from sysctl and file procsys
etcoresomaxconn. Default: 200
29 22:06:38.027 [main] DEBUG io.netty.channel.DefaultChannelId - -Dio.netty.machineId: 00:50:56:ff:fe:c0:00:01 (auto-detected)
30 22:06:38.038 [main] DEBUG io.netty.util.internal.InternalThreadLocalMap - -Dio.netty.threadLocalMap.stringBuilder.initialSize: 1024
31 22:06:38.038 [main] DEBUG io.netty.util.internal.InternalThreadLocalMap - -Dio.netty.threadLocalMap.stringBuilder.maxSize: 4096
32 22:06:38.090 [main] DEBUG io.netty.util.ResourceLeakDetector - -Dio.netty.leakDetection.level: simple
33 22:06:38.091 [main] DEBUG io.netty.util.ResourceLeakDetector - -Dio.netty.leakDetection.targetRecords: 4
34 22:06:38.144 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.numHeapArenas: 8
35 22:06:38.145 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.numDirectArenas: 8
36 22:06:38.145 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.pageSize: 8192
37 22:06:38.145 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.maxOrder: 11
38 22:06:38.145 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.chunkSize: 16777216
39 22:06:38.145 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.tinyCacheSize: 512
40 22:06:38.145 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.smallCacheSize: 256
41 22:06:38.145 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.normalCacheSize: 64
42 22:06:38.145 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.maxCachedBufferCapacity: 32768
43 22:06:38.145 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.cacheTrimInterval: 8192
44 22:06:38.145 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.useCacheForAllThreads: true
45 22:06:38.159 [main] DEBUG io.netty.buffer.ByteBufUtil - -Dio.netty.allocator.type: pooled
46 22:06:38.159 [main] DEBUG io.netty.buffer.ByteBufUtil - -Dio.netty.threadLocalDirectBufferSize: 0
47 22:06:38.159 [main] DEBUG io.netty.buffer.ByteBufUtil - -Dio.netty.maxThreadLocalCharBufferSize: 16384

客户端运行后

 1 D:javajava1.8jdk1.8injava.exe "-javaagent:D:我的软件itSoftIntelliJ IDEAIntelliJ IDEA 2018.1.5libidea_rt.jar=59302:D:我的软件itSoftIntelliJ IDEAIntelliJ IDEA 2018.1.5in" -Dfile.encoding=UTF-8 -classpath D:javajava1.8jdk1.8jrelibcharsets.jar;D:javajava1.8jdk1.8jrelibdeploy.jar;D:javajava1.8jdk1.8jrelibextaccess-bridge-64.jar;D:javajava1.8jdk1.8jrelibextcldrdata.jar;D:javajava1.8jdk1.8jrelibextdnsns.jar;D:javajava1.8jdk1.8jrelibextjaccess.jar;D:javajava1.8jdk1.8jrelibextjfxrt.jar;D:javajava1.8jdk1.8jrelibextlocaledata.jar;D:javajava1.8jdk1.8jrelibext
ashorn.jar;D:javajava1.8jdk1.8jrelibextsunec.jar;D:javajava1.8jdk1.8jrelibextsunjce_provider.jar;D:javajava1.8jdk1.8jrelibextsunmscapi.jar;D:javajava1.8jdk1.8jrelibextsunpkcs11.jar;D:javajava1.8jdk1.8jrelibextzipfs.jar;D:javajava1.8jdk1.8jrelibjavaws.jar;D:javajava1.8jdk1.8jrelibjce.jar;D:javajava1.8jdk1.8jrelibjfr.jar;D:javajava1.8jdk1.8jrelibjfxswt.jar;D:javajava1.8jdk1.8jrelibjsse.jar;D:javajava1.8jdk1.8jrelibmanagement-agent.jar;D:javajava1.8jdk1.8jrelibplugin.jar;D:javajava1.8jdk1.8jrelib
esources.jar;D:javajava1.8jdk1.8jrelib
t.jar;D:
etty	argetclasses;C:Userslitan.m2
epositoryorgspringframeworkootspring-boot-starter-data-redis2.0.3.RELEASEspring-boot-starter-data-redis-2.0.3.RELEASE.jar;C:Userslitan.m2
epositoryorgspringframeworkootspring-boot-starter2.0.3.RELEASEspring-boot-starter-2.0.3.RELEASE.jar;C:Userslitan.m2
epositoryorgspringframeworkootspring-boot2.0.3.RELEASEspring-boot-2.0.3.RELEASE.jar;C:Userslitan.m2
epositoryorgspringframeworkootspring-boot-autoconfigure2.0.3.RELEASEspring-boot-autoconfigure-2.0.3.RELEASE.jar;C:Userslitan.m2
epositoryorgspringframeworkootspring-boot-starter-logging2.0.3.RELEASEspring-boot-starter-logging-2.0.3.RELEASE.jar;C:Userslitan.m2
epositorychqoslogbacklogback-classic1.2.3logback-classic-1.2.3.jar;C:Userslitan.m2
epositorychqoslogbacklogback-core1.2.3logback-core-1.2.3.jar;C:Userslitan.m2
epositoryorgapachelogginglog4jlog4j-to-slf4j2.10.0log4j-to-slf4j-2.10.0.jar;C:Userslitan.m2
epositoryorgapachelogginglog4jlog4j-api2.10.0log4j-api-2.10.0.jar;C:Userslitan.m2
epositoryorgslf4jjul-to-slf4j1.7.25jul-to-slf4j-1.7.25.jar;C:Userslitan.m2
epositoryjavaxannotationjavax.annotation-api1.3.2javax.annotation-api-1.3.2.jar;C:Userslitan.m2
epositoryorgyamlsnakeyaml1.19snakeyaml-1.19.jar;C:Userslitan.m2
epositoryorgspringframeworkdataspring-data-redis2.0.8.RELEASEspring-data-redis-2.0.8.RELEASE.jar;C:Userslitan.m2
epositoryorgspringframeworkdataspring-data-keyvalue2.0.8.RELEASEspring-data-keyvalue-2.0.8.RELEASE.jar;C:Userslitan.m2
epositoryorgspringframeworkdataspring-data-commons2.0.8.RELEASEspring-data-commons-2.0.8.RELEASE.jar;C:Userslitan.m2
epositoryorgspringframeworkspring-tx5.0.7.RELEASEspring-tx-5.0.7.RELEASE.jar;C:Userslitan.m2
epositoryorgspringframeworkspring-oxm5.0.7.RELEASEspring-oxm-5.0.7.RELEASE.jar;C:Userslitan.m2
epositoryorgspringframeworkspring-aop5.0.7.RELEASEspring-aop-5.0.7.RELEASE.jar;C:Userslitan.m2
epositoryorgspringframeworkspring-context-support5.0.7.RELEASEspring-context-support-5.0.7.RELEASE.jar;C:Userslitan.m2
epositoryorgslf4jslf4j-api1.7.25slf4j-api-1.7.25.jar;C:Userslitan.m2
epositoryiolettucelettuce-core5.0.4.RELEASElettuce-core-5.0.4.RELEASE.jar;C:Userslitan.m2
epositoryioprojectreactor
eactor-core3.1.8.RELEASE
eactor-core-3.1.8.RELEASE.jar;C:Userslitan.m2
epositoryorg
eactivestreams
eactive-streams1.0.2
eactive-streams-1.0.2.jar;C:Userslitan.m2
epositoryio
etty
etty-common4.1.25.Final
etty-common-4.1.25.Final.jar;C:Userslitan.m2
epositoryio
etty
etty-transport4.1.25.Final
etty-transport-4.1.25.Final.jar;C:Userslitan.m2
epositoryio
etty
etty-buffer4.1.25.Final
etty-buffer-4.1.25.Final.jar;C:Userslitan.m2
epositoryio
etty
etty-resolver4.1.25.Final
etty-resolver-4.1.25.Final.jar;C:Userslitan.m2
epositoryio
etty
etty-handler4.1.25.Final
etty-handler-4.1.25.Final.jar;C:Userslitan.m2
epositoryio
etty
etty-codec4.1.25.Final
etty-codec-4.1.25.Final.jar;C:Userslitan.m2
epositoryorgspringframeworkootspring-boot-starter-web2.0.3.RELEASEspring-boot-starter-web-2.0.3.RELEASE.jar;C:Userslitan.m2
epositoryorgspringframeworkootspring-boot-starter-json2.0.3.RELEASEspring-boot-starter-json-2.0.3.RELEASE.jar;C:Userslitan.m2
epositorycomfasterxmljacksoncorejackson-databind2.9.6jackson-databind-2.9.6.jar;C:Userslitan.m2
epositorycomfasterxmljacksoncorejackson-annotations2.9.0jackson-annotations-2.9.0.jar;C:Userslitan.m2
epositorycomfasterxmljacksoncorejackson-core2.9.6jackson-core-2.9.6.jar;C:Userslitan.m2
epositorycomfasterxmljacksondatatypejackson-datatype-jdk82.9.6jackson-datatype-jdk8-2.9.6.jar;C:Userslitan.m2
epositorycomfasterxmljacksondatatypejackson-datatype-jsr3102.9.6jackson-datatype-jsr310-2.9.6.jar;C:Userslitan.m2
epositorycomfasterxmljacksonmodulejackson-module-parameter-names2.9.6jackson-module-parameter-names-2.9.6.jar;C:Userslitan.m2
epositoryorgspringframeworkootspring-boot-starter-tomcat2.0.3.RELEASEspring-boot-starter-tomcat-2.0.3.RELEASE.jar;C:Userslitan.m2
epositoryorgapache	omcatembed	omcat-embed-core8.5.31	omcat-embed-core-8.5.31.jar;C:Userslitan.m2
epositoryorgapache	omcatembed	omcat-embed-el8.5.31	omcat-embed-el-8.5.31.jar;C:Userslitan.m2
epositoryorgapache	omcatembed	omcat-embed-websocket8.5.31	omcat-embed-websocket-8.5.31.jar;C:Userslitan.m2
epositoryorghibernatevalidatorhibernate-validator6.0.10.Finalhibernate-validator-6.0.10.Final.jar;C:Userslitan.m2
epositoryjavaxvalidationvalidation-api2.0.1.Finalvalidation-api-2.0.1.Final.jar;C:Userslitan.m2
epositoryorgjbossloggingjboss-logging3.3.2.Finaljboss-logging-3.3.2.Final.jar;C:Userslitan.m2
epositorycomfasterxmlclassmate1.3.4classmate-1.3.4.jar;C:Userslitan.m2
epositoryorgspringframeworkspring-web5.0.7.RELEASEspring-web-5.0.7.RELEASE.jar;C:Userslitan.m2
epositoryorgspringframeworkspring-beans5.0.7.RELEASEspring-beans-5.0.7.RELEASE.jar;C:Userslitan.m2
epositoryorgspringframeworkspring-webmvc5.0.7.RELEASEspring-webmvc-5.0.7.RELEASE.jar;C:Userslitan.m2
epositoryorgspringframeworkspring-context5.0.7.RELEASEspring-context-5.0.7.RELEASE.jar;C:Userslitan.m2
epositoryorgspringframeworkspring-expression5.0.7.RELEASEspring-expression-5.0.7.RELEASE.jar;C:Userslitan.m2
epositorymysqlmysql-connector-java5.1.46mysql-connector-java-5.1.46.jar;C:Userslitan.m2
epositoryorgspringframeworkspring-core5.0.7.RELEASEspring-core-5.0.7.RELEASE.jar;C:Userslitan.m2
epositoryorgspringframeworkspring-jcl5.0.7.RELEASEspring-jcl-5.0.7.RELEASE.jar;C:Userslitan.m2
epositoryio
etty
etty-all4.1.25.Final
etty-all-4.1.25.Final.jar com.netty.example.TimeClient
 2 22:07:35.228 [main] DEBUG io.netty.util.internal.logging.InternalLoggerFactory - Using SLF4J as the default logging framework
 3 22:07:35.273 [main] DEBUG io.netty.channel.MultithreadEventLoopGroup - -Dio.netty.eventLoopThreads: 8
 4 22:07:35.329 [main] DEBUG io.netty.channel.nio.NioEventLoop - -Dio.netty.noKeySetOptimization: false
 5 22:07:35.329 [main] DEBUG io.netty.channel.nio.NioEventLoop - -Dio.netty.selectorAutoRebuildThreshold: 512
 6 22:07:35.387 [main] DEBUG io.netty.util.internal.PlatformDependent - Platform: Windows
 7 22:07:35.390 [main] DEBUG io.netty.util.internal.PlatformDependent0 - -Dio.netty.noUnsafe: false
 8 22:07:35.390 [main] DEBUG io.netty.util.internal.PlatformDependent0 - Java version: 8
 9 22:07:35.393 [main] DEBUG io.netty.util.internal.PlatformDependent0 - sun.misc.Unsafe.theUnsafe: available
10 22:07:35.394 [main] DEBUG io.netty.util.internal.PlatformDependent0 - sun.misc.Unsafe.copyMemory: available
11 22:07:35.395 [main] DEBUG io.netty.util.internal.PlatformDependent0 - java.nio.Buffer.address: available
12 22:07:35.396 [main] DEBUG io.netty.util.internal.PlatformDependent0 - direct buffer constructor: available
13 22:07:35.397 [main] DEBUG io.netty.util.internal.PlatformDependent0 - java.nio.Bits.unaligned: available, true
14 22:07:35.397 [main] DEBUG io.netty.util.internal.PlatformDependent0 - jdk.internal.misc.Unsafe.allocateUninitializedArray(int): unavailable prior to Java9
15 22:07:35.397 [main] DEBUG io.netty.util.internal.PlatformDependent0 - java.nio.DirectByteBuffer.<init>(long, int): available
16 22:07:35.397 [main] DEBUG io.netty.util.internal.PlatformDependent - sun.misc.Unsafe: available
17 22:07:35.398 [main] DEBUG io.netty.util.internal.PlatformDependent - -Dio.netty.tmpdir: C:UserslitanAppDataLocalTemp (java.io.tmpdir)
18 22:07:35.398 [main] DEBUG io.netty.util.internal.PlatformDependent - -Dio.netty.bitMode: 64 (sun.arch.data.model)
19 22:07:35.400 [main] DEBUG io.netty.util.internal.PlatformDependent - -Dio.netty.noPreferDirect: false
20 22:07:35.400 [main] DEBUG io.netty.util.internal.PlatformDependent - -Dio.netty.maxDirectMemory: 1890582528 bytes
21 22:07:35.400 [main] DEBUG io.netty.util.internal.PlatformDependent - -Dio.netty.uninitializedArrayAllocationThreshold: -1
22 22:07:35.401 [main] DEBUG io.netty.util.internal.CleanerJava6 - java.nio.ByteBuffer.cleaner(): available
23 22:07:35.420 [main] DEBUG io.netty.util.internal.PlatformDependent - org.jctools-core.MpscChunkedArrayQueue: available
24 22:07:36.030 [main] DEBUG io.netty.channel.DefaultChannelId - -Dio.netty.processId: 18472 (auto-detected)
25 22:07:36.032 [main] DEBUG io.netty.util.NetUtil - -Djava.net.preferIPv4Stack: false
26 22:07:36.033 [main] DEBUG io.netty.util.NetUtil - -Djava.net.preferIPv6Addresses: false
27 22:07:36.324 [main] DEBUG io.netty.util.NetUtil - Loopback interface: lo (Software Loopback Interface 1, 127.0.0.1)
28 22:07:36.325 [main] DEBUG io.netty.util.NetUtil - Failed to get SOMAXCONN from sysctl and file procsys
etcoresomaxconn. Default: 200
29 22:07:36.895 [main] DEBUG io.netty.channel.DefaultChannelId - -Dio.netty.machineId: 00:50:56:ff:fe:c0:00:01 (auto-detected)
30 22:07:36.905 [main] DEBUG io.netty.util.internal.InternalThreadLocalMap - -Dio.netty.threadLocalMap.stringBuilder.initialSize: 1024
31 22:07:36.905 [main] DEBUG io.netty.util.internal.InternalThreadLocalMap - -Dio.netty.threadLocalMap.stringBuilder.maxSize: 4096
32 22:07:36.938 [main] DEBUG io.netty.util.ResourceLeakDetector - -Dio.netty.leakDetection.level: simple
33 22:07:36.938 [main] DEBUG io.netty.util.ResourceLeakDetector - -Dio.netty.leakDetection.targetRecords: 4
34 22:07:36.992 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.numHeapArenas: 8
35 22:07:36.992 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.numDirectArenas: 8
36 22:07:36.992 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.pageSize: 8192
37 22:07:36.992 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.maxOrder: 11
38 22:07:36.992 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.chunkSize: 16777216
39 22:07:36.992 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.tinyCacheSize: 512
40 22:07:36.992 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.smallCacheSize: 256
41 22:07:36.992 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.normalCacheSize: 64
42 22:07:36.992 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.maxCachedBufferCapacity: 32768
43 22:07:36.992 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.cacheTrimInterval: 8192
44 22:07:36.992 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.useCacheForAllThreads: true
45 22:07:37.029 [main] DEBUG io.netty.buffer.ByteBufUtil - -Dio.netty.allocator.type: pooled
46 22:07:37.029 [main] DEBUG io.netty.buffer.ByteBufUtil - -Dio.netty.threadLocalDirectBufferSize: 0
47 22:07:37.029 [main] DEBUG io.netty.buffer.ByteBufUtil - -Dio.netty.maxThreadLocalCharBufferSize: 16384
48 22:07:37.080 [nioEventLoopGroup-2-1] DEBUG io.netty.buffer.AbstractByteBuf - -Dio.netty.buffer.bytebuf.checkAccessible: true
49 22:07:37.084 [nioEventLoopGroup-2-1] DEBUG io.netty.util.ResourceLeakDetectorFactory - Loaded default ResourceLeakDetector: io.netty.util.ResourceLeakDetector@75655d17
50 22:07:37.126 [nioEventLoopGroup-2-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.maxCapacityPerThread: 4096
51 22:07:37.126 [nioEventLoopGroup-2-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.maxSharedCapacityFactor: 2
52 22:07:37.126 [nioEventLoopGroup-2-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.linkCapacity: 16
53 22:07:37.126 [nioEventLoopGroup-2-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.ratio: 8
54 Now isMon Jul 16 22:07:37 CST 2018

上面客户端最后一行是收到的服务端的应答,此时的服务端也出现了收到消息的语句,最后部分代码如下(重点是最后一行的请求消息):

1 22:06:38.159 [main] DEBUG io.netty.buffer.ByteBufUtil - -Dio.netty.maxThreadLocalCharBufferSize: 16384
2 22:07:37.160 [nioEventLoopGroup-3-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.maxCapacityPerThread: 4096
3 22:07:37.160 [nioEventLoopGroup-3-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.maxSharedCapacityFactor: 2
4 22:07:37.160 [nioEventLoopGroup-3-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.linkCapacity: 16
5 22:07:37.160 [nioEventLoopGroup-3-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.ratio: 8
6 22:07:37.176 [nioEventLoopGroup-3-1] DEBUG io.netty.buffer.AbstractByteBuf - -Dio.netty.buffer.bytebuf.checkAccessible: true
7 22:07:37.179 [nioEventLoopGroup-3-1] DEBUG io.netty.util.ResourceLeakDetectorFactory - Loaded default ResourceLeakDetector: io.netty.util.ResourceLeakDetector@164ea267
8 The time server receive order:QUERY TIME ORDER

运行OK!

原文地址:https://www.cnblogs.com/xiaoyao-001/p/9308552.html