此篇文章,帮助你理解 Jedis Client是如何与 Redis Server通信的。
源码参考:jedis-demo
客户端的三层架构
1、操作层(api):发送哪些命令,及数据
2、消息处理层(protocol):编码格式
3、传输层(transfer):通过建立 Socket,发送数据,接收数据
操作 Jedis
2、创建一个 maven 项目,导入 Jedis 依赖
1 | <dependencies> |
3、测试 Jedis 客户端
1 | import redis.clients.jedis.Jedis; |
伪造 Hack拦截数据
1、编写 Hack.java 类,并启动。
1 | import java.io.InputStream; |
2、使用 Jedis 发送数据。启动之前,先启动 Hack
1 | import redis.clients.jedis.Jedis; |
3、Hack 拦截到数据
1 | *3 |
分析拦截数据
1、打开 Redis 官方文档:https://redis.io/documentation
2、搜索 protocol
3、找到 Specifications
===> Redis Protocol specification
===> https://redis.io/topics/protocol
4、先看一下简介:
1 | ** Redis 协议规范 ** |
5、使用规则
1 | RESP协议描述 |
6、把我们的翻译一下:
1 | *3 ===> 数组有 3个【($3,SET),($2,k1),($23,2021-03-29T17:55:12.291)】 |
手写 Jedis
一共三个类:MyJedis,MyConnection,MyProtocol
1、MyJedis
1 | import com.taopanfeng.connection.MyConnection; |
2、MyConnection
1 | import java.io.IOException; |
3、
1 | import java.io.IOException; |
4、测试 MyTest
1 | import com.taopanfeng.api.MyJedis; |