您好,登錄后才能下訂單哦!
Bulkhead一般用于服務(wù)調(diào)用客戶端,用于限定對(duì)特定的服務(wù)的并發(fā)請(qǐng)求數(shù)量,起到一下作用:
1、防?下游依賴被并發(fā)請(qǐng)求沖擊
2、防?發(fā)?連環(huán)故障
//允許最大的并發(fā)數(shù)量
resilience4j.bulkhead.backends.order.max-concurrent-call=1
//阻塞線程的最大時(shí)間量
resilience4j.bulkhead.backends.order.max-wait-time=5
@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;
}
private CircuitBreaker circuitBreaker;
private Bulkhead bulkhead;
//構(gòu)造方法
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用于限制特定時(shí)間段內(nèi)的執(zhí)?次數(shù),一般用于服務(wù)提供方,保護(hù)自己不受到?jīng)_擊。
//一個(gè)限制周期內(nèi)可訪問次數(shù)
resilience4j.ratelimiter.limiters.order.limit-for-period=3
//限制周期,每個(gè)周期之后,速率限制器將重置回limitForPeriod值
resilience4j.ratelimiter.limiters.order.limit-refresh-period-in-millis=30000
//線程等待允許執(zhí)行時(shí)間
resilience4j.ratelimiter.limiters.order.timeout-in-millis=1000
//開啟時(shí)間訂閱
resilience4j.ratelimiter.limiters.order.subscribe-for-events=true
//開啟監(jiān)控監(jiān)控
resilience4j.ratelimiter.limiters.order.register-health-indicator=true
@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);
}
private RateLimiter rateLimiter;
//構(gòu)造方法
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;
}
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。