目录
前言
为了怕自己忘记,当做笔记用。
一、如何集成?
1.引入包
<!--redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2.添加配置
application.yaml
spring:
#redis
redis:
lettuce:
pool:
max-active: 10
max-idle: 10
max-wait: 1
min-idle: 0
cluster:
max-redirects: 3
nodes: 192.168.3.170:6379,192.168.3.172:6379,192.168.3.175:6379,192.168.3.170:6389,192.168.3.172:6389,192.168.3.175:6389
password: xxxxxx
3.添加配置类
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory){
// 设置序列化
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
// 配置redisTemplate
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(connectionFactory);
RedisSerializer<?> stringSerializer = new StringRedisSerializer();
// key序列化
redisTemplate.setKeySerializer(stringSerializer);
// value序列化
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
// Hash key序列化
redisTemplate.setHashKeySerializer(stringSerializer);
// Hash value序列化
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}
4.使用示例
二、为什么?
1.配置文件的参数怎么填写?
在springboot的自动装配已经集成,所以看这个类RedisProperties就可以知道怎么配置。在这个包中:package org.springframework.boot.autoconfigure.data.redis;
2.为什么默认是Lettuce而不是Jedis?
因为在RedisAutoConfigration中第一个引入的是LettuceConnectionConfiguation。
并且LettuceConnectionConfiguation和JedisConnectionConfiguration中都用这个注解来标注@ConditionalOnMissingBean,当lettuce的存在后jedis就不会注入,防止多个注入。
3.什么时候会自动装配?
在spring.factories中的org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
这里已经写死了会加载这个RedisAutoConfiguation类。
当引入这个类RedisOperations 的时候就会自动装配,@ConditionnalOnClass这个注解的作用。
而RedisOperations 这个类是在spring-data-redis这个包中,而spring-data-redis是在 spring-boot-starter-data-redis引入。
<!--redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<!-- This module was also published with a richer model, Gradle metadata, -->
<!-- which should be used instead. Do not delete the following line which -->
<!-- is to indicate to Gradle or any Gradle module metadata file consumer -->
<!-- that they should prefer consuming it instead. -->
<!-- do_not_remove: published-with-gradle-metadata -->
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.3.2.RELEASE</version>
<name>spring-boot-starter-data-redis</name>
<description>Starter for using Redis key-value data store with Spring Data Redis and the Lettuce client</description>
<url>https://spring.io/projects/spring-boot</url>
<organization>
<name>Pivotal Software, Inc.</name>
<url>https://spring.io</url>
</organization>
<licenses>
<license>
<name>Apache License, Version 2.0</name>
<url>https://www.apache.org/licenses/LICENSE-2.0</url>
</license>
</licenses>
<developers>
<developer>
<name>Pivotal</name>
<email>info@pivotal.io</email>
<organization>Pivotal Software, Inc.</organization>
<organizationUrl>https://www.spring.io</organizationUrl>
</developer>
</developers>
<scm>
<connection>scm:git:git://github.com/spring-projects/spring-boot.git</connection>
<developerConnection>scm:git:ssh://git@github.com/spring-projects/spring-boot.git</developerConnection>
<url>https://github.com/spring-projects/spring-boot</url>
</scm>
<issueManagement>
<system>GitHub</system>
<url>https://github.com/spring-projects/spring-boot/issues</url>
</issueManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.3.2.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.3.2.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
<version>5.3.2.RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
总结
提示:这里对文章进行总结:
例如:备忘