---
title: "Jedis Cluster 扩展方案"
author: "bibithink"
date: "2016-09-28T10:58:37Z"
tags: ["Redis", "jedis", "pipeline"]
categories: ["企业架构"]
slug: "jedis-cluster"
draft: false
lastmod: "2016-09-28T10:59:58Z"
id: 2327518
url: "https://bibithink.iteye.com/blog/2327518"
---

# Infra Redis Client

Infra Redis client 基于 Jedis 实现了对 Redis 集群的访问封装。

## [](https://github.com/cityonsky/jedis-cluster-ext/tree/master/redis#redis-client-%E8%83%BD%E5%81%9A%E4%BB%80%E4%B9%88)Redis Client 能做什么？

Infra Redis client 提供如下特性:

- 支持双机房集群 failover 方案
- 支持以 pipeline 方式访问 Redis 集群
- 支持 Redis 访问实时监测

## [](https://github.com/cityonsky/jedis-cluster-ext/tree/master/redis#%E6%80%8E%E4%B9%88%E4%BD%BF%E7%94%A8-redis-client)怎么使用 Redis Client？

### [](https://github.com/cityonsky/jedis-cluster-ext/tree/master/redis#%E9%85%8D%E7%BD%AE-maven-%E4%BE%9D%E8%B5%96)配置 maven 依赖:

```
<dependency>
    <groupId>com.city.infra</groupId>
    <artifactId>city-infra-redis</artifactId>
    <version>0.0.1</version>
    <type>jar</type>
    <scope>compile</scope>
</dependency>
```

### [](https://github.com/cityonsky/jedis-cluster-ext/tree/master/redis#%E6%9E%84%E9%80%A0%E9%9B%86%E7%BE%A4%E5%B7%A5%E5%8E%82%E7%B1%BB)构造集群工厂类

```
public static RedisClusterFactory buildFactory() {
        RedisClusterFactory clusterFactory = new RedisClusterFactory();

        Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();
        jedisClusterNodes.add(new HostAndPort("10.100.1.1", 7639));
        jedisClusterNodes.add(new HostAndPort("10.100.1.1", 7640));

        Set<HostAndPort> standbyJedisClusterNodes = new HashSet<HostAndPort>();
        standbyJedisClusterNodes.add(new HostAndPort("10.100.1.2", 7543));
        standbyJedisClusterNodes.add(new HostAndPort("10.100.1.2", 7544));

        clusterFactory.setJedisClusterNodes(jedisClusterNodes);  //  设置主集群 
        clusterFactory.setStandbyJedisClusterNodes(standbyJedisClusterNodes);  //  设置备份集群

        return clusterFactory;
    }
```

### [](https://github.com/cityonsky/jedis-cluster-ext/tree/master/redis#%E8%AE%BF%E9%97%AE-redis-%E9%9B%86%E7%BE%A4)访问 Redis 集群

```
RedisAccessor redisAccessor = clusterFactory.createRedisAccessor("prefix:");
redisAccessor.set("foo", "1", 3600);
String value = redisAccessor.get("foo", "0");
```

### [](https://github.com/cityonsky/jedis-cluster-ext/tree/master/redis#pipeline-%E8%AE%BF%E9%97%AE%E6%96%B9%E5%BC%8F)pipeline 访问方式

pipeline 是 Redis 提供的一种批量处理命令机制，可以一次发送多个命令而不用同步等待响应结果，可以显著提升 Redis 访问性能，非常适合离线批量导入数据的业务。

Jedis 对 Redis 集群不支持 pipeline 方式访问，Infra Redis client 通过 Java 反射机制复用了 Jedis 对 Redis 单实例 pipeline 实现，在 Redis 集群节点变更不频繁的情况下，可以提供很好的访问性能。

pipeline 使用代码示例如下:

```
RedisPipelineAccessor redisPipelineAccessor = clusterFactory.createRedisPipelineAccessor();
redisPipelineAccessor.set("pipe:", "foo", "1", 3600);
redisPipelineAccessor.set("pipe:", "bar", "2", 3600);
redisPipelineAccessor.sync();
```

### [](https://github.com/cityonsky/jedis-cluster-ext/tree/master/redis#%E7%9B%91%E6%8E%A7-redis-%E4%BD%BF%E7%94%A8%E6%83%85%E5%86%B5)监控 Redis 使用情况

在具体业务场景中，往往会对 Redis 的使用做一些规范，比如单个 key-value 的长度不能超过一定阈值，以防止对 Redis 的访问产生抖动。通过实现 RedisMonitor 接口，可以监控所有 Redis 访问情况。

为了使用方便，可以继承 RedisMonitorBase 类，选择业务感兴趣的事件监测，示例代码如下：

```
public static class CustomMonitor extends RedisMonitorBase {
        private static final Logger LOGGER = LoggerFactory.getLogger(CustomMonitor.class);
        private static final int MAX_VALUE_LEN = 1024 * 1024;

        @Override
        public void onWrite(String prefix, String key, String value) {
            if (value.length() > MAX_VALUE_LEN) {
                LOGGER.warn("Too large redis value! prefix=[{}], key=[{}], value=[{}]", prefix, key, value);
            }
        }
    }
```

在构造 Redis 集群工厂后，创建 Redis 访问对象前，设置自定义的监控对象到 Redis 集群工厂中：

```
public static RedisClusterFactory buildFactory() {
        RedisClusterFactory clusterFactory = new RedisClusterFactory();

        Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();
        jedisClusterNodes.add(new HostAndPort("10.100.1.1", 7639));
        jedisClusterNodes.add(new HostAndPort("10.100.1.1", 7640));

        Set<HostAndPort> standbyJedisClusterNodes = new HashSet<HostAndPort>();
        standbyJedisClusterNodes.add(new HostAndPort("10.100.1.2", 7543));
        standbyJedisClusterNodes.add(new HostAndPort("10.100.1.2", 7544));

        clusterFactory.setJedisClusterNodes(jedisClusterNodes);
        clusterFactory.setStandbyJedisClusterNodes(standbyJedisClusterNodes);
        clusterFactory.setRedisMonitor(new CustomMonitor());  //  设置访问监控对象

        return clusterFactory;
    }
```

 

github 项目地址：https://github.com/cityonsky/jedis-cluster-ext/tree/master/redis