Feign Client Error Handling

前端 未结 2 1189
生来不讨喜
生来不讨喜 2021-01-20 04:18

I am using Feign Client,

I have a Location service. So I created a client for my LocationService using FeignClient.

@FeignClient(url=\"http://localho         


        
相关标签:
2条回答
  • 2021-01-20 04:55

    You could use a feign ErrorDecoder for exception handling. Below is the url for your reference.

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

    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);
        }
    
    }
    

    To get this ErrorDecoder you need create a bean for it as below :

    @Bean
    public MyErrorDecoder myErrorDecoder() {
      return new MyErrorDecoder();
    }
    
    0 讨论(0)
  • 2021-01-20 05:01

    You can define a fallback client that is called when an exception like timeout or connection refused comes up:

    @FeignClient(url="http://localhost:9003/location/v1", name="location-service", fallback=LocationFallbackClient.class)
    public interface LocationControllerVOneClient {
        ...
    }
    

    LocationFallbackClient must implement LocationControllerVOneClient.

    0 讨论(0)
提交回复
热议问题