当前位置:   article > 正文

Spring Boot 整合 Redis 全面教程:从配置到使用_springboot 配置redis_springboot redis配置

springboot redis配置

最后

由于篇幅有限,这里就不一一罗列了,20道常见面试题(含答案)+21条MySQL性能调优经验小编已整理成Word文档或PDF文档

MySQL全家桶笔记

还有更多面试复习笔记分享如下

Java架构专题面试复习

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

在配置类中创建 RedisTemplate Bean,用于进行 Redis 操作

@Configuration
public class RedisConfig {

  @Bean
  public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
    RedisTemplate<String, Object> template = new RedisTemplate<>();
    template.setConnectionFactory(connectionFactory);
    template.setKeySerializer(new StringRedisSerializer());
    template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
    return template;
  }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

上述示例中使用了 JSON 序列化器来对值进行序列化和反序列化,你也可以根据需要选择其他序列化器。

2. 注入 RedisTemplate

在服务类或控制器中注入 RedisTemplate

@Autowired
private RedisTemplate<String, Object> redisTemplate;

  • 1
  • 2
  • 3

3. 执行 Redis 操作

使用注入的 RedisTemplate 来进行 Redis 操作,设置键值对、获取值等

// 设置键值对
redisTemplate.opsForValue().set("key", "value");

// 获取值
String value = (String) redisTemplate.opsForValue().get("key");

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

四、使用 Spring Cache 简化缓存操作

1. 添加 Spring Cache 依赖

pom.xml 文件中添加 Spring Cache 相关依赖

<dependencies>
  <!-- Spring Data Redis -->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
  </dependency>
  
  <!-- Spring Cache -->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-cache</artifactId>
  </dependency>
</dependencies>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

2. 启用缓存支持

在启动类上添加 @EnableCaching 注解,启用缓存支持

@SpringBootApplication
@EnableCaching
public class Application {
  // ...
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

3. 使用缓存注解

在服务类或方法上使用 Spring Cache 提供的缓存注解,如 @Cacheable@CachePut@CacheEvict

@Service
public class demoService {

  @Cacheable(value = "cacheName", key = "#id")
  public Object getData(String id) {
    // 从数据库或其他数据源获取数据
    return data;
  }

  @CachePut(value = "cacheName", key = "#id")
  public Object updateData(String id, Object newData) {
    // 更新数据库或其他数据源的数据
    return newData;
  }

  @CacheEvict(value = "cacheName", key = "#id")
  public void deleteData(String id) {
    // 删除数据库或其他数据源的数据
  }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

五、使用 Redisson 实现分布式锁

1. 添加 Redisson 依赖

pom.xml 文件中添加 Redisson 相关依赖

<dependencies>
  <!-- Spring Data Redis -->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
  </dependency>
  
  <!-- Redisson -->
  <dependency>
    <groupId>org.redisson</groupId>
    <artifactId>redisson-spring-boot-starter</artifactId>
    <version>3.16.3</version>
  </dependency>
</dependencies>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

2. 配置 Redisson

在配置文件中添加 Redisson 配置,例如 application.properties

# 单节点 Redisson 配置
spring.redis.redisson.config=classpath:redisson-single-node.yml

