diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml
new file mode 100644
index 0000000000000000000000000000000000000000..2b63946d5b31084bbb7dda418ceb3d75eb686373
--- /dev/null
+++ b/.idea/uiDesigner.xml
@@ -0,0 +1,124 @@
+
+
+
+
+ -
+
+
+ -
+
+
+ -
+
+
+ -
+
+
+ -
+
+
+
+
+
+ -
+
+
+
+
+
+ -
+
+
+
+
+
+ -
+
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+ -
+
+
+ -
+
+
+ -
+
+
+ -
+
+
+
+
+ -
+
+
+ -
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index f8da3960feb06c4a85a64ab09fbd3c545c8a5245..f6c3be81df6b587feb82da5eb87b98969e2d8ce9 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -4,7 +4,20 @@
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -30,6 +43,7 @@
+
@@ -42,7 +56,11 @@
-
+
+
+
+
+
@@ -81,10 +99,14 @@
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -122,23 +188,29 @@
-
-
+
-
+
+
+
+
+
-
-
+
+
+
+
+
@@ -158,7 +230,7 @@
1706188747019
-
+
@@ -168,12 +240,39 @@
1706190216538
-
+
+
+ 1706191158591
+
+
+
+ 1706191158591
+
+
+
+ 1706191426158
+
+
+
+ 1706191426158
+
+
+
+
+
diff --git a/netty-client/pom.xml b/netty-client/pom.xml
index b23916e4f18c441aeee7954babdc4b39e2e9a451..e4d8b17c1227c852fc26587a052e70452fd94c74 100644
--- a/netty-client/pom.xml
+++ b/netty-client/pom.xml
@@ -22,5 +22,13 @@
io.netty
netty-all
+
+ org.springframework.boot
+ spring-boot-starter-logging
+
+
+ org.projectlombok
+ lombok
+
\ No newline at end of file
diff --git a/netty-client/src/main/java/tool/zyk/channel/ChannelTestClient.java b/netty-client/src/main/java/tool/zyk/channel/ChannelTestClient.java
new file mode 100644
index 0000000000000000000000000000000000000000..3efe59a460ecc4b8ecb24f1c0a96c890a246df19
--- /dev/null
+++ b/netty-client/src/main/java/tool/zyk/channel/ChannelTestClient.java
@@ -0,0 +1,43 @@
+package tool.zyk.channel;
+
+import io.netty.bootstrap.Bootstrap;
+import io.netty.buffer.ByteBufAllocator;
+import io.netty.channel.Channel;
+import io.netty.channel.ChannelFuture;
+import io.netty.channel.ChannelFutureListener;
+import io.netty.channel.ChannelInitializer;
+import io.netty.channel.nio.NioEventLoopGroup;
+import io.netty.channel.socket.nio.NioSocketChannel;
+import io.netty.handler.logging.LogLevel;
+import io.netty.handler.logging.LoggingHandler;
+import lombok.extern.slf4j.Slf4j;
+
+import java.util.concurrent.TimeUnit;
+
+/**
+ * author: zyk
+ * datetime: 2024/1/25 22:41
+ * describe:
+ */
+@Slf4j
+public class ChannelTestClient {
+ public static void main(String[] args) throws InterruptedException {
+ ChannelFuture channelFuture = new Bootstrap()
+ .group(new NioEventLoopGroup())
+ .channel(NioSocketChannel.class)
+ .handler(new ChannelInitializer() {
+ @Override
+ protected void initChannel(Channel channel) throws Exception {
+ System.out.println("init...");
+ channel.pipeline().addLast(new LoggingHandler(LogLevel.DEBUG));
+ }
+ })
+ .connect("127.0.0.1", 8081);
+ log.warn("channel:{}", channelFuture.channel());
+ channelFuture.addListener((ChannelFutureListener) future -> log.warn("channel in listener:{}", future.channel()));
+// channelFuture.sync();
+// log.warn("channel:{}",channelFuture.channel());
+
+
+ }
+}
diff --git a/netty-client/src/main/java/tool/zyk/eventgroup/EventGroupTestClient.java b/netty-client/src/main/java/tool/zyk/eventgroup/EventGroupTestClient.java
new file mode 100644
index 0000000000000000000000000000000000000000..2a65335b76c1eab5c1f0c065812e85f051574af0
--- /dev/null
+++ b/netty-client/src/main/java/tool/zyk/eventgroup/EventGroupTestClient.java
@@ -0,0 +1,44 @@
+package tool.zyk.eventgroup;
+
+import io.netty.bootstrap.Bootstrap;
+import io.netty.buffer.ByteBufAllocator;
+import io.netty.channel.Channel;
+import io.netty.channel.ChannelInitializer;
+import io.netty.channel.nio.NioEventLoopGroup;
+import io.netty.channel.socket.nio.NioSocketChannel;
+import io.netty.handler.codec.string.StringEncoder;
+import io.netty.handler.logging.LogLevel;
+import io.netty.handler.logging.LoggingHandler;
+import lombok.extern.slf4j.Slf4j;
+
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * author: zyk
+ * datetime: 2024/1/25 22:15
+ * describe:
+ */
+@Slf4j
+public class EventGroupTestClient {
+ public static void main(String[] args) throws InterruptedException {
+ Channel channel = new Bootstrap()
+ .group(new NioEventLoopGroup())
+ .channel(NioSocketChannel.class)
+ .handler(new ChannelInitializer() {
+ @Override
+ protected void initChannel(Channel channel) throws Exception {
+ System.out.println("init...");
+ channel.pipeline().addLast(new LoggingHandler(LogLevel.DEBUG));
+ }
+ })
+ .connect("127.0.0.1", 8081)
+ .sync()
+ .channel();
+ channel.writeAndFlush(ByteBufAllocator.DEFAULT.buffer().writeBytes("zhangyongkang".getBytes()));
+ TimeUnit.SECONDS.sleep(2);
+ channel.writeAndFlush(ByteBufAllocator.DEFAULT.buffer().writeBytes("zhangyongkang".getBytes()));
+
+ }
+}
diff --git a/netty-client/src/main/java/tool/zyk/handler_pipeline/HandlerClientTest.java b/netty-client/src/main/java/tool/zyk/handler_pipeline/HandlerClientTest.java
new file mode 100644
index 0000000000000000000000000000000000000000..2d5d5255eab0b17cca119761b81271ebbd97e900
--- /dev/null
+++ b/netty-client/src/main/java/tool/zyk/handler_pipeline/HandlerClientTest.java
@@ -0,0 +1,44 @@
+package tool.zyk.handler_pipeline;
+
+import io.netty.bootstrap.Bootstrap;
+import io.netty.buffer.ByteBufAllocator;
+import io.netty.channel.Channel;
+import io.netty.channel.ChannelFuture;
+import io.netty.channel.ChannelFutureListener;
+import io.netty.channel.ChannelInitializer;
+import io.netty.channel.nio.NioEventLoopGroup;
+import io.netty.channel.socket.nio.NioSocketChannel;
+import io.netty.handler.codec.string.StringEncoder;
+import io.netty.handler.logging.LogLevel;
+import io.netty.handler.logging.LoggingHandler;
+import lombok.extern.slf4j.Slf4j;
+
+import java.util.concurrent.TimeUnit;
+
+/**
+ * author: zyk
+ * datetime: 2024/1/25 22:59
+ * describe:
+ */
+@Slf4j
+public class HandlerClientTest {
+ public static void main(String[] args) throws InterruptedException {
+ ChannelFuture channelFuture = new Bootstrap()
+ .group(new NioEventLoopGroup())
+ .channel(NioSocketChannel.class)
+ .handler(new ChannelInitializer() {
+ @Override
+ protected void initChannel(Channel channel) throws Exception {
+ System.out.println("init...");
+ channel.pipeline().addLast(new LoggingHandler(LogLevel.DEBUG));
+ channel.pipeline().addLast(new StringEncoder());
+ }
+ })
+ .connect("127.0.0.1", 8082);
+ channelFuture.addListener((ChannelFutureListener) future -> {
+ Channel channel = future.channel();
+ log.info("channel:{}",channel);
+ channel.writeAndFlush("hello world");
+ });
+ }
+}
diff --git a/netty-server/pom.xml b/netty-server/pom.xml
index 7cd540242e7a4baf1539beda0c50bf52b16b06c6..4e99484852ce02543ec5f4b6673813d34cce0ee1 100644
--- a/netty-server/pom.xml
+++ b/netty-server/pom.xml
@@ -30,5 +30,14 @@
org.projectlombok
lombok
+
+ org.springframework.boot
+ spring-boot-starter-test
+
+
+ org.junit.jupiter
+ junit-jupiter-api
+ test
+
\ No newline at end of file
diff --git a/netty-server/src/main/java/tool/zyk/eventgroup/EventGroupServerTest.java b/netty-server/src/main/java/tool/zyk/eventgroup/EventGroupServerTest.java
new file mode 100644
index 0000000000000000000000000000000000000000..8207a8ad0efe66982b3b7deeba7c256635e29170
--- /dev/null
+++ b/netty-server/src/main/java/tool/zyk/eventgroup/EventGroupServerTest.java
@@ -0,0 +1,53 @@
+package tool.zyk.eventgroup;
+
+import io.netty.bootstrap.ServerBootstrap;
+import io.netty.buffer.ByteBuf;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.channel.ChannelInboundHandlerAdapter;
+import io.netty.channel.ChannelInitializer;
+import io.netty.channel.DefaultEventLoopGroup;
+import io.netty.channel.nio.NioEventLoopGroup;
+import io.netty.channel.socket.nio.NioServerSocketChannel;
+import io.netty.channel.socket.nio.NioSocketChannel;
+import io.netty.handler.logging.LogLevel;
+import io.netty.handler.logging.LoggingHandler;
+import lombok.extern.slf4j.Slf4j;
+
+import java.util.Optional;
+
+/**
+ * author: zyk
+ * datetime: 2024/1/25 22:14
+ * describe:
+ */
+@Slf4j
+public class EventGroupServerTest {
+ public static void main(String[] args) {
+ DefaultEventLoopGroup normalWorkers=new DefaultEventLoopGroup(2);
+ new ServerBootstrap()
+ .group(new NioEventLoopGroup(1), new NioEventLoopGroup(2))
+ .channel(NioServerSocketChannel.class)
+ .childHandler(new ChannelInitializer() {
+ @Override
+ protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception {
+ nioSocketChannel.pipeline().addLast(new LoggingHandler(LogLevel.DEBUG));
+ nioSocketChannel.pipeline().addLast(normalWorkers,"myHandler",
+ new ChannelInboundHandlerAdapter() {
+ @Override
+ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
+ System.out.println(msg);
+ ByteBuf byteBuf = msg instanceof ByteBuf ? (ByteBuf) msg : null;
+ Optional.ofNullable(byteBuf).ifPresent(byt -> {
+ byte[] buf = new byte[16];
+ ByteBuf len = byt.readBytes(buf, 0, byt.readableBytes());
+ log.info("传来的数据:{}", new String(buf));
+ });
+ }
+ }
+ );
+
+ }
+ })
+ .bind(8081);
+ }
+}
diff --git a/netty-server/src/main/java/tool/zyk/handler_pipeline/HandlerServerTest.java b/netty-server/src/main/java/tool/zyk/handler_pipeline/HandlerServerTest.java
new file mode 100644
index 0000000000000000000000000000000000000000..b05c94ad8b4a87947bb26dac10fbb871fc3df28d
--- /dev/null
+++ b/netty-server/src/main/java/tool/zyk/handler_pipeline/HandlerServerTest.java
@@ -0,0 +1,75 @@
+package tool.zyk.handler_pipeline;
+
+import io.netty.bootstrap.ServerBootstrap;
+import io.netty.channel.*;
+import io.netty.channel.nio.NioEventLoopGroup;
+import io.netty.channel.socket.nio.NioServerSocketChannel;
+import io.netty.channel.socket.nio.NioSocketChannel;
+import lombok.extern.slf4j.Slf4j;
+
+/**
+ * author: zyk
+ * datetime: 2024/1/25 23:00
+ * describe:
+ */
+@Slf4j
+public class HandlerServerTest {
+ public static void main(String[] args) {
+ new ServerBootstrap()
+ .channel(NioServerSocketChannel.class)
+ .group(new NioEventLoopGroup())
+ .childHandler(new ChannelInitializer() {
+ @Override
+ protected void initChannel(NioSocketChannel ch) throws Exception {
+ log.info("channel 进来了");
+ ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {
+ @Override
+ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
+ log.info("invoke 1");
+ super.channelRead(ctx, msg);
+ }
+ });
+ ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {
+ @Override
+ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
+ log.info("invoke 2");
+ ctx.channel().write(msg);
+// super.channelRead(ctx, msg);
+ }
+ });
+ ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {
+ @Override
+ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
+ log.info("invoke 3");
+ super.channelRead(ctx, msg);
+// ctx.channel().write(msg);
+ }
+ });
+
+ ch.pipeline().addLast(new ChannelOutboundHandlerAdapter() {
+ @Override
+ public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
+ log.info("outbound 4");
+ super.write(ctx, msg, promise);
+ }
+ });
+ ch.pipeline().addLast(new ChannelOutboundHandlerAdapter() {
+ @Override
+ public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
+ log.info("outbound 5");
+ super.write(ctx, msg, promise);
+ }
+ });
+ ch.pipeline().addLast(new ChannelOutboundHandlerAdapter() {
+ @Override
+ public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
+ log.info("outbound 6");
+ super.write(ctx, msg, promise);
+ }
+ });
+
+ }
+ })
+ .bind(8082);
+ }
+}
diff --git a/netty-server/src/test/java/tool/zyk/eventgroup/EventGroupTest.java b/netty-server/src/test/java/tool/zyk/eventgroup/EventGroupTest.java
new file mode 100644
index 0000000000000000000000000000000000000000..0e09cc2b9efc3f1247c8cffa8cb075abdb14b12a
--- /dev/null
+++ b/netty-server/src/test/java/tool/zyk/eventgroup/EventGroupTest.java
@@ -0,0 +1,42 @@
+package tool.zyk.eventgroup;
+
+import io.netty.channel.nio.NioEventLoopGroup;
+import lombok.extern.slf4j.Slf4j;
+import org.junit.jupiter.api.Test;
+
+import java.util.concurrent.TimeUnit;
+
+/**
+ * author: zyk
+ * datetime: 2024/1/25 22:30
+ * describe:
+ */
+@Slf4j
+public class EventGroupTest {
+
+ //处理普通任务
+ @Test
+ public void test1() throws InterruptedException {
+ NioEventLoopGroup eventExecutors = new NioEventLoopGroup(2);
+ log.debug("server start");
+ TimeUnit.SECONDS.sleep(2);
+ eventExecutors.execute(() -> {
+ log.debug("working ~");
+ });
+ eventExecutors.execute(() -> {
+ log.debug("working ~");
+ });
+ eventExecutors.execute(() -> {
+ log.debug("working ~");
+ });
+ }
+
+ //处理定时任务
+ @Test
+ public void invokeTimeTask() {
+ NioEventLoopGroup eventExecutors = new NioEventLoopGroup(2);
+ eventExecutors.scheduleAtFixedRate(() -> log.info("执行定时任务~"),
+ 0, 1, TimeUnit.SECONDS);
+
+ }
+}
diff --git a/netty-server/src/test/java/tool/zyk/promise/PromiseTest.java b/netty-server/src/test/java/tool/zyk/promise/PromiseTest.java
new file mode 100644
index 0000000000000000000000000000000000000000..09fc1e83c7494c3d59061dabee5fe74e1818f897
--- /dev/null
+++ b/netty-server/src/test/java/tool/zyk/promise/PromiseTest.java
@@ -0,0 +1,72 @@
+package tool.zyk.promise;
+
+import io.netty.channel.DefaultEventLoop;
+import io.netty.util.concurrent.DefaultPromise;
+import lombok.extern.slf4j.Slf4j;
+import org.junit.jupiter.api.Test;
+
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * author: zyk
+ * datetime: 2024/1/25 22:48
+ * describe:
+ */
+@Slf4j
+public class PromiseTest {
+
+ @Test//同步处理任务
+ public void test1() throws ExecutionException, InterruptedException {
+ DefaultEventLoop eventExecutors = new DefaultEventLoop();
+ DefaultPromise promise = new DefaultPromise<>(eventExecutors);
+
+ eventExecutors.execute(()->{
+ try {
+ TimeUnit.SECONDS.sleep(2);
+ } catch (InterruptedException e) {
+ throw new RuntimeException(e);
+ }
+ log.debug("success :{}",10);
+ promise.setSuccess(10);
+ });
+ eventExecutors.execute(()->{
+ try {
+ TimeUnit.SECONDS.sleep(1);
+ } catch (InterruptedException e) {
+ throw new RuntimeException(e);
+ }
+ log.debug("success :{}",20);
+ promise.setSuccess(20);
+ });
+
+ log.debug("start...");
+ log.debug("getNow:{}",promise.getNow());
+ log.debug("getBlock:{}",promise.get());//阻塞
+
+ }
+
+ @Test//异步处理任务
+ public void test2() throws InterruptedException {
+ DefaultEventLoop eventExecutors = new DefaultEventLoop();
+ DefaultPromise promise = new DefaultPromise<>(eventExecutors);
+
+ promise.addListener(future -> {//成功之后的回掉
+ log.debug("getNow:{}",future.getNow());
+ });
+
+ eventExecutors.execute(()->{
+ try {
+ TimeUnit.SECONDS.sleep(2);
+ } catch (InterruptedException e) {
+ throw new RuntimeException(e);
+ }
+ log.debug("success :{}",10);
+ promise.setSuccess(10);
+ });
+
+
+ log.debug("start..");
+ TimeUnit.SECONDS.sleep(10);
+ }
+}
diff --git a/pom.xml b/pom.xml
index 82098ecad16d3922eaa103dab6c77a00d6dfaf3a..4f9bdee8eeef34310f963ccb77438621aa24dd1b 100644
--- a/pom.xml
+++ b/pom.xml
@@ -24,6 +24,7 @@
UTF-8
4.1.39.Final
1.18.30
+ 5.7.0
@@ -37,6 +38,15 @@
lombok
${lombok.version}
+
+
+
+ org.junit.jupiter
+ junit-jupiter-api
+ ${junit-api.version}
+ test
+
+