Fallback methods at Zuul server in Spring cloud

谁说胖子不能爱 提交于 2019-12-24 10:07:24

问题


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:

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.

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

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

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: THREAD

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

  • 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.

My API Gateway service:

@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:


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

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

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



来源:https://stackoverflow.com/questions/37748378/fallback-methods-at-zuul-server-in-spring-cloud

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!