diff --git a/.idea/compiler.xml b/.idea/compiler.xml index 91dae2f..eeaf528 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -13,6 +13,7 @@ + @@ -33,6 +34,7 @@ + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml index de949f4..b426b26 100644 --- a/.idea/encodings.xml +++ b/.idea/encodings.xml @@ -15,6 +15,7 @@ + diff --git a/.idea/misc.xml b/.idea/misc.xml index 583a849..04e58ef 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -6,6 +6,7 @@ diff --git a/netty/common/pom.xml b/netty/common/pom.xml index c04ed68..fe7fbb9 100644 --- a/netty/common/pom.xml +++ b/netty/common/pom.xml @@ -3,7 +3,7 @@ 4.0.0 cn.bunny - bunny-template + netty 0.0.1-SNAPSHOT diff --git a/netty/model/pom.xml b/netty/model/pom.xml index 33eccf6..d3756f7 100644 --- a/netty/model/pom.xml +++ b/netty/model/pom.xml @@ -3,7 +3,7 @@ 4.0.0 cn.bunny - bunny-template + netty 0.0.1-SNAPSHOT diff --git a/netty/module/pom.xml b/netty/module/pom.xml index 3a479ab..484667b 100644 --- a/netty/module/pom.xml +++ b/netty/module/pom.xml @@ -3,7 +3,7 @@ 4.0.0 cn.bunny - bunny-template + netty 0.0.1-SNAPSHOT diff --git a/netty/pom.xml b/netty/pom.xml index 4325dff..8c943dd 100644 --- a/netty/pom.xml +++ b/netty/pom.xml @@ -9,7 +9,7 @@ cn.bunny - bunny-template + netty 0.0.1-SNAPSHOT pom bunny-template diff --git a/netty/service/pom.xml b/netty/service/pom.xml index f738218..d3b78eb 100644 --- a/netty/service/pom.xml +++ b/netty/service/pom.xml @@ -3,7 +3,7 @@ 4.0.0 cn.bunny - bunny-template + netty 0.0.1-SNAPSHOT diff --git a/netty/service/src/test/java/cn/bunny/CustomPasswordEncoderTest.java b/netty/service/src/test/java/cn/bunny/CustomPasswordEncoderTest.java deleted file mode 100644 index f952f74..0000000 --- a/netty/service/src/test/java/cn/bunny/CustomPasswordEncoderTest.java +++ /dev/null @@ -1,27 +0,0 @@ -package cn.bunny; - -import cn.bunny.security.custom.CustomPasswordEncoder; -import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.security.crypto.password.PasswordEncoder; - -@SpringBootTest(classes = CustomPasswordEncoder.class) -class CustomPasswordEncoderTest { - @Autowired - private CustomPasswordEncoder customPasswordEncoder; - @Autowired - private PasswordEncoder passwordEncoder; - - @Test - void testCustomPasswordEncoder() { - String encode = customPasswordEncoder.encode("111111"); - System.out.println(encode); - } - - @Test - void testPasswordEncoder() { - String encode = passwordEncoder.encode("111111"); - System.out.println(encode); - } -} \ No newline at end of file diff --git a/netty/service/src/test/java/cn/bunny/demo1/TestDemoClient.java b/netty/service/src/test/java/cn/bunny/demo1/TestDemoClient.java new file mode 100644 index 0000000..827af44 --- /dev/null +++ b/netty/service/src/test/java/cn/bunny/demo1/TestDemoClient.java @@ -0,0 +1,47 @@ +package cn.bunny.demo1; + + +import io.netty.bootstrap.Bootstrap; +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 java.util.Date; + +public class TestDemoClient { + public static void main(String[] args) throws InterruptedException { + // 启动类 + new Bootstrap() + // 添加EventLoop + .group(new NioEventLoopGroup()) + // 选择客户端 Channelt + .handler(new ChannelInitializer() { + @Override// 在连接建立后调用 + protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception { + nioSocketChannel.pipeline().addLast(new StringEncoder()); + } + }) + .connect("localhost", 8080) + .sync() + .channel() + // 向服务器发送数据 + .writeAndFlush("你好啊啊啊"); + } + + private static void m1() throws InterruptedException { + new Bootstrap() + .group(new NioEventLoopGroup()) // 1 + .channel(NioSocketChannel.class) // 2 + .handler(new ChannelInitializer() { // 3 + @Override + protected void initChannel(NioSocketChannel ch) { + ch.pipeline().addLast(new StringEncoder()); // 8 + } + }) + .connect("127.0.0.1", 8080) // 4 + .sync() // 5 + .channel() // 6 + .writeAndFlush(new Date() + ": hello world!"); // 7 + } +} diff --git a/netty/service/src/test/java/cn/bunny/demo1/TestDemoServer.java b/netty/service/src/test/java/cn/bunny/demo1/TestDemoServer.java new file mode 100644 index 0000000..43029ee --- /dev/null +++ b/netty/service/src/test/java/cn/bunny/demo1/TestDemoServer.java @@ -0,0 +1,36 @@ +package cn.bunny.demo1; + +import io.netty.bootstrap.ServerBootstrap; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInboundHandlerAdapter; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.nio.NioServerSocketChannel; +import io.netty.channel.socket.nio.NioSocketChannel; +import io.netty.handler.codec.string.StringDecoder; + +public class TestDemoServer { + public static void main(String[] args) { + // 服务器端启动器,负责组装 netty 组件,启动服务器 + new ServerBootstrap() + // 2. BossEventLoop,WorkEventLoop group组 + .group(new NioEventLoopGroup()) + // 3. 选择服务器ServerSocketChannel 实现 + .channel(NioServerSocketChannel.class) + // 4. boss 负责处理连接,决定work可以做哪些事情 + .childHandler(new ChannelInitializer() { + @Override + protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception { + // 将By特Buffer转为字符串 + nioSocketChannel.pipeline().addLast(new StringDecoder()); + nioSocketChannel.pipeline().addLast(new ChannelInboundHandlerAdapter() {// 自定义handler + @Override// 读事件 + public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + // 消息字符串 + System.out.println(msg); + } + }); + } + }).bind(8080); + } +} diff --git a/netty/service/src/test/java/cn/bunny/demo2/TestGroupLoop.java b/netty/service/src/test/java/cn/bunny/demo2/TestGroupLoop.java new file mode 100644 index 0000000..82f4136 --- /dev/null +++ b/netty/service/src/test/java/cn/bunny/demo2/TestGroupLoop.java @@ -0,0 +1,44 @@ +package cn.bunny.demo2; + +import io.netty.channel.DefaultEventLoopGroup; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.util.NettyRuntime; +import lombok.extern.slf4j.Slf4j; + +import java.util.concurrent.TimeUnit; + +@Slf4j +public class TestGroupLoop { + public static void main(String[] args) { + // io 事件普通任务、定时任务 + NioEventLoopGroup group = new NioEventLoopGroup(); + // 获取下一个事件循环对象 + System.out.println(group.next()); + System.out.println(group.next()); + System.out.println(group.next()); + System.out.println(group.next()); + // 执行普通任务 + group.next().submit(() -> { + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + log.debug("执行完成"); + }); + + // 执行定时任务 + group.next().scheduleAtFixedRate(() -> { + log.debug("定时任务的执行"); + }, 0, 1, TimeUnit.SECONDS); + } + + private static void test1() { + // io 事件普通任务,定时任务 + NioEventLoopGroup group = new NioEventLoopGroup(); + // 普通任务,定时任务 + DefaultEventLoopGroup eventLoopGroup = new DefaultEventLoopGroup(); + // 查看系统是几核 + System.out.println(NettyRuntime.availableProcessors()); + } +}