<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                對外提供api 接口調用服務, 為防止某個用戶請求過多導致服務器宕機,限制具體用戶請求接口的QPS 自定義注解 ``` import java.lang.annotation.*; /** * 登錄用戶訪問接口qps限制, 未登錄用戶這里不做限制 */ @Target({ ElementType.PARAMETER, ElementType.METHOD }) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface UserRestrictAccess { /** 限制時間,單位是秒 */ long ttl() default 1; /** 限制時間內用戶的訪問次數 */ int userQps() default 1; } ``` 注解實現 ``` @Aspect @Component @Slf4j public class UserRestrictAccessAspect { @Autowired RedisService redisService; @Before("@annotation(userRestrictAccess)") public void doBefore(JoinPoint point, UserRestrictAccess userRestrictAccess) throws Throwable { try { doHandler(point, userRestrictAccess); } catch (BusinessException be) { throw be; } catch (Exception e) { log.error("UserRestrictAccessAspect {} ", e.getMessage()); } } private void doHandler(JoinPoint point, UserRestrictAccess userRestrictAccess) { //獲取當前線程的訪問對象 ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = requestAttributes.getRequest(); //獲取訪問路徑 String requestURI = request.getRequestURI(); //獲取用戶token Long userId = SecurityUtils.getUserId(); // 登錄的才需要做限制 if (userId == null) { return; } //存在redis中的key String key = BaseConstant.USER_RESTRICT_ACCESS_REDIS + ":pre_" + requestURI + ":" + userId; // 獲取方法上的注解 int userQps = userRestrictAccess.userQps(); long ttl = userRestrictAccess.ttl(); //從redis中獲取用戶再限定時間內訪問接口的次數 Integer value = (Integer) redisService.get(key); if (Objects.nonNull(value) && value >= userQps) { throw new BusinessException("您的操作過于頻繁,請待會再試"); } if (Objects.isNull(value)) { //不存在key則設置初始值并且設置過期時間 redisService.setCacheObject(key, 1, ttl, TimeUnit.SECONDS); } else { // 存在則將訪問次數 +1 redisService.redisTemplate.opsForValue().increment(key); } } } ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看