Netflix Feign - Propagate Status and Exception through Microservices

后端 未结 6 792
予麋鹿
予麋鹿 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:21

    What we do is as follows:

    Share common jar which contains exceptions with both microservices.

    1.) In microservices A convert exception to a DTO class lets say ErrorInfo. Which will contain all the attributes of your custom exception with a String exceptionType, which will contain exception class name.

    2.) When it is received at microservice B it will be handled by ErrorDecoder in microservice B and It will try to create an exception object from exceptionType as below:

    @Override
    public Exception decode(String methodKey, Response response) {       
    
    ErrorInfo errorInfo = objectMapper.readValue(details, ErrorInfo.class);
    Class exceptionClass;
    
    Exception decodedException;
    
    try {
    
        exceptionClass = Class.forName(errorInfo.getExceptionType());  
    
        decodedException = (Exception) exceptionClass.newInstance();
    
        return decodedException;
    
     }
    
     catch (ClassNotFoundException e) {
    
        return new PlatformExecutionException(details, errorInfo);
    
     }
      return defaultErrorDecoder.decode(methodKey, response);
     }
    

提交回复
热议问题