一、为什么用 Redis?
没有 Redis,你的数据库迟早会被高并发打爆。
| 场景 | Redis 的作用 |
|---|---|
| 首页数据 | 缓存热点数据 |
| 用户登录 | 存储 Session / Token |
| 秒杀库存 | 原子计数 |
| 分布式系统 | 分布式锁 |
二、Mac 安装 Redis(完整步骤)
✅ Step 1:安装 Homebrew(Mac 神器)
如果你还没装 Homebrew(终端里执行 brew -v没反应):
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
安装完成后,按终端提示把 brew加入环境变量。
✅ Step 2:安装 Redis
brew install redis
安装完成后,你会看到 Redis 的版本号。
✅ Step 3:启动 Redis(两种方式)
✅ 方式一:后台运行(推荐)
brew services start redis
✅ 开机自启,关终端也不停。
✅ 方式二:前台运行(调试用)
redis-server
✅ Step 4:验证 Redis 是否安装成功
打开 新终端窗口,执行:
redis-cli ping
✅ 返回:
PONG
🎉 恭喜,Redis 已经在你的 Mac 上跑起来了。
✅ Step 5:Redis 基础命令
redis-cli
127.0.0.1:6379> set name beiluo
OK
127.0.0.1:6379> get name
"beiluo"
三、Spring Boot 整合 Redis(Redisson)
⚠️ 强烈建议用 Redisson,不要用 Jedis/Lettuce
Redisson 自带分布式锁、布隆过滤器,企业级开发首选。
✅ Step 1:创建 Spring Boot 项目
- Spring Boot:2.6.13
- Java:8 / 11 / 17
- 依赖:Spring Web
✅ Step 2:引入 Redisson 依赖(关键)
⚠️ 版本一定要对!
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson-spring-boot-starter</artifactId>
<version>3.16.8</version>
</dependency>
📌 Spring Boot 2.6.x + Redisson 3.16.8 = 稳
✅ Step 3:application.yml 配置
server:
port: 8080
spring:
redis:
host: localhost
port: 6379
✅ Step 4:Redisson 配置类(核心)
一定要显式指定 Codec(编码方式),否则你会遇到各种反序列化报错。
@Configuration
public class RedisConfig {
@Bean
public RedissonClient redissonClient() {
Config config = new Config();
config.useSingleServer()
.setAddress("redis://127.0.0.1:6379");
// ✅ 使用 JSON 编解码器(最通用)
config.setCodec(new JsonJacksonCodec());
return Redisson.create(config);
}
}
四、Spring Boot 操作 Redis(实战代码)
✅ 1️⃣ 字符串读写(最常用)
@RestController
@RequestMapping("/redis")
public class RedisTestController {
@Autowired
private RedissonClient redissonClient;
@GetMapping("/set")
public String set() {
RBucket<String> bucket =
redissonClient.getBucket("demo", StringCodec.INSTANCE);
bucket.set("hello-redis");
return "set ok";
}
@GetMapping("/get")
public String get() {
RBucket<String> bucket =
redissonClient.getBucket("demo", StringCodec.INSTANCE);
return bucket.get();
}
}
访问:
http://localhost:8080/redis/get
✅ 返回 hello-redis
✅ 2️⃣ 对象存储(JSON)
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private Integer id;
private String name;
}
@GetMapping("/user")
public User user() {
RBucket<User> bucket =
redissonClient.getBucket("user:1", JsonJacksonCodec.INSTANCE);
User user = new User(1, "beiluo");
bucket.set(user);
return bucket.get();
}
✅ 3️⃣ 分布式锁(Redisson 杀手锏)
@GetMapping("/lock")
public String lock() {
RLock lock = redissonClient.getLock("order:lock");
try {
lock.lock();
// 模拟业务操作
Thread.sleep(1000);
return "业务执行成功";
} catch (InterruptedException e) {
return "异常";
} finally {
lock.unlock();
}
}
✅ 解决 并发安全问题
五、会踩的坑(提前避雷)
❌ 坑 1:Redis 没启动
Unable to connect to Redis
✅ 解决:
brew services start redis
❌ 坑 2:Codec 不一致
| 报错 | 原因 |
|---|---|
Unsupported protocol version |
Marshalling 冲突 |
Unrecognized token |
JSON 解析字符串失败 |
✅ 解决 :显式指定 StringCodec或 JsonJacksonCodec
❌ 坑 3:Redisson 版本太高
| Spring Boot | Redisson |
|---|---|
| 2.6.x | ✅ 3.16.8 |
| 3.x | ✅ 3.23+ |