In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you how to use Gateway in Spring Cloud to achieve extended support for dynamic current limit, the content is concise and easy to understand, it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
The deficiency of native RequestRateLimiter
Configuration mode
Spring: cloud: gateway: routes:-id: requestratelimiter_route uri: lb://pigx-upms order: 10000 predicates:-Path=/admin/** filters:-name: RequestRateLimiter args: redis-rate-limiter.replenishRate: 1 redis-rate-limiter.burstCapacity: 3 key-resolver: "# { @ remoteAddrKeyResolver} "# SPEL expression to the corresponding bean-StripPrefix=1
RequestRateLimiterGatewayFilterFactory
Public GatewayFilter apply (Config config) {KeyResolver resolver = getOrDefault (config.keyResolver, defaultKeyResolver); RateLimiter limiter = getOrDefault (config.rateLimiter, defaultRateLimiter); boolean denyEmpty = getOrDefault (config.denyEmptyKey, this.denyEmptyKey); HttpStatusHolder emptyKeyStatus = HttpStatusHolder .parse (getOrDefault (config.emptyKeyStatus, this.emptyKeyStatusCode)) Return (exchange, chain)-> {return exchange.getResponse () .setComplete ();};}
In the actual production process, it must not be able to meet our needs
Routing information in production is to save database persistence or configuration center. RequestRateLimiterGatewayFilterFactory can not dynamically change current limiting parameters with the change of persistent data, and can not change the traffic threshold in real time according to traffic.
What is Sentinel Spring Cloud Gateway flow control support Sentinel?
With the popularity of microservices, the stability between services and services is becoming more and more important. Sentinel takes traffic as the starting point to protect the stability of services from many dimensions, such as flow control, fuse degradation, system load protection and so on.
Starting from version 1.6.0, Sentinel provides an adaptation module for Spring Cloud Gateway, which can limit the flow of two resource dimensions: route dimension: the routing entry configured in the Spring configuration file, and the resource name is the corresponding routeId custom API dimension: users can use the API provided by Sentinel to customize some API groups.
Pom relies on com.alibaba.cloud spring-cloud-alibaba-sentinel-gateway com.alibaba.csp sentinel-datasource-nacos to configure local routing rules and its sentinel data source spring: application: name: sentinel-spring-cloud-gateway cloud: gateway: enabled: true discovery: locator: lower-case-service-id: true routes:-id: pigx_route uri: https:// Api.readhub.cn predicates:-Path=/topic/** sentinel: datasource.ds1.nacos: server-addr: 127.0.0.1 server-addr 8848 data-id: gw-flow group-id: DEFAULT_GROUP ruleType: gw-api-group filter: enabled: true configure current limiting policy in nacos data source
Commonly used current limiting strategy constant
Take client IP as current limiting factor public static final int PARAM_PARSE_STRATEGY_CLIENT_IP = 0, client HOST as current limiting factor public static final int PARAM_PARSE_STRATEGY_HOST = 1, client HEADER parameter as current limiting factor public static final int PARAM_PARSE_STRATEGY_HEADER = 2, client request parameter as current limiting factor public static final int PARAM_PARSE_STRATEGY_URL_PARAM = 3 Take the client request Cookie as the current limiting factor public static final int PARAM_PARSE_STRATEGY_COOKIE = 4
Core source code parsing SentinelGatewayFilter
By extending the filter of Gateway, sentinel compares the source codes by selecting different GatewayParamParser overprocessing request flow-limiting factors and configurations in the data source:
Public Mono filter (ServerWebExchange exchange, GatewayFilterChain chain) {Route route = exchange.getAttribute (ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR); Mono asyncResult = chain.filter (exchange); if (route! = null) {String routeId = route.getId (); Object [] params = paramParser.parseParameterFor (routeId, exchange, r-> r.getResourceMode () = SentinelGatewayConstants.RESOURCE_MODE_ROUTE_ID) String origin = Optional.ofNullable (GatewayCallbackManager.getRequestOriginParser ()) .map (f-> f.apply (exchange)) .orElse (""); asyncResult = asyncResult.transform (new SentinelReactorTransformer (new EntryConfig (routeId, EntryType.IN, 1, params, new ContextConfig (contextName (routeId), origin);} Set matchingApis = pickMatchingApiDefinitions (exchange) For (String apiName: matchingApis) {Object [] params = paramParser.parseParameterFor (apiName, exchange, r-> r.getResourceMode () = = SentinelGatewayConstants.RESOURCE_MODE_CUSTOM_API_NAME); asyncResult = asyncResult.transform (new EntryConfig (apiName, EntryType.IN, 1, params));} return asyncResult;} effect demonstration
The above nacos is configured to pass only 5 requests per second. Let's test it with jmeter 4.0concurrently with 10 threads.
Through the above figure, it can be proved that the sentinel current limit is really effective.
Dynamic modification of current limiting parameters
As the data source of sentinel, sentinel-datasource-nacos can refresh the current limiting parameters and their thresholds in real time from the nacos console above.
At present, sentinel dashboard 1.6.2 does not implement gateway flow control graphical control. 1.7.0 will add this function.
The above content is how to use Gateway extension to support dynamic current limit in Spring Cloud. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.