【问题标题】:Resilience4j never call fallback methodResilience4j 从不调用回退方法
【发布时间】:2022-04-26 19:28:25
【问题描述】:

我是 Resilience4j 和断路器模式的新手。

我为resilience4j 编写了一个示例。详情如下:

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.2.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>ir.co.isc</groupId>
<artifactId>circuit-breaker</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>circuit-breaker</name>
<description>Demo project for Spring Boot</description>

<properties>
    <java.version>11</java.version>
    <spring-cloud.version>Hoxton.SR6</spring-cloud.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- resilience4j dependency-->
    <dependency>
        <groupId>io.github.resilience4j</groupId>
        <artifactId>resilience4j-spring-boot2</artifactId>
        <version>1.5.0</version>
        <type>pom</type>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
        <version>2.3.0.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
        <version>2.3.2.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

application.yml:

resilience4j:
circuitbreaker:
    configs:
        default:
            registerHealthIndicator: true
            slidingWindowSize: 10
            minimumNumberOfCalls: 5
            permittedNumberOfCallsInHalfOpenState: 3
            automaticTransitionFromOpenToHalfOpenEnabled: true
            waitDurationInOpenState: 5s
            failureRateThreshold: 20
            eventConsumerBufferSize: 10
    instances:
        mainService:
            baseConfig: default

控制器类:

package ir.co.isc.circuitbreaker;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MainServiceController {


@Autowired
private MainService mainService;


@GetMapping("/getSleuthTest")
@ResponseStatus(HttpStatus.OK)
public ResponseEntity<String> getSleuthTest(){
    String response = mainService.getResponse();
    return new ResponseEntity<>(response, HttpStatus.OK);
  }
 }

服务类:

package ir.co.isc.circuitbreaker;

import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class MainService {

private static final String MAIN_SERVICE = "mainService";

@Autowired
private RestTemplate restTemplate;

@Bean
public RestTemplate getRestTemplate() {
    return new RestTemplate();
}

@CircuitBreaker(name = MAIN_SERVICE, fallbackMethod="testFallBack")
public String getResponse(){
    return restTemplate.getForObject("http://localhost:8081/serviceOne", String.class);
}

private ResponseEntity<String> testFallBack(Exception e){
    return new ResponseEntity<>("In fallback method", HttpStatus.INTERNAL_SERVER_ERROR);
  }
}

SpringBootApplication 类:

package ir.co.isc.circuitbreaker;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class CircuitBreakerApplication {

public static void main(String[] args) {
    SpringApplication.run(CircuitBreakerApplication.class, args);
   }

}

我使用 postman runner 来调用我的 API。我将跑步者迭代次数设置为 200。 在 10 次成功的 API 调用后,我在此 url 中停止第三方: http://localhost:8081/serviceOne

据我了解,在停止第三方 API 并记录成功调用的最小次数后,resilience4j 开始计算故障率,当故障率大于 failureRateThreshold 时,调用回退方法(此处为服务类中的 testFallBack),电路状态从接近打开模式并返回 testFallBack() 方法中描述的我想要的答案。

但这永远不会发生(从未调用过 testFallBack() 方法)。我的申请有什么问题?

【问题讨论】:

  • 停止混合来自不同版本框架的模块,您正在混合 2.3.0 和 2.3.2 导致不同的依赖项。从 org.springframework.boot 依赖项中删除版本标记。如果这是您使用的 application.yml 缩进错误并且属性不会生效,最后,您将需要启用断路器(仅添加注释是不够的 afaik,您还需要后处理器)。
  • 如何启用断路器? @M.Deinum
  • 如果你正确地包含了启动器,显然它应该可以工作。因此,要么是您的配置,要么是您向依赖项添加了类型。
  • 我使用启动器和配置类来配置断路器注册表。但它还没有工作。弹性4j @M.Deinum 没有明确和完整的例子
  • 有,它在resilience4j 存储库中以及在同一文档中提到的。

标签: spring-boot circuit-breaker resilience4j


【解决方案1】:

总的来说,经过这么多谷歌,我觉得只能从 Controller 调用 fallBack。我已经尝试了多次,但从服务中它不起作用。

由于您也是从服务调用它,这就是您面临此问题的原因,而不是如果您从控制器调用它,它应该适合您。

【讨论】:

    猜你喜欢
    • 2021-02-03
    • 1970-01-01
    • 2017-05-03
    • 1970-01-01
    • 1970-01-01
    • 2022-08-14
    • 1970-01-01
    • 2022-06-14
    • 2021-02-05
    相关资源
    最近更新 更多