Jedis Cluster expansion plan

Infra Redis Client

Infra Redis client implements access encapsulation to Redis cluster based on Jedis.

What can Redis Client do?

The Infra Redis client provides the following features:

  • Support dual-room cluster failover solution
  • Support pipeline access to Redis cluster
  • Support Redis access real-time monitoring

How to use Redis Client?

Configure maven dependencies:

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

Construct the cluster factory class

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); // set the main cluster
        clusterFactory.setStandbyJedisClusterNodes(standbyJedisClusterNodes); // Set backup cluster

        return clusterFactory;
    }

Access Redis Cluster

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

pipeline access method

Pipeline is a batch processing command mechanism provided by Redis. It can send multiple commands at a time without waiting for the response result synchronously. It can significantly improve the access performance of Redis and is very suitable for offline batch data import business.

Jedis does not support pipeline access to Redis cluster. Infra Redis client reuses Jedis's implementation of Redis single-instance pipeline through Java reflection mechanism, which can provide good access performance when Redis cluster nodes change infrequently.

The pipeline usage code example is as follows:

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

Monitor Redis usage

In specific business scenarios, some specifications are often made for the use of Redis. For example, the length of a single key-value cannot exceed a certain threshold to prevent access to Redis from jittering. By implementing the RedisMonitor interface, all Redis accesses can be monitored.

For the convenience of use, you can inherit the RedisMonitorBase class and select the event monitoring that the business is interested in. The sample code is as follows:

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);
            }
        }
    }

After constructing the Redis cluster factory and before creating the Redis access object, set a custom monitoring object to the Redis cluster factory:

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()); // Set the access monitor object

        return clusterFactory;
    }

 

github project address: https://github.com/cityonsky/jedis-cluster-ext/tree/master/redis

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326647488&siteId=291194637