springcloud zuul 网关限流

1.添加pom包

<dependency>
        <groupId>com.marcosbarbero.cloud</groupId>
        <artifactId>spring-cloud-zuul-ratelimit</artifactId>
        <version>2.0.6.RELEASE</version>
    </dependency>

2.启动类

@SpringBootApplication
@EnableDiscoveryClient
@EnableZuulProxy
public class ZuulServerApplication {

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

3.yml配置

##############################
# 网关配置
#############################
server:
  port: 8080
spring:
  application:
      name: cmtt-gateway
      desc: 系统中心配置
feign:
   hystrix:
      enabled: true
#feginClient 熔断超时配置
hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 60000
#ribbon超时和重试配置【重试次数=MaxAutoRetries +  MaxAutoRetriesNextServer + (MaxAutoRetries * MaxAutoRetriesNextServer)】
# 熔断时间与ribbon超时关系:
# 重试时间 =(MaxAutoRetries+MaxAutoRetriesNextServer+MaxAutoRetriesNextServer*MaxAutoRetries)*ReadTimeout+网络响应时间
# 重试时间 <timeoutInMilliseconds
ribbon:
  OkToRetryOnAllOperations: false   #是否对所有请求进行重试
  MaxAutoRetries: 0     #对当前实例的重试次数,默认0
  MaxAutoRetriesNextServer: 0 #对切换实例的重试次数,默认1
  ReadTimeout: 60000     #负载均衡超时时间,默认值5000
  ConnectTimeout: 60000  #ribbon请求连接的超时时间,默认值2000
zuul:
   host:
      socket-timeout-millis: 88000
      connect-timeout-millis: 88000
   routes:
      auth:
         path:  /biz/sys/**
         serviceId: cmtt-biz-sys
         sensitive-headers: Access-Control-Allow-Origin
         ignored-headers: Access-Control-Allow-Origin,H-APP-Id,Access-Token,APPToken
      pmis:
         path:  /biz/pmis/**
         serviceId: cmtt-biz-project
         sensitive-headers: Access-Control-Allow-Origin
         ignored-headers: Access-Control-Allow-Origin,H-APP-Id,Access-Token,APPToken
      fms:
         path: /biz/fms/**
         serviceId: cmtt-biz-fms
   ratelimit: 
        key-prefix: springcloud-book #按粒度拆分的临时变量key前缀
        enabled: true #启用开关
        repository: IN_MEMORY #key存储类型,默认是IN_MEMORY本地内存,此外还有多种形式
        behind-proxy: true #表示代理之后
        default-policy: #全局限流策略,可单独细化到服务粒度
          limit: 2 #在一个单位时间窗口的请求数量
          quota: 1 #在一个单位时间窗口的请求时间限制
          refresh-interval: 3 #单位时间窗口
          type: 
            - user #可指定用户粒度
            - origin #可指定客户端地址粒度
            - url #可指定url粒度

全局开启了限流,策略是,3秒内访问不允许超过 2 次,并且这 2 次请求要小于 1 秒。

下面是针对某个服务

##############################
# 网关配置
#############################
server:
  port: 8080
spring:
  application:
      name: cmtt-gateway
      desc: 系统中心配置
feign:
   hystrix:
      enabled: true
#feginClient 熔断超时配置
hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 60000
#ribbon超时和重试配置【重试次数=MaxAutoRetries +  MaxAutoRetriesNextServer + (MaxAutoRetries * MaxAutoRetriesNextServer)】
# 熔断时间与ribbon超时关系:
# 重试时间 =(MaxAutoRetries+MaxAutoRetriesNextServer+MaxAutoRetriesNextServer*MaxAutoRetries)*ReadTimeout+网络响应时间
# 重试时间 <timeoutInMilliseconds
ribbon:
  OkToRetryOnAllOperations: false   #是否对所有请求进行重试
  MaxAutoRetries: 0     #对当前实例的重试次数,默认0
  MaxAutoRetriesNextServer: 0 #对切换实例的重试次数,默认1
  ReadTimeout: 60000     #负载均衡超时时间,默认值5000
  ConnectTimeout: 60000  #ribbon请求连接的超时时间,默认值2000
zuul:
   host:
      socket-timeout-millis: 88000
      connect-timeout-millis: 88000
   routes:
      auth:
         path:  /biz/sys/**
         serviceId: cmtt-biz-sys
         sensitive-headers: Access-Control-Allow-Origin
         ignored-headers: Access-Control-Allow-Origin,H-APP-Id,Access-Token,APPToken
      pmis:
         path:  /biz/pmis/**
         serviceId: cmtt-biz-project
         sensitive-headers: Access-Control-Allow-Origin
         ignored-headers: Access-Control-Allow-Origin,H-APP-Id,Access-Token,APPToken
      fms:
         path: /biz/fms/**
         serviceId: cmtt-biz-fms
   ratelimit: 
        key-prefix: springcloud-book #按粒度拆分的临时变量key前缀
        enabled: true #启用开关
        repository: IN_MEMORY #key存储类型,默认是IN_MEMORY本地内存,此外还有多种形式
        behind-proxy: true #表示代理之后
        default-policy: #全局限流策略,可单独细化到服务粒度
          limit: 2 #在一个单位时间窗口的请求数量
          quota: 1 #在一个单位时间窗口的请求时间限制
          refresh-interval: 3 #单位时间窗口
          type: 
            - user #可指定用户粒度
            - origin #可指定客户端地址粒度
            - url #可指定url粒度

        policies:
          cmtt-biz-sys:
          limit: 5
          quota: 5
          efresh-interval: 10

policies 配置,含义是我们对 client-a 服务进行特殊限流配置,10秒内请求数量不得大于 5 次,这 5 次请求总时长不能大于 5秒,其他服务对限流策略还是按照 上面默认的,不冲突

最后启动测试,10秒内访问第六次就报错

posted @ 2020-09-12 19:48  散落人间  阅读(491)  评论(0编辑  收藏  举报
interface food{} class A implements food{} class B implements food{} class C implements food{} public class StaticFactory { private StaticFactory(){} public static food getA(){ return new A(); } public static food getB(){ return new B(); } public static food getC(){ return new C(); } } class Client{ //客户端代码只需要将相应的参数传入即可得到对象 //用户不需要了解工厂类内部的逻辑。 public void get(String name){ food x = null ; if ( name.equals("A")) { x = StaticFactory.getA(); }else if ( name.equals("B")){ x = StaticFactory.getB(); }else { x = StaticFactory.getC(); } } }