- Fusing
- Current limiting
- retry
1. Speed limit router
Speed limit is one of the common means in high concurrency scenarios, which can effectively guarantee the overall stability of the service. Spring Cloud Gateway provides a Redis based flow limit scheme. So we need to add the corresponding dependency package spring boot starter data Redis reactive first
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis-reactive</artifactId> </dependency>
Redis address and current restriction related configuration need to be added to the configuration file
server: port: 8080 spring: application: name: spring-cloud-gateway redis: host: localhost password: password port: 6379 cloud: gateway: discovery: locator: enabled: true routes: - id: requestratelimiter_route uri: http://example.org filters: - name: RequestRateLimiter args: redis-rate-limiter.replenishRate: 10 redis-rate-limiter.burstCapacity: 20 key-resolver: "#{@userKeyResolver}" predicates: - Method=GET
- filter name must be RequestRateLimiter
- redis-rate-limiter.replenishRate : how many requests per second are allowed to be processed by the user
- redis-rate-limiter.burstCapacity : capacity of token bucket, the maximum number of requests allowed to complete in one second
- Key resolver: using spiel to reference bean s by name
Set the current limiting policy in the project and create the Config class. (understand the source code and ask: 1791743380)
package com.springcloud.gateway.config; import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import reactor.core.publisher.Mono; /** * Created with IntelliJ IDEA. * * @Date: 2019/7/11 * @Time: 23:45 * @email: inwsy@hotmail.com * Description: */ @Configuration public class Config { @Bean KeyResolver userKeyResolver() { return exchange -> Mono.just(exchange.getRequest().getQueryParams().getFirst("user")); } }
The Config class needs to be annotated with @ Configuration.
The current can be limited according to the user field in the request parameter, or according to the IP address of the request. The settings are as follows:
@Bean public KeyResolver ipKeyResolver() { return exchange -> Mono.just(exchange.getRequest().getRemoteAddress().getHostName()); }
In this way, the gateway can restrict the flow of requests according to different policies.
2. Fuse router
Spring Cloud Gateway can also take advantage of the fusing feature of Hystrix to degrade the service when the traffic is too large. In the same way, we will first add a dependency to the project.
In this way, the gateway can restrict the flow of requests according to different policies. 2. Fuse router In the previous series of Spring Cloud articles, you should have a certain understanding of fusing. If you don't know about fusing, you can read this article first: "learn from me Spring Cloud | Article 4: fuse Hystrix" Spring Cloud Gateway can also take advantage of the fusing feature of Hystrix to degrade the service when the traffic is too large. In the same way, we will first add a dependency to the project.
Configuration example
spring: cloud: gateway: routes: - id: hystrix_route uri: http://example.org filters: - Hystrix=myCommandName
After configuration, gateway will use myCommandName as the name to generate the HystrixCommand object for fuse management. If you want to add the callback content after fusing, you need to add some configuration in.
spring: cloud: gateway: routes: - id: hystrix_route uri: lb://spring-cloud-producer predicates: - Path=/consumingserviceendpoint filters: - name: Hystrix args: name: fallbackcmd fallbackUri: forward:/incaseoffailureusethis
Fallbackuri: forward: / incaseoffailureuset his is the path to be called when fallback is configured. When the fallback calling Hystrix is called, the request will be forwarded to the URI / incaseoffailureuse.
3. Retry router
RetryGatewayFilter is a GatewayFilter Factory provided by Spring Cloud Gateway for request retry.
Configuration example
spring: cloud: gateway: routes: - id: retry_test uri: lb://spring-cloud-producer predicates: - Path=/retry filters: - name: Retry args: retries: 3 statuses: BAD_GATEWAY
Retry GatewayFilter controls the retry mechanism through these four parameters: retries, statuses, methods, and series.
- Retries: the number of retries. The default value is 3
- statuses: the status return code of HTTP. Please refer to: org.springframework.http.HttpStatus
- methods: specifies which method's request needs retry logic. The default value is GET method. Value reference: org.springframework.http.HttpMethod
- series: configuration of status codes of some columns, value reference: org.springframework.http.HttpStatus.Series . The retry logic will only be performed if a certain segment of the status code matches. The default value is SERVER_ERROR, the value is 5, i.e. 5xx (status code at the beginning of 5). There are 5 values in total.