前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >『互联网架构』软件架构-redis的通信协议(protocol)(52)

『互联网架构』软件架构-redis的通信协议(protocol)(52)

作者头像
IT架构圈
发布2019-05-15 15:56:01
3970
发布2019-05-15 15:56:01
举报
文章被收录于专栏:IT架构圈IT架构圈

redis的通信协议是什么?双方约定了一种编码方式,客户端将要发送的命令进行编码,然后服务端收到后,使用同样的协议进行解码,服务端处理完成后,再次编码返回给客户端,客户端解码拿到返回结果,这样就完成了一次通信。

(一)协议介绍

https://redis.io/topics/protocol

  1. 易于实现
  2. 可以高效地被计算机分析(parse)
  3. 可以很容易地被人类读懂
(二)编码后的字符标记解释
  1. For Simple Strings the first byte of the reply is "+" 回复
  2. For Errors the first byte of the reply is "-" 错误
  3. For Integers the first byte of the reply is ":" 整数
  4. For Bulk Strings the first byte of the reply is "$" 字符串
  5. For Arrays the first byte of the reply is "*" 数组
(三)模拟Redis客户端&分片
  • 客户端

Jedis跟redis通讯很简单发送了socket,然后发送了resp这种‘暗语’进行通讯。

代码语言:javascript
复制
import java.io.IOException;import java.io.InputStream;import java.net.Socket;
public class RedisClient {
    private Socket socket;    private  String host;    private int port;
    public RedisClient() throws IOException {        this.socket = new Socket("192.168.79.100",6379);    }
    public RedisClient(String host, int port) {        this.host = host;        this.port = port;        try {            this.socket = new Socket(host,port);        } catch (IOException e) {            e.printStackTrace();        }    }
    //set wk 2018    public String set( String key, String value) throws IOException {       //"*3\r\n$3\r\nset\r\n$2\r\nwk\r\n$4\r\n2018"        StringBuilder stringBuilder=new StringBuilder();        stringBuilder.append("*3").append("\r\n");        stringBuilder.append("$").append(CommandRedis.set.name().length()).append("\r\n");        stringBuilder.append(CommandRedis.set).append("\r\n");        stringBuilder.append("$").append(key.getBytes().length).append("\r\n");        stringBuilder.append(key).append("\r\n");        stringBuilder.append("$").append(value.getBytes().length).append("\r\n");        stringBuilder.append(value).append("\r\n");        socket.getOutputStream().write(stringBuilder.toString().getBytes());        byte[] b=new byte[2048];        socket.getInputStream().read(b);        return new String(b);
    }

    public String get( String key) throws IOException {        //"*3\r\n$3\r\nset\r\n$2\r\nwk\r\n$4\r\n2018"        StringBuilder stringBuilder=new StringBuilder();        stringBuilder.append("*2").append("\r\n");        stringBuilder.append("$").append(CommandRedis.get.name().length()).append("\r\n");        stringBuilder.append(CommandRedis.get).append("\r\n");        stringBuilder.append("$").append(key.getBytes().length).append("\r\n");        stringBuilder.append(key).append("\r\n");        socket.getOutputStream().write(stringBuilder.toString().getBytes());        byte[] b=new byte[2048];        socket.getInputStream().read(b);        return new String(b);
    }
    public String setnx(String key, String value) throws Exception {        StringBuffer stringBuffer = new StringBuffer();        stringBuffer.append("*3").append("\r\n");        stringBuffer.append("$5").append("\r\n");        stringBuffer.append("setnx").append("\r\n");        stringBuffer.append("$").append(key.getBytes().length).append("\r\n");        stringBuffer.append(key).append("\r\n");        stringBuffer.append("$").append(value.getBytes().length).append("\r\n");        stringBuffer.append(value).append("\r\n");        socket.getOutputStream().write(stringBuffer.toString().getBytes());        byte[] b = new byte[2048];        socket.getInputStream().read(b );        return new String(b);    }
    // 管道    public void pipeline(String key)throws Exception {        StringBuffer stringBuffer = new StringBuffer();        stringBuffer.append("*2").append("\r\n");        stringBuffer.append("$"+CommandRedis.subscribe.name().length()).append("\r\n");        stringBuffer.append(CommandRedis.subscribe).append("\r\n");        stringBuffer.append("$").append(key.getBytes().length).append("\r\n");        stringBuffer.append(key).append("\r\n");        socket.getOutputStream().write(stringBuffer.toString().getBytes());        InputStream inputStream = socket.getInputStream();        while (true) {            byte[] b = new byte[2048];            inputStream.read(b );            System.out.println(new String(b));        }
    }
    //subscribe    public void subscribe(String key)throws Exception {        StringBuffer stringBuffer = new StringBuffer();        stringBuffer.append("*2").append("\r\n");        stringBuffer.append("$"+CommandRedis.subscribe.name().length()).append("\r\n");        stringBuffer.append(CommandRedis.subscribe).append("\r\n");        stringBuffer.append("$").append(key.getBytes().length).append("\r\n");        stringBuffer.append(key).append("\r\n");        socket.getOutputStream().write(stringBuffer.toString().getBytes());        InputStream inputStream = socket.getInputStream();        while (true) {            byte[] b = new byte[2048];            inputStream.read(b );            System.out.println(new String(b));        }    }
    public void close(){        if(socket != null){            try {                socket.close();            } catch (IOException e) {                e.printStackTrace();            }        }    }
}
代码语言:javascript
复制
public enum CommandRedis {
    set,get,incr,subscribe
}
  • 分片

