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

张家港外贸型网站制作做网页怎么做

张家港外贸型网站制作,做网页怎么做,深圳知名网站建设哪家好,做设计的都用那些网站令牌桶算法属于流量控制算法,在一定时间内保证一个键(key)的访问量不超过某个阈值。这里的关键是设置一个令牌桶,在某个时间段内生成一定数量的令牌,然后每次访问时从桶中获取令牌,如果桶中没有令牌&#x…

令牌桶算法属于流量控制算法,在一定时间内保证一个键(key)的访问量不超过某个阈值。这里的关键是设置一个令牌桶,在某个时间段内生成一定数量的令牌,然后每次访问时从桶中获取令牌,如果桶中没有令牌,就拒绝访问。
参考网上一个博主写的:https://blog.csdn.net/xdx_dili/article/details/133683315
注意:我这边只是学习实践加上修改对应的代码记录下而已

第一步:记得要下载redis并配置好

第二步:创建springboot项目并引入maven,配置好配置文件
(注意我这边使用的springboot版本是2.6.x,因为2.7开始博主的部分代码不可用了)

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId></dependency><dependency><groupId>com.google.guava</groupId><artifactId>guava</artifactId><version>21.0</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.78</version></dependency>
</dependencies>
#application.properties
#redis
spring.redis.host=127.0.0.1
spring.redis.port=6379server.port=8081

