Netflix Feign - Propagate Status and Exception through Microservices

后端 未结 6 794
予麋鹿
予麋鹿 2021-02-05 05:04

I\'m using Netflix Feign to call to one operation of a Microservice A to other other operation of a Microservice B which validates a code using Spring Boot.

The operation

6条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-05 05:11

    You could use a feign ErrorDecoder

    https://github.com/OpenFeign/feign/wiki/Custom-error-handling

    Here is an example

    public class MyErrorDecoder implements ErrorDecoder {
    
        private final ErrorDecoder defaultErrorDecoder = new Default();
    
        @Override
        public Exception decode(String methodKey, Response response) {
            if (response.status() >= 400 && response.status() <= 499) {
                return new MyBadRequestException();
            }
            return defaultErrorDecoder.decode(methodKey, response);
        }
    
    }
    

    For spring to pick up the ErrorDecoder you have to put it on the ApplicationContext:

    @Bean
    public MyErrorDecoder myErrorDecoder() {
      return new MyErrorDecoder();
    }
    

提交回复
热议问题