欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot + Mybatis Plus 整合 Redis的詳細(xì)步驟

 更新時(shí)間:2025年03月18日 10:38:19   作者:測(cè)試開發(fā)小白變怪獸  
文章詳細(xì)介紹了Redis在用戶管理系統(tǒng)中的應(yīng)用,包括用戶信息緩存、Token存儲(chǔ)、接口限流、重復(fù)提交攔截和熱點(diǎn)數(shù)據(jù)預(yù)加載等場(chǎng)景,并提供了具體的實(shí)現(xiàn)方案和步驟,感興趣的朋友一起看看吧

Redis 在用戶管理系統(tǒng)中的典型應(yīng)用場(chǎng)景

結(jié)合你的用戶增刪改查接口,以下是 Redis 的實(shí)用場(chǎng)景和具體實(shí)現(xiàn)方案:

場(chǎng)景作用實(shí)現(xiàn)方案
用戶信息緩存減少數(shù)據(jù)庫壓力,加速查詢響應(yīng)使用 Spring Cache + Redis 注解緩存
登錄 Token 存儲(chǔ)分布式 Session 或 JWT Token 管理將 Token 與用戶信息綁定,設(shè)置過期時(shí)間
接口限流防止惡意刷接口基于 Redis 計(jì)數(shù)器實(shí)現(xiàn)滑動(dòng)窗口限流
重復(fù)提交攔截防止用戶重復(fù)提交表單用 Redis 存儲(chǔ)請(qǐng)求唯一標(biāo)識(shí),設(shè)置短期過期
熱點(diǎn)數(shù)據(jù)預(yù)加載提前緩存高頻訪問數(shù)據(jù)定時(shí)任務(wù) + Redis 存儲(chǔ)

Mac M1 安裝 Redis 詳細(xì)步驟

1. 通過 Homebrew 安裝 Redis

# 安裝 Homebrew(如果尚未安裝)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# 安裝 Redis
brew install redis

2. 啟動(dòng) Redis 服務(wù)

# 前臺(tái)啟動(dòng)(測(cè)試用,Ctrl+C 退出)
redis-server
# 后臺(tái)啟動(dòng)(推薦)
brew services start redis

3. 驗(yàn)證安裝

# 連接 Redis 客戶端
redis-cli ping  # 應(yīng)返回 "PONG"

Spring Boot 3 整合 Redis

1. 添加依賴

pom.xml 中:

		<!-- Spring Cache 核心依賴 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>

        <!-- Redis 驅(qū)動(dòng) -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>

2. 配置 Redis 連接

application.yml

spring:
  data:
    redis:
      host: localhost
      port: 6379
      # password: your-password  # 如果設(shè)置了密碼
      lettuce:
        pool:
          max-active: 8
          max-idle: 8

3. 示例

配置類

package com.example.spring_demo01.config;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.*;
import java.time.Duration;
@Configuration
public class RedisConfig {
    // 配置 RedisTemplate
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);
        // Key 序列化
        template.setKeySerializer(new StringRedisSerializer());
        // Value 序列化為 JSON
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        // Hash 結(jié)構(gòu)序列化
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
        return template;
    }
    // 配置緩存管理器
    @Bean
    public CacheManager cacheManager(RedisConnectionFactory factory) {
        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
                .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()))
                .entryTtl(Duration.ofMinutes(30)); // 設(shè)置默認(rèn)過期時(shí)間
        return RedisCacheManager.builder(factory)
                .cacheDefaults(config)
                .build();
    }
}

接口限流工具類

package com.example.spring_demo01.utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
@Component
public class RateLimiter {
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    public boolean allowRequest(String userId) {
        String key = "rate_limit:" + userId;
        long now = System.currentTimeMillis();
        long windowMs = 60_000; // 1 分鐘
        // 移除窗口外的請(qǐng)求記錄
        redisTemplate.opsForZSet().removeRangeByScore(key, 0, now - windowMs);
        // 統(tǒng)計(jì)當(dāng)前窗口內(nèi)請(qǐng)求數(shù)
        Long count = redisTemplate.opsForZSet().zCard(key);
        if (count != null && count >= 10) {
            return false; // 超過限制
        }
        // 記錄本次請(qǐng)求
        redisTemplate.opsForZSet().add(key, UUID.randomUUID().toString(), now);
        redisTemplate.expire(key, windowMs, TimeUnit.MILLISECONDS);
        return true;
    }
}

實(shí)體類

package com.example.spring_demo01.entity;
import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;
import java.io.Serializable;
@Data
@TableName("user")
@JsonIgnoreProperties(ignoreUnknown = true) // 防止 JSON 反序列化問題
public class User implements Serializable { // 實(shí)現(xiàn) Serializable
    @TableId(type = IdType.AUTO) // 主鍵自增
    private Long id;
    private String name;
    private Integer age;
    private String email;
}

