手写简单jedis架构体系

1 篇文章 0 订阅
1 篇文章 0 订阅

手写简单jedis架构体系(仿)

总体分析

jedis与redis服务器对接大体分为三层(自下向上)

1.transfer传输层

2.message   potocol   协议层

3.api操作层

1.transfer传输层(创建Connection类)

public class Connection {
    private Socket socket;
    private String host;
    private int port;
    private OutputStream outputStream;
    private InputStream inputStream;

    public Connection(String host, int port) {//定义传输host,port
        this.host = host;
        this.port = port;
    }

    //todo socket应该使用IO复用,不应该一直创建
    public Connection connection(){//建立链接

        if (!isConnectioned()) {
            try {
                socket = new Socket(host, port);
                inputStream = socket.getInputStream();
                outputStream = socket.getOutputStream();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return this;
    }

    private boolean isConnectioned() {
        return this.socket != null && this.socket.isBound() && !this.socket.isClosed() && this.socket.isConnected() && !this.socket.isInputShutdown() && !this.socket.isOutputShutdown();//等等事件
    }

    public Connection sendConnection(Potocol.Command co , byte[] ... args){//和api建立联系,发送数据和命令
        connection(); //连接
        Potocol.sendCommand(outputStream,co,args);// 数据操作 发送数据
        return this;
    }

    /**
     * 返回一个执行状态
     * @return
     */
    public String getStatusCocdereply(){

        byte[] bytes = new byte[1024];
        try {
            socket.getInputStream().read(bytes);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new String(bytes);

    }
}

2.message potocol 协议层(创建Potocol类)

public class Potocol {
    /**
     * 定义一些相关指令 ,即 * - + : $
     */
    public static final String DOLLAR_BYTE = "$";
    public static final String ASTERISK_BYTE = "*";
    public static final String BLANK_BYTE = "\r\n"; //在每一个数据后都有一个/r/n的换行

    //生成相关协议  组装数据(*3 $3 SET $3 zsh $2 11)
    public static void sendCommand(OutputStream os,Command co ,byte[] ... args){

        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(ASTERISK_BYTE).append(args.length+1).append(BLANK_BYTE); //*3
        stringBuffer.append(DOLLAR_BYTE).append(co.name().length()).append(BLANK_BYTE); //$3
        stringBuffer.append(co.name()).append(BLANK_BYTE); //SET

        for (final byte[] arg:args){ //$3   zsh  //$2 11
            stringBuffer.append(DOLLAR_BYTE).append(arg.length).append(BLANK_BYTE);
            stringBuffer.append(new String(arg)).append(BLANK_BYTE);
        }

        try {
            os.write(stringBuffer.toString().getBytes());//写出去
        } catch (IOException e) {
            e.printStackTrace();
        }

    }



    /**
     * 定义一个枚举类 存放命令
     */
    public static  enum Command{
        SET , GET , KEYS
    }

}

3.api操作层(创建Clint类)

public class Clint {

    Connection connection; //连接connection

    public Clint(String host ,int port) {
        this.connection = new Connection(host,port);
    }

    public String set(String key, String value){

        connection.sendConnection(Potocol.Command.SET,key.getBytes(),value.getBytes());
        return  connection.getStatusCocdereply();
    }

    public String get(String key){
        connection.sendConnection(Potocol.Command.GET,key.getBytes());
        return connection.getStatusCocdereply();
    }
}

4.main操作

public class App 
{
    public static void main( String[] args )
    {
        Clint jedis = new Clint("127.0.0.1",6379);
        String zsh = jedis.set("zsh", "999");
        String s = jedis.get("123");
        System.out.println(s);
        System.out.println(zsh);
//        System.out.println( "Hello World!" );
        //使用jedis.set方法给redis传输的数据 通过serverSocket获取
//        *3  *表示数组 ,此处标示数组中有3个元素
//        $3  $表示字符串 ,此处标示字符串长度为3
//        SET
//        $3  字符串长度为3
//        zsh
//        $2   字符串长度为2
//        11
        // + 表示字符  - 表示错误  :表示数字
    }
}

5.使用ServerSocket拦截器获取jedis传输数据的格式

/**
 * 模拟redis服务端,拦截获取执行set操作时的数据
 */
public class Hack {

    public static void main(String[] args) throws Exception{
        ServerSocket serverSocket = new ServerSocket(6379);//host为本机127.0.0.1  
        //若redis在本地,地址为127.0.0.1,则会报错(地址已被用)
        Socket accept = serverSocket.accept();
        byte[] bytes = new byte[1024];
        accept.getInputStream().read(bytes);
        System.out.println(new String(bytes));
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值