整合Redis
Redis的Java客户端有很多,例如Jedis、JRdis、Spring Data Redis等,SpringBoot借助于Spring Data Redis为Redis提供了开箱即用自动化配置,开发者只需要添加相关依赖并配置Redis连接信息即可,具体步骤如下:
1. 创建SpringBoot Web项目,添加如下依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
默认情况下,spring-boot-starter-data-redis使用的Redis工具是Lettuce,如果使用Jedis,可以先排除Lettuce并引入Jedis,修改为如下依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<exclusions>
<exclusion>
<artifactId>lettuce-core</artifactId>
<groupId>io.lettuce</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
2. 配置Redis:
spirng:
redis:
database: 0
host: localhost
port: 6379
password: 123456
jedis:
pool:
max-active: 8 #最大连接数
max-idle: 8 #最大空闲连接数
max-wait: -1ms #最大阻塞等待事件,默认为-1,表示没有限制
min-idle: 0 #最小空闲连接数
如果使用了Lettuce,则只需将配置中的Jedis修改为lettuce即可。
在SpringBoot的自动配置类中提供了RedisAutoConfiguration进行Redis的配置,源码如下:
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package org.springframework.boot.autoconfigure.data.redis;
import java.net.UnknownHostException;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
@Configuration(
proxyBeanMethods = false
)
@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;
}
}
可以看出application.properties中配置的信息将被注入RedisProperties中,如果没有提供RedisTemplate或者StringRedisTemplate实例,则SpringBoot默认提供这 两个实例,RedisTemplate和StringRedisTemplate实例提供了Redis的基本操作方法。
3. 创建实体类:
public class Book implements Serializable {
private int id;
private String name;
private String author;
//省略getter和setter以及toString
4. 创建Controller:
//测试redis
@Autowired
RedisTemplate redisTemplate;
@Autowired
StringRedisTemplate stringRedisTemplate;
@GetMapping("testRedis")
public void test(){
ValueOperations<String, String> ops1 = stringRedisTemplate.opsForValue();
//#插入一条数据
ops1.set("name", "三国演义");
//取出
String name = ops1.get("name");
System.out.println(name);
ValueOperations ops2 = redisTemplate.opsForValue();
Book b1 = new Book();
b1.setId(1);
b1.setName("红楼梦");
b1.setAuthor("曹雪芹");
//插入数据
ops2.set("b1", b1);
//取出
Book book = (Book) ops2.get("b1");
System.out.println(book);
}
- StringRedisTemplate是RedisTemplate的子类,StringRedisTemplate中的key和value都是字符串,采用的序列化方案是StringRedisSerializer,而RedisTemplate则可以用来操作对象,RedisTemplate采用的序列化方案是JdkSerializationRedisSerializer。无论是StringRedisTemplate还是RedisTemplate,操作Redis的方法都是一致的。
- StringRedisTemplate和RedisTemplate都是通过opsForValue、opsForZSet或者opsForSet等方法首先获取一个操作对象,再使用该操作对象完成数据的读写。
5. 测试:
启动项目访问对应路径,控制台打印:
三国演义
Book{id=1, name='红楼梦', author='曹雪芹'}