From cf90766bec6258728dac2ec9fcb71e7bd0bc56a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=A6=E8=8B=B1=E6=9D=B0?= <327782001@qq.com> Date: Thu, 27 Apr 2023 19:46:12 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E5=8D=8F=E8=AE=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../shuyu/advance_02_protocol/TestHttp.java | 79 ++++++++++++++++ .../advance_02_protocol/TestRedisClient.java | 89 +++++++++++++++++++ 2 files changed, 168 insertions(+) create mode 100644 netty-03-jinjie/src/main/java/com/kwan/shuyu/advance_02_protocol/TestHttp.java create mode 100644 netty-03-jinjie/src/main/java/com/kwan/shuyu/advance_02_protocol/TestRedisClient.java diff --git a/netty-03-jinjie/src/main/java/com/kwan/shuyu/advance_02_protocol/TestHttp.java b/netty-03-jinjie/src/main/java/com/kwan/shuyu/advance_02_protocol/TestHttp.java new file mode 100644 index 0000000..fc2c9c6 --- /dev/null +++ b/netty-03-jinjie/src/main/java/com/kwan/shuyu/advance_02_protocol/TestHttp.java @@ -0,0 +1,79 @@ +package com.kwan.shuyu.advance_02_protocol; + +import io.netty.bootstrap.ServerBootstrap; +import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.SimpleChannelInboundHandler; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.SocketChannel; +import io.netty.channel.socket.nio.NioServerSocketChannel; +import io.netty.handler.codec.http.*; +import io.netty.handler.logging.LogLevel; +import io.netty.handler.logging.LoggingHandler; +import lombok.extern.slf4j.Slf4j; + +import static com.google.common.net.HttpHeaders.CONTENT_LENGTH; + +/** + * 测试 netty提供的 Http协议接口 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/4/27 19:38 + */ +@Slf4j +public class TestHttp { + public static void main(String[] args) { + final NioEventLoopGroup boss = new NioEventLoopGroup(); + final NioEventLoopGroup worker = new NioEventLoopGroup(); + try { + final ServerBootstrap bs = new ServerBootstrap(); + bs.channel(NioServerSocketChannel.class); + bs.group(boss, worker); + bs.childHandler(new ChannelInitializer() { + @Override + protected void initChannel(SocketChannel ch) throws Exception { + ch.pipeline().addLast(new LoggingHandler(LogLevel.DEBUG)); + // 做http服务器端 , 解码器对请求进行解码 + ch.pipeline().addLast(new HttpServerCodec()); + /** + * SimpleChannelInboundHandler 可以指定 只关心某一种类型的Handler + */ + ch.pipeline().addLast(new SimpleChannelInboundHandler() { // 或者:HttpContent + @Override + protected void channelRead0(ChannelHandlerContext ctx, HttpRequest msg) throws Exception { + log.debug("******************************************************** Get请求"); + // 请求行 + log.debug("msg.uri() = " + msg.uri()); + log.debug("msg.headers() = " + msg.headers()); + // 返回 响应 new DefaultFullHttpResponse(Http协议版本,Http状态码) + final DefaultFullHttpResponse response = new DefaultFullHttpResponse(msg.protocolVersion(), HttpResponseStatus.OK); + final byte[] bytes = "

Hello, World

".getBytes(); + response.headers().setInt(CONTENT_LENGTH, bytes.length); // 【告诉客户端 消息长度,防止一直转圈】 + response.content().writeBytes(bytes); + // 写回响应 + ctx.writeAndFlush(response); + } + }); + // 下面自己随便写的 + ch.pipeline().addLast(new SimpleChannelInboundHandler() { + @Override + protected void channelRead0(ChannelHandlerContext ctx, HttpContent msg) throws Exception { + log.debug("########################################################## Post请求"); + // 请求行 + log.debug("msg.toString() = " + msg.toString()); + } + }); + } + }); + final ChannelFuture channelFuture = bs.bind(8080).sync(); + channelFuture.channel().closeFuture().sync(); + } catch (InterruptedException e) { + log.error("Server error", e); + } finally { + boss.shutdownGracefully(); + worker.shutdownGracefully(); + } + } +} \ No newline at end of file diff --git a/netty-03-jinjie/src/main/java/com/kwan/shuyu/advance_02_protocol/TestRedisClient.java b/netty-03-jinjie/src/main/java/com/kwan/shuyu/advance_02_protocol/TestRedisClient.java new file mode 100644 index 0000000..1ed234b --- /dev/null +++ b/netty-03-jinjie/src/main/java/com/kwan/shuyu/advance_02_protocol/TestRedisClient.java @@ -0,0 +1,89 @@ +package com.kwan.shuyu.advance_02_protocol; + +import io.netty.bootstrap.Bootstrap; +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelFuture; +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.SocketChannel; +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.nio.charset.Charset; + +/** + * 以redis协议 为例 【首先开启redis】 + * 命令 :set key value + * 命令内容 :set name zhangsan + * 首先 把整个看成一个数组 + * + * 协议内容: + * ``` + * 3 数组个数 + * $3 key命令长度 + * set + * $4 key值 内容长度 + * name + * $8 value值 内容长度 + * zhangsan + * ``` + * + */ +@Slf4j +public class TestRedisClient { + public static void main(String[] args) { + final byte[] LINE = {'\r', '\n'}; + final NioEventLoopGroup worker = new NioEventLoopGroup(); + try { + final Bootstrap bs = new Bootstrap(); + bs.channel(NioSocketChannel.class); + bs.group(worker); + bs.handler(new ChannelInitializer() { + @Override + protected void initChannel(SocketChannel ch) throws Exception { + ch.pipeline().addLast(new LoggingHandler(LogLevel.DEBUG)); + ch.pipeline().addLast(new ChannelInboundHandlerAdapter() { + // 连接建立 运行 ,模拟redis协议 发送数据 + @Override + public void channelActive(ChannelHandlerContext ctx) throws Exception { + // 分配 ByteBuf + final ByteBuf buf = ctx.alloc().buffer(); + buf.writeBytes("*3".getBytes()); + buf.writeBytes(LINE); + buf.writeBytes("$3".getBytes()); + buf.writeBytes(LINE); + buf.writeBytes("set".getBytes()); + buf.writeBytes(LINE); + buf.writeBytes("$4".getBytes()); + buf.writeBytes(LINE); + buf.writeBytes("name".getBytes()); + buf.writeBytes(LINE); + buf.writeBytes("$8".getBytes()); + buf.writeBytes(LINE); + buf.writeBytes("zhanglisi".getBytes()); + buf.writeBytes(LINE); + ctx.writeAndFlush(buf); + } + // read事件 接受redis的返回结果 + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + ByteBuf buf = (ByteBuf) msg; + final String s = buf.toString(Charset.defaultCharset()); + System.out.println("... ... redis返回结果 = " + s); + } + }); + } + }); + final ChannelFuture channelFuture = bs.connect("localhost", 6379).sync(); + channelFuture.channel().closeFuture().sync(); + } catch (InterruptedException e) { + log.error("Client error", e); + } finally { + worker.shutdownGracefully(); + } + } +} \ No newline at end of file -- GitLab