Springboot2.0整合redis–Lettuce版本
本文集成的是单机版redis
springboot版本和redis的渊源
在springboot1.x版本之前java操作redis的客户端使用的是Jedis,到了springboot2.x之后使用的客户端是Lettuce
两者的区别是
Jedis :
Jedis实际上是直接连接的redis server ,如果是在多线程的环境下,它是非线程安全的,这个时候只有使用连接池,为每一个Jedis实例增加物理连接
Lettuce :
Lettuce 的连接是基于Netty的,连接实例(StatefulRedisConnection) 可以在多个线程之间并发访问,因为StatefulRedisConnection是线程安全的,一个实例连接就可以满足多线程环境,也可以自定义伸缩
pom配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- redis依赖commons-pool 这个依赖一定要添加 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
application.yaml
server:
port: 8080
spring:
redis:
host: 172.30.66.104
port: 6379
# 密码 没有则可以不填
password: 19e.com.cn
database: 0
# lettuce简单配置
lettuce:
pool:
# 最大活跃链接数 默认8
max-active: 5
# 最大空闲连接数 默认8
max-idle: 10
# 最小空闲连接数 默认0
min-idle: 0
RedisConfig
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* redis配置
*
* @AutoConfigureAfter(RedisAutoConfiguration.class) 是让我们这个配置类在内置的配置类之后在配置,
* 这样就保证我们的配置类生效,并且不会被覆盖配置 选择性配置
*/
@Configuration
//@AutoConfigureAfter(RedisAutoConfiguration.class)
public class RedisConfig {
/**
* 配置自定义redisTemplate
*
* @return
*/
@Bean
RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
//使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值
Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
serializer.setObjectMapper(mapper);
template.setValueSerializer(serializer);
//使用StringRedisSerializer来序列化和反序列化redis的key值
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer(serializer);
template.afterPropertiesSet();
return template;
}
}
测试
测试对象
import java.io.Serializable;
public class User implements Serializable {
private static final long serialVersionUID = 3529219554011221820L;
private String name;
private Integer age;
public User() {
}
public User(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
单元测试
import com.qiuwei.common.demo.User;
import org.junit.Test;
import org.junit.runner.RunWith;
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.junit4.SpringRunner;
@SpringBootTest
@RunWith(SpringRunner.class)
public class RedisTest {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void redisTest() {
// redis存储数据
String key = "name";
redisTemplate.opsForValue().set(key, "qiuwei");
// 获取数据
String value = (String) redisTemplate.opsForValue().get(key);
System.out.println("key:" + key + "value:" + value);
User user = new User("qw",12);
String userKey = "qiuwei";
redisTemplate.opsForValue().set(userKey, user);
User newUser = (User) redisTemplate.opsForValue().get(userKey);
System.out.println("key:" + userKey + "value:" + newUser);
}
}
结果
完整项目地址 github : https://github.com/imqw/common-springboot/tree/redis-Lettuce