简体   繁体   English

如何在基于Spring的微服务架构中正确处理服务间异常

[英]How to correctly handle inter-service exception in a spring-based microservices architecture

I have an application developed with a microservices architecture. 我有一个使用微服务架构开发的应用程序。 Each microservice is a spring-boot application that communicates with others via FeignClient interface. 每个微服务都是一个弹簧启动应用程序,可通过FeignClient接口与其他微服务进行通信。

Let A, a microservice (RestAPI) that calls microservice B. In normal conditions, B replies with an Object X, that is the JSON-response that A serves to client. 假设A是一个调用微服务B的微服务(RestAPI)。在正常情况下,B用对象X进行回复,这是A为客户端提供的JSON响应。

But, if B throws an exception, I obtain a chinese-box exception to the client like this: 但是,如果B抛出异常,我会像这样向客户端获取一个中文框异常:

{
    "timestamp": 1511965051071,
    "status": 500,
    "error": "Internal Server Error",
    "exception": "Exception",
    "message": { "\"timestamp\":1511965051052,\"status\":422,\"error\":\"Unprocessable Entity\",\"exception\":\"java.lang.MyException\",\"message\":\"Error message from B\",\"path\":\"PATH-OF-B-SERVICE\"}",
    "path": "PATH-OF-A-SERVICE"
}

In other words, MyException (status 422) is "embedded" in A Exception (status 500). 换句话说,MyException(状态422)被“嵌入”在A Exception(状态500)中。

I would like to reply the client with the inner JSON, that is: 我想用内部JSON回复客户端,即:

{
    "timestamp": 1511965051052,
    "status": 422,
    "error": "Unprocessable Entity",
    "exception": "java.lang.MyException",
    "message": "ErrormessagefromB",
    "path": "PATH-OF-B-SERVICE"
}

How can I do that? 我怎样才能做到这一点?

What about this solution based on the combination of a feign ErrorDecoder (extracting what you need from feign exception) and a spring @ExceptionHandler (dumps what you need in a ResponseEntity<Object> )? 基于ErrorDecoder (从@ExceptionHandler异常中提取您需要的内容)和spring @ExceptionHandler (在ResponseEntity<Object>转储您的所需内容)的组合, 该解决方案又如何呢?

I chose to translate the error payload to one that is generic to my API, but actually, you could keep the same. 我选择将错误有效载荷转换为我的API通用的错误有效载荷,但实际上,您可以保持不变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么要将基于邮件的服务间通信系统转换为基于消息的系统 - Why should you convert a mail based inter-service communication system to a Message based system EJB 中的 Facades 与基于 Spring 的 Web 应用程序中的服务是否相同 - Are Facades in EJB the same thing as service in a spring-based web application 基于模块化Spring的应用程序 - Modular Spring-based application 如何在基于Spring的Web应用程序中发现性能瓶颈 - How to find a performance bottleneck in a Spring-Based Web Application 如何使用 maven 创建基于 spring 的可执行文件 jar? - How to create spring-based executable jar with maven? 如何使用微服务架构处理共享数据源 - How to handle shared datasources using microservices architecture 由于配置错误,无法加载基于Spring的应用程序 - Spring-based Application not loading due to misconfiguration 在基于Spring的应用程序中不推荐使用AbstractExcelView - AbstractExcelView is deprecated in Spring-based application 如何将非Spring客户端与基于Spring的jar的配置脱钩? - How can I decouple a non-Spring client from the configuration of a Spring-based jar? 如何有效地处理Spring Boot微服务? - How to efficiently handle spring boot microservices?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM