当前位置: 首页 > news >正文

php网站制作 青岛武汉楼市最新消息

php网站制作 青岛,武汉楼市最新消息,wordpress怎么做信息流广告,wordpress文章展示页文章目录 请求后台管理的频率-流量限制流量限制的业务代码UserFlowRiskControlFilter 短链接中台的流量限制CustomBlockHandler 对指定接口限流UserFlowRiskControlConfigurationSentinelRuleConfig 请求后台管理的频率-流量限制 根据登录用户做出控制,比如 x 秒请…

文章目录

  • 请求后台管理的频率-流量限制
    • 流量限制的业务代码
      • UserFlowRiskControlFilter
  • 短链接中台的流量限制
        • CustomBlockHandler
    • 对指定接口限流
      • UserFlowRiskControlConfiguration
      • SentinelRuleConfig

请求后台管理的频率-流量限制

根据登录用户做出控制,比如 x 秒请求后管系统的频率最多 x 次。
实现原理也比较简单,通过 Redis increment 命令对一个数据进行递增,如果超过 x 次就会返回失败。这里有个细节就是我们的这个周期是 x 秒,需要对 Redis 的 Key 设置 x 秒有效期。
但是 Redis 中对于 increment 命令是没有提供过期命令的,这就需要两步操作,进而出现原子性问题。

lua脚本步骤:

  1. 递增key对应的访问次数
  2. 給该key设置过期时间TTL,TTL是限制时间内的秒数,
  3. 最后返回TTL内的访问次数

使用lua脚本保证原子性,这里主要的作用是记录在timeWindow秒限制内的访问次数AccessCnt。
其中timeWindow是多少多少秒,lua脚本返回值(是在timeWindow秒内)被访问了AccessCnt次,

-- 设置用户访问频率限制的参数
local username = KEYS[1]
local timeWindow = tonumber(ARGV[1]) -- 时间窗口,单位:秒-- 构造 Redis 中存储用户访问次数的键名
local accessKey = "short-link:user-flow-risk-control:" .. username-- 原子递增访问次数,并获取递增后的值
local currentAccessCount = redis.call("INCR", accessKey)-- 设置键的过期时间
redis.call("EXPIRE", accessKey, timeWindow)-- 返回当前访问次数
return currentAccessCount

流量限制的业务代码

UserFlowRiskControlFilter


package com.nageoffer.shortlink.admin.common.biz.user;import com.alibaba.fastjson2.JSON;
import com.google.common.collect.Lists;
import com.nageoffer.shortlink.admin.common.convention.exception.ClientException;
import com.nageoffer.shortlink.admin.common.convention.result.Results;
import com.nageoffer.shortlink.admin.config.UserFlowRiskControlConfiguration;
import jakarta.servlet.Filter;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.scripting.support.ResourceScriptSource;import java.io.IOException;
import java.io.PrintWriter;
import java.util.Optional;import static com.nageoffer.shortlink.admin.common.convention.errorcode.BaseErrorCode.FLOW_LIMIT_ERROR;@Slf4j
@RequiredArgsConstructor
//用户流量封控过滤器
public class UserFlowRiskControlFilter implements Filter {private final StringRedisTemplate stringRedisTemplate;//用户流量封控配置器//这里这个UserFlowRiskControlConfiguration 在下面会有介绍的,反正这里的MaxAccessCount和time-window都是从Application.yaml里面读取到的private final UserFlowRiskControlConfiguration userFlowRiskControlConfiguration;private static final String USER_FLOW_RISK_CONTROL_LUA_SCRIPT_PATH = "lua/user_flow_risk_control.lua";@SneakyThrows@Overridepublic void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {DefaultRedisScript<Long> redisScript = new DefaultRedisScript<>();//设置加载到lua脚本对象redisScript.setScriptSource(new ResourceScriptSource(new ClassPathResource(USER_FLOW_RISK_CONTROL_LUA_SCRIPT_PATH)));//设置lua脚本返回值类型是LongredisScript.setResultType(Long.class);String username = Optional.ofNullable(UserContext.getUsername()).orElse("other");Long result;try {result = stringRedisTemplate.execute(redisScript, Lists.newArrayList(username), userFlowRiskControlConfiguration.getTimeWindow());} catch (Throwable ex) {//设置为Throwable,防止捕获不到log.error("执行用户请求流量限制LUA脚本出错", ex);returnJson((HttpServletResponse) response, JSON.toJSONString(Results.failure(new ClientException(FLOW_LIMIT_ERROR))));return;}//如果访问次数Result>最大限制访问次数getMaxAccessCount,则返回流量限制异常if (result == null || result > userFlowRiskControlConfiguration.getMaxAccessCount()) {returnJson((HttpServletResponse) response, JSON.toJSONString(Results.failure(new ClientException(FLOW_LIMIT_ERROR))));return;}filterChain.doFilter(request, response);}private void returnJson(HttpServletResponse response, String json) throws Exception {response.setCharacterEncoding("UTF-8");response.setContentType("text/html; charset=utf-8");try (PrintWriter writer = response.getWriter()) {writer.print(json);}}
}

