(Thirty-eight) spring cloud micro Services Architecture b2b2c limiting e-commerce --gateway

Please add source e-commerce platform Penguin beg: 3536247259. In Spring Cloud Gateway, there is a filter Filter, can be realized in the three filter self "pre" the Filter type. But limiting as the most basic function of a gateway, Spring Cloud Gateway provides the official RequestRateLimiterGatewayFilterFactory this class for Redis lua script and realized the token bucket approach.

Specific source do not intend to tell the reader can view their own, smaller amount of code, first in the form of case to explain how to use the built-in current limiting filter plant in Spring Cloud Gateway is implemented in current limit.

First, the introduction of reactive starting dependence and reliance gateway redis pom file in the project, the code is as follows:


 <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifatId>spring-boot-starter-data-redis-reactive</artifactId>
</dependency>

Do the following configuration in the configuration file:


server:
  port: 8081
spring:
  cloud:
    gateway:
      routes:
      - id: limit_route
        uri: http://httpbin.org:80/get
        predicates:
        - After=2017-01-20T17:42:47.789-07:00[America/Denver]
        filters:
        - name: RequestRateLimiter
          args:
            key-resolver: '#{@hostAddrKeyResolver}'
            redis-rate-limiter.replenishRate: 1
            redis-rate-limiter.burstCapacity: 3
  application:
    name: gateway-limiter
  redis:
    host: localhost
    port: 6379
    database: 0


In the above configuration file, the program for the designated port 8081, redis configuration information and the configuration of the flow restrictor RequestRateLimiter filter, the filter configuration requires three parameters:

burstCapacity, the total capacity of the token bucket.

replenishRate, average fill rate of the token bucket per second.

key-resolver, the resolver for the name of Bean object keys of limiting. It acquires the SpEL expression Bean Spring objects from a container according to # {@ beanName}.

KeyResolver resolve the need to achieve a method, such as to limit current, is determined according to need hostAddress Hostname. After the realization of complete KeyResolver, this class will need to register Bean Ioc container.


public class HostAddrKeyResolver implements KeyResolver {

    @Override
    public Mono<String> resolve(ServerWebExchange exchange) {
        return Mono.just(exchange.getRequest().getRemoteAddress().getAddress().getHostAddress());
    }

}

 @Bean
    public HostAddrKeyResolver hostAddrKeyResolver() {
        return new HostAddrKeyResolver();
    }

The flow restrictor can go uri, KeyResolver time code is as follows:


public class UriKeyResolver  implements KeyResolver {

    @Override
    public Mono<String> resolve(ServerWebExchange exchange) {
        return Mono.just(exchange.getRequest().getURI().getPath());
    }

}

 @Bean
    public UriKeyResolver uriKeyResolver() {
        return new UriKeyResolver();
    }

 

Users can also be limiting dimensions to:


   @Bean
    KeyResolver userKeyResolver() {
        return exchange -> Mono.just(exchange.getRequest().getQueryParams().getFirst("user"));
    }


Pressure measurement performed with jmeter configured to cycle requests 10thread lcoalhost: 8081, cycle interval 1s. Seen from the results of the pressure measured by the request part by part request fails.

Guess you like

Origin blog.csdn.net/wiyzq/article/details/90901674