Spring boot
2.x system after redis
switching becomes Lettuce
realized. In order to fit the Spring boot
idea and advantages of automatic assembly, try to use the time to use the system already provided, and if not then in your own custom-related bean
objects.
Redis Introduction
Redis is an open source use written in ANSI C, support network, based on the persistence of memory can log type, Key-Value database, and provides multi-lingual API. It supports Memcached compared to relatively more storage type (character, hash, sets, ordered sets, lists, GEO), while Redis is thread-safe. Since March 15, 2010, Redis development work was hosted by VMware, 2013 began in May, Redis development sponsored by Pivotal.
Lettuce & Jedis
Lettuce and Jedis are connected to the client program Redis Server. Jedis on implementation is directly connected redis server, multi-threaded environment, non-thread-safe, unless you are using a connection pool, increase physical connection for each instance of Jedis. Lettuce based Netty connection example of (StatefulRedisConnection), may concurrently access among multiple threads, and thread-safe, concurrent access to meet in a multithreaded environment, and it is scalable design, a connection instance can not be enough demand increase the connection instance.
Import dependence
In pom.xml
add spring-boot-starter-data-redis
-dependent, in Spring boot 2.x
later versions provide the underlying default implementation is to use Lettuce
instead jedis
. However 两种方式都提供的默认的实现
, you can choose according to their own version of the project
lettuce version
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
复制代码
jedis version
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<exclusions>
<exclusion>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
复制代码
Configuration Properties
In application.yaml
add the following, the corresponding source code can be found hereorg.springframework.boot.autoconfigure.data.redis.RedisProperties
spring:
redis:
host: 192.168.123.225
database: 0
port: 6379
password:
# 保留一个
lettuce:
pool:
max-active: 8
max-wait: -1ms
max-idle: 8
min-idle: 1
jedis:
pool:
max-active: 8
max-wait: -1ms
max-idle: 8
min-idle: 1
复制代码
redis automatic configuration View
In front of me have said, to make use of Spring
already provided a good bean
, specific location org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration
at this time found that Spring
has helped us to automatically inject redisTemplate
StringRedisTemplate
RedisConnectionFactory
this time the default does not meet our general demand, this time only need to define one RedisTemplate
can, without the need All objects have their own re-custom, Lettuce
and Jedis
is the realization of their own, just add or exclude specific java class can be switched to their differentRedisConnectionFactory
@Configuration
@ConditionalOnClass({RedisOperations.class})
@EnableConfigurationProperties({RedisProperties.class})
@Import({LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class})
public class RedisAutoConfiguration {
public RedisAutoConfiguration() {
}
@Bean
@ConditionalOnMissingBean(
name = {"redisTemplate"}
)
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
RedisTemplate<Object, Object> template = new RedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
@Bean
@ConditionalOnMissingBean
public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
StringRedisTemplate template = new StringRedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
}
复制代码
Partial block
@Configuration
@ConditionalOnClass({GenericObjectPool.class, JedisConnection.class, Jedis.class})
class JedisConnectionConfiguration extends RedisConnectionConfiguration {
...
@Configuration
@ConditionalOnClass({RedisClient.class})
class LettuceConnectionConfiguration extends RedisConnectionConfiguration {
private final RedisProperties properties;
...
复制代码
Test code directly see here will be able to learn together SpringBoot | CHAPTER 9: Lettuce Redis integration