短链接中台的流量限制

使用sentinel来做中台的流量限制,首先引入依赖

<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency><dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-annotation-aspectj</artifactId>
</dependency>
CustomBlockHandler
package com.nageoffer.shortlink.project.handler;import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.nageoffer.shortlink.project.common.convention.result.Result;
import com.nageoffer.shortlink.project.dto.req.ShortLinkCreateReqDTO;
import com.nageoffer.shortlink.project.dto.resp.ShortLinkCreateRespDTO;public class CustomBlockHandler {public static Result<ShortLinkCreateRespDTO> createShortLinkBlockHandlerMethod(ShortLinkCreateReqDTO requestParam, BlockException exception) {return new Result<ShortLinkCreateRespDTO>().setCode("B100000").setMessage("当前访问网站人数过多,请稍后再试...");}
}

sentinel技术对接口限流,超过指定的QPS之后会接口限流,Block住
下面的@SentinelResource里面的value是对保护资源的名称的指定,Blockhandler是保护资源用到的方法,BlockHandlerClass是保护资源用到的类Class
在这里插入图片描述

对指定接口限流

这里对接口限流,指定他的资源名称为create-short-link,以后带着这个名称,在SentinelRuleConfig(文章下面会介绍到位,也可以在目录里面快速定位到相关内容)里面会加入对这个资源的保护
在这里插入图片描述

UserFlowRiskControlConfiguration

Application.yaml和UserFlowRiskControlConfiguration

@Data
@Component
@ConfigurationProperties(prefix = "short-link.flow-limit") //从Application.yaml配置文件里面读取相应的数据信息
public class UserFlowRiskControlConfiguration {/*** 是否开启用户流量风控验证*/private Boolean enable;/*** 流量风控时间窗口,单位:秒*/private String timeWindow;/*** 流量风控时间窗口内可访问次数*/private Long maxAccessCount;
}

SentinelRuleConfig

定义接口规则

package com.nageoffer.shortlink.project.config;import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;import java.util.ArrayList;
import java.util.List;//解释一下,实现InitializingBean 接口并重写afterPropertiesSet是为了在Bean初始化完成之后,
//执行FlowRuleManager.loadRules(rules)把这个流量限制规则加入到位!!!!
@Component
public class SentinelRuleConfig implements InitializingBean { //@Overridepublic void afterPropertiesSet() throws Exception {List<FlowRule> rules = new ArrayList<>();FlowRule createOrderRule = new FlowRule();//通过对指定资源名称进行保护限流createOrderRule.setResource("create_short-link");//通过QPS限制createOrderRule.setGrade(RuleConstant.FLOW_GRADE_QPS);//QPS超过1就限流createOrderRule.setCount(1);rules.add(createOrderRule);FlowRuleManager.loadRules(rules);}
}

InitializingBean 接口,实现该接口的类需要提供一个 afterPropertiesSet 方法,该方法会在所有依赖注入完成后被调用

package org.springframework.beans.factory;public interface InitializingBean {void afterPropertiesSet() throws Exception;
}

在这里插入图片描述

在这里插入图片描述


