返回 导航

SpringBoot / Cloud

hangge.com

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 连接信息:
(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:表示连接池最小空闲连接数
(2)如果项目使用了 Lettuce,则只需要将后面 4 个配置中的 jedis 修改为 lettuce 即可。
# 基本连接信息配置
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

2,使用样例

(1)首先创建一个 User 实体类,内容如下:
@Getter
@Setter
@ToString
public class User implements Serializable {
    private Integer id;
    private String name;
    private Integer age;
}

(2)接着创建一个 Controller 进行测试:
(1) RedisTemplate StringRedisTemplate 实例提供了 Redis 的基本操作方法,如果开发者自己没有提供,则 Spring Boot 默认会提供这两个实例:
  • StringRedisTemplate RedisTemplate 的子类,StringRedisTemplate 中的 key value 都是字符串,采用的序列化方案是 StringRedisSerializer
  • RedisTemplate 则可以用来操作对象,RedisTemplate 采用的序列化方案是 JdkSerializationRedisSerializer
(2)无论 StringRedisTemplate 还是 RedisTemplate,它们都是通过 opsForValueopsForZSet 或者 opsForSet 等方法先获取一个操作对象,再使用该操作对象完成数据的读写。
@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)

回到顶部