从事互联网应用平台的开发,安全一直是个工作量很大的领域,而流控则属于安全的一部分,为此tio-http特意内置了流控功能,我们来看看tio-http流控的使用步骤
1、实现SessionRateLimiter
package org.tio.xxxx;
import java.util.HashMap;
import java.util.Map;
import org.tio.http.common.HttpRequest;
import org.tio.http.common.HttpResponse;
import org.tio.http.common.intf.SessionRateLimiter;
public class MySessionRateLimiter implements SessionRateLimiter {
public static final MySessionRateLimiter me = new MySessionRateLimiter();
private MySessionRateLimiter() {
map.put("/demo/query", 2000);
map.put("/demo/update", 1000);
}
private final Map map = new HashMap<>();
@Override
public Integer interval(HttpRequest request) {
String path = request.getRequestLine().getPath();
return map.get(path);
}
@Override
public HttpResponse response(HttpRequest request, Long lastAccessTime) {
request.close();
return null;//Resps.json(request, RespVo.fail("访问过快"));
}
}
2、把SessionRateLimiter设置到HttpConfig
httpConfig.setSessionRateLimiter(MySessionRateLimiter.me);
3、打完收功
4、了解更多