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

t么做文献索引ot网站外贸建站主机

t么做文献索引ot网站,外贸建站主机,徐州58同城网,知舟网站建设1、Redis实现限流方案的核心原理#xff1a; redis实现限流的核心原理在于redis 的key 过期时间#xff0c;当我们设置一个key到redis中时#xff0c;会将key设置上过期时间#xff0c;这里的实现是采用lua脚本来实现原子性的。2、准备 引入相关依赖 dependency…1、Redis实现限流方案的核心原理 redis实现限流的核心原理在于redis 的key 过期时间当我们设置一个key到redis中时会将key设置上过期时间这里的实现是采用lua脚本来实现原子性的。2、准备 引入相关依赖 dependency groupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId /dependency dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-aop/artifactId /dependency dependencygroupIdcn.hutool/groupIdartifactIdhutool-all/artifactIdversion5.8.23/version /dependency dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-redis/artifactIdversion3.1.5/version /dependency dependencygroupIdorg.yaml/groupIdartifactIdsnakeyaml/artifactIdversion2.2/version /dependency dependencygroupIdorg.apache.commons/groupIdartifactIdcommons-pool2/artifactId /dependency dependencygroupIdcom.google.protobuf/groupIdartifactIdprotobuf-java/artifactIdversion3.25.1/version /dependency添加redis配置信息 server:port: 6650nosql:redis:host: XXX.XXX.XXX.XXXport: 6379password:database: 0spring:cache:type: redisredis:host: ${nosql.redis.host}port: ${nosql.redis.port}password: ${nosql.redis.password}lettuce:pool:enabled: truemax-active: 8max-idle: 8min-idle: 0max-wait: 1000配置redis Conf Configuration public class RedisConfig {/*** 序列化* jackson2JsonRedisSerializer** param redisConnectionFactory 复述,连接工厂* return {link RedisTemplate}{link Object}, {link Object}*/Beanpublic RedisTemplateObject, Object redisTemplate(RedisConnectionFactory redisConnectionFactory) {RedisTemplateObject, Object template new RedisTemplate();template.setConnectionFactory(redisConnectionFactory);Jackson2JsonRedisSerializer jackson2JsonRedisSerializer new Jackson2JsonRedisSerializer(Object.class);ObjectMapper om new ObjectMapper();om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);jackson2JsonRedisSerializer.setObjectMapper(om);template.setKeySerializer(jackson2JsonRedisSerializer);template.setHashKeySerializer(jackson2JsonRedisSerializer);template.setValueSerializer(jackson2JsonRedisSerializer);template.setHashValueSerializer(jackson2JsonRedisSerializer);template.afterPropertiesSet();return template;}/*** 加载lua脚本* return {link DefaultRedisScript}{link Long}*/Beanpublic DefaultRedisScriptLong limitScript() {DefaultRedisScriptLong redisScript new DefaultRedisScript();redisScript.setScriptSource(new ResourceScriptSource(new ClassPathResource(luaFile/rateLimit.lua)));redisScript.setResultType(Long.class);return redisScript;}}3、限流实现 编写核心lua脚本 local key KEYS[1] -- 取出key对应的统计判断统计是否比限制大如果比限制大直接返回当前值 local count tonumber(ARGV[1]) local time tonumber(ARGV[2]) local current redis.call(get, key) if current and tonumber(current) count thenreturn tonumber(current) end --如果不比限制大进行重新设置时间 current redis.call(incr, key) if tonumber(current) 1 thenredis.call(expire, key, time) end return tonumber(current)编写注解 limiter Target(ElementType.METHOD) Retention(RetentionPolicy.RUNTIME) Documented public interface RateLimiter {/*** 限流key*/String key() default rate_limit:;/*** 限流时间,单位秒*/int time() default 60;/*** 限流次数*/int count() default 100;/*** 限流类型*/LimitType limitType() default LimitType.DEFAULT; }增加注解类型 public enum LimitType {/*** 默认策略全局限流*/DEFAULT,/*** 根据请求者IP进行限流*/IP }添加IPUtils Slf4j public class IpUtils {/**ip的长度值*/private static final int IP_LEN 15;/** 使用代理时多IP分隔符*/private static final String SPLIT_STR ,;/*** 获取IP地址* p* 使用Nginx等反向代理软件 则不能通过request.getRemoteAddr()获取IP地址* 如果使用了多级反向代理的话X-Forwarded-For的值并不止一个而是一串IP地址X-Forwarded-For中第一个非unknown的有效IP字符串则为真实IP地址*/public static String getIpAddr(HttpServletRequest request) {String ip null;try {ip request.getHeader(x-forwarded-for);if (StrUtil.isBlank(ip)) {ip request.getHeader(Proxy-Client-IP);}if (StrUtil.isBlank(ip)) {ip request.getHeader(WL-Proxy-Client-IP);}if (StrUtil.isBlank(ip)) {ip request.getHeader(HTTP_CLIENT_IP);}if (StrUtil.isBlank(ip)) {ip request.getHeader(HTTP_X_FORWARDED_FOR);}if (StrUtil.isBlank(ip)) {ip request.getRemoteAddr();}} catch (Exception e) {log.error(IPUtils ERROR , e);}//使用代理则获取第一个IP地址if (!StrUtil.isBlank(ip) ip.length() IP_LEN) {if (ip.indexOf(SPLIT_STR) 0) {ip ip.substring(0, ip.indexOf(SPLIT_STR));}}return ip;} }核心处理类 Aspect Component Slf4j public class RateLimiterAspect {Resourceprivate RedisTemplateObject, Object redisTemplate;Resourceprivate RedisScriptLong limitScript;Before(annotation(rateLimiter))public void doBefore(JoinPoint point, RateLimiter rateLimiter) throws Throwable {String key rateLimiter.key();int time rateLimiter.time();int count rateLimiter.count();String combineKey getCombineKey(rateLimiter, point);ListObject keys Collections.singletonList(combineKey);try {Long number redisTemplate.execute(limitScript, keys, count, time);if (number null || number.intValue() count) {throw new ServiceException(访问过于频繁请稍候再试);}log.info(限制请求{},当前请求{},缓存key{}, count, number.intValue(), key);} catch (ServiceException e) {throw e;} catch (Exception e) {throw new RuntimeException(服务器限流异常请稍候再试);}}public String getCombineKey(RateLimiter rateLimiter, JoinPoint point) {StringBuilder stringBuilder new StringBuilder(rateLimiter.key());if (rateLimiter.limitType() LimitType.IP) {stringBuilder.append(IpUtils.getIpAddr(((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest())).append(-);}MethodSignature signature (MethodSignature) point.getSignature();Method method signature.getMethod();Class? targetClass method.getDeclaringClass();stringBuilder.append(targetClass.getName()).append(-).append(method.getName());return stringBuilder.toString();} }到此我们就可以利用注解对请求方法进行限流了
http://www.dnsts.com.cn/news/236657.html