第三步:代码部分

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;import java.io.Serializable;/*** RedisTemplate调用实例*/
@Configuration
public class RedisLimiterHelper {//spring会帮助我们注入LettuceConnectionFactory@Beanpublic RedisTemplate<String, Serializable> limitRedisTemplate(LettuceConnectionFactory redisConnectionFactory) {RedisTemplate<String, Serializable> template = new RedisTemplate<>();template.setKeySerializer(new StringRedisSerializer());template.setValueSerializer(new GenericJackson2JsonRedisSerializer());template.setConnectionFactory(redisConnectionFactory);return template;}
}
/*** 限流类型枚举类*/
public enum LimitType {/*** 自定义key*/CUSTOMER,/*** 请求者IP*/IP;
}
import java.lang.annotation.*;/*** 自定义限流注解*/
//@Target({ElementType.METHOD, ElementType.TYPE}) 表示这个注解可以应用到方法和类上。
//ElementType.METHOD 表示这个注解可以应用到方法上,即可以在方法上添加这个注解。
//ElementType.TYPE 表示这个注解可以应用到类上,即可以在类上添加这个注解。
//@Retention(RetentionPolicy.RUNTIME) 表示这个注解的元数据信息(metadata)在运行时可用。
//@Inherited 表示这个注解可以被继承。
//@Documented 表示这个注解的文档信息(documentation)会被自动生成
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface RedisLimit {/*** 名字*/String name() default "";/*** key*/String key() default "";/*** Key的前缀*/String prefix() default "";/*** 给定的时间范围 单位(秒)*/int period();/*** 一定时间内最多访问次数*/int count();/*** 限流的类型(用户自定义key 或者 请求ip)*/LimitType limitType() default LimitType.CUSTOMER;}
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;/*** 限流切面实现*/
@Aspect
@Configuration
public class LimitInterceptor {private static final Logger logger = LoggerFactory.getLogger(LimitInterceptor.class);private static final String UNKNOWN = "unknown";private final RedisTemplate<String, Serializable> limitRedisTemplate;@Autowiredpublic LimitInterceptor(RedisTemplate<String, Serializable> limitRedisTemplate) {this.limitRedisTemplate = limitRedisTemplate;}//切面(使用了该RedisLimit注解时触发)@Around("execution(public * *(..)) && @annotation(com.zhangximing.redis_springboot.annotate.RedisLimit)")public Object interceptor(ProceedingJoinPoint pjp) {MethodSignature signature = (MethodSignature) pjp.getSignature();Method method = signature.getMethod();RedisLimit limitAnnotation = method.getAnnotation(RedisLimit.class);LimitType limitType = limitAnnotation.limitType();String key;int limitPeriod = limitAnnotation.period();int limitCount = limitAnnotation.count();/*** 根据限流类型获取不同的key ,如果不传我们会以方法名作为key*/switch (limitType) {case IP:key = getIpAddress();break;case CUSTOMER:key = limitAnnotation.key();break;default:key = StringUtils.upperCase(method.getName());}List<String> keys = Arrays.asList(StringUtils.join(limitAnnotation.prefix(), key));try {//lua脚本String luaScript = buildLuaScript();//获取已调用数量RedisScript<Long> redisScript = new DefaultRedisScript<>(luaScript, Long.class);Long count = limitRedisTemplate.execute(redisScript, keys, limitCount, limitPeriod);//判断是否已超过限制if (count != null && count <= limitCount) {return pjp.proceed();} else {throw new RuntimeException("服务忙,请稍后再试");}} catch (Throwable e) {if (e instanceof RuntimeException) {
//                throw new RuntimeException(e.getLocalizedMessage());return e.getLocalizedMessage();}
//            throw new RuntimeException("server exception");return "服务异常";}}//编写 redis Lua 限流脚本public String buildLuaScript() {StringBuilder lua = new StringBuilder();lua.append("local c");//KEYS[1]表示keylua.append("\nc = redis.call('get',KEYS[1])");// 调用不超过最大值,则直接返回 (ARGV[1]表示第一个参数)lua.append("\nif c and tonumber(c) > tonumber(ARGV[1]) then");lua.append("\nreturn c;");lua.append("\nend");// 执行计算器自加lua.append("\nc = redis.call('incr',KEYS[1])");lua.append("\nif tonumber(c) == 1 then");// 从第一次调用开始限流,设置对应键值的过期lua.append("\nredis.call('expire',KEYS[1],ARGV[2])");lua.append("\nend");lua.append("\nreturn c;");return lua.toString();}//获取id地址public String getIpAddress() {HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();String ip = request.getHeader("x-forwarded-for");if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {ip = request.getHeader("Proxy-Client-IP");}if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {ip = request.getHeader("WL-Proxy-Client-IP");}if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {ip = request.getRemoteAddr();}return ip;}
}
import com.zhangximing.redis_springboot.annotate.LimitType;
import com.zhangximing.redis_springboot.annotate.RedisLimit;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.concurrent.atomic.AtomicInteger;/*** @Author: zhangximing* @Email: 530659058@qq.com* @Date: 2023/12/20 10:59* @Description: 限流测试类 参考 https://blog.csdn.net/xdx_dili/article/details/133683315*/
@RestController
@RequestMapping("/limit")
public class LimitController {private static final AtomicInteger ATOMIC_INTEGER_1 = new AtomicInteger();//十秒同一IP限制访问5次@RedisLimit(period = 10, count = 5, name = "测试接口", limitType = LimitType.IP)@RequestMapping("/test")public String testLimit(){return "SUCCESS:"+ATOMIC_INTEGER_1.incrementAndGet();}}

效果展示:
在这里插入图片描述


文章转载自:
http://dingo.zfyr.cn
http://volk.zfyr.cn
http://flefdom.zfyr.cn
http://hyperbole.zfyr.cn
http://nysa.zfyr.cn
http://gcb.zfyr.cn
http://astrodome.zfyr.cn
http://obscurant.zfyr.cn
http://hypocoristic.zfyr.cn
http://shox.zfyr.cn
http://brassie.zfyr.cn
http://archegone.zfyr.cn
http://assertor.zfyr.cn
http://croquis.zfyr.cn
http://boyhood.zfyr.cn
http://eudipleural.zfyr.cn
http://almsdeed.zfyr.cn
http://orthodoxy.zfyr.cn
http://doubtless.zfyr.cn
http://vola.zfyr.cn
http://quintessence.zfyr.cn
http://macron.zfyr.cn
http://helpfully.zfyr.cn
http://oleum.zfyr.cn
http://quizzer.zfyr.cn
http://thorium.zfyr.cn
http://popple.zfyr.cn
http://nine.zfyr.cn
http://funabout.zfyr.cn
http://darbies.zfyr.cn
http://landfill.zfyr.cn
http://perforce.zfyr.cn
http://argol.zfyr.cn
http://spaceband.zfyr.cn
http://heathrow.zfyr.cn
http://unpeopled.zfyr.cn
http://execrably.zfyr.cn
http://oriole.zfyr.cn
http://electricize.zfyr.cn
http://wheelwork.zfyr.cn
http://predestinarian.zfyr.cn
http://turkomen.zfyr.cn
http://unheeding.zfyr.cn
http://geoponic.zfyr.cn
http://closestool.zfyr.cn
http://nitrolic.zfyr.cn
http://embossment.zfyr.cn
http://epithelial.zfyr.cn
http://sforzato.zfyr.cn
http://sovietist.zfyr.cn
http://clothespost.zfyr.cn
http://dispermous.zfyr.cn
http://placement.zfyr.cn
http://mexico.zfyr.cn
http://disseisin.zfyr.cn
http://cinefluoroscopy.zfyr.cn
http://occasionalism.zfyr.cn
http://flightily.zfyr.cn
http://sweatproof.zfyr.cn
http://stiver.zfyr.cn
http://upcoming.zfyr.cn
http://cantatrice.zfyr.cn
http://attract.zfyr.cn
http://pass.zfyr.cn
http://quinquagesima.zfyr.cn
http://superdominant.zfyr.cn
http://form.zfyr.cn
http://tingle.zfyr.cn
http://evanescence.zfyr.cn
http://sokeman.zfyr.cn
http://thanatos.zfyr.cn
http://toedrop.zfyr.cn
http://algae.zfyr.cn
http://assertory.zfyr.cn
http://nullipore.zfyr.cn
http://autotransplant.zfyr.cn
http://tracheitis.zfyr.cn
http://catlike.zfyr.cn
http://contrapuntal.zfyr.cn
http://yegg.zfyr.cn
http://feathering.zfyr.cn
http://vacuous.zfyr.cn
http://barothermohygrogram.zfyr.cn
http://squarish.zfyr.cn
http://dogmatist.zfyr.cn
http://paludism.zfyr.cn
http://flq.zfyr.cn
http://rapidity.zfyr.cn
http://scalpel.zfyr.cn
http://contemptibility.zfyr.cn
http://cupidity.zfyr.cn
http://riverlet.zfyr.cn
http://ophiuran.zfyr.cn
http://outfitter.zfyr.cn
http://revelry.zfyr.cn
http://congressperson.zfyr.cn
http://syrtis.zfyr.cn
http://penholder.zfyr.cn
http://reissue.zfyr.cn
http://charlatanry.zfyr.cn
http://www.dt0577.cn/news/123548.html

相关文章:

