该片 是带你 了解限流 简单的配置,避免初学者弄的 云里雾里,
前提: 你的gateway已搭建好了,可以运行基本路由
限流 加入jar , 如下包括 熔断 和 限流,可以一次加上
org.springframework.cloud
spring-cloud-starter-netflix-hystrix
org.springframework.boot
spring-boot-starter-webflux
org.springframework.boot
spring-boot-starter-data-redis-reactive
org.springframework.boot
spring-boot-starter-actuator
com.github.vladimir-bukhtoyarov
bucket4j-core
4.0.0
2 在yml配置,根据你的具体配置,如下配置 是只做说明
routes:
#netty 路由过滤器,http或https开头
- id: login-route
uri: lb://springboot-dubbo-order
predicates:
- Path=/sys/**
filters:
#转发请求时去掉1级前缀 转发时剥离路径个数
- StripPrefix=0
- name: RequestRateLimiter #固定的,如果自定义限流这个可以变
args:
name: fallbackcmd #这是熔断 可以暂时不要
fallbackUri: forward:/fallback #这是熔断 可以暂时不要
key-resolver: '#{@hostAddrKeyResolver}' #hostAddrKeyResolver是具体bean
redis-rate-limiter.replenishRate: 1 #令牌桶每秒填充平均速率
redis-rate-limiter.burstCapacity: 3 #令牌桶总容量
//如上配置 表示该url 限制用户 一秒访只能访问三次
3 然后在配置里面 或者 主程序里面 配置 hostAddrKeyResolver bean
@Bean
public KeyResolver hostAddrKeyResolver() {
return exchange -> Mono.just(exchange.getRequest().getRemoteAddress().getHostName());
}
//bean 表示 如上方法 是通过什么方式 拿到访问用户信息