SprigCloud|14編:Spring Cloud Gateway上級アプリケーションを勉強します。


SpringCloudシリーズ教程|14編:Spring Cloud Gateway上級アプリケーション
Springboot:2.1.6.RELEASE
SprigCloud:Green wich.SR 1
特別な説明がない場合、本シリーズの教程は全部以上のバージョンを採用します。
前回はGatewayと登録センターの使用について話しました。また、GatawayのFilterの基本使用について、この文章はFilterの高級機能を紹介し続けます。
  • ヒューズ
  • 限流
  • 再試行
  • 1.制限速度ルータ
    速度制限は、高合併シーンでよく使われる手段の一つであり、サービスの全体的な安定性を効果的に確保することができ、Spring Coud GatewayはRedisに基づく限流スキームを提供しています。ですから、まず対応する依存パッケージを追加します。spring-boot-starter-data-redis-reactive。
    
      org.springframework.boot
      spring-boot-starter-data-redis-reactive
    
    
    設定ファイルにはRedisアドレスとストリーム制限に関する設定を追加する必要があります。
    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の名前は必ずRequest RateLimiter
  • でなければなりません。
  • redis-rate-limiter.replenishRate:ユーザが毎秒何個の要求を処理することができますか?
  • redis-rate-limiter.burst Capacity:トークンバケットの容量は、一秒で完了できる最大要求数
  • です。
  • key-resover:SpELを使ってbean
  • を引用します。
    プロジェクトに限流のポリシーを設定し、Configクラスを作成します。
    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類は@Configration注解を必要とします。
    要求パラメータのuserフィールドに応じてフロー制限を行い、要求IPアドレスに応じてフロー制限を設定することもできます。以下のように設定します。
    @Bean
    public KeyResolver ipKeyResolver() {
        return exchange -> Mono.just(exchange.getRequest().getRemoteAddress().getHostName());
    }
    
    このようにゲートウェイは、異なるポリシーに従って要求を制限することができる。
    2.ヒューズルータ
    以前のSpring Cloudシリーズの文章の中で、皆さんは溶断に対して一定の理解があったはずです。もし知らないなら、先にこの文章を読むことができます。
    Spring Cloud GatewayもHystrixの溶断特性を利用して、流量が大きい時にサービスを格下げします。同じように、まずプロジェクトに依存を追加します。
    
      org.springframework.cloud
      spring-cloud-starter-netflix-hystrix
    
    
    設定例
    spring:
      cloud:
        gateway:
          routes:
          - id: hystrix_route
            uri: http://example.org
            filters:
            - Hystrix=myCommandName
    
    設定後、gatewayはmyCommundNameを名称としてHystrixCommandオブジェクトを生成し、溶断管理を行います。溶断後のコールバック内容を追加するには、いくつかの設定を追加する必要があります。
    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:/incaseoff failureusethisはfallbackを配置した時に調整するパスで、Hystrixを呼び出したfallbackが呼び出された時に、このURIに転送するように要求します。
    3.ルータを再試験する
    RetryGatewayFilterは、Spring Cloud Gatewayが再試行を要求して提供するGateway Filter Factoryである。
    設定例
    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は、この4つのパラメータによって、再試行機構を制御します。retries、statuses、methods、およびseries。
  • retries:再試行回数、デフォルト値は3回
  • です。
  • statuses:HTTPの状態戻りコードは、値を取ります。org.springframe ework.http.Httpstatus
  • を参照してください。
  • methods:どのような方法を指定する要求がリトライロジックを必要としていますか?標準値はGET方法で、値を参考にしてください。org.springframewark.http.http Method
  • series:いくつかの列の状態コードの構成、参考を取る:org.springframe eweet.http.HttpStatus.Series。該当する状態コードは再試験ロジックになります。標準値はSERVER_です。ERRORは、値が5、つまり5 XX(5から始まる状態コード)で、5つの値があります。
  • 以上はプロジェクトでよく使われているゲートウェイの操作です。Spring Coud GateWayの利用については公式サイトを参照してください。
    スプリング-cloud-gateway公式サイト
    例コード-Github
    参考:
    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/