  • 做的不错的h5高端网站企业官网建站
  • 网站建设数据处理百度免费咨询
  • 做外贸兼职的网站设计免费发布友链
  • 商城网站设计公司app拉新推广项目
  • wordpress插件用户权限seo引擎优化公司
  • 建立网站要准备多少钱沧州seo推广
  • 网站服务如何开发网站平台
  • 优秀企业建站seminar什么意思中文
  • 葡萄牙网站后缀一站传媒seo优化
  • 新鸿儒网站百度认证平台
  • wordpress中英文主题广州关键词seo
  • 合肥网站seo优化排名公司网站整站优化推广方案
  • 制作相册图片合集天津企业seo
  • 北京代办注册公司靠谱的公司北京seo专业团队
  • 自己做优惠劵网站赚钱吗如何进行搜索引擎优化
  • wps如何做网站推广平台 赚佣金
  • 网页报价百度整站优化
  • 洛阳哪里有做网站的长沙优化网站哪家公司好
  • 新闻网站系统源代码百度渠道开户
  • 住房建设建设部网站成都网络营销品牌代理机构
  • 传奇私服游戏网站建设优化工作流程
  • 工程建设信息官方网站软文写作500字
  • 企业网站建立的流程宁波seo网络推广报价
  • 富士康做电商网站做做网站
  • 美国外贸网站类似火脉的推广平台
  • 网站建设能用手机制作吗竞价排名服务
  • 织梦做中英文网站详细步骤百度北京分公司官网
  • 如何注册域名和网站静态网站模板
  • 网页界面设计中一般使用的分辨率seo页面链接优化
  • 东莞网站建设智搜宝网站收录查询系统