Redis11-原理-通信协议
1、RESP协议
(1)概念
- Redis是一个CS架构的软件,通信一般分两步(不包括pipeline和PubSub):
- ①客户端(client)向服务端(server)发送一条命令
- ②服务端解析并执行命令,返回响应结果给客户端
- 因此客户端发送命令的格式、服务端响应结果的格式必须有一个规范,这个规范就是通信协议。
- 而在Redis中采用的是RESP(Redis Serialization Protocol)协议:
- lRedis1.2版本引入了RESP协议
- lRedis 2.0版本中成为与Redis服务端通信的标准,称为RESP2
- lRedis 6.0版本中,从RESP2升级到了RESP3协议,增加了更多数据类型并且支持6.0的新特性–客户端缓存
- 但目前,默认使用的依然是RESP2协议,也是我们要学习的协议版本(以下简称RESP)。
(2)数据类型
- 在RESP中,通过首字节的字符来区分不同数据类型,常用的数据类型包括5种:
- 单行字符串:首字节是 ‘+’,后面跟上单行字符串,以CRLF( “\r\n” )结尾。例如返回"OK": “+OK\r\n”
- 错误(Errors):首字节是 ‘-’,与单行字符串格式一样,只是字符串是异常信息,例如:“-Error message\r\n”
- 数值:首字节是 ‘:’,后面跟上数字格式的字符串,以CRLF结尾。例如:“:10\r\n”
- 多行字符串:首字节是 ‘$’,表示二进制安全的字符串,最大支持512MB:
- 如果大小为0,则代表空字符串:“$0\r\n\r\n”
- 如果大小为-1,则代表不存在:“$-1\r\n”

- 数组:首字节是 ‘*’,后面跟上数组元素个数,再跟上元素,元素数据类型不限:


2、模拟Redis客户端
(1)建立连接
- Redis支持TCP通信,因此我们可以使用Socket来模拟客户端,与Redis服务端建立连接:

(2)发送请求
- 这里我们以set命令为例,发送请求就是输出下面内容:



(3)解析结果
- 响应的结果可能是之前讲的5种数据类型中的任意一种,需要判断后读取:


(4)测试功能

(5)完整代码
public class Main {
static Socket s;
static PrintWriter writer;
static BufferedReader reader;
public static void main(String[] args) {
try {
String host = "192.168.119.180";
int port = 6379;
s = new Socket(host, port);
writer = new PrintWriter(new OutputStreamWriter(s.getOutputStream(), StandardCharsets.UTF_8));
reader = new BufferedReader(new InputStreamReader(s.getInputStream(), StandardCharsets.UTF_8));
sendRequest("auth", "123321");
Object obj = handleResponse();
System.out.println("obj = " + obj);
sendRequest("set", "name", "虎哥");
obj = handleResponse();
System.out.println("obj = " + obj);
sendRequest("get", "name");
obj = handleResponse();
System.out.println("obj = " + obj);
sendRequest("mget", "name", "num", "msg");
obj = handleResponse();
System.out.println("obj = " + obj);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (reader != null) reader.close();
if (writer != null) writer.close();
if (s != null) s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static Object handleResponse() throws IOException {
int prefix = reader.read();
switch (prefix) {
case '+':
return reader.readLine();
case '-':
throw new RuntimeException(reader.readLine());
case ':':
return Long.parseLong(reader.readLine());
case '$':
int len = Integer.parseInt(reader.readLine());
if (len == -1) {
return null;
}
if (len == 0) {
return "";
}
return reader.readLine();
case '*':
return readBulkString();
default:
throw new RuntimeException("错误的数据格式!");
}
}
private static Object readBulkString() throws IOException {
int len = Integer.parseInt(reader.readLine());
if (len <= 0) {
return null;
}
List<Object> list = new ArrayList<>(len);
for (int i = 0; i < len; i++) {
list.add(handleResponse());
}
return list;
}
private static void sendRequest(String ... args) {
writer.println("*" + args.length);
for (String arg : args) {
writer.println("$" + arg.getBytes(StandardCharsets.UTF_8).length);
writer.println(arg);
}
writer.flush();
}
}