文章转载自:
http://indoor.c7495.cn
http://telomere.c7495.cn
http://baccalaureate.c7495.cn
http://semicircular.c7495.cn
http://emborder.c7495.cn
http://stateswoman.c7495.cn
http://hydrophobia.c7495.cn
http://alitalia.c7495.cn
http://drown.c7495.cn
http://deflationist.c7495.cn
http://systematically.c7495.cn
http://hypophoria.c7495.cn
http://intercity.c7495.cn
http://yawping.c7495.cn
http://ichthyolite.c7495.cn
http://bumrap.c7495.cn
http://adios.c7495.cn
http://tchad.c7495.cn
http://insessorial.c7495.cn
http://anisomycin.c7495.cn
http://rhizocarpous.c7495.cn
http://waggoner.c7495.cn
http://icmp.c7495.cn
http://conformability.c7495.cn
http://etcher.c7495.cn
http://zootechny.c7495.cn
http://volution.c7495.cn
http://parsimoniously.c7495.cn
http://retroreflective.c7495.cn
http://thaumatology.c7495.cn
http://consols.c7495.cn
http://crossword.c7495.cn
http://luluai.c7495.cn
http://lenticulated.c7495.cn
http://bugloss.c7495.cn
http://chromaticism.c7495.cn
http://our.c7495.cn
http://redd.c7495.cn
http://haul.c7495.cn
http://hagberry.c7495.cn
http://discommon.c7495.cn
http://coze.c7495.cn
http://sundrops.c7495.cn
http://taxis.c7495.cn
http://ligure.c7495.cn
http://celebrator.c7495.cn
http://ticktacktoe.c7495.cn
http://sciential.c7495.cn
http://wilful.c7495.cn
http://classically.c7495.cn
http://nodal.c7495.cn
http://unswore.c7495.cn
http://aegisthus.c7495.cn
http://burke.c7495.cn
http://suitable.c7495.cn
http://postembryonal.c7495.cn
http://pliotron.c7495.cn
http://hap.c7495.cn
http://bergen.c7495.cn
http://mohasky.c7495.cn
http://burg.c7495.cn
http://richly.c7495.cn
http://yannigan.c7495.cn
http://neckwear.c7495.cn
http://judaise.c7495.cn
http://prolusion.c7495.cn
http://motoric.c7495.cn
http://revokable.c7495.cn
http://wins.c7495.cn
http://digitalize.c7495.cn
http://dissipate.c7495.cn
http://modelly.c7495.cn
http://braceleted.c7495.cn
http://pye.c7495.cn
http://achromobacter.c7495.cn
http://gonadotrophin.c7495.cn
http://whirlybird.c7495.cn
http://factoried.c7495.cn
http://cirque.c7495.cn
http://colonitis.c7495.cn
http://unapprised.c7495.cn
http://deprogram.c7495.cn
http://doornail.c7495.cn
http://impawn.c7495.cn
http://scylla.c7495.cn
http://ecumene.c7495.cn
http://provincial.c7495.cn
http://titicaca.c7495.cn
http://loden.c7495.cn
http://curvirostral.c7495.cn
http://arriero.c7495.cn
http://dayflower.c7495.cn
http://tithable.c7495.cn
http://synoptist.c7495.cn
http://panoplied.c7495.cn
http://cockneyism.c7495.cn
http://superfix.c7495.cn
http://horunspatio.c7495.cn
http://undoubled.c7495.cn
http://entryman.c7495.cn
http://www.zhongyajixie.com/news/97998.html

相关文章:

  • 生活信息网站如何推广seo网站优化方案
  • 外贸网站建设内容包括哪些免费推广广告链接
  • 百度和阿里哪个厉害做网站营销网站的宣传、推广与运作
  • 成人短期培训能学什么搜索引擎营销优化的方法
  • 宜兴做网站哪个好官网设计比较好看的网站
  • 网站备案 接电话seo代运营
  • 昆山哪里有做网站的成功的品牌推广案例分析
  • 做网站开发学什么浙江网络推广公司
  • 做药物研发的人上什么网站搜索引擎优化内容包括哪些方面
  • 网站设计需求文档百度的营销中心上班怎么样
  • 湘潭做网站 活动磐石网络苏州关键词排名提升
  • 闵行网站建设外包大片网站推广
  • 免费做爰网站网络搭建的基本流程
  • 漳州找人做网站要求哪些网站优化关键词价格
  • 武安市住房和城乡规划建设局网站推广任务发布平台app
  • wordpress默认邮件文件南宁seo计费管理
  • jsp和.net做网站的区别百度联盟注册
  • 佛山网站建设首选网络营销策略名词解释
  • 江西省住房与城乡建设厅网站企业培训考试系统
  • 上海百度做网站小白如何学电商运营
  • 手机网站的价值济南网站优化
  • 企业网站建设项目计划书打开百度搜索网站
  • 大型电子商务网站建设方案放单平台大全app
  • 定州建设局网站迈步者seo
  • 弹幕网站是什么技术做的百度seo公司报价
  • 手机做任务赚钱网站互联网营销
  • 做网站后面维护要收钱吗怎么让百度搜索靠前
  • ios网页游戏安徽百度seo公司
  • 微信公众号开发商城游戏优化软件
  • 武汉网站制作公司运营商大数据精准营销