SpringCloud Zuul过滤器和谷歌Gauva实现限流
作者:玉天恒 时间:2022-08-31 11:59:21
前提:
已经配置Zuul网关
参考:
https://www.jb51.net/article/182894.htm
限流方式:
1)nginx层限流
2)网关层限流
1.添加限流过滤器
import com.alibaba.fastjson.JSON;
import com.google.common.util.concurrent.RateLimiter;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.apache.commons.lang.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.PRE_TYPE;
/**
* 订单限流接口
*/
@Component
public class OrderRateLimiterFilter extends ZuulFilter {
//每秒产生1000个令牌
private static final RateLimiter RATE_LIMITER = RateLimiter.create(1000);
@Override
public String filterType() {
return PRE_TYPE;
}
@Override
public int filterOrder() {
return -4;
}
@Override
public boolean shouldFilter() {
RequestContext context = RequestContext.getCurrentContext();
HttpServletRequest request = context.getRequest();
///apigateway/order/api/v1/order/test
System.out.println(request.getRequestURI());
//http://127.0.0.1:9000/apigateway/order/api/v1/order/test
System.out.println(request.getRequestURL());
//限流的接口
List<String> noFIlter = new ArrayList<>();
noFIlter.add("/apigateway/order/**");
AntPathMatcher matcher = new AntPathMatcher();
for (String pattern : noFIlter) {//pattern--/user/**
if (StringUtils.isNotEmpty(pattern)
&& matcher.match(pattern, request.getRequestURI())) {
return true;
}
}
return false;
}
@Override
public Object run() throws ZuulException {
//可以用JMeter来进行测试
RequestContext context = RequestContext.getCurrentContext();
//tryAcquire达到最大流量时,立刻限流,也可以配置参数
if (!RATE_LIMITER.tryAcquire()) {
Map<String, Object> result = new HashMap<>();
result.put("code", 429);
result.put("msg", "目前访问量过大,限流了...");
context.setSendZuulResponse(false);
context.setResponseStatusCode(HttpStatus.TOO_MANY_REQUESTS.value());
context.setResponseBody(JSON.toJSONString(result));
//解决中文乱码
context.getResponse().setCharacterEncoding("UTF-8");
context.getResponse().setContentType("text/html;charset=UTF-8");
}
return null;
}
}
令牌桶限流图解
来源:https://www.cnblogs.com/tianhengblogs/p/12501235.html
标签:Gauva,Spring,Cloud,Zuul,限流
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java生成压缩文件的实例代码
2023-02-04 21:11:09
![](https://img.aspxhome.com/file/2023/1/62721_0s.jpg)
c++中vector<int>和vector<int*>的用法及区别
2023-03-28 02:29:37
基于JSON实现传输byte数组过程解析
2021-11-23 05:07:21
![](https://img.aspxhome.com/file/2023/4/107554_0s.png)
c# 实现IComparable、IComparer接口、Comparer类的详解
2022-07-23 00:25:56
Android性能优化之ViewPagers + Fragment缓存优化
2022-08-13 10:23:35
![](https://img.aspxhome.com/file/2023/7/94987_0s.png)
Android 自绘控件
2022-02-19 17:08:13
![](https://img.aspxhome.com/file/2023/0/137330_0s.png)
Java深入讲解二十三种设计模式之中的策略模式
2022-11-25 13:23:15
![](https://img.aspxhome.com/file/2023/5/99855_0s.png)
Android获取点击屏幕的位置坐标
2023-12-14 21:18:31
![](https://img.aspxhome.com/file/2023/5/110735_0s.jpg)
java字符串相似度算法
2023-11-26 12:33:25
Android实现简单旋转动画
2023-11-07 09:50:58
![](https://img.aspxhome.com/file/2023/0/91700_0s.jpg)
一行命令同时修改maven项目中多个module的版本号的方法
2023-09-13 09:43:10
Android实现签名涂鸦手写板
2022-07-27 04:10:14
![](https://img.aspxhome.com/file/2023/6/138506_0s.jpg)
C#多线程之线程同步WaitHandle
2022-08-10 10:16:12
![](https://img.aspxhome.com/file/2023/3/94623_0s.jpg)
C#深拷贝方法探究及性能比较(多种深拷贝)
2022-08-30 18:17:02
![](https://img.aspxhome.com/file/2023/5/77235_0s.png)
无法从 int? 转换为 int 运行时出现错误
2022-01-02 08:26:11
![](https://img.aspxhome.com/file/2023/1/96361_0s.png)
springboot+vue实现登录功能的最新方法整理
2022-08-31 21:40:23
![](https://img.aspxhome.com/file/2023/9/75679_0s.png)
Java Spring5学习之JdbcTemplate详解
2023-11-25 20:17:23
![](https://img.aspxhome.com/file/2023/2/60412_0s.png)
Android LuBan与Compressor图片压缩方式
2022-11-29 01:18:41
Java编程中使用XFire框架调用WebService程序接口
2023-11-06 20:16:33
![](https://img.aspxhome.com/file/2023/0/58990_0s.png)
java 代码块与静态代码块加载顺序
2021-08-01 15:19:35