SpringCloud:Spring Cloud GatewayでRedisRateLimiterを構成する方法

4529 ワード

目次
一、Redisのインストールと構成
二、Mavenの配置
三、アプリケーション.propertiesファイルの構成
四、Javaコード実現
一、Redisのインストールと構成
1、ダウンロードアドレス:
Linux版:https://redis.io/download
Windows版:https://github.com/microsoftarchive/redis/releases
2、配置
Linux版では、ドキュメントを表示できます.https://redis.io/documentation
Windows版では、redis.windows-service.confファイルを変更し、パスワードを設定します.
requirepass 12345678
Redisサービスは、サービス内で検索できます.ファイルのパスを実行できます.
"C:\Program Files\Redis\redis-server.exe"--service-run "C:\Program Files\Redis\redis.windows-service.conf"
このサービスを開始します.
二、Mavenの配置
    UTF-8     UTF-8     1.8     2.2.1.RELEASE
    org.springframework.boot     spring-boot-starter-parent     2.2.1.RELEASE     
             org.springframework.boot         spring-boot-starter                   org.springframework.boot         spring-boot-starter-webflux                   org.springframework.boot         spring-boot-starter-actuator                   org.springframework.boot         spring-boot-starter-data-redis-reactive     
             org.springframework.cloud         spring-cloud-starter         ${spring-cloud.version}                   org.springframework.cloud         spring-cloud-starter-gateway         ${spring-cloud.version}                   org.springframework.cloud         spring-cloud-starter-netflix-ribbon         ${spring-cloud.version}     
Spring-boot-starter-data-redis-reactiveはSpringBootが使用するRedis関連依存である.
三、アプリケーション.propertiesファイルの構成
server.port=8080 spring.application.name=gateway_server
# redisspring.redis.password=12345678
 # default true management.endpoint.gateway.enabled=true management.endpoints.web.exposure.include=*
logging.level.org.springframework.cloud.gateway=TRACE
 # default true ribbon.eureka.enabled=false
 # default true spring.cloud.loadbalancer.ribbon.enabled=true  # default false spring.cloud.gateway.actuator.verbose.enabled=true
######################################################################## spring.cloud.gateway.routes[0].id=my_route spring.cloud.gateway.routes[0].uri=lb://my-load-balanced-service spring.cloud.gateway.routes[0].predicates[0]=Path=/test/**spring.cloud.gateway.routes[0].filters[0].name=RequestRateLimiter spring.cloud.gateway.routes[0].filters[0].args[redis-rate-limiter.replenishRate]=1 spring.cloud.gateway.routes[0].filters[0].args[redis-rate-limiter.burstCapacity]=2 spring.cloud.gateway.routes[0].filters[0].args[key-resolver]=#{@userkeyResolver}
my-load-balanced-service.ribbon.listOfServers=localhost:9090, 127.0.0.1:9090 my-load-balanced-service.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RoundRobinRule
Spring.redis.password:Redisログインパスワードを設定します.redis-rate-limiter.replenishRate:同じkeyに対して、放棄されたリクエストを含まない1秒当たりのリクエスト数を許可します.これは実際にトークンバケツの充填率です.redis-rate-limiter.burstCapacity:同じkeyに対して、1秒以内に許可された最大リクエスト数です.これは実際にトークンバケツが収容できる最大トークン数です.0に設定するとを選択すると、すべてのリクエストが拒否されます.
以上の2つのパラメータは同じkeyに基づいており、このkeyがどのように解析されるかは、key-resolverというパラメータを使用する必要があります.userkeyResolverという名前のBeanを検索します.
ここで、Redisの関連構成パラメータは以下のように参照されます.https://docs.spring.io/spring-boot/docs/2.2.4.RELEASE/reference/html/appendix-application-properties.html#common-application-properties
RedisRateLimiterの構成ドキュメントを参照してください.https://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.2.1.RELEASE/reference/html/#the-requestratelimiter-gatewayfilter-factory
四、Javaコード実現
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.server.ServerWebExchange;

import reactor.core.publisher.Mono;

@Configuration
@SpringBootApplication
public class AppBoot {

	@Bean
	public KeyResolver userkeyResolver() {
		return exchange -> Mono.just(exchange.getRequest().getRemoteAddress().getAddress().getHostAddress());
	}

	public static void main(String[] args) {
		SpringApplication.run(AppBoot.class, args);
	}
}

上記のコードにより、userkeyResolverという名前のKeyResolverタイプのBeanが生成されます.このBeanはHTTPリクエストのIPを解析し、このIPはKeyとして使用されます.もちろん、このKeyを別のルールで生成し、RedisRateLimiterに提供して使用することもできます.
このようにアクセスすることでhttp://127.0.0.1:8080/test/testApp/TestServletこのアドレスは、Redisを使用してストリーム制限管理を行うことができます.