网站链接云数据库,展示网站动画怎么做的,网址ip域名,管理外贸网站模板其实网关是很强大#xff0c;能做的事情很多#xff0c;包含很多过滤器包括限流#xff0c;具体的网关可以参考我的另外一篇博文Spring Cloud GateWay-过滤器
今天我们来说下网关如何限流#xff0c;主要两种方案#xff1a;
Spring Cloud GateWay整合hystrx
environme…其实网关是很强大能做的事情很多包含很多过滤器包括限流具体的网关可以参考我的另外一篇博文Spring Cloud GateWay-过滤器
今天我们来说下网关如何限流主要两种方案
Spring Cloud GateWay整合hystrx
environment: test
management:security:enabled: false
spring:jackson:date-format: yyyy-MM-dd HH:mm:ssdefault-property-inclusion: NON_NULLtime-zone: GMT8servlet:multipart:file-size-threshold: 100MBmax-file-size: 4GBmax-request-size: 4GBthymeleaf:cache: falsemode: LEGACYHTML5cloud:# 服务发现nacos:discovery:server-addr: nacos-headless.ota.svc.cluster.local:8848 #服务端urlnamespace: ota-prdgateway:discovery:locator:enabled: trueenabled: trueroutes:# 公共服务 基础数据 base服务- id: vota-api-coreuri: lb://vota-api-corepredicates:- Path/mgapi/vota-api-core/**filters:- name: Hystrixargs:name: vota-api-corefallbackUri: forward:/fallback- name: StripPrefixargs:parts: 2- name: SessionAccessargs:ignorePath:- /mgapi- /static- /client-api- /busi- /zuulvota-api-core:ribbon:ConnectTimeout: 500MaxAutoRetries: 0MaxAutoRetriesNextServer: 1OkToRetryOnAllOperations: trueReadTimeout: 3600000hystrix:command:vota-api-core:execution:isolation:thread:timeoutInMilliseconds: 2401000上面的代码块通过熔断器这种超时实现其实上面这样的配置是不会生效的通过配置文件其实想要通过hystrx的线程隔离模式但是呢这样的配置不会生效的这个地方很多人都会陷入误区首先呢hystrx在springcloud中要不被feign整合要不被gateway整合前者默认是线程池后置默认是信号量具体两者的区别信号量是单线程不会涉及到header上下文传递但是呢性能不好线程池呢会涉及到上下文传递问题这个地方我就不会扩散了 线程池资源隔离模式
hystrix:command:vota-api-core:execution:isolation:strategy: THREADtimeoutInMilliseconds: 2401000这样明确指定了隔离模式才行否则还是走的默认的而且只有线程池资源隔离才会有超时概念否则信号量资源隔离是没有的 只有并发数的配置 信号量资源隔离模式
hystrix:command:vota-api-core:execution:isolation:strategy: SEMAPHOREsemaphore:maxConcurrentRequests: 10timeoutInMilliseconds: 2401000 这里默认是10可以调整的50/100上面这两种配置都是整合hystrx的但是目前hystrx是Netflix旗下的组建以及不维护了现在启用的都是CircuitBreaker spring:application:name: vota-api-gatewaycloud:gateway:discovery:locator:# 表明 Gateway 开启服务注册和发现的功能并且 Spring Cloud Gateway 自动根据服务发现为每一个服务创建了一个 router这个 router 将以服务名开头的请求路径转发到对应的服务enabled: true# 是将请求路径上的服务名配置为小写因为服务注册的时候向注册中心注册时服务名被转成大写了)lower-case-service-id: trueroutes:- id: vota-api-proxyuri: lb://vota-api-core/v50/vehicle/api/predicates:# - Path/v50/vehicle/api/register,/v50/vehicle/api/{segment}/current/time- Path/v50/vehicle/api/**- MethodPOSTfilters:- name: CircuitBreakerargs:name: fallbackCmdAfallbackUri: forward:/fallbackA- name: RequestRateLimiterargs:# 解析限流 Key 的 Bean 对象的名字使用 SpEL 表达式key-resolver: #{vehicleKeyResolver}# 限流实现rate-limiter: #{redisRateLimiter}# 令牌桶每秒填充平均速率redis-rate-limiter.replenishRate: 2000# 令牌桶总容量redis-rate-limiter.burstCapacity: 5000public static void main(String[] args) {SpringApplication.run(VehicleGatewayApplication.class, args);}/*** p限流和防重放的区别分别的定义/p* p令牌桶限流/p* p注册接口使用 REGISTER vin 作为 Key/p* p非注册接口使用 API_NAME deviceId 作为 Key/p* p令牌桶算法基于 Lua Redis 实现Lua 脚本位置为spring-cloud-gateway-core-2.1.5.RELEASE* .jar/META-INF/scripts/request_rate_limiter.lua/p* p如需自定义算法则继承 AbstractRateLimiter 类/p** return KeyResolver* see org.springframework.cloud.gateway.filter.ratelimit.RedisRateLimiter* see org.springframework.cloud.gateway.filter.factory.RequestRateLimiterGatewayFilterFactory*/Beanpublic KeyResolver vehicleKeyResolver() {return exchange - {String value exchange.getRequest().getPath().value();return Mono.just(exchange.getRequest().getPath().value());};}