The spring boot configuration supports both single machine and cluster redis

The official environment uses the cluster version of redis, and the single-machine version for development. The leader requires that the configuration file be used to determine whether it is a single-machine or a cluster. Since the single-machine version has been implemented, the preparation is based on the single-machine version. Development, and then found spring boot1. Version 2 is already relatively old, so I upgraded the version. Due to the upgrade of the spring boot version, other configurations have also been modified. The final modified configuration is as follows:

 

pom.xml

<properties>
	<java.version>1.8</java.version>
	<spring.version>4.3.9.RELEASE</spring.version>
	<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>1.5.8.RELEASE</version>
</parent>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-redis</artifactId>
       <!--It turned out to be spring-boot-starter-redis-->
</dependency>
<dependency>
	<groupId>redis.clients</groupId>
	<artifactId>jedis</artifactId>
	<version>2.9.0</version>
</dependency>

 The redis.properties file is basically unchanged:

# REDIS (RedisProperties)
# Redis server addresses (clusters separated by commas)
#spring.redis.host=xxx.xxx.xxx.xxx:xxxxx
spring.redis.host=xxx.xxx.xxx.xxx:xxxxx,xxx.xxx.xxx.xxx:xxxxx
# Redis server connection password (default is empty)
spring.redis.password=123456
# Connection timeout (ms)
spring.redis.timeout=2000
spring.redis.max-redirects=8

 Note: host becomes ip:port, and multiple ips and ports in the cluster are divided by ",". Why is this divided in the RedisClusterConfiguration class in the spring framework. Take a look at the source code:

//RedisClusterConfiguration类:
private static final String REDIS_CLUSTER_NODES_CONFIG_PROPERTY = "spring.redis.cluster.nodes"; //The default is to configure this
……
public RedisClusterConfiguration(PropertySource<?> propertySource) {

	notNull(propertySource, "PropertySource must not be null!");

	this.clusterNodes = new LinkedHashSet<RedisNode>();
        //There is spring.redis.cluster.nodes configuration, split this property, add node
	if (propertySource.containsProperty(REDIS_CLUSTER_NODES_CONFIG_PROPERTY)) {
		appendClusterNodes(commaDelimitedListToSet(propertySource.getProperty(REDIS_CLUSTER_NODES_CONFIG_PROPERTY)
				.toString()));
	 }
         ……
	}
//The function will be called, separated by ",":
public static String[] commaDelimitedListToStringArray(String str) {
	return delimitedListToStringArray(str, ",");
}
//Then use ":" to split and assemble into RedisNode
private void appendClusterNodes(Set<String> hostAndPorts) {

	for (String hostAndPort : hostAndPorts) {
		addClusterNode(readHostAndPortFromString(hostAndPort));
	}
}

private RedisNode readHostAndPortFromString(String hostAndPort) {
	String[] args = split(hostAndPort, ":");

	notNull(args, "HostAndPort need to be seperated by  ':'.");
	isTrue(args.length == 2, "Host and Port String needs to specified as host:port");
	return new RedisNode(args[0], Integer.valueOf(args[1]).intValue());
}

 In the cacheconfig class becomes this:

@Bean
	public RedisClusterConfiguration getClusterConfiguration() {
		if (host.split(",").length > 1) {
                        //If the host is in cluster mode, perform the following operations
			Map<String, Object> source = new HashMap<String, Object>();

			source.put("spring.redis.cluster.nodes", host);

			source.put("spring.redis.cluster.timeout", timeout);

			source.put("spring.redis.cluster.max-redirects", redirects);
                        //In the comments of the source code, you can see that it is configured like this, thinking that writing this way does not require authentication in Connection, but it was determined that it was too naive.
source.put("spring.redis.cluster.password", password);

			return new RedisClusterConfiguration(new MapPropertySource("RedisClusterConfiguration", source));
		} else {
			return null;
		}

	}

	@Bean
	public JedisConnectionFactory jedisConnectionFactory() {
		if (host.split(",").length == 1) {
			JedisConnectionFactory factory = new JedisConnectionFactory();
			factory.setHostName(host.split(":")[0]);
			factory.setPort(Integer.valueOf(host.split(":")[1]));
			factory.setPassword(password);
			factory.setTimeout(timeout);
			return factory;
		} else {
			JedisConnectionFactory jcf = new JedisConnectionFactory(getClusterConfiguration());
			jcf.setPassword(password); //Cluster password authentication
			return jcf;
		}

	}
//After this transformation, the redisTemplate template does not need to be changed, and the redisUtil class written before does not need to be changed

 

Guess you like

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