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

后端 未结 2 689

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

    I couldn't get it to work with both @HystrixCommand and @Retryable, regardless of order of annotations on @Configuration class or on @Retryable method due to order of interceptors. I solved this by creating another class with the matching set of methods and had the @HystrixCommand annotated methods delegate to the corresponding @Retryable method in the second class. You could probably have the two classes implement the same interface. This is kind of a pain in the butt, but until order can be configured, this is all I could come up with. Still waiting on a real solution from Dave Syer and the spring cloud guys.

    public class HystrixWrapper {
        @Autowired
        private RetryableWrapper retryableWrapper;
    
        @HystrixCommand
        public Response doSomething(...) {
            return retryableWrapper.doSomething(...);
        }
    }
    
    public class RetryableWrapper {
        @Autowired
        private RestTemplate restTemplate;
    
        @Retryable
        public Response doSomething(...) {
            // do something with restTemplate;
        }
    }
    

提交回复
热议问题