default gateway设置,default gateway什么意思中文
一、背景在我们平时开发过程中,一般一个请求都是需要经过多个微服务的,比如:请求从A服务流过B服务,如果A服务请求过快,导致B服务响应慢,那么必然会导致系统出现问题。因为,我们就需要有限流操作。
二、实现功能提供自定义的限流键生成,需要实现按键解析器接口。提供默认的限流算法,实现实现速率限制器接口。当限流的键为空时,直接不限流,放行,由参数春天。云。网关。路线。过滤器。参数。拒绝空键来控制限流时返回客户端的相应码有春天。云。网关。路线。过滤器。参数。状态代码来控制,需要写这个org。spring框架。http。http状态类的枚举值请求速率限制器只能使用名称||参数这种方式来配置,不能使用简写的方式来配置。
请求速率限制器过滤器的存储利率限制器参数是在再贴现率限制器的配置属性名称属性配置的。构造方法中用到了。三、网关层限流限流的键生成规则,默认是PrincipalNameKeyResolver来实现限流算法,默认是再贴现率限制器来实现,是令牌桶算法。
1、使用默认的存储来限流在春云网关中默认提供了请求速率限制器过滤器来实现限流操作。
1.引入jar包
依赖项依赖项groupIdcom.alibaba.cloud/groupId artifactId spring-cloud-starter-Ali-nacos-discovery/artifactId/dependency依赖项groupIdorg.springframework.cloud/groupId artifact id spring-cloud-starter-gateway/artifact id/依赖关系依赖项groupIdorg.springframework.cloud/groupId artifactId spring-cloud-starter-负载平衡器/artifactId/依赖项依赖项groupIdorg.springframework.boot/groupId artifact id spring-boot-starter-data-redis-reactive/artifact id/dependency依赖项groupIdorg.projectlombok/groupId artifact id lombok/artifact id/dependency/dependencies2.编写配置文件
春天:应用程序:名称:网关-9205云:玉米片发现:服务器-地址:本地主机:8847网关:路由: - id:用户-提供者-9206 uri : lb ://用户-提供者-9206预测: -路径=/用户/*段/?*),$ \ { segment }-name :请求速率限制器参数: #如果
返回的key是空的话,则不进行限流 deny-empty-key: false # 每秒产生多少个令牌 redis-rate-limiter.replenishRate: 1 # 1秒内最大的令牌,即在1s内可以允许的突发流程,设置为0,表示阻止所有的请求 redis-rate-limiter.burstCapacity: 1 # 每次请求申请几个令牌 redis-rate-limiter.requestedTokens: 1 redis: host: 192.168.7.1 database: 12 port: 6379 password: 123456server: port: 9205debug: true3.网关正常响应
4.网关限流响应
2、自定义限流算法和限流key1.自定义限流key
编写一个类实现 KeyResolver 接口即可。
package com.huan.study.gateway;import lombok.extern.slf4j.Slf4j;import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;import org.springframework.cloud.gateway.route.Route;import org.springframework.cloud.gateway.support.ServerWebExchangeUtils;import org.springframework.http.server.reactive.ServerHttpRequest;import org.springframework.stereotype.Component;import org.springframework.web.server.ServerWebExchange;import reactor.core.publisher.Mono;import java.util.Optional;/** * 限流的key获取 * * @author huan.fu 2021/9/7 - 上午10:25 */@Slf4j@Componentpublic class DefaultGatewayKeyResolver implements KeyResolver { @Override public Mono<String> resolve(ServerWebExchange exchange) { // 获取当前路由 Route route = exchange.getAttribute(ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR); ServerHttpRequest request = exchange.getRequest(); String uri = request.getURI().getPath(); log.info("当前返回的uri:", uri); return Mono.just(Optional.ofNullable(route).map(Route::getId).orElse("") + "/" + uri); }}配置文件中的写法(部分)
spring: cloud: gateway: routes: - id: user-provider-9206 filters: - name: RequestRateLimiter args: # 返回限流的key key-resolver: "#{@defaultGatewayKeyResolver}"2.自定义限流算法
编写一个类实现 RateLimiter ,此处使用内存限流
package com.huan.study.gateway;import com.google.common.collect.Maps;import com.google.common.util.concurrent.RateLimiter;import lombok.Getter;import lombok.Setter;import lombok.ToString;import lombok.extern.slf4j.Slf4j;import org.springframework.cloud.gateway.filter.ratelimit.AbstractRateLimiter;import org.springframework.cloud.gateway.support.ConfigurationService;import org.springframework.context.annotation.Primary;import org.springframework.stereotype.Component;import reactor.core.publisher.Mono;/** * @author huan.fu 2021/9/7 - 上午10:36 */@Component@Slf4j@Primarypublic class DefaultGatewayRateLimiter extends AbstractRateLimiter<DefaultGatewayRateLimiter.Config> { /** * 和配置文件中的配置属性相对应 */ private static final String CONFIGURATION_PROPERTY_NAME = "default-gateway-rate-limiter"; private RateLimiter rateLimiter = RateLimiter.create(1); protected DefaultGatewayRateLimiter(ConfigurationService configurationService) { super(DefaultGatewayRateLimiter.Config.class, CONFIGURATION_PROPERTY_NAME, configurationService); } @Override public Mono<Response> isAllowed(String routeId, String id) { log.info("网关默认的限流 routeId:,id:", routeId, id); Config config = getConfig().get(routeId); return Mono.fromSupplier(() -> { boolean acquire = rateLimiter.tryAcquire(config.requestedTokens); if (acquire) { return new Response(true, Maps.newHashMap()); } else { return new Response(false, Maps.newHashMap()); } }); } @Getter @Setter @ToString public static class Config { /** * 每次请求多少个 token */ private Integer requestedTokens; }}配置文件中的写法(部分)
spring: cloud: gateway: routes: - id: user-provider-9206 filters: - name: RequestRateLimiter args: # 自定义限流规则 rate-limiter: "#{@defaultGatewayRateLimiter}"复制代码注意: 这个类需要加上 @Primary 注解。
3.配置文件中的写法
spring: application: name: gateway-9205 cloud: nacos: discovery: server-addr: localhost:8847 gateway: routes: - id: user-provider-9206 uri: lb://user-provider-9206 predicates: - Path=/user/** filters: - RewritePath=/user(?<segment>/?.*), $\{segment} - name: RequestRateLimiter args: # 自定义限流规则 rate-limiter: "#{@defaultGatewayRateLimiter}" # 返回限流的key key-resolver: "#{@defaultGatewayKeyResolver}" # 如果返回的key是空的话,则不进行限流 deny-empty-key: false # 限流后向客户端返回的响应码429,请求太多 status-code: TOO_MANY_REQUESTS # 每次请求申请几个令牌 default-gateway-rate-limiter 的值是在 defaultGatewayRateLimiter 中定义的。 default-gateway-rate-limiter.requestedTokens: 1server: port: 9205debug: true作者:huan1993
链接:https://juejin.cn/post/7005060165892309022
来源:掘金