当前位置:网站首页>【微服务|SCG】gateway整合sentinel
【微服务|SCG】gateway整合sentinel
2022-07-07 21:50:00 【步尔斯特】
sentinel服务1.6.0以上的版本可支持整合网关进行统一流控
并且提供两种资源维度的限流:
- route维度:即在配置文件中配置的路由条目,资源名为对应的 routeId,这种属于粗粒度的限流,一般是对某个微服务进行限流。
自定义API维度:用户可以利用 Sentinel 提供的API来自定义一些API分组,这种属于细粒度的限流,针对某一类的uri进行匹配限流,可以跨多个微服务。
sentinel整合gateway依赖
<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>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-spring-cloud-gateway-adapter</artifactId>
</dependency>
因为心跳优先发送了appType,会导致前台页面显示不出来网关的控制台所以么我们需要事先在发送心跳之前配置appType。
配置JVM启动参数
可以在IDEA中,或者java -jar配置启动参数-Dcsp.sentinel.app.type=1启动类直接设置系统参数
public static void main(String[] args) { System.setProperty("csp.sentinel.app.type", "1"); SpringApplication.run(GatewayApplication.class); }
sentinel整合gateway配置文件
spring:
cloud: #配置SpringCloudGateway的路由
sentinel:
transport:
dashboard: ip:port #sentinel控制台的请求地址
datasource:
ds1:
nacos:
server-addr: ip:port
data-id: ${
spring.application.name}-${
spring.profiles.active}-sentinel-gw-flow
group-id: DEFAULT_GROUP
data-type: json
rule-type: gw-flow //流控规则
ds2:
nacos:
server-addr: ip:port
data-id: ${
spring.application.name}-${
spring.profiles.active}-sentinel-gw-api-group
group-id: DEFAULT_GROUP
data-type: json
rule-type: gw-api-group //api类型
eager: true #立即加载
log:
dir: /data/sentinel/gateway //日志存放目录
自定义api组
[
{
"apiName": "itsmApiFlow",
"predicateItems": [
{
"pattern": "/service-request/detail/getInitInfo"
},
{
"pattern": "/service-request/pool/**",
"matchStrategy": 1
}
]
}
]
- api类型及流控规则
[
{
"resource": "service-asset",
"resourceMode": 0,
"count": 100,
"intervalSec": 1
},
{
"resource": "itsmApiFlow",
"resourceMode": 1,
"count": 100,
"intervalSec": 1
}
]
网关服务和其他服务的区别
在页面的展示上网关服务和其他服务有着明显的不同

