I encounter an exception when using thread pool mode of resilience4j-bulkhead.
I encountered this exception while testing the maximum connection pool, which manifested as an error page when I first accessed the get interface, so I added a global exception catch and ran the result as shown in the following code:
全局异常处理:org.springframework.web.context.request.async.AsyncRequestTimeoutException
2025-01-22T23:00:50.220+08:00 ERROR 35832 --- [cloud-client] [p-nio-80-exec-8] top.zfmx.GlobalExceptionHandler : 全局异常处理:null
I used the global exception to catch the following code:
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(RuntimeException.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public ResponseEntity<String> exception(Exception e){
System.out.println("全局异常处理:" + e);
log.error("全局异常处理:{}", e.getMessage());
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
}
}
server api:
@GetMapping("/api/{id}")
public String bulkhead(@PathVariable("id") Integer id) {
if (id < 0) throw new RuntimeException("id不能小于0");
if (id == 9999)
try{
TimeUnit.SECONDS.sleep(5);
}catch (InterruptedException e) {
e.printStackTrace();
}
return "Hello, bulkhead! input id: " + id + "\t" + IdUtil.simpleUUID();
}
The client uses Feign:
@RestController
public class CustomerController {
@Resource
private FeignApi feignApi;
@GetMapping("/api/{id}")
@Bulkhead(name = "cloud-server", fallbackMethod = "myFallback", type = Bulkhead.Type.THREADPOOL)
public CompletableFuture<String> myBulkheadTHREADPOOL(@PathVariable("id") Integer id) {
System.out.println(Thread.currentThread().getName() + "\t" + "----开始进入");
try{
TimeUnit.SECONDS.sleep(1);}catch (InterruptedException e){e.printStackTrace();}
System.out.println(Thread.currentThread().getName() + "\t" + "----结束进入");
return CompletableFuture.supplyAsync(() -> feignApi.bulkhead(id) + "\t Bulkhead.Type.THREADPOOL");
}
public CompletableFuture<String> myFallback(@PathVariable("Id") Integer id,Throwable t) {
return CompletableFuture.supplyAsync(()-> "超出最大数量,请稍后再试!" + t.getMessage());
}
}
The problem occurred in the configuration for resilience4j is:
resilience4j:
timelimiter:
configs:
default:
timeout-duration: 10s # 防止计时器超过1s直接服务降级逻辑
thread-pool-bulkhead:
configs:
default:
max-thread-pool-size: 1
core-thread-pool-size: 1
queue-capacity: 1
instances:
cloud-provider-payment:
baseConfig: default
When I changed the configuration to the following code, everything worked fine
resilience4j:
timelimiter:
configs:
default:
timeout-duration: 10s # 防止计时器超过1s直接服务降级逻辑
thread-pool-bulkhead:
configs:
default:
max-thread-pool-size: 2
core-thread-pool-size: 2
queue-capacity: 1
instances:
cloud-provider-payment:
baseConfig: default
I really don't know what happened, why did the original configuration time out.