当前位置:网站首页>限流设计及实现
限流设计及实现
2022-06-26 18:20:00 【[email protected]】
第一步:添加依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
</dependency>
第二步:添加sentinel及路由规则(假如已有则无需设置)
server:
port: 9000
spring:
application:
name: sca-gateway
cloud:
nacos:
discovery:
server-addr: localhost:8848
config:
server-addr: localhost:8848 #配置中心地址
file-extension: yml
gateway:
routes: #配置网关路由规则
- id: route01 #路由id,自己指定一个唯一值即可
#uri: http://localhost:8081/ #网关帮我们转发的url
uri: lb://sca-provider #lb表示负载均衡,sca-provider为服务名
predicates: ###断言(谓词):匹配请求规则(进行逻辑判断) http://ip:port/nacos/provider/echo/01
- Path=/nacos/provider/echo/** #请求路径定义,此路径对应uri中的资源
# - After=2021-11-20T23:59:59.789+08:00[Asia/Shanghai]
#- Query=pageSize,\d+
filters: ##网关过滤器,用于对谓词中的内容进行判断分析以及处理
- StripPrefix=1 #转发之前去掉path中第一层路径,例如nacos
#限流设计及实现
sentinel:
eager: true
transport:
dashboard: localhost:8180
第三步:启动网关项目,检测sentinel控制台的网关菜单。
启动时,添加sentinel的jvm参数,通过此菜单可以让网关服务在sentinel控制台显示不一样的菜单,代码如下。
-Dcsp.sentinel.app.type=1
说明,假如没有发现请求链路,API管理,关闭网关项目,关闭sentinel,然后重启sentinel,重启网关项目.
第四步:在sentinel面板中设置限流策略,如图所示:
第五步:通过url进行访问检测是否实现了限流操作
六,基于请求属性限流
七,自定义API维度限流(重点)
第二步:新建分组流控规则,如图所示:
第三步:进行访问测试,如图所示
八,定制流控网关返回值
定义配置类,设计流控返回值,代码如下:
方法一:在启动类同包或者子包下重新写一个类:GatewayConfig
@Configuration
public class GatewayConfig {
public GatewayConfig(){
GatewayCallbackManager.setBlockHandler(new BlockRequestHandler() {
@Override
public Mono<ServerResponse> handleRequest(ServerWebExchange serverWebExchange, Throwable throwable) {
Map<String,Object> map=new HashMap<>();
map.put("state",429);
map.put("message","two many request");
String jsonStr= JSON.toJSONString(map);
return ServerResponse.ok().body(Mono.just(jsonStr),String.class);
}
});
}
}
方法二:在启动类中写
配置网关:
进行访问测试,如图所示
版权声明
本文为[[email protected]@yxg]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_49143405/article/details/121629269
边栏推荐
- 分页查询、JOIN关联查询优化
- sql中的几种删除操作
- Several delete operations in SQL
- 博云,站在中国容器潮头
- Comparing the size relationship between two objects turns out to be so fancy
- How to create and enforce indexes
- Clion编译catkin_ws(ROS工作空间包的简称)加载CMakeLists.txt出现的问题
- Do you know how to compare two objects
- Chinese (Simplified) language pack
- 必须要掌握的面试重点——索引和事务(附讲B-树与B+树)
猜你喜欢
博云,站在中国容器潮头
Clion breakpoint single step debugging
wm_concat()和group_concat()函数
Leetcode interview question 29 clockwise print matrix
Boyun, standing at the forefront of China's container industry
最小生成树、最短路径、拓扑排序、关键路径
必须要掌握的面试重点——索引和事务(附讲B-树与B+树)
JVM entry door (1)
Properties file garbled
MySQL download and configuration MySQL remote control
随机推荐
sqlite数据库的系统表sqlite_master
Soft test preparation multimedia system
DoS及攻击方法详解
必须要掌握的面试重点——索引和事务(附讲B-树与B+树)
图像二值化处理
刻录光盘的程序步骤
ROS query topic specific content common instructions
成功解决之Jenkins报错:The goal you specified requires a project to execute but there is no POM
Ethereum技术架构介绍
JVM入个门(1)
VCD video disc
Several delete operations in SQL
VCD-影音光碟
Procedure steps for burning a disc
物联网协议的王者:MQTT
DVD-数字通用光盘
Crawl Douban to read top250 and import it into SqList database (or excel table)
Dos et détails de la méthode d'attaque
Row lock analysis and deadlock
Boyun, standing at the forefront of China's container industry