service層

package com.example.spring_demo01.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.spring_demo01.entity.User;
import com.example.spring_demo01.mapper.UserMapper;
import com.example.spring_demo01.service.UserService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.io.Serializable;
@Service
public class UserServiceImpl
        extends ServiceImpl<UserMapper, User>
        implements UserService {
    // 對(duì) MyBatis Plus 的 getById 方法添加緩存
    @Cacheable(value = "user", key = "#id")
    @Override
    public User getById(Serializable id) {
        return super.getById(id);
    }
    // 更新時(shí)清除緩存
    @CacheEvict(value = "user", key = "#entity.id")
    @Override
    public boolean updateById(User entity) {
        return super.updateById(entity);
    }
    // 刪除時(shí)清除緩存
    @CacheEvict(value = "user", key = "#id")
    @Override
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }
}

controller層

package com.example.spring_demo01.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.spring_demo01.annotation.AdminOnly;
import com.example.spring_demo01.entity.User;
import com.example.spring_demo01.service.UserService;
import com.example.spring_demo01.utils.RateLimiter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import java.time.Duration;
import java.util.List;
import java.util.UUID;
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private RateLimiter rateLimiter;
    // ------------------------------ 增 ------------------------------
    @PostMapping
    public String addUser(@RequestBody User user, @RequestHeader String clientId) {
        String key = "SUBMIT_LOCK:" + clientId + ":" + user.hashCode();
        // 10秒內(nèi)不允許重復(fù)提交
        Boolean success = redisTemplate.opsForValue()
                .setIfAbsent(key, "", Duration.ofSeconds(10));
        if (Boolean.FALSE.equals(success)) {
            throw new RuntimeException("請(qǐng)勿重復(fù)提交");
        }
        userService.save(user);
        return "新增成功";
    }
    // ------------------------------ 刪 ------------------------------
    @DeleteMapping("/{id}")
    public String deleteUser(@PathVariable Long id) {
        userService.removeById(id);
        return "刪除成功";
    }
    @DeleteMapping("/batch")
    public String deleteBatch(@RequestBody List<Long> ids) {
        userService.removeByIds(ids);
        return "批量刪除成功";
    }
    // ------------------------------ 改 ------------------------------
    @PutMapping
    public String updateUser(@RequestBody User user) {
        userService.updateById(user);
        return "更新成功";
    }
    // ------------------------------ 查 ------------------------------
    @GetMapping("/{id}")
    @AdminOnly
    public User getUserById(@PathVariable Long id) {
        return userService.getById(id);
    }
    @GetMapping("/list")
    public List<User> listUsers(
            @RequestParam(required = false) String name,
            @RequestParam(required = false) Integer age) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        if (name != null) {
            wrapper.like("name", name); // 模糊查詢姓名
        }
        if (age != null) {
            wrapper.eq("age", age);     // 精確查詢年齡
        }
        return userService.list(wrapper);
    }
    @GetMapping("/page")
    public Page<User> pageUsers(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestHeader(value = "Authorization") String token) {
        // 從 Token 中獲取用戶ID
        log.info("token:{}", token);
        log.info("User:{}", redisTemplate.opsForValue().get(token.split(" ")[1]));
        User user = (User) redisTemplate.opsForValue().get(token.split(" ")[1]);
        if (user == null) throw new RuntimeException("未登錄");
        // 限流校驗(yàn)
        if (!rateLimiter.allowRequest("PAGE_" + user.getId())) {
            throw new RuntimeException("請(qǐng)求過于頻繁");
        }
        return userService.page(new Page<>(pageNum, pageSize));
    }
    // ------------------------------ other ------------------------------
    @GetMapping("/error")
    public String getError() {
        throw new RuntimeException();
    }
    @PostMapping("/login")
    public String login(@RequestBody User user) {
        log.info("login user:{}", user);
        // 驗(yàn)證用戶邏輯(示例簡(jiǎn)化)
        User dbUser = userService.getOne(new QueryWrapper<User>()
                .eq("id", user.getId())
                .eq("name", user.getName()));
        if (dbUser == null) {
            throw new RuntimeException("登錄失敗");
        }
        // 生成 Token 并存儲(chǔ)
        String token = "TOKEN_" + UUID.randomUUID();
        redisTemplate.opsForValue().set(
                token,
                dbUser,
                Duration.ofMinutes(30)
        );
        return token;
    }
}

在啟動(dòng)類添加注解:

