I have spring boot micro-services deployed with zuul gateway and Eureka which is working fine, but I notice that when a request sends out bad request, which actually throw an exception from service class (Invalid User/Customer Id) the relevant error response took more than 2 Seconds. The success scenarios are working fine with acceptable response times. As you can see in the below log the ERROR took more time. These services are hosted in AWS, but it worked fine in the local environment without zuul integration.
2020-04-03 04:14:38.102 DEBUG 14156 --- [nio-8084-exec-9] c.t.book.filter.AuthorizationFilter : Auth UserId in filter: uid-b7e9d6f7-f5bf-4e74-9ac1-1d1120ceb43f
2020-04-03 04:14:38.103 DEBUG 14156 --- [nio-8084-exec-9] c.t.t.controller.TransactionController : Create transaction inputs: {"bookId":"bid-1","customerId":"cid-8","transactionType":"credit","amount":5000.0,"note":"date validation test","dueDate":1585026193934,"imageUrl":"http://123.com","customerBookType":null,"requiredAvailable":true}, timeZone: Asia/Colombo
2020-04-03 04:14:39.884 ERROR 14156 --- [nio-8084-exec-9] c.t.t.controller.TransactionController : Invalid customer id for the transaction: {"bookId":"bid-1","","transactionType":"credit","amount":5000.0,"note":"date validation test","dueDate":1585026193934,"imageUrl":"http://123.com","customerBookType":null,"requiredAvailable":true}
com.book.exception.InvalidUserException: Invalid customer id
Also I tried sending request directly bypassing the zuul, but here also I am getting the delay. Please look in to this, If you need more information on this, let me know.
Updated
I notice that this happens intermittently, like the first call and second call takes more than 2 seconds then following 4 to 5 requests take normal time, then again one request take more time. But in the local environment this behavior is not there. (As I mentioned earlier this happens only for ERROR scenarios)
Code
Service class
public Customer updateCustomer(CustomerUpdateRequestDto customerUpdateRequestDto) {
try {
String customerId = customerUpdateRequestDto.getCustomerId().trim();
Optional<Customer> customerOptional = customerRepository
.findById(customerId);
if (customerOptional.isPresent()) {
Customer customer = customerOptional.get();
customer.setDisplayName(customerUpdateRequestDto.getName().trim());
customer.setImageUrl(customerUpdateRequestDto.getImageUrl() == null ?
null : customerUpdateRequestDto.getImageUrl().trim());
customer.setMobileNo(customerUpdateRequestDto.getMobileNo().getDisplayNumber());
customer.setUpdatedAt(new Date());
customerRepository.save(customer);
return customer;
} else {
throw new InvalidUserException(INVALID_CUSTOMER_ID);
}
} catch (DataAccessException e) {
throw new BookException(FAILED_UPDATE_CUSTOMER, e);
}
}
Method call from the controller
private ResponseEntity<ResponseWrapper> updateCustomerIfValid(CustomerUpdateRequestDto customerUpdateRequestDto) {
try {
Customer customer = customerService.updateCustomer(customerUpdateRequestDto);
CustomerResponseDto customerResponseDto = new CustomerResponseDto(customer);
log.debug("Updating customer: {} was successful", customerResponseDto.toLogJson());
ResponseWrapper responseWrapper =
new ResponseWrapper(ResponseStatusType.SUCCESS, SUCCESS_MESSAGE_UPDATE, customerResponseDto);
return new ResponseEntity<>(responseWrapper, HttpStatus.OK);
} catch (InvalidUserException e) {
log.error("Updating customer was failed for customer: {}", customerUpdateRequestDto.toLogJson(), e);
return getBadRequestError(ErrorResponseStatusType.INVALID_CUSTOMER_ID);
}
}
protected ResponseEntity<ResponseWrapper> getBadRequestError(ErrorResponseStatusType errorResponseStatusType) {
ResponseWrapper responseWrapper =
new ErrorResponseWrapper(errorResponseStatusType, null);
return new ResponseEntity<>(responseWrapper, HttpStatus.BAD_REQUEST);
}
Thank you