【问题标题】:How to use resilience4j on calling method?如何在调用方法上使用resilience4j?
【发布时间】:2019-03-22 11:31:13
【问题描述】:

我尝试使用弹簧重试进行断路并重试,如下所示,它按预期工作,但问题无法将“maxAttempts/openTimeout/resetTimeout”配置为环境变量(错误应该是常量)。我的问题是如何使用resilience4j 来实现以下要求?

还请建议有一种方法可以将环境变量传递给“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>

【问题讨论】:

    标签: spring-boot spring-retry resilience4j


    【解决方案1】:

    你使用的不是resilience4j,而是spring-retry。 您应该调整问题的标题。

    【讨论】:

    • 对。正如我提到的,我尝试使用 spring-retry 并遇到问题,所以想使用 Resilience4j 来实现相同的功能。我的要求是 Spring 独立应用程序应该在 MongoDB 中连接和插入记录,并希望在插入方法/功能上使用断路器(假设最大尝试 3)和重试(重试尝试 4)。因此,如果 mongo 不可用重试 4 次,最多 3 次尝试,并且电路应该打开一段时间。所有配置都是通过 ENV 变量。任何代码示例都会有所帮助。
    【解决方案2】:
    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();
    

    【讨论】:

    • 如果您使用的是 Spring Boot,则事件更简单。但请查看我们的用户指南。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-03
    • 2014-04-22
    • 2021-02-05
    • 2011-09-12
    • 1970-01-01
    • 2015-02-28
    • 2011-03-07
    相关资源
    最近更新 更多