spring-cloud with RestTemplate//Ribbon/Eureka - retry when server not available

后端 未结 2 690

I managed to successfully get my RestTemplate client discover remote service using Eureka and forward calls to it using Ribbon as described in the documentation. Basically,

2条回答
  •  醉酒成梦
    2020-12-11 10:48

    The RestTemplate support on its own does not know how to do any retrying (whereas the Feign client and the proxy support in Spring Cloud does, as you noticed). I think this is probably a good things because it gives you the option to add it yourself. For instance, using Spring Retry you can do it in a simple declarative style:

    @Retryable
    public Object doSomething() {
       // use your RestTemplate here
    }
    

    (and add @EnableRetry to your @Configuration). It makes a nice combination with @HystrixCommand (from Spring Cloud / Javanica):

    @HystrixCommand
    @Retryable
    public Object doSomething() {
       // use your RestTemplate here
    }
    

    In this form, every failure counts towards the circuit breaker metrics (maybe we could change that, or maybe it makes sense to leave it like that), even if the retry is successful.

提交回复
热议问题