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 start.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 -->
		<dependency>
			<groupId>redis.clients</groupId>
			<artifactId>jedis</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.data</groupId>
			<artifactId>spring-data-redis</artifactId>
		</dependency>

1.3 redis cluster test example

        @Test
        public void testCluster() {
            Set<HostAndPort> nodes = new HashSet<HostAndPort>();
            nodes.add(new HostAndPort("192.168.126.166",7000));
            nodes.add(new HostAndPort("192.168.126.166",7001));
            nodes.add(new HostAndPort("192.168.126.166",7002));
            nodes.add(new HostAndPort("192.168.126.166",7003));
            nodes.add(new HostAndPort("192.168.126.166",7004));
            nodes.add(new HostAndPort("192.168.126.166",7005));
            JedisCluster jedisCluster = new JedisCluster(nodes);
            jedisCluster.set("redisCluster","Cluster testing");
            System.out.println(jedisCluster.get("redisCluster"));
        }

The test results are as follows:

Test successful!!!

1.4 edit redis.properties file

#Identify IP address and port number information IP:PORT
redis.node=192.168.126.166:6379
redis.nodes=192.168.126.166:6379,192.168.126.166:6380,192.168.126.166:6381
redis.sentinel=192.168.126.166:26379
redis.cluster=192.168.126.166:7000,192.168.126.166:7001,192.168.126.166:7002,192.168.126.166:7003,192.168.126.166:7004,192.168.126.166:7005

1.5 edit redis configuration class

//Identity configuration class
@Configuration
@PropertySource("classpath:/properties/redis.properties")
public class JedisConfig {
	
	//colony
	@Value("${redis.cluster}")
	private String nodes;
	@Bean
	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);
		    nodeSet.add(hostAndPort);
		
		}
		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