前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >网络防抖动在Springboot中有哪些应用?

网络防抖动在Springboot中有哪些应用?

原创
作者头像
小马哥学JAVA
发布2024-06-25 11:18:18
2470
发布2024-06-25 11:18:18

在 Spring Boot 中,网络防抖动(Debounce)技术可以应用于多种场景,以避免短时间内重复处理相同的请求,提高系统性能和用户体验。以下是一些具体的应用场景和实现方式:

一、表单提交防抖动

1.1 场景描述

在表单提交时,用户可能会不小心多次点击提交按钮,导致重复提交。防抖动技术可以避免这种情况。

1.2 实现方式

可以结合前端和后端的防抖动技术来解决这个问题。

  • 前端防抖动:使用 JavaScript 或前端框架的防抖动方法。
  • 后端防抖动:在 Spring Boot 控制器中实现防抖动逻辑。
代码语言:javascript
复制
java复制代码import org.springframework.stereotype.Service;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Service
public class FormSubmissionService {

    private final Map<String, Long> requestTimestamps = new ConcurrentHashMap<>();
    private final long debounceInterval = 5000; // 5 秒防抖动间隔

    public boolean isAllowed(String key) {
        long currentTime = System.currentTimeMillis();
        long lastRequestTime = requestTimestamps.getOrDefault(key, 0L);

        if (currentTime - lastRequestTime > debounceInterval) {
            requestTimestamps.put(key, currentTime);
            return true;
        } else {
            return false;
        }
    }
}

@RestController
public class FormController {

    @Autowired
    private FormSubmissionService formSubmissionService;

    @PostMapping("/submitForm")
    public ResponseEntity<String> submitForm(@RequestBody FormData formData) {
        String key = formData.getUserId(); // 使用用户 ID 作为防抖动键
        if (formSubmissionService.isAllowed(key)) {
            // 处理表单提交
            return ResponseEntity.ok("Form submitted successfully");
        } else {
            return ResponseEntity.status(HttpStatus.TOO_MANY_REQUESTS).body("Too many requests, please try again later.");
        }
    }
}

二、API 调用防抖动

2.1 场景描述

当前端频繁调用某个 API 时,服务器可能会受到压力。通过防抖动,可以限制短时间内的频繁调用,保护服务器资源。

2.2 实现方式

可以使用限流工具如 Bucket4j 来实现 API 调用防抖动。

代码语言:javascript
复制
java复制代码import io.github.bucket4j.Bandwidth;
import io.github.bucket4j.Bucket;
import io.github.bucket4j.Bucket4j;
import io.github.bucket4j.Refill;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.time.Duration;

@RestController
public class ApiController {

    private final Bucket bucket;

    @Autowired
    public ApiController() {
        Bandwidth limit = Bandwidth.classic(10, Refill.greedy(10, Duration.ofMinutes(1)));
        this.bucket = Bucket4j.builder().addLimit(limit).build();
    }

    @GetMapping("/api/request")
    public ResponseEntity<String> handleRequest(@RequestParam String param) {
        if (bucket.tryConsume(1)) {
            // 处理 API 请求
            return ResponseEntity.ok("Request processed: " + param);
        } else {
            return ResponseEntity.status(HttpStatus.TOO_MANY_REQUESTS).body("Too many requests, please try again later.");
        }
    }
}

三、用户登录防抖动

3.1 场景描述

在用户登录操作中,如果用户多次尝试登录(例如碰到恶意攻击),会对系统产生较大压力。防抖动可以限制短时间内的多次登录尝试。

3.2 实现方式

可以通过缓存来限制短时间内多次登录尝试。

代码语言:javascript
复制
java复制代码import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@Configuration
public class CacheConfig {

    @Bean
    public CacheManager cacheManager() {
        return new ConcurrentMapCacheManager("loginAttempts");
    }
}

@Service
public class LoginService {

    @Autowired
    private CacheManager cacheManager;

    public boolean isAllowed(String username) {
        Cache cache = cacheManager.getCache("loginAttempts");
        Integer attempts = cache.get(username, Integer.class);
        if (attempts == null) {
            cache.put(username, 1);
            return true;
        } else if (attempts >= 3) {
            return false;
        } else {
            cache.put(username, attempts + 1);
            return true;
        }
    }
}

@RestController
public class LoginController {

    @Autowired
    private LoginService loginService;

    @PostMapping("/login")
    public ResponseEntity<String> login(@RequestBody LoginRequest loginRequest) {
        String username = loginRequest.getUsername();
        if (loginService.isAllowed(username)) {
            // 处理登录
            return ResponseEntity.ok("Login successful");
        } else {
            return ResponseEntity.status(HttpStatus.TOO_MANY_REQUESTS).body("Too many login attempts, please try again later.");
        }
    }
}

四、搜索请求防抖动

4.1 场景描述

在搜索功能中,用户可能会在短时间内频繁发起搜索请求,导致服务器压力增大。防抖动可以限制短时间内的多次搜索请求。

4.2 实现方式

可以通过自定义防抖动逻辑来限制搜索请求。

代码语言:javascript
复制
java复制代码import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Service
public class SearchService {

    private final Map<String, Long> requestTimestamps = new ConcurrentHashMap<>();
    private final long debounceInterval = 3000; // 3 秒防抖动间隔

    public boolean isAllowed(String key) {
        long currentTime = System.currentTimeMillis();
        long lastRequestTime = requestTimestamps.getOrDefault(key, 0L);

        if (currentTime - lastRequestTime > debounceInterval) {
            requestTimestamps.put(key, currentTime);
            return true;
        } else {
            return false;
        }
    }

    public String performSearch(String query) {
        // 执行搜索逻辑
        return "Search results for: " + query;
    }
}

@RestController
public class SearchController {

    @Autowired
    private SearchService searchService;

    @GetMapping("/search")
    public ResponseEntity<String> search(@RequestParam String query) {
        String key = query; // 使用搜索查询作为防抖动键
        if (searchService.isAllowed(key)) {
            String results = searchService.performSearch(query);
            return ResponseEntity.ok(results);
        } else {
            return ResponseEntity.status(HttpStatus.TOO_MANY_REQUESTS).body("Too many search requests, please try again later.");
        }
    }
}

总结

防抖动技术在 Spring Boot 中有广泛的应用,可以有效防止短时间内的重复请求,提高系统性能和用户体验。常见的应用场景包括表单提交、防止频繁 API 调用、登录防抖动和搜索请求防抖动等。在实际项目中,可以根据具体需求选择合适的防抖动技术和实现方式,以达到最佳效果。

我正在参与2024腾讯技术创作特训营最新征文,快来和我瓜分大奖!

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、表单提交防抖动
    • 1.1 场景描述
      • 1.2 实现方式
      • 二、API 调用防抖动
        • 2.1 场景描述
          • 2.2 实现方式
          • 三、用户登录防抖动
            • 3.1 场景描述
              • 3.2 实现方式
              • 四、搜索请求防抖动
                • 4.1 场景描述
                  • 4.2 实现方式
                  • 总结
                  领券
                  问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档