Follow me SpringCloud | CHAPTER 14: Spring Cloud Gateway Advanced Applications

SpringCloud series of tutorials | CHAPTER 14: Spring Cloud Gateway Advanced Applications

Springboot: 2.1.6.RELEASE

SpringCloud: Greenwich.SR1

Unless otherwise specified, the full use of this tutorial series or later

We talked for use on a Gateway and registration centers, as well as the basic use of Filter Gataway in this article we will continue to introduce advanced features of Filter.

  • Fuse
  • Limiting
  • Retry

1. speed router

One of the high-speed concurrency scenarios the more commonly used methods, can effectively guarantee the stability of the overall service, Spring Cloud Gateway provides current limiting scheme based on the Redis. So we need to add the corresponding dependencies spring-boot-starter-data-redis-reactive

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

Configuration file to add Redis address and limiting the related configuration

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: allowing the user to how many requests per second
  • redis-rate-limiter.burstCapacity: token bucket capacity, the maximum number of requests is completed in one second
  • key-resolver: the SpEL bean references by name

Set policies limiting the project to create Config classes.

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: [email protected]
 * Description:
 */
@Configuration
public class Config {
    @Bean
    KeyResolver userKeyResolver() {
        return exchange -> Mono.just(exchange.getRequest().getQueryParams().getFirst("user"));
    }
}

Config class need to add @Configuration comment.

The request parameter field to the user restrictor may be provided upon request to the IP address of the flow restrictor is provided as follows:

@Bean
public KeyResolver ipKeyResolver() {
    return exchange -> Mono.just(exchange.getRequest().getRemoteAddress().getHostName());
}

Such gateways can be limiting for the request based on different strategies.

2. blown router

Before the Spring Cloud series of articles, we should have some understanding of the fuse, as too do not understand you can first read this article: "follow me SpringCloud | Part IV: Fuse Hystrix"

Spring Cloud Gateway can also take advantage of fusing characteristics Hystrix performs service degradation during heavy traffic, also add to the project we first rely on.

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

Configuration Example

spring:
  cloud:
    gateway:
      routes:
      - id: hystrix_route
        uri: http://example.org
        filters:
        - Hystrix=myCommandName

After configuration, gateway will be used to generate the name myCommandName HystrixCommand manage objects to fuse. If you want to add a callback contents of the fuse, you need to add some configuration.

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: / incaseoffailureusethis path configured to be adjusted when fallback, when calling the fallback Hystrix is ​​invoked, the request will be forwarded to / incaseoffailureuset this URI.

3. Retry Router

RetryGatewayFilter Spring Cloud Gateway is to provide a request retry GatewayFilter Factory.

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 by these four parameters to control the retry mechanism: retries, statuses, methods, and series.

  • retries: retries, the default is 3 times
  • statuses: HTTP status code returned, the values ​​refer to: org.springframework.http.HttpStatus
  • methods: a method which needs to be specified in the request retry logic, the default value is the GET method, the reference value: org.springframework.http.HttpMethod
  • series: Status Code is a series configuration, the reference value: org.springframework.http.HttpStatus.Series. The state will comply with certain codes of conduct retry logic, the default value is SERVER_ERROR, the value is 5, which is 5XX (5 beginning with status code), a total of five values.

The above is the project of some common gateway operations, using more about Spring Cloud GateWay Refer to the examiner network.

spring-cloud-gateway官网

Sample Code -Github

reference:

https://cloud.spring.io/spring-cloud-gateway/single/spring-cloud-gateway.html
https://windmt.com/2018/05/11/spring-cloud-16-spring-cloud-gateway-others/

Guess you like

Origin www.cnblogs.com/babycomeon/p/11173609.html