SpringCloud Gateway动态路由实现熔断规则

SpringCloud Gateway动态路由实现熔断规则

背景

当大量请求进入网关时发现有很多服务拥挤,异常的报错。检查后发现网关实现了动态路由,但是yaml中的限流配置并不对动态路由服务生效,配置如下:

hystrix:
  command:
    defalut:
      execution:
        isolation:
          strategy: THREAD
          thread:
            timeoutInMilliseconds: 6000
    fallbackcmd:
      execution:
        isolation:
          thread:
            ## 超时时间
            timeoutInMilliseconds: 20000

此部分配置仅对配置中指定的服务生效,并不对动态路由的服务生效。网上找了半天,没找到解决方法,于是做一下记录。

方案

SpringCloudGateway动态路由实现RouteLocator自定义路由转发器,完成容器注入的代码如下:

        builder.routes()
    		.route(r -> r.path(URL) .filters(f ->
            	f.filter(new CustomRouteLocatorRequestFilter())


                // 修改返回 reponse 内容返回体
                .modifyResponseBody(String.class, String.class,
                    (exchange, s) -> {
				自定义规则,此部分省略
				.....
                      
                    }).hystrix(config -> config.setName(CUSTOMROUTELOCATORID_HYSTRIX_NAME)
                    .setSetter(HystrixObservableCommand.Setter
                            .withGroupKey(HystrixCommandGroupKey.Factory.asKey("Group"))
                            .andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
                                    .withExecutionIsolationStrategy(HystrixCommandProperties.ExecutionIsolationStrategy.THREAD)
                                    .withExecutionIsolationThreadInterruptOnTimeout(true)
                                    .withExecutionTimeoutInMilliseconds(6000)
                                    .withFallbackEnabled(true)
                                    .withFallbackIsolationSemaphoreMaxConcurrentRequests(100)))
                .setFallbackUri(CUSTOMROUTELOCATORID_HYSTRIX_PATH))
        )
        // baseUrl
        .uri(shopexProperties.getUrl())
        .order(0)
        .id(CUSTOMROUTELOCATORID)
    )
    .build();

上述代码中,setSetter后即为设置动态路由的熔断规则,对应上述静态配置。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring Cloud Gateway可以通过集成熔断器来实现熔断功能。在Spring Cloud Gateway中,可以使用Hystrix或者Resilience4j作为熔断器。 使用Hystrix实现熔断的步骤如下: 1. 引入`spring-cloud-starter-netflix-hystrix`依赖。 2. 在启动类上添加`@EnableCircuitBreaker`注解开启熔断功能。 3. 在路由配置中,通过添加`hystrix`参数来开启熔断器。例如: ``` spring: cloud: gateway: routes: - id: my_route uri: http://example.org predicates: - Path=/foo/** filters: - RewritePath=/foo/(?<segment>.*), /$\{segment} - Hystrix=myHystrixCommand ``` 4. 在配置文件中配置Hystrix的相关属性,例如: ``` hystrix: command: myHystrixCommand: execution: isolation: thread: timeoutInMilliseconds: 5000 ``` 5. 自定义熔断处理逻辑,实现`fallback`方法,处理服务降级逻辑。 使用Resilience4j实现熔断的步骤如下: 1. 引入`resilience4j-spring-boot2`依赖。 2. 在配置文件中配置Resilience4j的相关属性,例如: ``` resilience4j: circuitbreaker: configs: myCircuitBreakerConfig: failureRateThreshold: 50 waitDurationInOpenState: 5000 ringBufferSizeInClosedState: 5 instances: myCircuitBreaker: baseConfig: myCircuitBreakerConfig ``` 3. 在路由配置中,通过添加`resilience4j`参数来开启熔断器。例如: ``` spring: cloud: gateway: routes: - id: my_route uri: http://example.org predicates: - Path=/foo/** filters: - RewritePath=/foo/(?<segment>.*), /$\{segment} - Resilience4j=myCircuitBreaker ``` 4. 自定义熔断处理逻辑,实现`fallback`方法,处理服务降级逻辑。 以上是Spring Cloud Gateway实现熔断的基本步骤,具体的配置和处理逻辑可以根据实际需求进行调整和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值