RedisClient分片方式,简单模拟,理解概念分片。

代码语言:javascript
复制
import java.io.IOException;import java.util.ArrayList;import java.util.List;
public class TestRedisClient {    public static void main(String[] args) throws IOException {
 /*       RedisClient redisClient=new RedisClient();        System.out.println(redisClient.set("wk","悟空"));        System.out.println(redisClient.get("wk"));*/

        List<RedisClient> pool=new ArrayList<>();        pool.add(new RedisClient("192.168.0.12",6379));        pool.add(new RedisClient("192.168.0.12",6380));        pool.add(new RedisClient("192.168.0.12",6381));        Crc16Sharding crc16Sharding=new Crc16Sharding(pool);        for (int i=0;i<100;i++){            String key="xx"+i;
            RedisClient redisClient=crc16Sharding.crc16(key);            redisClient.set(key,i+"");            System.out.println(redisClient.get(key));        }
    }}
代码语言:javascript
复制
import com.sun.org.apache.xerces.internal.impl.xpath.regex.Match;import java.util.List;
public class Crc16Sharding {    List<RedisClient> pool;
    public Crc16Sharding(List<RedisClient> pool) {        this.pool = pool;    }
    /**     * 通过一个key可以定位到一块 节点     * @param key     * @return     */    public RedisClient crc16(String key){
        int num=Math.abs(key.hashCode()%pool.size());        return  pool.get(num);       /* if(key.length()<3){           return pool.get(0);        }else  if(key.length()<6){            return  pool.get(1);        }else{            return  pool.get(2);        }*/    }}
代码语言:javascript
复制
import java.io.IOException;import java.util.ArrayList;import java.util.List;
public class TestRedisClient {    public static void main(String[] args) throws IOException {        List<RedisClient> pool=new ArrayList<>();        pool.add(new RedisClient("192.168.0.12",6379));        pool.add(new RedisClient("192.168.0.12",6380));        pool.add(new RedisClient("192.168.0.12",6381));        Crc16Sharding crc16Sharding=new Crc16Sharding(pool);        for (int i=0;i<100;i++){            String key="xx"+i;
            RedisClient redisClient=crc16Sharding.crc16(key);            redisClient.set(key,i+"");            System.out.println(redisClient.get(key));;        }    }}
(四)redis使用技巧
  1. 如果是订单表,取的话也是通过唯一的订单号来进行,不要通过循环去redis里面取。
  2. 开关类型的放到本地的jvm内存中,尽量减少redis的压力。
  3. 避免使用慢查询 hgetall
  4. 存储redis的购物车,有的人都是很多的好几M的,几百个商品,redis存储的内容的压缩。减少通信的宽带。优化存储内容。
  5. 少用字符串存储,可以存储map格式放入redis。获取redis里面的map,直接通过key就可以取到对应的值。
(五)监控&运维
  • Open-falcon

http://open-falcon.org/ open-falcon是小米开源监控系统,强大灵活的数据采集、采集、传输、绘制、查询、报警等。采用全部golang编写,portal和dashboard使用python编写。

  • Redis-migrate-tool

https://github.com/vipshop/redis-migrate-tool 2年多没更新了,可以了解下 Redis-migrate-tool是唯品会开源针对redis数据运维工具。 支持单独、twemproxy集群、redis custer、aof、rdb文件迁移。支持实时迁移、异构迁移、过滤等功能。

PS:介绍了运维工具,redis通信的内容拼接,redis存储的技巧,redis正常的时候没问题,并发量大的时候一定要注意,在大流量的情况下,代码的细节至关重要!

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-05-08,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 编程坑太多 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • (一)协议介绍
  • (二)编码后的字符标记解释
  • (三)模拟Redis客户端&分片
  • (四)redis使用技巧
  • (五)监控&运维
相关产品与服务
云数据库 Redis
腾讯云数据库 Redis(TencentDB for Redis)是腾讯云打造的兼容 Redis 协议的缓存和存储服务。丰富的数据结构能帮助您完成不同类型的业务场景开发。支持主从热备,提供自动容灾切换、数据备份、故障迁移、实例监控、在线扩容、数据回档等全套的数据库服务。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档