SpringBoot 整合 Redis 的简单使用

前文

Redis 的下载安装 https://blog.csdn.net/Woo_home/article/details/89155996
Redis 的简单实用 https://blog.csdn.net/Woo_home/article/details/103901770

简介

Redis 的 Java 客户端有很多种,例如 Jedis、JRedis、Spring Data Redis 等,SpringBoot 借助于 Spring Data Redis 为 Redis 提供了开箱即用的自动化配置,开发者只需要添加相关的依赖并配置 Redis 连接信息即可

添加依赖

<dependencies>

       <!-- 导入 web -->
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-web</artifactId>
       </dependency>

	<!-- 添加 Redis -->
	<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>

	<!-- 添加 Jedis 客户端-->
	<dependency>
		<groupId>redis.clients</groupId>
		<artifactId>jedis</artifactId>
	</dependency>

	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-test</artifactId>
		<scope>test</scope>
		<exclusions>
			<exclusion>
				<groupId>org.junit.vintage</groupId>
				<artifactId>junit-vintage-engine</artifactId>
			</exclusion>
		</exclusions>
	</dependency>
</dependencies>

配置 Redis

spring.redis.database=0
# Redis 的实例地址
spring.redis.host=127.0.0.1
# Redis 的默认端口号
spring.redis.port=6379
# Redis 的登录密码
spring.redis.password=
# Redis 连接池的最大连接数
spring.redis.jedis.pool.max-active=8
# Redis 连接池中的最大空闲连接数
spring.redis.jedis.pool.max-idle=8
# 表示连接池的最大阻塞等待时间,默认为 -1,表示没有限制
spring.redis.jedis.pool.max-wait=-1ms
# 表示连接池的最小空闲连接数
spring.redis.jedis.pool.min-idle=0

在设置密码的时候如果是不知道 Redis 的密码的可以这样做:
启动 redis 的客户端程序,输入以下命令

config get requirepass   # 获取密码

在这里插入图片描述
如果要设置新的密码,输入以下命令

config set requirepass 你要设置的密码 # 设置密码

然后再次输入获取密码的命令时可能会报以下的错误

(error) NOAUTH Authentication required.

只要输入 auth 你设置的密码 就可以了

创建实体类

package com.example.demo.entity;

import java.io.Serializable;

public class User implements Serializable {

    private Integer id;

    private String name;

    private String info;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getInfo() {
        return info;
    }

    public void setInfo(String info) {
        this.info = info;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", info='" + info + '\'' +
                '}';
    }
}

@RedisAutoConfiguration

SpringBoot 的自动配置类中提供了 RedisAutoConfiguration 注解进行 Redis 的自动装配,源码如下:

@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;
    }
}

从 @RedisAutoConfiguration 注解的源码可以知道,该注解定义了两个 Bean:

  • RedisTemplate
  • StringRedisTemplate

而且 application.properties 中的配置信息会被注入到 RedisProperties 中,如果开发者自己没有提供 RedisTemplate 或者 StringRedisTemplate 实例,则 SpringBoot 默认会提供这两个实例,RedisTemplate 和 StringRedisTemplate 实例则提供了 Redis 的基本操作方法

编写控制器代码

RedisTemplate 与 StringRedisTemplate 操作

RedisTemplate

package com.example.demo.controller;

import com.example.demo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("test")
    public void test() {
        ValueOperations opsForValue = redisTemplate.opsForValue();
        User setUser = new User();
        setUser.setId(1);
        setUser.setName("John");
        setUser.setInfo("我爱打篮球");
        opsForValue.set("user",setUser);
        User getUser = (User) opsForValue.get("user");
        System.out.println(getUser);
    }
}

在浏览器中访问 http://localhost:8080/test,控制台显示如下:
在这里插入图片描述

StringRedisTemplate

package com.example.demo.controller;

import com.example.demo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping("test")
    public void test() {
        ValueOperations<String,String> opsForValue = stringRedisTemplate.opsForValue();
        opsForValue.set("name","lisa");
        String name = opsForValue.get("name");
        System.out.println(name);
    }
}

在浏览器中访问 http://localhost:8080/test,控制台显示如下:
在这里插入图片描述

  • StringRedisTemplate 是 RedisTemplate 的子类,StringRedisTemplate 中的 key 和 value 都是字符串,采用的序列化的方案是 StringRedisSerializer,而 RedisTemplate 则可以用来操作对象,RedisTemplate 采用的序列化方案是 JdkSerializationRedisSerializer,无论是 StringRedisTemplate 还是 RedisTemplate,操作 Redis 的方法都是一样的

  • StringRedisTemplate 和 RedisTemplate 都是通过 opsForValue、opsForZSet 或者 opsForSet 等方法首先获取一个操作对象,再使用该操作对象完成数据的读写

发布了227 篇原创文章 · 获赞 1032 · 访问量 24万+

猜你喜欢

转载自blog.csdn.net/Woo_home/article/details/104174729