Explanation: After SpringBoot 2.x, the original jedis used was replaced by lettuce
jedis: the direct connection used, if multiple threads are used, is unsafe. If you want to avoid unsafe, use the jedis pool connection pool, which is more like BIO mode
lettuce: Using netty, instances can be shared in multiple threads, and there is no thread insecurity. It can reduce thread data. More like NIO Moss
1. Build a springboot project, configure redis
All configuration classes of springboot have an automatic configuration class; RedisAutoConfiguration
The automatic configuration class will bind a xx.properties(RedisProperties) Configuration file, this configuration file is bound to application.properties
@Bean
@ConditionalOnMissingBean( //可以自定义一个redisTemplate来替换这个默认的
name = {
"redisTemplate"}
)
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
// 默认的redisTemplate没有过多的设置,redis对象都是需要序列化的
// 两个泛型都是Object的类型,后面使用需要强制转换成<String,Object>
RedisTemplate<Object, Object> template = new RedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
@Bean
@ConditionalOnMissingBean //由于String是redis中最常用的类型,所以暗度提出来一个bean
public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
StringRedisTemplate template = new StringRedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
- 1. Import dependencies
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
- 2. Configure the connection
spring.redis.host=127.0.0.1
spring.redis.port=6379
- 3. Test
@Autowired
private RedisTemplate redisTemplate;
@Test
void contextLoads() {
redisTemplate.opsForValue().set("key1","binhaizhijun");
System.out.println(redisTemplate.opsForValue().get("key1"));
}
The opsForXxx() in redisTemplate can operate on different data types. The api and redis instructions are the same;
except for the basic operations, the commonly used methods can be directly operated through redisTemplate, such as morning and basic CRUD to
get the connection object of redis as follows :
RedisConnection connection = redisTemplate.getConnectionFactory().getConnection();
connection.flushAll();
connection.flushDb();
2. Customize RedisTemplate
When redis is storing objects, if the object is not serialized, it will report an error Failed to serialize object using DefaultSerializer;
(usually the json string is stored in enterprise development); so the general entity class needs to implement the serialization interface;
Redis uses the jdk default by default Serialization method, if you need to change the serialization method, you need to customize
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
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;
/**
* @ProjectName: qun-redis
* @Package: com.qun.config
* @ClassName: RedisConfig
* @Description: redisTemplate 序列化的配置类
* @Author: 滨海之君
* @CreateDate: 2021/3/2 22:06
*/
@Configuration
public class RedisConfig {
@Bean
@SuppressWarnings("all")
public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory factory){
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(factory);
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
// key采用String的序列化方式
template.setKeySerializer(stringRedisSerializer);
// hash的key采用String的序列化方式
template.setHashKeySerializer(stringRedisSerializer);
//value序列化方式采用jackson
template.setValueSerializer(jackson2JsonRedisSerializer);
// hash的value序列化方式采用jackson
template.setHashValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
}