[redis combat 1] SpringBoot2.0 integrates Redis custom injection bean component configuration

Abstract: For Redis, I believe that many friends have already heard about it, and what's more, they have already applied it to many projects! That's right, it is one of the most widely used caching middleware in the industry at present, and it can be regarded as one of the best. Starting from this article, we will lay the foundation for the microservice project based on SpringBoot2.0 integration and open the middle The actual combat road of Redis!

Content: In this article, we will first integrate the cache middleware Redis based on the project built by SpringBoot2.0, add common configuration information related to Redis to the project, and customize the template operation components StringRedisTemplate and RedisTemplate injected into Redis, and finally give Let's make a simple demo and start the actual combat journey of Redis!

(1) The first step is of course to add the dependent Jar of the middleware Redis, as shown below:

        <!-- redis -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-redis</artifactId>
            <version>1.3.3.RELEASE</version>
        </dependency>
Then add the common relevant configuration information of Redis in the configuration file application.properties, including basic information such as host and port. Here we provide two configuration methods, namely "stand-alone mode" and "cluster mode" configuration, as follows Show:  
#redis 单机配置
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=
 
spring.redis.jedis.pool.min-idle=100
spring.redis.jedis.pool.max-idle=300
spring.redis.jedis.pool.max-active=500
 
#集群配置
#spring.redis.cluster.nodes=127.0.0.1:6379,127.0.0.1:6380,127.0.0.1:6381,127.0.0.1:6382
In this configuration file, we also added the concept of "connection pool". Among them, the minimum number of available connections in the connection pool is 100, and the maximum number of available connections is 300. If it is not enough and needs to be dynamically expanded, We will eventually increase the number of active links to 500! (If 500 are not enough, you have to block and wait. During the waiting period, if the time exceeds the timeout period of the default configuration, an error similar to connection reset or connection error will be reported.)

(2) Next, we will customize the operation template components injected into Redis based on the integrated and built project, mainly StringRedisTemplate and RedisTemplate.

It is worth mentioning that in traditional Java Web projects, such as Spring+SpringMVC+Mybatis integration projects, a JedisUtil tool class is usually directly encapsulated based on Jedis, which is similar to the previous use of JDBCUtil to operate the DB database. , its defects are quite obvious (such as the need to manually create links, close link resources, etc.)

The advent of SpringBoot has brought advantages such as "convention takes precedence over configuration" and "startup dependency", which saves many operations that may consume resources such as manually creating and closing links in the past, that is, it is directly built into SpringBoot Redis. The starting dependency is in the middle, and for how to operate Redis more conveniently, SpringBoot directly encapsulates and provides two template operation components StringRedisTemplate and RedisTemplate. As shown below, we custom inject these two template operation components, that is, mainly specify other Related strategies for serialization:

/**
 * @EnableCaching:开启缓存(注解生效的)
 * redis的操作组件自定义注入配置
 **/
@Configuration
@EnableCaching
public class RedisConfig {
 
    @Autowired
    private RedisConnectionFactory connectionFactory;
 
    @Bean
    public RedisTemplate redisTemplate(){
        RedisTemplate<String,Object> redisTemplate=new RedisTemplate<>();
        redisTemplate.setConnectionFactory(connectionFactory);
        //设置序列化策略
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
 
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
 
    @Bean
    public StringRedisTemplate stringRedisTemplate(){
        StringRedisTemplate stringRedisTemplate=new StringRedisTemplate();
        stringRedisTemplate.setConnectionFactory(connectionFactory);
        return stringRedisTemplate;
    }
}

Guess you like

Origin blog.csdn.net/wufaqidong1/article/details/131396081
Recommended