簡體   English   中英

錯誤請求的響應時間慢(服務異常) spring 使用 zuul 啟動

[英]Slow response time for Bad requests (Service exceptions) spring boot with zuul

我有 spring 使用 zuul 網關和 Eureka 部署的啟動微服務,它工作正常,但我注意到當請求發出錯誤請求時,這實際上從服務 class(無效的用戶/客戶 ID)拋出異常,相關的錯誤響應采取超過 2 秒。 成功場景運行良好,響應時間可接受。 正如您在下面的日志中看到的那樣,錯誤花費了更多時間。 這些服務托管在 AWS 中,但在沒有 zuul 集成的本地環境中運行良好。

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

我也嘗試繞過zuul直接發送請求,但在這里我也遇到了延遲。 請查看此內容,如果您需要有關此內容的更多信息,請告訴我。

更新

我注意到這種情況會間歇性地發生,例如第一次調用和第二次調用需要 2 秒以上,然后接下來的 4 到 5 個請求需要正常時間,然后再一次請求需要更多時間。 但是在本地環境中,這種行為是不存在的。 (正如我之前提到的,這只發生在 ERROR 場景中)

代碼

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

    }

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

謝謝

我發現了問題,上述微服務的擴展是.war,我沒有注意到,將其轉換為jar后,延遲不再是他們的。 無論如何需要檢查為什么會發生這種情況 for.war 擴展。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM