栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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后即为设置动态路由的熔断规则,对应上述静态配置。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/336855.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号