  • 1
  • 2
  • 3

redisson-single-node.yml 配置文件中配置 Redisson 的连接信息

3. 使用 Redisson 获取锁:

在你的代码中使用 Redisson 获取分布式锁:

@Autowired
private RedissonClient redissonClient;

public void doSomething() {
  RLock lock = redissonClient.getLock("lockName");
  try {
    lock.lock();
    // 执行需要加锁的操作
  } finally {
    lock.unlock();
  }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

六、完善 Redis 的其他配置

除了基本配置,还可以根据实际需求完善 Redis 的其他配置,例如连接池配置、超时设置等。

一、连接池配置

Redis 使用连接池来管理和复用与 Redis 服务器的连接,以提高连接的效率和性能。

1. 在 配置文件中配置连接池相关参数

打开 Redis 配置文件 redis.conf,找到以下配置项并进行修改

# 最大连接数
maxclients 10000

# TCP 连接的队列长度
tcp-backlog 511

# 连接池中的最大空闲连接数
maxidle 100

# 连接池中的最小空闲连接数
minidle 10

# 连接超时时间(毫秒)
timeout 3000

# 是否开启 TCP 连接的保活机制
tcp-keepalive 0

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
2. 通过客户端连接池配置对象进行配置

在 Spring Boot 项目中,可以通过 Redis 连接池配置对象 JedisPoolConfig 进行配置

@Configuration
public class RedisConfig {


# **最后**

分享一些系统的面试题,大家可以拿去刷一刷,准备面试涨薪。

**这些面试题相对应的技术点:**

*   JVM
*   MySQL
*   Mybatis
*   MongoDB
*   Redis
*   Spring
*   Spring boot
*   Spring cloud
*   Kafka
*   RabbitMQ
*   Nginx
*   ......

**大类就是:**

*   Java基础
*   数据结构与算法
*   并发编程
*   数据库
*   设计模式
*   微服务
*   消息中间件

![程序员,每个月给你发多少工资,你才会想老板想的事?](https://img-blog.csdnimg.cn/img_convert/d2f960817c0da4e2ba4d28ac9e61b786.webp?x-oss-process=image/format,png)

![程序员,每个月给你发多少工资,你才会想老板想的事?](https://img-blog.csdnimg.cn/img_convert/59e7eb6dd47d17721059ce0722245591.webp?x-oss-process=image/format,png)

![程序员,每个月给你发多少工资,你才会想老板想的事?](https://img-blog.csdnimg.cn/img_convert/d0799028dfdcb2810e345ca69a3990aa.webp?x-oss-process=image/format,png)

![程序员,每个月给你发多少工资,你才会想老板想的事?](https://img-blog.csdnimg.cn/img_convert/cdee45f6005041dd26bced858d39e677.webp?x-oss-process=image/format,png)

![程序员,每个月给你发多少工资,你才会想老板想的事?](https://img-blog.csdnimg.cn/img_convert/9438a6386ab4d380c49ed2f977227b86.webp?x-oss-process=image/format,png)

![程序员,每个月给你发多少工资,你才会想老板想的事?](https://img-blog.csdnimg.cn/img_convert/26182e33e15b75d1b0144e7527e6b3ea.webp?x-oss-process=image/format,png)

![程序员,每个月给你发多少工资,你才会想老板想的事?](https://img-blog.csdnimg.cn/img_convert/c0341ea43d4614d3070932ec0eb73f5c.webp?x-oss-process=image/format,png)

![程序员,每个月给你发多少工资,你才会想老板想的事?](https://img-blog.csdnimg.cn/img_convert/fb619db505c1d4ecc7da06b2412fcb49.webp?x-oss-process=image/format,png)

![程序员,每个月给你发多少工资,你才会想老板想的事?](https://img-blog.csdnimg.cn/img_convert/f39b2ad60920aef09937d38315295b01.webp?x-oss-process=image/format,png)



> **本文已被[CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】](https://bbs.csdn.net/forums/4f45ff00ff254613a03fab5e56a57acb)收录**

**[需要这份系统化的资料的朋友,可以点击这里获取](https://bbs.csdn.net/forums/4f45ff00ff254613a03fab5e56a57acb)**

[外链图片转存中...(img-RrRYTaeI-1715464334991)]

[外链图片转存中...(img-Yrc6IaSu-1715464334991)]



> **本文已被[CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】](https://bbs.csdn.net/forums/4f45ff00ff254613a03fab5e56a57acb)收录**

**[需要这份系统化的资料的朋友,可以点击这里获取](https://bbs.csdn.net/forums/4f45ff00ff254613a03fab5e56a57acb)**

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/寸_铁/article/detail/861280
推荐阅读
相关标签
  

闽ICP备14008679号