Spring Cloud中Zuul服务器的回退方法 [英] Fallback methods at Zuul server in Spring cloud

查看:583
本文介绍了Spring Cloud中Zuul服务器的回退方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Spring Cloud Netflix的新手,对此有一些疑问.
我将API网关与在Zuul边缘服务器上运行的Hystrix一起使用,我想确保我是否正确理解以下行为:

I am new in Spring Cloud Netflix and have some questions concerning it.
I am using the API Gateway together with Hystrix running at Zuul edge server and I would like to make sure if I understand the following behavior correctly:

我杀死了一个在"API网关"后面运行的微服务,然后强制hystrix打开电路.在那之后(大约10秒钟),我向未运行的微服务发送了一个请求,并收到TIMEOUT错误.但是,当我在很短的时间内(最多1秒)发送许多(数十个)请求时,我会收到SHORTCIRCUIUI OPEN错误.
我很惊讶为什么在电路断开的情况下仍然收到超时错误,因此应使用hystrix来避免此类故障.但是我猜想原因是,如果自上次请求以来的时间> circuitBreaker.sleepWindowInMilliseconds,则API网关会尝试再次连接到微服务以检查其是否仍然存在,但不是,因此->超时.这也可以解释为什么我在短时间内对许多请求都出现SHORTCIRCUIUI OPEN错误.

I kill a microservice running "behind" API Gateway then I force hystrix to open the circuit. After that (ca 10 seconds) I send a request to the non-running microservice and receive TIMEOUT error. However when I send many (tens) of requests in a very short time (up to 1 sec), I receive SHORTCIRCUIT OPEN error.
I was surprised why I receive TIMEOUT error although the circuit is open and therefore hystrix should come to play to avoid such kind of failures. But I guess the reason is that if time since last request > circuitBreaker.sleepWindowInMilliseconds, then the API Gateway tries to connect to the microservice again to check if it's alive, but it wasn't, thus -> TIMEOUT. That would also explain, why I got SHORTCIRCUIT OPEN error on many requests in short time.

接下来的事情是,当我收到"TIMEOUT"或"SHORTCIRCUIT"错误时,我得到了:

The next thing is that as I receive "TIMEOUT" or "SHORTCIRCUIT" error I got:

HystrixRuntimeException: Microservice (timed-out | short-circuited) and no fallback available

如何在zuul服务器上指定一个后备(所有路由都可以相同)以避免这种异常?
到现在为止我一直在尝试:

How can I specify a fallback at zuul server (this can be the same for all routes) to avoid this exception ?
What I have tried until now:

  • 根据设置执行隔离穿线策略 为了避免超时:

  • According this to I set execution isolation strategy to THREAD to avoid time-out:

hystrix: command.default.execution.isolation.strategy:螺纹

hystrix: command.default.execution.isolation.strategy: THREAD

但是在查看hystrix.stream之后,我得到了propertyValue_executionIsolationStrategy":"SEMAPHORE"

but after looking at the hystrix.stream I got propertyValue_executionIsolationStrategy":"SEMAPHORE"

  • github 上似乎有解决方案的暗示通过编写自定义的RibbonCommand并覆盖getFallback(),但在查看
  • There seems to be a hint for solution on github by writing a custom RibbonCommand and overriding getFallback(), but after looking at RibbonCommand interface. I found out, that neither RibbonCommand nor its super interfaces doesn't define such a method.

我的API网关服务:

@SpringBootApplication
@EnableSidecar // This annotation includes @EnableCircuitBreaker, @EnableDiscoveryClient, and @EnableZuulProxy
@EnableHystrixDashboard
public class ApiGatewayApplication {

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

}

application.yml

application.yml

server:
  port: 10000
sidecar:
  port: 8000
endpoints:
  restart:
    enabled: true
  shutdown:
    enabled: true
  health:
    sensitive: false
eureka:
  client:
    registerWithEureka: true
    fetchRegistry: true
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
zuul:
  routes:
    microservice:
      path: /microservice/**

hystrix:
  command.default.execution.isolation.strategy: THREAD

debug: true

推荐答案

最新版本在Zuul级别支持回退.这是通过扩展ZuulFallbackProvider来完成的.请访问以下链接获取示例: https://github.com/spring-cloud-samples/zuul-server/blob/master/src/main/java/zuulserver/ZuulServerApplication.java

The latest release supports fallback at Zuul level. It is done by extending the ZuulFallbackProvider. Please visit the below link for an example: https://github.com/spring-cloud-samples/zuul-server/blob/master/src/main/java/zuulserver/ZuulServerApplication.java

对于某些类型的路由配置(例如,如果您具有基于url的配置),回退触发本身不会完成

The fallback triggering itself is not done for certain types of route configurations (for eg if you have a url based config)

这些简单的url路由不会作为HystrixCommand执行,也无法通过Ribbon来平衡多个URL."

"These simple url-routes don’t get executed as a HystrixCommand nor can you loadbalance multiple URLs with Ribbon."

这篇关于Spring Cloud中Zuul服务器的回退方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