Spring云中Zuul服务器的回退方法

时间:2016-06-10 12:31:46

标签: java spring netflix-zuul hystrix spring-cloud-netflix

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

我杀了一个运行的微服务"背后"然后我强制hystrix打开电路。之后(大约10秒)我向非运行的微服务发送请求并收到TIMEOUT错误。但是,当我在很短的时间内(最多1秒)发送许多(数十个)请求时,我收到SHORTCIRCUIT OPEN错误。
我很惊讶为什么我收到TIMEOUT错误,虽然电路是开路的,因此应该发挥hystrix来避免这种故障。但我想原因是,如果自上次请求以来的时间> circuitBreaker.sleepWindowInMilliseconds,然后API网关再次尝试连接到微服务以检查它是否存活,但它不是,因此 - >超时。这也可以解释为什么我在短时间内在许多请求上出现SHORTCIRCUIT OPEN错误。

接下来就是我收到" TIMEOUT"或" SHORTCIRCUIT"我得到的错误:

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

如何在zuul服务器上指定回退(这可以对所有路由都相同)以避免此异常?
我到现在为止所尝试的:

  • 根据this我将执行隔离策略设置为THREAD 避免超时:

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

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

  • 通过编写自定义RibbonCommand并覆盖getFallback(),但在查看github界面后,似乎有一个关于RibbonCommand解决方案的提示。我发现,RibbonCommand及其超级接口都没有定义这样的方法。

我的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

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

1 个答案:

答案 0 :(得分:1)

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

对于某些类型的路由配置(例如,如果您有基于网址的配置),不执行回退触发本身

"这些简单的url-routes不能作为HystrixCommand执行,也不能用Ribbon对多个URL进行负载均衡。"