Bulkhead
Bulkhead is generally used for service calling clients to limit the number of concurrent requests for a specific service. It has the following functions:
1. Prevent downstream dependency from being impacted by concurrent requests
2. Prevention of continuous failure
1. Configure rule "order"
//Maximum number of concurrent allowed resilience4j.bulkhead.backends.order.max-concurrent-call=1 //Maximum amount of time to block a thread resilience4j.bulkhead.backends.order.max-wait-time=5
2. Use of annotation
@PostMapping("/order") @io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker(name = "order") @io.github.resilience4j.bulkhead.annotation.Bulkhead(name = "order") public CoffeeOrder createOrder() { NewOrderRequest orderRequest = NewOrderRequest.builder() .customer("Li Lei") .items(Arrays.asList("capuccino")) .build(); CoffeeOrder order = coffeeOrderService.create(orderRequest); log.info("Order ID: {}", order != null ? order.getId() : "-"); return order; }
3. Use configuration registration method
private CircuitBreaker circuitBreaker; private Bulkhead bulkhead; //Construction method public CustomerController(CircuitBreakerRegistry circuitBreakerRegistry, BulkheadRegistry bulkheadRegistry) { circuitBreaker = circuitBreakerRegistry.circuitBreaker("order"); bulkhead = bulkheadRegistry.bulkhead("order"); } @GetMapping("/menu") public List<Coffee> readMenu() { return Try.ofSupplier( Bulkhead.decorateSupplier(bulkhead, CircuitBreaker.decorateSupplier(circuitBreaker, () -> coffeeService.getAll()))) .recover(CircuitBreakerOpenException.class, Collections.emptyList()) .recover(BulkheadFullException.class, Collections.emptyList()) .get(); }
RateLimiter
RateLimiter is used to limit the number of executions in a specific period of time. It is generally used by service providers to protect themselves from impact.
1. Configure current limiting strategy - "order"
//Access times in a limited period resilience4j.ratelimiter.limiters.order.limit-for-period=3 //Limit period, after each period, the rate limiter will reset back to the limitForPeriod value resilience4j.ratelimiter.limiters.order.limit-refresh-period-in-millis=30000 //Thread wait allowed execution time resilience4j.ratelimiter.limiters.order.timeout-in-millis=1000 //Open time subscription resilience4j.ratelimiter.limiters.order.subscribe-for-events=true //Turn on monitoring resilience4j.ratelimiter.limiters.order.register-health-indicator=true
2. Use annotation method
@PostMapping(path = "/", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) @ResponseStatus(HttpStatus.CREATED) @io.github.resilience4j.ratelimiter.annotation.RateLimiter(name = "order") public CoffeeOrder create(@RequestBody NewOrderRequest newOrder) { log.info("Receive new Order {}", newOrder); Coffee[] coffeeList = coffeeService.getCoffeeByName(newOrder.getItems()) .toArray(new Coffee[]{}); return orderService.createOrder(newOrder.getCustomer(), coffeeList); }
3. Use configuration registration method
private RateLimiter rateLimiter; //Construction method public CoffeeOrderController(RateLimiterRegistry rateLimiterRegistry) { rateLimiter = rateLimiterRegistry.rateLimiter("order"); } @GetMapping("/{id}") public CoffeeOrder getOrder(@PathVariable("id") Long id) { CoffeeOrder order = null; try { order = rateLimiter.executeSupplier(() -> orderService.get(id)); log.info("Get Order: {}", order); } catch (RequestNotPermitted e) { log.warn("Request Not Permitted! {}", e.getMessage()); } return order; }