溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

服務(wù)治理-Resilience4j(限流)

發(fā)布時(shí)間:2020-08-03 06:25:53 來(lái)源:網(wǎng)絡(luò) 閱讀:551 作者:shayang88 欄目:編程語(yǔ)言

Bulkhead

Bulkhead一般用于服務(wù)調(diào)用客戶端,用于限定對(duì)特定的服務(wù)的并發(fā)請(qǐng)求數(shù)量,起到一下作用:
1、防?下游依賴被并發(fā)請(qǐng)求沖擊
2、防?發(fā)?連環(huán)故障

1、配置規(guī)則“order”

//允許最大的并發(fā)數(shù)量
resilience4j.bulkhead.backends.order.max-concurrent-call=1
//阻塞線程的最大時(shí)間量
resilience4j.bulkhead.backends.order.max-wait-time=5

2、注解方式使用

@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、使用配置注冊(cè)方式

        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

RateLimiter用于限制特定時(shí)間段內(nèi)的執(zhí)?次數(shù),一般用于服務(wù)提供方,保護(hù)自己不受到?jīng)_擊。

1、配置限流策略-“order”

//一個(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

2、使用注解方式

@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、使用配置注冊(cè)方式

    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;
    }
向AI問一下細(xì)節(jié)

免責(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)容。

AI