<Executor name="tomcatThreadPool" namePrefix="tomcatThreadPool-" maxThreads="1000" maxIdleTime="300000" minSpareThreads="200"/>
<Connector executor="tomcatThreadPool" port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" minProcessors="5" maxProcessors="75" acceptCount="1000"/>
/** * 自定义注解 限流 */ @Target({ElementType.PARAMETER, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface ServiceLimit { String description() default ""; }
/** * 限流 AOP */ @Component @Scope @Aspect public class LimitAspect { //每秒只发出100个令牌,此处是单进程服务的限流,内部采用令牌捅算法实现 private static RateLimiter rateLimiter = RateLimiter.create(100.0); //Service层切点 限流 @Pointcut("@annotation(com.itstyle.seckill.common.aop.ServiceLimit)") public void ServiceAspect() { } @Around("ServiceAspect()") public Object around(ProceedingJoinPoint joinPoint) { Boolean flag = rateLimiter.tryAcquire(); Object obj = null; try { if(flag){ obj = joinPoint.proceed(); } } catch (Throwable e) { e.printStackTrace(); } return obj; } }
@Override @ServiceLimit @Transactional public Result startSeckil(long seckillId, long userId) { //省略部分业务代码,详见秒杀源码 }
#统一在http域中进行配置 #限制请求 limit_req_zone $binary_remote_addr $uri zone=api_read:20m rate=50r/s; #按ip配置一个链接 zone limit_conn_zone $binary_remote_addr zone=perip_conn:10m; #按server配置一个链接 zone limit_conn_zone $server_name zone=perserver_conn:100m; server { listen 80; server_name seckill.52itstyle.com; index index.jsp; location / { #请求限流排队经过 burst默认是0 limit_req zone=api_read burst=5; #链接数限制,每一个IP并发请求为2 limit_conn perip_conn 2; #服务所限制的链接数(即限制了该server并发链接数量) limit_conn perserver_conn 1000; #链接限速 limit_rate 100k; proxy_pass http://seckill; } } upstream seckill { fair; server 172.16.1.120:8080 weight=1 max_fails=2 fail_timeout=30s; server 172.16.1.130:8080 weight=1 max_fails=2 fail_timeout=30s; }
# 安装 yum -y install httpd-tools # 查看ab版本 ab -v # 查看帮助 ab --help 测试命令: ab -n 1000 -c 100 http://127.0.0.1/ 测试结果: Server Software: openresty/1.13.6.1 #服务器软件 Server Hostname: 127.0.0.1 #IP Server Port: 80 #请求端口号 Document Path: / #文件路径 Document Length: 12 bytes #页面字节数 Concurrency Level: 100 #请求的并发数 Time taken for tests: 4.999 seconds #总访问时间 Complete requests: 1000 #总请求树 Failed requests: 0 #请求失败数量 Write errors: 0 Total transferred: 140000 bytes #请求总数据大小 HTML transferred: 12000 bytes #html页面实际总字节数 Requests per second: 200.06 [#/sec] (mean) #每秒多少请求,这个是很是重要的参数数值,服务器的吞吐量 Time per request: 499.857 [ms] (mean) #用户平均请求等待时间 Time per request: 4.999 [ms] (mean, across all concurrent requests) # 服务器平均处理时间,也就是服务器吞吐量的倒数 Transfer rate: 27.35 [Kbytes/sec] received #每秒获取的数据长度 Connection Times (ms) min mean[+/-sd] median max Connect: 0 0 0.8 0 4 Processing: 5 474 89.1 500 501 Waiting: 2 474 89.2 500 501 Total: 9 475 88.4 500 501 Percentage of the requests served within a certain time (ms) 50% 500 66% 500 75% 500 80% 500 90% 501 95% 501 98% 501 99% 501 100% 501 (longest request)