JavaWeb限流QPS简易框架
Java Web利用filter实现拦截请求,统计信息、并控制单台机器QPS。
/**
* 网络流量控制器
*/
public class TrafficFilter implements Filter { private ITrafficStatic trafficStatic;
private ITrafficHandler trafficHandler; @Override
public void init(FilterConfig filterConfig) throws ServletException {
trafficHandler = new AgentTrafficHandler();
trafficStatic = new TrafficStatic();
trafficStatic.init();
} @Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
// step1: parse bizFunId
String bizFunId = request.getParameter("funBizId");
if (StringUtils.isBlank(bizFunId)) {
chain.doFilter(request, response);
return;
}
bizFunId = StringUtils.trim(bizFunId);
// step2: check whether it should be cached.
if (!trafficStatic.shouldLimitTraffic(bizFunId)) {
chain.doFilter(request, response);
return;
}
// step3: static the visitor.
if (trafficStatic.isOutOfTrafficLimit(bizFunId)) {
trafficHandler.handle((HttpServletRequest) request, (HttpServletResponse) response);
return;
}
int visNum = trafficStatic.incAndGetTraffic(bizFunId);
if (trafficStatic.isOutOfTrafficLimit(bizFunId, visNum)) {
trafficHandler.handle((HttpServletRequest) request, (HttpServletResponse) response);
return;
} else {
chain.doFilter(request, response);
return;
}
} @Override
public void destroy() {
trafficStatic.destroy();
}
}
public interface ITrafficStatic { public void init(); public void destroy(); public boolean shouldLimitTraffic(String bizId); public int incAndGetTraffic(String bizId); public boolean isOutOfTrafficLimit(String bizId, int number); public boolean isOutOfTrafficLimit(String bizId); }
public class TrafficStatic implements ITrafficStatic { private static final AvatarLogger LOGGER = AvatarLoggerFactory.getLogger(TrafficStatic.class); private Map<Integer, Map<String, AtomicInteger>> staticMap = new ConcurrentHashMap<Integer, Map<String, AtomicInteger>>();
private volatile Map<String, Integer> limitMap = new HashMap<String, Integer>();
private volatile Map<String, Integer> tempMap; @Override
public void init() {
initCleanThread();
initSyncThread();
} @Override
public void destroy() {
} private void initCleanThread() {
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
sleep(60 * 1000); // sync pv every 1 min.
int version = (int) System.currentTimeMillis() / 1000 - 60; Iterator<Map.Entry<Integer, Map<String, AtomicInteger>>> iterator = staticMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<Integer, Map<String, AtomicInteger>> entry = iterator.next();
if (entry.getKey() <= version) {
iterator.remove();
}
}
} // while
}
}).start();
} private void initSyncThread() {
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
sleep(60 * 1000); // sync pv every 1 min.
if (MapUtils.isNotEmpty(tempMap)) {
tempMap.clear();
tempMap = null;
}
int version = getTimestampVersion();
tempMap = limitMap;
limitMap = readConfigFromLion();
for (int i = version; i < (version + 5*60); ++i) {
if (!staticMap.containsKey(i)) {
staticMap.put(i, new ConcurrentHashMap<String, AtomicInteger>());
}
checkAndNewMapEntries(limitMap, staticMap.get(i));
}
} // while
}
}).start();
} private static Map<String, Integer> readConfigFromLion() {
try {
Map<String, Integer> map = JsonUtils.fromJson(PropertiesLoaderSupportUtils.getProperty("tpfun-promo-web.networktraffic.traffic-config", "{}"), Map.class);
return MapUtils.isEmpty(map) ? MapUtils.EMPTY_MAP : map;
} catch (Exception e) {
LOGGER.error("[networktraffic] error with reading config from lion", e);
return MapUtils.EMPTY_MAP;
}
} private void checkAndNewMapEntries(Map<String, Integer> source, Map<String, AtomicInteger> target) {
for (Map.Entry<String, Integer> entry : source.entrySet()) {
if (!target.containsKey(entry.getKey())) {
target.put(entry.getKey(), new AtomicInteger(0));
}
}
} private void sleep(long mills) {
try {
Thread.sleep(mills);
} catch (InterruptedException e) {
LOGGER.error("[networktraffic] PvCounterBiz threads.sleep error", e);
}
} private int getTimestampVersion() {
return (int) (System.currentTimeMillis() / 1000);
} @Override
public boolean shouldLimitTraffic(String bizId) {
return limitMap.containsKey(bizId);
} @Override
public int incAndGetTraffic(String bizId) {
int ver = getTimestampVersion();
if (!staticMap.containsKey(ver)) {
return 1;
}
Map<String, AtomicInteger> map = staticMap.get(ver);
if (MapUtils.isEmpty(map) || !map.containsKey(bizId)) {
return 1;
}
return map.get(bizId).incrementAndGet();
} @Override
public boolean isOutOfTrafficLimit(String bizId, int number) {
int ver = getTimestampVersion();
if (!limitMap.containsKey(bizId)) {
return false;
}
return (number > limitMap.get(bizId));
} @Override
public boolean isOutOfTrafficLimit(String bizId) {
int ver = getTimestampVersion();
if (!staticMap.containsKey(ver)) {
return false;
}
Map<String, AtomicInteger> map = staticMap.get(ver);
if (MapUtils.isEmpty(map) || !map.containsKey(bizId)) {
return false;
}
return isOutOfTrafficLimit(bizId, map.get(bizId).intValue());
} }
public interface ITrafficHandler { void handle(HttpServletRequest request, HttpServletResponse response); }
public class AgentTrafficHandler implements ITrafficHandler { private AjaxTrafficHandler ajaxTrafficHandler = new AjaxTrafficHandler();
private MobileTrafficHandler mobileTrafficHandler = new MobileTrafficHandler(); @Override
public void handle(HttpServletRequest request, HttpServletResponse response) {
if (StringUtils.contains(request.getRequestURI(), "ajax")) {
ajaxTrafficHandler.handle(request, response);
} else {
mobileTrafficHandler.handle(request, response);
}
} }
public class AjaxTrafficHandler implements ITrafficHandler { private static final AvatarLogger LOGGER = AvatarLoggerFactory.getLogger(MobileTrafficHandler.class); private final static String RETURN_JSON = "{code:509, message:'out of max req limit'}"; @Override
public void handle(HttpServletRequest request, HttpServletResponse response) {
response.setHeader("Content-Type", "application/json;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
response.setStatus(200);
try {
response.getOutputStream().write(RETURN_JSON.getBytes("UTF-8"));
} catch (IOException e) {
LOGGER.error(e);
}
} }
public class MobileTrafficHandler implements ITrafficHandler { private static final AvatarLogger LOGGER = AvatarLoggerFactory.getLogger(MobileTrafficHandler.class); private final static String RETURN_HTML = "<html>" +
"<head>" +
"<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\" />" +
"\n<meta charset='utf-8'>" +
"<title>大众点评网</title>" +
"</head><body>" +
"<center>挤爆了、请稍等...</center>" +
"</body>" +
"<script type='text/javascript'>(function(){function reload(){ window.location.reload(); setTimeout(reload, 1500);} setTimeout(reload, 1500);})();" +
"</script>" +
"</html>"; @Override
public void handle(HttpServletRequest request, HttpServletResponse response) {
response.setHeader("Content-Type", "text/html;charset=utf-8");
response.setCharacterEncoding("utf-8");
response.setStatus(200);
try {
response.getOutputStream().write(RETURN_HTML.getBytes("UTF-8"));
} catch (IOException e) {
LOGGER.error(e);
}
} }
JavaWeb限流QPS简易框架的更多相关文章
- Spring Cloud Alibaba | Sentinel: 服务限流高级篇
目录 Spring Cloud Alibaba | Sentinel: 服务限流高级篇 1. 熔断降级 1.1 降级策略 2. 热点参数限流 2.1 项目依赖 2.2 热点参数规则 3. 系统自适应限 ...
- 流量治理神器-Sentinel的限流模式,选单机还是集群?
大家好,架构摆渡人.这是我的第5篇原创文章,还请多多支持. 上篇文章给大家推荐了一些限流的框架,如果说硬要我推荐一款,我会推荐Sentinel,Sentinel的限流模式分为两种,分别是单机模式和集群 ...
- spring cloud gateway 之限流篇
转载请标明出处: https://www.fangzhipeng.com 本文出自方志朋的博客 在高并发的系统中,往往需要在系统中做限流,一方面是为了防止大量的请求使服务器过载,导致服务不可用,另一方 ...
- SpringCloudGateWay之限流
一.引言在高并发系统中,经常需要限制系统中的电流化妆.一方面是防止大量的请求使服务器过载,导致服务不可用,另一方面是防止网络攻击.常用的限流方法,如hystrix.应用线程池隔离.超过线程池的负载和g ...
- 简易RPC框架-客户端限流配置
*:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...
- WebApiThrottle限流框架使用手册
阅读目录: 介绍 基于IP全局限流 基于IP的端点限流 基于IP和客户端key的端点限流 IP和客户端key的白名单 IP和客户端key自定义限制频率 端点自定义限制频率 关于被拒请求的计数器 在we ...
- 【Dnc.Api.Throttle】适用于.Net Core WebApi接口限流框架
Dnc.Api.Throttle 适用于Dot Net Core的WebApi接口限流框架 使用Dnc.Api.Throttle可以使您轻松实现WebApi接口的限流管理.Dnc.Api.Thr ...
- webapi限流框架WebApiThrottle
为了防止网站意外暴增的流量比如活动.秒杀.攻击等,导致整个系统瘫痪,在前后端接口服务处进行流量限制是非常有必要的.本篇主要介绍下Net限流框架WebApiThrottle的使用. WebApiThro ...
- 限流10万QPS、跨域、过滤器、令牌桶算法-网关Gateway内容都在这儿
一.微服务网关Spring Cloud Gateway 1.1 导引 文中内容包含:微服务网关限流10万QPS.跨域.过滤器.令牌桶算法. 在构建微服务系统中,必不可少的技术就是网关了,从早期的Zuu ...
随机推荐
- C#解析JSON数据
本篇文章主要介绍C#对Json数据的读取. 主要操作过程是: 发送Http请求获取Json数据 把获取的Json数据转换成C#中的类 下面我们以12306火车票余票的数据为例进行切入. 首先来看一下h ...
- 看看国外的javascript题目,你能全部做对吗?(分享)
本文转自@Aaron的博客,拿过来分享一下.原文:看看国外的javascript题目,你能全部做对吗? 题目一: (function(){ return typeof arguments; })(); ...
- php getenv 和 putenv 用法
getenv 的功能是取得一个指定的环境变量. getenv('REMOTE_ADDR') 和 $_SERVER['REMOTE_ADDR'] 功能一样 但是当Web服务器API是ASAPI (IIS ...
- [ArcGIS Engine]栅格数据处理 RasterDataset RasterLayer Raster RasterBandCollection
原文 http://www.cnblogs.com/zoe-j/archive/2012/03/22/2412403.html 1.由RasterLayer 得到RasterDataset IRast ...
- Qt编程之QtScript
需求是这样的: 需要给一个硬件接口测试些东西,用js来调用,js做成不同的独立模块分别测试不同的硬件接口,包括DMD内存,PCIE带宽等等.需要用一个exe来载入这些js文件分别调用测试模块.exe用 ...
- MFC radio button 绑定变量用法
我们在对话框中拖动一个radio button控件,然后点击类向导,结果却发现在Member Variables里看不到Radio控件的ID.这是为什么? 2.1 三个Radio Button,ID分 ...
- PCRE的安装及使用
摘自http://www.cnblogs.com/renhao/archive/2011/08/17/2143264.html PCRE的安装及使用 1.主页地址:http://www.pcre.or ...
- Python的字符串与数字
Python3.0通过“input”实现读取控制台的输入与用户实现交互.值得注意的是input接受的所有数据都是字符串,即使输入的是数字,依然会被当作字符串来处理.这就会出现一些问题,所以需要进行类型 ...
- Unity扩展 四种Menu的区别
[MenuItem("Tools\AddColor")] : 在Unity菜单中添加一种快捷,执行public static方式 [AddComponentMenu(" ...
- iOS 10 的一些变化
原文链接:http://www.jianshu.com/p/9756992a35ca