Offer to come, dig friends take it! I am participating in the 2022 Spring Recruit Punch card activity. Click here for details.

I. Problem Description:

If we have some interface on line to synchronize requests like:

1. Send emails in bulk

2. Perform tens of thousands of data processing at a time

3. Call the payment interface synchronously

The following exception may occur if we have the above operations

There are actually two solutions:

  1. Because the called takes a long time to process, the caller can choose to increase the wait time.
  2. Modify the processing logic of the called party to optimize the design.
  3. Modify the processing logic of call release, modify direct call to implicit call.

If the problem is not spreading, I want to be a salt fish, to ensure that the service does not report errors, we can directly change the feign wait time

Ii. Solutions:

In the solution section, we will first post some existing code and logic in response to the current scenario.

Part of the code

1. Feign interface is defined as follows:

@FeignClient(name = "payment-service", path = "/payment")
public interface PaymentFeign {
​
    @PostMapping("/create")
    PaymentVo create(@RequestBody @Validated PaymentDto paymentDto);
}
Copy the code

2. The call code is as follows:

@Autowired private PaymentFeign paymentFeign; @GetMapping("/create") public OrderVo create(@Validated OrderDto orderDto) { log.info("uri:/order/create lang:{}", LocaleContextHolder.getLocale().getLanguage()); // Ignore line N of logical code paymentfeign.create (paymentDto); log.info("uri:/order/create paymentFegin.create cost: {} ms", System.currentTimeMillis() - start); return orderVo; }Copy the code

3. Feign timeout configuration

Feign: client: config: default: # Log level loggerLevel: full # Timeout setting 1.5 seconds timeout connectTimeout: 1500 readTimeout: 1500 # Circuitbreaker: Enabled: TrueCopy the code

To sum up the code, I called /payment/create in XXX /create and I set the feign timeout to 1.5 seconds. But because the /payment/create call timed out, TimeOut is returned.

The solution

Solution a:

Increased the default timeout of feign to 5s

Feign: client: config: default: # Log level loggerLevel: full # Timeout setting 5 seconds timeout connectTimeout: 5000 readTimeout: 5000 # Circuitbreaker: Enabled: TrueCopy the code

Scheme 2:

Specifies the contextId of the FEIGN interface to set the current FEIGN timeout to 5s.

Feign: client: config: default: # Log level loggerLevel: full # Timeout setting connectTimeout: 1500 readTimeout: 1500 Payment-core: ConnectTimeout: 5000 readTimeout: 5000 # circuitbreaker circuitbreaker: enabled: trueCopy the code

Sets the contextId property on PaymentFeign

@FeignClient(name = "payment-service", contextId = "payment-core",  path = "/payment")
public interface PaymentFeign {
​
    @PostMapping("/create")
    PaymentVo create(@RequestBody @Validated PaymentDto paymentDto);
}
Copy the code

In doing so, we can reduce the impact, reduce the downstream impact.

Iii. Summary:

Reference address: Spring-cloud-OpenFeign