网关限流的规则和自定义 API 的实体和管理逻辑:
- GatewayFlowRule:网关限流规则,针对 API Gateway 的场景定制的限流规则,可以针对不同 route 或自定义的 API 分组进行限流,支持针对请求中的参数、Header、来源 IP 等进行定制化的限流。
- ApiDefinition:用户自定义的 API 定义分组,可以看做是一些 URL 匹配的组合。比如我们可以定义一个 API 叫 my_api,请求 path 模式为 /foo/** 和 /baz/** 的都归到 my_api 这个 API 分组下面。限流的时候可以针对这个自定义的 API 分组维度进行限流。
其中网关限流规则 GatewayFlowRule 的字段解释如下:
- resource:资源名称,可以是网关中的 route 名称或者用户自定义的 API 分组名称。
- resourceMode:规则是针对 API Gateway 的 route(RESOURCE_MODE_ROUTE_ID)还是用户在 Sentinel 中定义的 API 分组(RESOURCE_MODE_CUSTOM_API_NAME),默认是 route。
- grade:限流指标维度,同限流规则的 grade 字段。
- count:限流阈值
- intervalSec:统计时间窗口,单位是秒,默认是 1 秒。
- controlBehavior:流量整形的控制效果,同限流规则的 controlBehavior 字段,目前支持快速失败和匀速排队两种模式,默认是快速失败。
- burst:应对突发请求时额外允许的请求数目。
- maxQueueingTimeoutMs:匀速排队模式下的最长排队时间,单位是毫秒,仅在匀速排队模式下生效。
- paramItem:参数限流配置。若不提供,则代表不针对参数进行限流,该网关规则将会被转换成普通流控规则;否则会转换成热点规则。其中的字段:
- parseStrategy:从请求中提取参数的策略,目前支持提取来源 IP(PARAM_PARSE_STRATEGY_CLIENT_IP)、Host(PARAM_PARSE_STRATEGY_HOST)、任意 Header(PARAM_PARSE_STRATEGY_HEADER)和任意 URL 参数(PARAM_PARSE_STRATEGY_URL_PARAM)四种模式。
- fieldName:若提取策略选择 Header 模式或 URL 参数模式,则需要指定对应的 header 名称或 URL 参数名称。
- pattern:参数值的匹配模式,只有匹配该模式的请求属性值会纳入统计和流控;若为空则统计该请求属性的所有值。(1.6.2 版本开始支持)
- matchStrategy:参数值的匹配策略,目前支持精确匹配(PARAM_MATCH_STRATEGY_EXACT)、子串匹配(PARAM_MATCH_STRATEGY_CONTAINS)和正则匹配(PARAM_MATCH_STRATEGY_REGEX)。(1.6.2 版本开始支持)
用户可以通过 GatewayRuleManager.loadRules(rules) 手动加载网关规则,或通过 GatewayRuleManager.register2Property(property) 注册动态规则源动态推送(推荐方式)。
自定义了一些 API 分组:
private void initCustomizedApis() {
Set<ApiDefinition> definitions = new HashSet<>();
ApiDefinition api1 = new ApiDefinition("some_customized_api")
.setPredicateItems(new HashSet<ApiPredicateItem>() {
{
add(new ApiPathPredicateItem().setPattern("/product/baz"));
add(new ApiPathPredicateItem().setPattern("/product/foo/**")
.setMatchStrategy(SentinelGatewayConstants.PARAM_MATCH_STRATEGY_PREFIX));
}});
ApiDefinition api2 = new ApiDefinition("another_customized_api")
.setPredicateItems(new HashSet<ApiPredicateItem>() {
{
add(new ApiPathPredicateItem().setPattern("/ahas"));
}});
definitions.add(api1);
definitions.add(api2);
GatewayApiDefinitionManager.loadApiDefinitions(definitions);
}
注意:
- Sentinel 网关流控默认的粒度是 route 维度以及自定义 API 分组维度,默认不支持 URL 粒度。若通过 Spring Cloud Alibaba 接入,请将 spring.cloud.sentinel.filter.enabled 配置项置为 false(若在网关流控控制台上看到了 URL 资源,就是此配置项没有置为 false)。
- 若使用 Spring Cloud Alibaba Sentinel 数据源模块,需要注意网关流控规则数据源类型是 gw-flow,若将网关流控规则数据源指定为 flow 则不生效。
边栏推荐
猜你喜欢

There is another problem just online... Warm

行測-圖形推理-4-字母類

Line test - graphic reasoning - 2 - black and white lattice class

The wonderful relationship between message queue and express cabinet

Interview questions: how to test app performance?
![LeetCode206. Reverse linked list [double pointer and recursion]](/img/3c/84351e771ac9763c1e5f7b4921c099.jpg)
LeetCode206. Reverse linked list [double pointer and recursion]

Ligne - raisonnement graphique - 4 - classe de lettres

iNFTnews | NFT技术的广泛应用及其存在的问题
![[record of question brushing] 3 Longest substring without duplicate characters](/img/44/1cd8128d93c9c273e0f4718d84936e.png)
[record of question brushing] 3 Longest substring without duplicate characters
![Cause analysis and solution of too laggy page of [test interview questions]](/img/33/2c2256fd98b908ddaf5573f644ad7f.png)
Cause analysis and solution of too laggy page of [test interview questions]
随机推荐
Sword finger offer 63 Maximum profit of stock
Quelles sont les similitudes et les différences entre les communautés intelligentes et les villes intelligentes?
Use JfreeChart to generate curves, histograms, pie charts, and distribution charts and display them to JSP-1
Redhat下安装fedora
Cases of agile innovation and transformation of consumer goods enterprises
开发那些事儿:Go加C.free释放内存,编译报错是什么原因?
Installing vmtools is gray
Line test - graphic reasoning - 4 - alphabetic class
Why does the market need low code?
LeetCode203. Remove linked list elements
网络安全-永恒之蓝
Innovation today | five key elements for enterprises to promote innovation
The author of LinkedList said he didn't use LinkedList himself
线上面试,该如何更好的表现自己?这样做,提高50%通过率~
Txt file virus
【刷题记录】3. 无重复字符的最长子串
Advantages and disadvantages of rest ful API
Unity与WebGL的相爱相杀
Guessing game (read data from file)
双非大厂测试员亲述:对测试员来说,学历重要吗?