SpringBoot项目整合Redis作为缓存中间件的详细步骤

文章详细介绍了如何在SpringBoot项目中整合Redis作为缓存中间件,包括添加依赖、配置YML、使用redisTemplate以及测试示例。在测试中遇到的堆外内存溢出问题,作者提供了Lettuce客户端的升级和Jedis作为替代的解决方案。文章最后提到了分布式环境下可能面临的线程数据一致性、缓存击穿、穿透和雪崩等问题。
摘要由CSDN通过智能技术生成

SpringBoot项目整合Redis作为缓存中间件的详细步骤


有更好的建议,欢迎评论区留言~
有不详细或者不准确的地方,欢迎评论区指正~
有技术群嘛 hahh 可以拉我么 ~

1.链接

哔哩教程视频
Redis官方

2.整合步骤

1.添加pom依赖

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-redis</artifactId>
		</dependency>

2.配置yml文件
yml配置,安装redis时设置了密码,就需要配置 password

spring:
  redis:
    host: 192.168.13.128
    port: 6379
    password: 123456

3.使用springBoot自动配置
(RedisAutoConfiguration) 的 redisTemplate/stringRedisTemplate
redis自动配置的源码

3.测试Demo

使用的是自动配置的stringRedisTemplate

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import java.util.UUID;

@Slf4j
@SpringBootTest
class PuductApplicationTests {
    
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    
    @Test
    public void  teststringRedisTemplate(){
        ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
        ops.set("我是key","我是value值"+ UUID.randomUUID().toString());
        String hello = ops.get("我是key");
        System.out.println("之前保存的:"+ hello);
    }
}

业务中存取对象

    public Map<String, List<Catalog2Vo>> getCatalogJson() {
        String catalogJson = redisTemplate.opsForValue().get("catalogJson");
        if(StringUtil.isEmpty(catalogJson)) {
            Map<String, List<Catalog2Vo>> catalogJsonDB  = getCatalogJsonFromDB();   //查询数据库
            catalogJson = JSON.toJSONString(catalogJsonDB); //对象转json字符串
            redisTemplate.opsForValue().set("catalogJson",catalogJson);
            return catalogJsonDB;
        }
        Map<String, List<Catalog2Vo>> result = JSON.parseObject(catalogJson,new TypeReference<Map<String, List<Catalog2Vo>>>(){});
        return result;
    }

4.遇到的问题

压测产生堆外内存溢出 OutOfDirectMemoryError 分析:

1)、SpringBoot2.0 以后默认使用 Lettuce 操作 Redis 的客户端,它使用 Netty 进行网络通信

2)、Lettuce 的 bug 导致 Netty 堆外内存溢出 可设置:-Dio.netty.maxDirectMemory

解决方案:(不能直接使用-Dio.netty.maxDirectMemory去调大堆外内存)

方案内容
方案一升级 Lettuce 客户端。(优点使用 Netty 作为底层网络框架,吞吐量大)
方案二切换使用 Jedis (缺点许久未更新)
如果使用方案二,data-redis 里面排除 lettuce-core
再引入 jedis , springboot 里面有该依赖,所以不用写版本号
		<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>

5.待考虑问题

  • 分布式项目,线程数据不一致,加锁问题
  • 缓存击穿、穿透、雪崩问题的解决
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值