package com.example.spring_demo01;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cache.annotation.EnableCaching;
@SpringBootApplication
@MapperScan("com.example.spring_demo01.mapper")
@ServletComponentScan // 啟用 Servlet 組件掃描(如 Filter、Servlet)
@EnableCaching // 啟動(dòng)緩存,Redis使用
public class SpringDemo01Application {
    public static void main(String[] args) {
        SpringApplication.run(SpringDemo01Application.class, args);
    }
}

常見問題排查

Q1: 連接 Redis 超時(shí)

  • 檢查服務(wù)狀態(tài):運(yùn)行 redis-cli ping 確認(rèn) Redis 是否正常運(yùn)行
  • 查看端口占用lsof -i :6379
  • 關(guān)閉防火墻sudo ufw allow 6379

Q2: Spring Boot 無法注入 RedisTemplate

  • 確認(rèn)配置類:添加 @EnableCaching 和 @Configuration
  • 檢查序列化器:顯式配置序列化方式避免 ClassCastException
@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        return template;
    }
}

總結(jié)

通過 Redis 你可以為項(xiàng)目快速實(shí)現(xiàn):

  • 高性能緩存層 - 降低數(shù)據(jù)庫負(fù)載
  • 分布式會(huì)話管理 - 支持橫向擴(kuò)展
  • 精細(xì)化流量控制 - 保障系統(tǒng)穩(wěn)定性

到此這篇關(guān)于SpringBoot + Mybatis Plus 整合 Redis的詳細(xì)步驟的文章就介紹到這了,更多相關(guān)SpringBoot Mybatis Plus 整合 Redis內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Netty進(jìn)階之EventExecutorGroup源碼詳解

    Netty進(jìn)階之EventExecutorGroup源碼詳解

    這篇文章主要介紹了Netty進(jìn)階之EventExecutorGroup源碼詳解,EventExecutorGroup繼承了JDK的ScheduledExecutroService,那么它就擁有了執(zhí)行定時(shí)任務(wù),執(zhí)行提交的普通任務(wù),需要的朋友可以參考下
    2023-11-11
  • 解決IDEA JSP沒有代碼提示問題的幾種方法

    解決IDEA JSP沒有代碼提示問題的幾種方法

    這篇文章主要介紹了解決IDEA JSP沒有代碼提示問題的幾種方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • 淺談java 重寫equals方法的種種坑

    淺談java 重寫equals方法的種種坑

    這篇文章主要介紹了淺談java 重寫equals方法的種種“坑”,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-01-01
  • 深入了解final在java中的應(yīng)用

    深入了解final在java中的應(yīng)用

    談到final關(guān)鍵字,想必很多人都不陌生,在使用匿名內(nèi)部類的時(shí)候可能會(huì)經(jīng)常用到final關(guān)鍵字。另外,Java中的String類就是一個(gè)final類,那么今天我們就來了解final這個(gè)關(guān)鍵字的用法。
    2019-06-06
  • Java對(duì)象和Json文本轉(zhuǎn)換工具類的實(shí)現(xiàn)

    Java對(duì)象和Json文本轉(zhuǎn)換工具類的實(shí)現(xiàn)

    Json?是一個(gè)用于Java對(duì)象和Json文本相互轉(zhuǎn)換的工具類,本文主要介紹了Java對(duì)象和Json文本轉(zhuǎn)換工具類,具有一定的參考價(jià)值,感興趣的可以了解一下
    2022-03-03
  • JAVA生成短8位UUID的實(shí)例講解

    JAVA生成短8位UUID的實(shí)例講解

    這篇文章主要介紹了JAVA生成短8位UUID的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • Java并發(fā)編程ReentrantReadWriteLock加讀鎖流程

    Java并發(fā)編程ReentrantReadWriteLock加讀鎖流程

    這篇文章主要介紹了Java并發(fā)編程ReentrantReadWriteLock加讀鎖流程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-05-05
  • Springboot獲取前端反饋信息并存入數(shù)據(jù)庫的實(shí)現(xiàn)代碼

    Springboot獲取前端反饋信息并存入數(shù)據(jù)庫的實(shí)現(xiàn)代碼

    這篇文章主要介紹了Springboot獲取前端反饋信息并存入數(shù)據(jù)庫的實(shí)現(xiàn)代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-03-03
  • 舉例講解Java中的Stream流概念

    舉例講解Java中的Stream流概念

    這篇文章主要介紹了舉例講解Java中的Stream流概念,是Java入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-09-09
  • 利用Spring Social輕松搞定微信授權(quán)登錄的方法示例

    利用Spring Social輕松搞定微信授權(quán)登錄的方法示例

    這篇文章主要介紹了利用Spring Social輕松搞定微信授權(quán)登錄的方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-12-12

最新評(píng)論