当前位置:网站首页>[microservices SCG] gateway integration Sentinel
[microservices SCG] gateway integration Sentinel
2022-07-07 23:14:00 【Bulst】
sentinel service 1.6.0 The above versions can support the integration gateway for unified flow control
It also provides flow restriction of two resource dimensions :
- route dimension : That is, the routing entries configured in the configuration file , The resource name is the corresponding routeId, This is a coarse-grained current limiting , Generally, it limits the flow of a micro service .
Customize API dimension : Users can take advantage of Sentinel Provided API From defining something API grouping , This is a fine-grained current limiting , For a certain kind of uri Carry out matching current limiting , Can span multiple microservices .
sentinel Integrate gateway rely on
<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>
Because the heartbeat is sent first appType, The front page will not display the console of the gateway, so we need to configure it before sending the heartbeat appType.
To configure JVM Launch parameters
Can be in IDEA in , perhaps java -jar Configure startup parameters-Dcsp.sentinel.app.type=1
The startup class directly sets system parameters
public static void main(String[] args) { System.setProperty("csp.sentinel.app.type", "1"); SpringApplication.run(GatewayApplication.class); }
sentinel Integrate gateway The configuration file
spring:
cloud: # To configure SpringCloudGateway The routing
sentinel:
transport:
dashboard: ip:port #sentinel The request address of the console
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 // Flow control rules
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 type
eager: true # Immediately load
log:
dir: /data/sentinel/gateway // Log storage directory
Customize api Group
[
{
"apiName": "itsmApiFlow",
"predicateItems": [
{
"pattern": "/service-request/detail/getInitInfo"
},
{
"pattern": "/service-request/pool/**",
"matchStrategy": 1
}
]
}
]
- api Type and flow control rules
[
{
"resource": "service-asset",
"resourceMode": 0,
"count": 100,
"intervalSec": 1
},
{
"resource": "itsmApiFlow",
"resourceMode": 1,
"count": 100,
"intervalSec": 1
}
]
The difference between gateway service and other services
In the display of the page, there is an obvious difference between the Internet off service and other services
Rules and customization of gateway flow restriction API The entity and management logic of :
- GatewayFlowRule: Gateway current limiting rules , in the light of API Gateway The flow restriction rules of scenario customization , It can be different route Or custom API Group for current limiting , Support for parameters in the request 、Header、 source IP And so on .
- ApiDefinition: User defined API Define groups , It can be seen as some URL Matching combination . For example, we can define a API It's called my_api, request path The model is /foo/** and /baz/** All belong to my_api This API Group below . When limiting current, you can customize API Group dimension to limit current .
Among them, the gateway current limiting rules GatewayFlowRule The fields of are explained as follows :
- resource: Resource name , It can be... In the gateway route Name or user-defined API Group name .
- resourceMode: The rule is for API Gateway Of route(RESOURCE_MODE_ROUTE_ID) It's the user Sentinel As defined in API grouping (RESOURCE_MODE_CUSTOM_API_NAME), The default is route.
- grade: Limitation index dimension , Same as the current limiting rule grade Field .
- count: Current limiting threshold
- intervalSec: Statistics time window , The unit is seconds , The default is 1 second .
- controlBehavior: Flow shaping control effect , Same as the current limiting rule controlBehavior Field , At present, it supports fast failure and uniform queuing , The default is to fail fast .
- burst: Number of additional requests allowed in response to a burst request .
- maxQueueingTimeoutMs: Maximum queuing time in uniform queuing mode , In milliseconds , Only in uniform queuing mode .
- paramItem: Parameter current limiting configuration . If you do not provide , It means that current limiting is not carried out for parameters , The gateway rules will be converted into normal flow control rules ; Otherwise, it will be converted into hot spot rule . Fields in :
- parseStrategy: Strategy to extract parameters from the request , Extraction sources are currently supported IP(PARAM_PARSE_STRATEGY_CLIENT_IP)、Host(PARAM_PARSE_STRATEGY_HOST)、 arbitrarily Header(PARAM_PARSE_STRATEGY_HEADER) And arbitrary URL Parameters (PARAM_PARSE_STRATEGY_URL_PARAM) Four patterns .
- fieldName: If the extraction strategy is chosen Header Mode or URL Parameter mode , You need to specify the corresponding header Name or URL Parameter name .
- pattern: Matching pattern of parameter values , Only the request attribute values matching this pattern will be included in statistics and flow control ; If it is blank, all values of the request attribute will be counted .(1.6.2 Version starting support )
- matchStrategy: Matching strategy for parameter values , At present, accurate matching is supported (PARAM_MATCH_STRATEGY_EXACT)、 The substring matching (PARAM_MATCH_STRATEGY_CONTAINS) And regular match (PARAM_MATCH_STRATEGY_REGEX).(1.6.2 Version starting support )
The user can go through GatewayRuleManager.loadRules(rules) Manually load gateway rules , Or through GatewayRuleManager.register2Property(property) Register dynamic rule source dynamic push ( Recommend ways ).
Customized some API grouping :
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);
}
Be careful :
- Sentinel The default granularity of gateway flow control is route Dimension and customization API Grouping dimension , Not supported by default URL Particle size . If through Spring Cloud Alibaba Access , Please put spring.cloud.sentinel.filter.enabled The configuration item is set to false( If you see it on the gateway flow control console URL resources , This is not configured false).
- If you use Spring Cloud Alibaba Sentinel Data source module , Note that the data source type of gateway flow control rules is gw-flow, If the gateway flow control rule data source is specified as flow Do not take effect .
边栏推荐
- Classification and prediction of heartbeat signal
- What does the model number of asemi rectifier bridge kbpc1510 represent
- Software evaluation center ▏ what are the basic processes and precautions for automated testing?
- DTC社群运营怎么做?
- Microbial health network, how to restore microbial communities
- 肠道里的微生物和皮肤上的一样吗?
- Talk about the design and implementation logic of payment process
- 聊聊 Dart 的空安全 (null safety) 特性
- When copying something from the USB flash disk, an error volume error is reported. Please run CHKDSK
- ArcGIS: two methods of attribute fusion of the same field of vector elements
猜你喜欢
小程序多种开发方式对比-跨端?低代码?原生?还是云开发?
GEE(四):计算两个变量(影像)之间的相关性并绘制散点图
Anta DTC | Anta transformation, building a growth flywheel that is not only FILA
Line test - graphic reasoning - 1 - Chinese character class
微信论坛交流小程序系统毕业设计毕设(1)开发概要
Software test classification
聊聊 Dart 的空安全 (null safety) 特性
消费品企业敏捷创新转型案例
数字藏品加速出圈,MarsNFT助力多元化文旅经济!
Comparison of various development methods of applets - cross end? Low code? Native? Or cloud development?
随机推荐
Network security CSRF
GEE(四):计算两个变量(影像)之间的相关性并绘制散点图
oc 可变參数传递
./ setup. Insufficient sh permission
网络安全-联合查询注入
成年人只有一份主业是要付出代价的,被人事劝退后,我哭了一整晚
Introduction to anomaly detection
Digital collections accelerated out of the circle, and marsnft helped diversify the culture and tourism economy!
数字藏品加速出圈,MarsNFT助力多元化文旅经济!
力扣解法汇总648-单词替换
Inftnews | web5 vs Web3: the future is a process, not a destination
Unity dynamically merges mesh textures
微信论坛交流小程序系统毕业设计毕设(2)小程序功能
UE4_UE5全景相机
USB(十四)2022-04-12
为什么市场需要低代码?
Why does the market need low code?
微信论坛交流小程序系统毕业设计毕设(6)开题答辩PPT
Wechat forum exchange applet system graduation design completion (4) opening report
位运算(Bit Operation)