Offer arrives, dig friends to pick up! I am participating in the 2022 Spring Recruitment Check-In Event, click to view the event details .
1. Problem description:
If we have some synchronous request interfaces on the line such as:
1. Bulk email
2. Execute tens of thousands of data processing at a time
3. Synchronously call the payment interface
If we have the above operation, the following exception may occur
Actually there are two solutions here:
- Since the processing time of the callee is relatively long, the caller can choose to increase the waiting time .
- Modify the processing logic of the callee and optimize the design.
- Modify the processing logic of the call, modify the direct call to an implicit call.
The problem is not divergent, I just want to make a salted fish, to ensure that the service does not report errors, we can directly change the waiting time of feign
Second, the solution:
In the solution part, we still post some of the existing code and logic to respond to the current scene.
part of the code
1. Feign
The interface is defined as follows:
@FeignClient(name = "payment-service", path = "/payment")
public interface PaymentFeign {
@PostMapping("/create")
PaymentVo create(@RequestBody @Validated PaymentDto paymentDto);
}
复制代码
2. The calling 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());
// 忽略逻辑代码 N 行
paymentFeign.create(paymentDto);
log.info("uri:/order/create paymentFegin.create cost: {} ms", System.currentTimeMillis() - start);
return orderVo;
}
复制代码
3. feign timeout configuration
feign:
client:
config:
default:
# 日志级别
loggerLevel: full
# 超时设置 1.5 秒超时
connectTimeout: 1500
readTimeout: 1500
# 断路器
circuitbreaker:
enabled: true
复制代码
Summarize the above code in one sentence: I called it in the xxx/create
interface , /payment/create
and I set the timeout of feign to 1.5s. However, TimeOut is returned due to a timeout during the /payment/create
call .
solution
Option One:
Increase the default timeout of feign to 5s
feign:
client:
config:
default:
# 日志级别
loggerLevel: full
# 超时设置 5 秒超时
connectTimeout: 5000
readTimeout: 5000
# 断路器
circuitbreaker:
enabled: true
复制代码
Option II:
Specify the contextId of the feign interface to set the current feign timeout to 5s.
feign:
client:
config:
default:
# 日志级别
loggerLevel: full
# 超时设置
connectTimeout: 1500
readTimeout: 1500
payment-core:
connectTimeout: 5000
readTimeout: 5000
# 断路器
circuitbreaker:
enabled: true
复制代码
PaymentFeign
Set the contextId property on for
@FeignClient(name = "payment-service", contextId = "payment-core", path = "/payment")
public interface PaymentFeign {
@PostMapping("/create")
PaymentVo create(@RequestBody @Validated PaymentDto paymentDto);
}
复制代码
In doing so, we can reduce the impact and reduce the impact downstream.
3. Summary:
Reference address information: spring-cloud-openfeign