Как использовать resilience4j при вызове метода? - PullRequest
0 голосов
/ 22 марта 2019

Я пытался использовать пружинный повтор для прерывания цепи и повтор, как показано ниже, и он работает, как и ожидалось, но проблема не в том, чтобы сконфигурировать «maxAttempts / openTimeout / resetTimeout» в качестве переменных env (ошибка должна быть константой). Мой вопрос: как использовать resilience4j для достижения нижеуказанного требования?

также, пожалуйста, предложите, что есть способ передать переменные env в "maxAttempts / openTimeout / resetTimeout".

@CircuitBreaker(value = {
        MongoServerException.class,
        MongoSocketException.class,
        MongoTimeoutException.class
        MongoSocketOpenException.class},
        maxAttempts =  2,
        openTimeout = 20000L ,
        resetTimeout = 30000L)
public void insertDocument(ConsumerRecord<Long, GenericRecord> consumerRecord){

        retryTemplate.execute(args0 -> {
            LOGGER.info(String.format("Inserting record with key -----> %s", consumerRecord.key().toString()));
            BasicDBObject dbObject = BasicDBObject.parse(consumerRecord.value().toString());
            dbObject.put("_id", consumerRecord.key());
            mongoCollection.replaceOne(<<BasicDBObject with id>>, getReplaceOptions());
            return null;
        });

}

@Recover
public void recover(RuntimeException t) {
    LOGGER.info(" Recovering from Circuit Breaker ");
}

используются зависимости

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.retry</groupId>
        <artifactId>spring-retry</artifactId>
    </dependency>

Ответы [ 2 ]

0 голосов
/ 29 марта 2019
CircuitBreakerConfig circuitBreakerConfig = CircuitBreakerConfig.custom()
    .waitDurationInOpenState(Duration.ofMillis(20000))
    .build();
CircuitBreakerRegistry circuitBreakerRegistry = CircuitBreakerRegistry.of(circuitBreakerConfig);
CircuitBreaker circuitBreaker = circuitBreakerRegistry.circuitBreaker("mongoDB");

RetryConfig retryConfig = RetryConfig.custom().maxAttempts(3)
    .retryExceptions(MongoServerException.class,
        MongoSocketException.class,
        MongoTimeoutException.class
        MongoSocketOpenException.class)
    .ignoreExceptions(CircuitBreakerOpenException.class).build();
Retry retry = Retry.of("helloBackend", retryConfig);

Runnable decoratedRunnable = Decorators.ofRunnable(() -> insertDocument(ConsumerRecord<Long, GenericRecord> consumerRecord))
.withCircuitBreaker(circuitBreaker)
.withRetry(retry)
.decorate();

String result = Try.runRunnable(decoratedRunnable )
                .recover(exception -> ...).get();
0 голосов
/ 22 марта 2019

Вы используете не resilience4j, а Spring-Retry. Вы должны адаптировать заголовок вашего вопроса.

...