Springboot2.x集成单节点Redis
Springboot2.x集成单节点Redis
说明
在Springboot 1.x版本中,默认使用Jedis客户端来操作Redis,而在Springboot 2.x 版本中,默认使用Lettuce客户端来操作Redis。Springboot 提供了RedisTemplate来统一封装了对Redis操作,开发者只需要使用RedisTemplate 就可以实现,而不用关心具体的操作实现。
准备条件
pom.xml中引入相关jar
<!-- 集成Redis --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!-- Jedis 客户端 --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency> <!-- lettuce客户端需要使用到 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> </dependency>
application.yml配置属性示例。
spring: redis: host: 192.168.8.121 port: 6379 password: enjoyitlife timeout: 30000 jedis: pool: max-active: 256 max-wait: 30000 max-idle: 64 min-idle: 32 lettuce: pool: max-active: 256 max-idle: 64 max-wait: 30000 min-idle: 32
单机模式下的整合教程
Jedis客户端整合
JedisSingleConfig.java 相关配置
package top.enjoyitlife.redis.jedis; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.RedisStandaloneConfiguration; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.StringRedisSerializer; @Configuration @Profile("jedisSingle") public class JedisSingleConfig { @Value("${spring.redis.host}") private String host; @Value("${spring.redis.port}") private int port; @Value("${spring.redis.password}") private String password; @Value("${spring.redis.timeout}") private int timeout; @Value("${spring.redis.jedis.pool.max-idle}") private int maxIdle; @Value("${spring.redis.jedis.pool.max-wait}") private long maxWaitMillis; @Bean public JedisConnectionFactory redisConnectionFactory() throws Exception{ RedisStandaloneConfiguration rc= new RedisStandaloneConfiguration(); rc.setHostName(host); rc.setPort(port); rc.setPassword(password); JedisConnectionFactory connectionFactory = new JedisConnectionFactory(rc); return connectionFactory; } @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){ RedisTemplate<String, Object> template = new RedisTemplate<String, Object>(); template.setKeySerializer(new StringRedisSerializer()); template.setValueSerializer(new StringRedisSerializer()); template.setConnectionFactory(redisConnectionFactory); return template; } }
Lettuce客户端整合
package top.enjoyitlife.redis.lettuce; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.RedisStandaloneConfiguration; import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.StringRedisSerializer; @Configuration @Profile("lettuceSingle") public class LettuceSingleConfig { @Value("${spring.redis.host}") private String host; @Value("${spring.redis.port}") private int port; @Value("${spring.redis.password}") private String password; @Bean public LettuceConnectionFactory redisConnectionFactory() { RedisStandaloneConfiguration rc= new RedisStandaloneConfiguration(); rc.setHostName(host); rc.setPort(port); rc.setPassword(password); // 关键区别点 return new LettuceConnectionFactory(rc); } @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){ RedisTemplate<String, Object> template = new RedisTemplate<String, Object>(); template.setKeySerializer(new StringRedisSerializer()); template.setValueSerializer(new StringRedisSerializer()); template.setConnectionFactory(redisConnectionFactory); return template; } }
Springboot通过RedisStandaloneConfiguration来统一了连接方式,区别Jedis客户端是通过JedisConnectionFactory进行初始化,而Lettuce客户端是通过LettuceConnectionFactory初始化。
单元测试
Jedis单元测试
JedisSingleTest.java 单元测试代码。
package top.enjoyitlife.redis.jedis; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.test.context.ActiveProfiles; @SpringBootTest @ActiveProfiles("jedisSingle") class JedisSingleTest { @Autowired private RedisTemplate<String, Object> redisTemplate; @Test void contextLoads() { String name=redisTemplate.opsForValue().get("name").toString(); System.out.println(name); } }
Lettuce 单元测试
package top.enjoyitlife.redis.lettuce; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.test.context.ActiveProfiles; @SpringBootTest @ActiveProfiles("lettuceSentinel") class LettuceSingleTest { @Autowired private RedisTemplate<String, Object> redisTemplate; @Test void contextLoads() { String name = redisTemplate.opsForValue().get("name").toString(); System.out.println(name); } }
补充
Jedis在是直接连接的的redis实例,如果在多线程环境下是非线程安全的,只有使用连接池,为每个Jedis增加物理连接。
而Lettuce的连接是基于Netty事件驱动模型的,连接实例可以在多个线程间并发访问,StatefulRedisConnection是线程安全的,可以满足多线程环境下的并发访问。
从Springboot 2.X默认采用了Lettuce客户端来看,官方还是建议使用Lettuce。
相关推荐
王道革 2020-11-25
wangdonghello 2020-11-03
Langeldep 2020-11-16
chenhualong0 2020-11-16
聚合室 2020-11-16
koushr 2020-11-12
MRFENGG 2020-11-11
guoyanga 2020-11-10
fackyou00 2020-11-10
Orangesss 2020-11-03
dongCSDN 2020-10-31
rainandtear 2020-10-30
Quietboy 2020-10-30
liuyulong 2020-10-29
fansili 2020-10-29
温攀峰 2020-10-23
jackbon 2020-10-19
kaixinfelix 2020-10-04