模仿Jedis写一个Redis Client

话不多说,有必要先了解以下两点:
  1. Socket编程
    参考: https://www.cnblogs.com/wangcq/p/3520400.html
  2. Redis服务端与客户端之间的通信协议:RESP
    参考:https://www.jianshu.com/p/c0c18f9f5494
一切尽在代码中
package com.itdr.demo;

import java.io.IOException;
import java.net.Socket;

/**
 * Class: RedisClientDemo 
 * version: JDK 1.8
 * create: 2019-10-13 16:48:33
 * @author: heynn
 * 模仿Jedis写一个redis客户端
 */
public class RedisClientDemo {

    private Socket socket;

    public RedisClientDemo(String host, int port) throws IOException {
        // 和服务器建立TCP连接
        this.socket = new Socket(host, port);
    }

    /**
     * set key value
     */
    private String set(String key, String value) throws IOException {
        // Redis通信协议:RESP,设置请求数据包
        StringBuilder request = new StringBuilder();
        // 请求内容,换行分割,服务器辨识,有几个部分:3个
        request.append("*3").append("\r\n");
        // 第1部分
        request.append("$3").append("\r\n");// 数据长度:3
        request.append("SET").append("\r\n");//数据内容:SET
        // 第2部分
        request.append("$").append(key.getBytes().length).append("\r\n");// 数据长度:key.getBytes().length
        request.append(key).append("\r\n");//数据内容:key
        // 第3部分
        request.append("$").append(value.getBytes().length).append("\r\n");// 数据长度:alue.getBytes().length
        request.append(value).append("\r\n");//数据内容:value

        String sendContent = request.toString();
        System.out.println("发送到服务器的内容:\n"+sendContent);

        // 发送到服务器
        socket.getOutputStream().write(sendContent.getBytes());

        // 接收返回
        byte[] response = new byte[1024];
        socket.getInputStream().read(response);

        return new String(response);
    }

    /**
     * get key
     */
    private String get(String key) throws IOException {
        // 注释看set()
        StringBuilder request = new StringBuilder();
        request.append("*2").append("\r\n");
        request.append("$3").append("\r\n");
        request.append("GET").append("\r\n");
        request.append("$").append(key.getBytes().length).append("\r\n");
        request.append(key).append("\r\n");
        String sendContent = request.toString();
        socket.getOutputStream().write(sendContent.getBytes());
        byte[] response = new byte[1024];
        socket.getInputStream().read(response);
        return new String(response);
    }
    
     /**
     * 测试
     */
    public static void main(String[] args) throws Exception {
        RedisClientDemo redis = new RedisClientDemo("你的服务器IP", 6379);
        String res = redis.set("key_1","value_1");
        System.out.println(res);
        String val = redis.get("key_1");
        System.out.println(val);
    }
}

结果截图

不可描述

思路 / 步骤:
  1. 通过Socket建立网络上两个进程之间的通信,也就是redis服务端与客户端
  2. 打开Socket的输入输出流
  3. 按照redis通信协议RESP来进行读写操作
  4. 最后需要使用close()方法关闭Socket(Jedis的close()方法是把它还给连接池),这里就不再写
  5. 结束
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值