(38)java版spring cloud+spring bootソーシャル電子商取引プラットフォーム-gateway限流
2892 ワード
電子商取引のプラットフォームのソースコードはペンギンをプラスして求めてください:一零三八七四六二六.Spring Cloud Gatewayには、Filterフィルタがあるので、「pre」タイプのFilterで上記3種類のフィルタを自分で実現することができます.しかし、限流はゲートウェイの最も基本的な機能として、Spring Cloud Gateway公式はRequestRateLimiterGatewayFilterFactoryというクラスを提供し、Redisとluaスクリプトを適用してトークンバケツの方式を実現した.
具体的なソースコードはここで説明するつもりはありません.読者は自分で見ることができます.コード量が少ないので、Spring Cloud Gatewayに内蔵されたストリーム制限フィルタ工場を使用してストリーム制限を実現する方法を例として説明します.
まず、gatewayの開始依存とredisのreactive依存を、エンジニアリングpomファイルに導入します.コードは次のとおりです.
プロファイルで次の設定を行います.
上記のプロファイルでは、指定されたプログラムのポートが8081で、redisの情報が構成され、RequestRateLimiterのストリーム制限フィルタが構成されています.このフィルタには3つのパラメータが必要です.
burstCapacity、トークンバケツの総容量.
replenishRate、トークンバケツ毎秒充填平均速度.
key-resolverは、ストリームを制限するキーの解析器に使用されるBeanオブジェクトの名前です.SpEL式を使用して、#{@beanName}に従ってSpringコンテナからBeanオブジェクトを取得します.
KeyResolverはresolveメソッドを実装する必要があり,たとえばHostnameによるストリーム制限はhostAddressで判断する必要がある.KeyResolverを実装した後、このクラスのBeanをIocコンテナに登録する必要があります.
uriに従ってストリームを制限できます.この場合、KeyResolverコードは次のようになります.
ユーザーの次元でストリームを制限することもできます.
jmeterで圧力測定を行い、10 threadデサイクル要求lcoalhost:8081、サイクル間隔1 sを構成する.圧力測定の結果,一部の要求が通過し,一部の要求が失敗したことが分かった.
具体的なソースコードはここで説明するつもりはありません.読者は自分で見ることができます.コード量が少ないので、Spring Cloud Gatewayに内蔵されたストリーム制限フィルタ工場を使用してストリーム制限を実現する方法を例として説明します.
まず、gatewayの開始依存とredisのreactive依存を、エンジニアリングpomファイルに導入します.コードは次のとおりです.
org.springframework.cloud
spring-cloud-starter-gateway
org.springframework.boot
spring-boot-starter-data-redis-reactive
プロファイルで次の設定を行います.
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
上記のプロファイルでは、指定されたプログラムのポートが8081で、redisの情報が構成され、RequestRateLimiterのストリーム制限フィルタが構成されています.このフィルタには3つのパラメータが必要です.
burstCapacity、トークンバケツの総容量.
replenishRate、トークンバケツ毎秒充填平均速度.
key-resolverは、ストリームを制限するキーの解析器に使用されるBeanオブジェクトの名前です.SpEL式を使用して、#{@beanName}に従ってSpringコンテナからBeanオブジェクトを取得します.
KeyResolverはresolveメソッドを実装する必要があり,たとえばHostnameによるストリーム制限はhostAddressで判断する必要がある.KeyResolverを実装した後、このクラスのBeanをIocコンテナに登録する必要があります.
public class HostAddrKeyResolver implements KeyResolver {
@Override
public Mono resolve(ServerWebExchange exchange) {
return Mono.just(exchange.getRequest().getRemoteAddress().getAddress().getHostAddress());
}
}
@Bean
public HostAddrKeyResolver hostAddrKeyResolver() {
return new HostAddrKeyResolver();
}
uriに従ってストリームを制限できます.この場合、KeyResolverコードは次のようになります.
public class UriKeyResolver implements KeyResolver {
@Override
public Mono resolve(ServerWebExchange exchange) {
return Mono.just(exchange.getRequest().getURI().getPath());
}
}
@Bean
public UriKeyResolver uriKeyResolver() {
return new UriKeyResolver();
}
ユーザーの次元でストリームを制限することもできます.
@Bean
KeyResolver userKeyResolver() {
return exchange -> Mono.just(exchange.getRequest().getQueryParams().getFirst("user"));
}
jmeterで圧力測定を行い、10 threadデサイクル要求lcoalhost:8081、サイクル間隔1 sを構成する.圧力測定の結果,一部の要求が通過し,一部の要求が失敗したことが分かった.