Mac 安装 Redis + Spring Boot 整合 Redis(完整实战指南)

一、为什么用 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 解析字符串失败

解决 :显式指定 StringCodecJsonJacksonCodec


❌ 坑 3:Redisson 版本太高

Spring Boot Redisson
2.6.x ✅ 3.16.8
3.x ✅ 3.23+

相关推荐
元Y亨H16 小时前
MacBook Air 开发神器:IDEA 与 PyCharm 极简安装及环境配置
macos
用户3169353811831 天前
Java连接Redis
redis
yuanyxh1 天前
macOS 应用 - 纯对话生成
前端·macos·ai编程
用户3521802454752 天前
当 Prompt 学会"热更新":Spring Boot × Nacos3 AI 实战
java·spring boot·ai编程
昵称为空C2 天前
手撸一个动态 SQL 执行引擎:不重启服务,在线增删改查任意数据库
spring boot·后端
霸道流氓气质3 天前
领域驱动设计(DDD)在 Spring Boot 微服务中的实践指南
运维·spring boot·微服务
于先生吖3 天前
SpringBoot对接大模型开发AI命理测算系统:八字排盘与AI解析接口源码全解
人工智能·spring boot·后端
AI创界者3 天前
PilotTTS 一键整合包(Win/Mac):8G 显存畅跑,实测解锁情绪与副语言的精准控制
人工智能·macos·aigc·音视频
小小工匠3 天前
Redis - 事务机制:能实现 ACID 属性吗
数据结构·redis·性能优化·并发·持久化