I am learning micro service architecture and have a question about proper communication between services.
I have a main service that makes an HttpClient
request to a specific micro service, which in turn performs certain business logic. After executing the business logic, the micro service returns a response to the main service, which provides a response to the user.
Question: what is the correct response that a micro service should produce? Necessary object/objects, or something like ResponseDto
with a response body?
The problem is that if an error occurs in the micro service, details of error not transmitted to the main service, but remains at the micro service level (basic asp mechanism returns some kind of error, but it’s not good enough for handling).
Code part – main controller:
[HttpGet("GetBook/{bookId:int}")]
public IActionResult GetBook(int bookId)
{
var jwtModel = HttpContext.GetJwtModel();
var response = _bookService.GetBook(bookId, jwtModel);
return Ok(response);
}
Main service (which makes a request to a specific controller and receives a response)
public BookDto GetBook(int bookId, JwtModel jwtModel)
{
var requestUri = $"{ServiceUrlVariables.BookApi}/GetBook/{bookId}";
var response = ResponseHelper.GetResponse(_httpClientFactory, requestUri, jwtModel);
var result = ResponseHelper.DeserializeResponse<BookDto>(response);
return result;
}
Micro service controller
[HttpGet("GetBook/{bookId:int}")]
[ProducesResponseType(StatusCodes.Status200OK)]
public IActionResult GetBook(int bookId)
{
_bookIdValidator.ValidateAndThrow(bookId);
var book = _bookService.GetBook(bookId);
_logger.LogInformation("Book with ID {@Id} outputted successfully", bookId);
return Ok(book);
}
Microservice service
public BookDto GetBook(int bookId)
{
var book = _bookAccessor.GetBook(bookId);
var bookDto = _mapper.Map<BookDto>(book);
return bookDto;
}
I’m newbie and It’s a bit hard for me to understand this things for now