• Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

preface

  • Most of the Web development data is stored in the database, but as the data grows, our interfaces are slow. At this time, we will introduce Redis to cache data that does not change frequently to achieve fast response

stand-alone

  • To introduce Redis in Spring, we configure redis link factory and other information. In SpringBoot, we simply configure redisTemplate serialization and cache factory in the configuration class.
@Bean @Primary public RedisTemplate<Object, Object> redisTemplate() throws CommonException { RedisTemplate template = new RedisTemplate(); template.setConnectionFactory(getRedisConnectionFactory()); / / use Jackson2JsonRedisSerializer to serialization and deserialization of redis value value (the default using JDK serialization) Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper mapper = new ObjectMapper(); mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); serializer.setObjectMapper(mapper); / / shiro defaults to using the JDK, so the serialization USES the default JDK, convenient shirosession processing template. SetValueSerializer (serializer); / / use StringRedisSerializer to serialization and deserialization redis template. The key value of setKeySerializer (serializer); //template.setValueSerializer(serializer); template.setHashKeySerializer(serializer); template.setHashValueSerializer(serializer); template.afterPropertiesSet(); return template; } @Bean public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory factory) { StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(); stringRedisTemplate.setConnectionFactory(factory); return stringRedisTemplate; }Copy the code

The cluster

  • But problems will arise as the project progresses. The biggest problem of single machine is that if Redis service breaks down due to unexpected circumstances, our program will also be implicated, which makes our service directly unavailable.
  • Based on this situation, redis cluster came into being. The advantage of clustering is that we don’t need to worry about service downtime because each node is deployed as a separate service. One of them goes down and the other one will perform election and data disaster recovery operations again. There is no need to worry about downtime for us users.
  • There is also data, with master and slave nodes in the Redis cluster. The master node writes data and the slave node synchronizes data. Data synchronization within the cluster allows us to avoid data loss.
  • In the standalone configuration, it can be found that we need the RedisConnectionFactory factory when configuring the RedisTemplate. There is no configuration for this factory, the main reason is that we need to transform it in the cluster. So they are configured together in the cluster section.
  • As for the factory configuration, we mainly focus on configurationJedisConnectionFactory. Click on the source code to see a method
public JedisConnectionFactory(RedisClusterConfiguration clusterConfig) {
    this((RedisClusterConfiguration)clusterConfig, (JedisClientConfiguration)(new JedisConnectionFactory.MutableJedisClientConfiguration()));
}
Copy the code
  • So we only need to construct the cluster configuration can RedisClusterConfiguration object. If it is a single machine, we use the no-parameter construction
@Bean public JedisConnectionFactory getRedisConnectionFactory() throws CommonException { JedisConnectionFactory connectionFactory = null; if (connectionFactory == null && redisProperties ! = null && redisProperties.getHost() ! // connectionFactory = new JedisConnectionFactory(); } if (connectionFactory == null && redisProperties ! = null && redisProperties.getCluster().getNodes() ! = null) {// connectionFactory = new JedisConnectionFactory(getClusterConfig()); } if (null == connectionFactory) {throw new CommonException(" Failed to initialize redis connection pool "); } try { connectionFactory.setUsePool(true); JedisPoolConfig config = getJedisPoolConfig(); connectionFactory.setPoolConfig(config); connectionFactory.setHostName(redisProperties.getHost()); connectionFactory.setPort(redisProperties.getPort()); connectionFactory.setDatabase(redisProperties.getDatabase()); connectionFactory.setPassword(redisProperties.getPassword()); } catch (Exception e) { logger.error("redis connection factory init failed"); } return connectionFactory; }Copy the code

conclusion

  • Redis clustering greatly improves the availability, high availability, of our projects. Let’s have some time to understand the data synchronization principle and election mechanism of the cluster