SpringBoot - Redis的整合与使用详解1(单Redis实例)
作者:hangge | 2019-11-28 08:10
一、Redis 的整合与使用:单 Redis 实例
1,安装配置
(1)首先编辑 pom.xml 文件,添加相关依赖:
- Spring Boot 借助于 Spring Data Redis 为 Redis 提供了开箱即用的自动化配置,我们只需要添加相关依赖并配置 Redis 连接信息即可。
- 默认情况下,spring-boot-starter-data-redis 使用的 Redis 工具是 Lettuce。考虑到有的开发者习惯使用 Jedis,这里从 spring-boot-starter-data-redis 中排除 Lettuce 并引入 Jedis。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<exclusions>
<exclusion>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
(2)接着在 application.properties 中配置 Redis 连接信息,下面是 Spring Boot 1.x 版本的配置:
(1)配置说明:
- spring.redis.database:表示使用的 Redis 库的编号,Redis 中提供了 16 个 database(编号为 0~15)
- spring.redis.host:表示 Redis 实例的地址
- spring.redis.port:表示 Redis 端口号(默认为 6379)
- spring.redis.password:表示 Redis 登录密码
- spring.redis.jedis.pool.max-active:表示 Redis 连接池的最大连接数
- spring.redis.jedis.pool.max-idle:表示 Redis 连接池中的最大空闲连接数
- spring.redis.jedis.pool.max-wait:表示连接池的最大阻塞等待使用,默认为 -1,表示没有限制
- spring.redis.jedis.pool.min-idle:表示连接池最小空闲连接数
# 基本连接信息配置 spring.redis.database=0 spring.redis.host=192.168.60.133 spring.redis.port=6379 spring.redis.password=123 # 连接池信息配置 spring.redis.jedis.pool.max-active=8 spring.redis.jedis.pool.max-idle=8 spring.redis.jedis.pool.max-wait=-1ms spring.redis.jedis.pool.min-idle=0
- 如果是 Spring Boot 2.x 或 3.x 版本则需要使用如下配置结构。spring.data.redis 是从 Spring Boot 2.x 版本开始引入的新的配置前缀,也用于配置 Redis 的相关参数。
# 基本连接信息配置 spring.data.redis.database=0 spring.data.redis.host=192.168.121.128 spring.data.redis.port=6379 spring.data.redis.password=123 # 连接池信息配置 spring.data.redis.jedis.pool.max-active=8 spring.data.redis.jedis.pool.max-idle=8 spring.data.redis.jedis.pool.max-wait=-1ms spring.data.redis.jedis.pool.min-idle=0
2,使用样例
(1)首先创建一个 User 实体类,内容如下:@Getter
@Setter
@ToString
public class User implements Serializable {
private Integer id;
private String name;
private Integer age;
}
(1) RedisTemplate 和 StringRedisTemplate 实例提供了 Redis 的基本操作方法,如果开发者自己没有提供,则 Spring Boot 默认会提供这两个实例:
- StringRedisTemplate 是 RedisTemplate 的子类,StringRedisTemplate 中的 key 和 value 都是字符串,采用的序列化方案是 StringRedisSerializer。
- RedisTemplate 则可以用来操作对象,RedisTemplate 采用的序列化方案是 JdkSerializationRedisSerializer。
@RestController
public class HelloController {
@Autowired
RedisTemplate redisTemplate; //默认提供的用来操作对象的redis操作实例
@Autowired
StringRedisTemplate stringRedisTemplate; //默认提供的用来操作字符串的redis操作实例
@RequestMapping("/test")
public void test() {
//保存一个字符串
ValueOperations<String, String> ops1 = stringRedisTemplate.opsForValue();
ops1.set("message", "欢迎访问 hangge.com");
//读取一个字符串
String message = ops1.get("message");
System.out.println("读取一个字符串:" + message);
//保存一个对象
ValueOperations ops2 = redisTemplate.opsForValue();
User u1 = new User();
u1.setId(1);
u1.setName("hangge");
u1.setAge(100);
ops2.set("user", u1);
//读取一个对象
User u2 = (User) ops2.get("user");
System.out.println("读取一个对象:" + u2.toString());
}
}
(3)使用浏览器访问 http://localhost:8080/test,可以看到控制台输入如下:
全部评论(0)