zoukankan      html  css  js  c++  java
  • Redis随笔(六)RESP的协议规范

    1、官网文档

    https://redis.io/topics/protocol

    http://www.redis.cn/topics/protocol.html

    2、协议介绍

    redis协议规范(Redis Protocol specification)。

    redis协议在以下几点之间做出了折衷:

      (1)简单的实现

      (2)快速地被计算机解析

      (3)简单得可以能被人工解析

      (4)网络层,Redis在TCP端口6379上监听到来的连接(本质就是socket),客户端连接到来时,Redis服务器为此创建一个TCP连接。在客户端与服务器端之间传输的每个Redis命令或者数据都以 结尾。

    3、请求

    Redis接收由不同参数组成的命令。一旦收到命令,将会立刻被处理,并回复给客户端

    4、新的统一请求协议

    在这个统一协议里,发送给Redis服务端的所有参数都是二进制安全的。以下是通用形式:

    *后面数量表示存在几个$
    $后面数量表示字符串的长度

    例子:

    *3
    $3
    SET
    $5
    mykey
    $7
    myvalue

    上面的命令看上去像是单引号字符串,所以可以在查询中看到每个字节的准确值:

    "*3
    $3
    SET
    $5
    mykey
    $7
    myvalue
    "

    在Redis的回复中也使用这样的格式。批量回复时,这种格式用于每个参数$6 mydata 。 实际的统一请求协议是Redis用于返回列表项,并调用 Multi-bulk回复。 仅仅是N个以以* 为前缀的不同批量回复,是紧随的参数(批量回复)数目。

    5、回复

    Redis用不同的回复类型回复命令。它可能从服务器发送的第一个字节开始校验回复类型:

      (1)用单行回复,回复的第一个字节将是“+”

      (2)错误消息,回复的第一个字节将是“-”

      (3)整型数字,回复的第一个字节将是“:”

      (4)批量回复,回复的第一个字节将是“$”

      (5)多个批量回复,回复的第一个字节将是“*”

    例子:状态回复(或者单行回复)

    以“+”开始以“ ”结尾的单行字符串形式。例如:

    "+OK
    "

    客户端库将在“+”后面返回所有数据,正如上例中字符串“OK”一样。

    有关与其他的操作请查看官方文档。

    6、模拟Redis服务和客户端通讯,实现RESP协议通信

    枚举类

    public enum CommandRedis {
        SET, GET, SETNX
    }

    实现类

    package com.protocol;
    
    import org.junit.Test;
    
    import java.io.IOException;
    import java.net.Proxy;
    import java.net.Socket;
    
    /**
     * Created by wxh on 2018/1/29.
     */
    public class RedisClientByResp {
    
        private Socket socket;
    
        public RedisClientByResp() {
            try {
                socket = new Socket("192.168.56.180", 6379);
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("连接失败" + e.getMessage());
            }
        }
    
        /**
         * 设置值
         * @param key
         * @param value
         * @return
         * @throws IOException
         */
        public String set(String key, String value) throws IOException {
            StringBuilder sb = new StringBuilder();
            sb.append("*3").append("
    ");
            sb.append("$").append(CommandRedis.SET.name().length()).append("
    ");
            sb.append(CommandRedis.SET.name()).append("
    ");
            // 注意中文汉字。一个汉字两个字节,长度为2
            sb.append("$").append(key.getBytes().length).append("
    ");
            sb.append(key).append("
    ");
            sb.append("$").append(value.getBytes().length).append("
    ");
            sb.append(value).append("
    ");
            System.out.println(sb.toString());
    
            socket.getOutputStream().write(sb.toString().getBytes());
            byte[] b = new byte[2048];
            socket.getInputStream().read(b);
            return new String(b);
        }
    
        /**
         * 获取值
         * @param key
         * @return
         * @throws Exception
         */
        public String get(String key) throws Exception {
            StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append("*2").append("
    ");
            stringBuffer.append("$").append(CommandRedis.GET.name().length()).append("
    ");
            stringBuffer.append(CommandRedis.GET).append("
    ");
            stringBuffer.append("$").append(key.getBytes().length).append("
    ");
            stringBuffer.append(key).append("
    ");
    
            socket.getOutputStream().write(stringBuffer.toString().getBytes());
            byte[] b = new byte[2048];
            socket.getInputStream().read(b);
            return new String(b);
        }
    
        /**
         * 设置值:不会覆盖存在的值
         * @param key
         * @param value
         * @return
         * @throws Exception
         */
        public String setnx(String key, String value) throws Exception {
            StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append("*3").append("
    ");
            stringBuffer.append("$").append(CommandRedis.SETNX.name().length()).append("
    ");
            stringBuffer.append(CommandRedis.SETNX.name()).append("
    ");
            stringBuffer.append("$").append(key.getBytes().length).append("
    ");
            stringBuffer.append(key).append("
    ");
            stringBuffer.append("$").append(value.getBytes().length).append("
    ");
            stringBuffer.append(value).append("
    ");
    
            socket.getOutputStream().write(stringBuffer.toString().getBytes());
            byte[] b = new byte[2048];
            socket.getInputStream().read(b);
            return new String(b);
        }
    
        public static void main(String[] args) throws Exception {
            System.out.println(new RedisClientByResp().set("mykey" ,"myvalue"));
            System.out.println(new RedisClientByResp().get("mykey"));
        }
    }
  • 相关阅读:
    【转】 springBoot(5)---单元测试,全局异常
    【转】 springBoot(4)---热部署,配置文件使用
    【转】 springBoot(3)---目录结构,文件上传
    【转】 springBoot(2)---快速创建项目,初解jackson
    【转】 springBoot(1)---springboot初步理解
    【转】 SpringBoot+MyBatis+MySQL读写分离
    简单请求 vs 非简单请求
    H5新增的API
    图片
    vue更新dom的diff算法
  • 原文地址:https://www.cnblogs.com/c-xiaohai/p/8376891.html
Copyright © 2011-2022 走看看