How to integrate Redis cluster in Spring

Keywords: Programming Redis Spring Linux Jedis

In the last article, I talked about--- How to build a redis cluster? Let the cluster have the capacity expansion of fragmented data memory and the high availability of sentry . Today, I'll show you how Spring integrates Redis cluster

First of all, I'd like to tell you about the characteristics of segmentation and sentry:

  • 1. The main function of sharding is to realize the expansion of memory data
  • 2. The main function of sentinels is to realize high availability of redis
  • 3. If one redis node in the redis partition goes down, the whole redis partition will not work normally
  • 4. Although the Redis sentinel can realize the high availability of Redis, the sentinel itself can not realize the high availability, so there is a risk in the program call

The cluster does contain the advantages of both fragmentation and sentry. It not only realizes the expansion of memory data, but also realizes the high availability of redis. Moreover, the cluster ensures one or a certain number of redis downtime, as well as the normal operation of the program. This is why we use the cluster instead of fragmentation or sentry.
There's a lot of nonsense. Let's move on to today's topic

1.Spring integrates Redis cluster

1.1 enable redis cluster on linux system --- sh

Then ps -ef |grep redis checks whether all redis servers are enabled

1.2 add redis dependency (skip this if it was added earlier)

        <!--spring integration redis -->

1.3 redis cluster test example

        public void testCluster() {
            Set<HostAndPort> nodes = new HashSet<HostAndPort>();
            nodes.add(new HostAndPort("",7000));
            nodes.add(new HostAndPort("",7001));
            nodes.add(new HostAndPort("",7002));
            nodes.add(new HostAndPort("",7003));
            nodes.add(new HostAndPort("",7004));
            nodes.add(new HostAndPort("",7005));
            JedisCluster jedisCluster = new JedisCluster(nodes);
            jedisCluster.set("redisCluster","Cluster testing");

The test results are as follows:

Test successful!!!

1.4 edit file

#Identify IP address and port number information IP:PORT

1.5 edit redis configuration class

//Identity configuration class
public class JedisConfig {
	private String nodes;
	public JedisCluster jedisCluster() {
		Set<HostAndPort> nodeSet = new HashSet<HostAndPort>();
		String[] nodeArray = nodes.split(",");
		for(String node : nodeArray) {//node=host:port
		    String host = node.split(":")[0];
		    int port = Integer.parseInt(node.split(":")[1]);
		    HostAndPort hostAndPort = new HostAndPort(host,port);
		return new JedisCluster(nodeSet);

1.6 modify RedisAOP configuration

Finally, the test is carried out. If the test is successful, then Spring integrates Redis cluster successfully!!!

Posted by Xoom3r on Mon, 23 Mar 2020 23:53:00 -0700