相关文章:

  • 网站群建设方案6中国芗城区城乡建设局网站
  • 做美团网站需要多少钱个人做公司网页怎么做
  • 重庆seo网站建设优化阿里云怎么搭载wordpress
  • 怎么建立网站快捷方式怎么建立企业网站平台
  • 可以做锚文本链接的网站网站推广与宣传怎么做
  • 吉林网站优化合肥建站软件
  • 郑州网站建设行情网站左侧边栏导航代码
  • 站长工具seo词语排名制作网站的固定成本
  • 网站开发 技术路线官方网站开发模板
  • 开发一个网站需要多少钱注册logo商标设计要求
  • 广州微信网站建设如何海淀网站建设价格
  • 宠物医院网站开发wordpress上传类型
  • 做软件常用的网站京东云wordpress
  • 网站的自动登录是怎么做的小程序下载
  • 网站建设ftp软件网页一键生成app软件
  • 做网站域名需要在哪里备案php网站开发实例教程代码
  • 企业网站优化排名方案企业网站推广外包
  • 金融投资网站 php源码前几年做那些网站能致富
  • 小程序拉新推广平台秦皇岛做网站优化
  • 写作墨问题 网站诚信网站备案中心
  • 石岩网站建设公司网站开发怎么入账
  • 行业网站搭建什么大型网站用python做的
  • 超炫html5网站模板上海网站建设小程序开发
  • 2020站群seo系统同步wordpress站点
  • 腾讯云怎么做网站WordPress完整安裝包
  • 中国交通建设官方网站dw网页设计代码茶文化
  • 昆明网站建设公司排名猫咪科技北京旅游外贸网站建设
  • 极速建站旅游攻略网站开发
  • 网站域名绑定ip个人网页模板html免费
  • 做自己照片视频网站三乡网站开发