首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Resilience4j从不调用回退方法

Resilience4j从不调用回退方法
EN

Stack Overflow用户
提问于 2020-07-28 08:00:50
回答 1查看 4K关注 0票数 1

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

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

pom.xml:

代码语言:javascript
复制
<?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:

代码语言:javascript
复制
resilience4j:
circuitbreaker:
    configs:
        default:
            registerHealthIndicator: true
            slidingWindowSize: 10
            minimumNumberOfCalls: 5
            permittedNumberOfCallsInHalfOpenState: 3
            automaticTransitionFromOpenToHalfOpenEnabled: true
            waitDurationInOpenState: 5s
            failureRateThreshold: 20
            eventConsumerBufferSize: 10
    instances:
        mainService:
            baseConfig: default

控制器类:

代码语言:javascript
复制
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);
  }
 }

服务舱:

代码语言:javascript
复制
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类:

代码语言:javascript
复制
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);
   }

}

我使用邮递员来调用我的API。我将runner迭代设置为200。在10次成功的API调用之后,我在这个url:http://localhost:8081/serviceOne中停止了第三方

据我所知,在停止第三方API并记录了最少的成功调用次数之后,resilience4j开始计算故障率,当故障率超过failureRateThreshold时,回退方法(这里是服务类中的testFallBack )调用,电路状态从接近开放模式转变为开放模式,并返回在testFallBack()方法中描述的我的愿望答案。

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

EN

回答 1

Stack Overflow用户

发布于 2022-04-26 11:28:25

总的来说,经过这么多谷歌,我觉得fallBack只能从控制器调用。我已经试过好几次了,但是在服务部门,它没有起作用。

因为您也是从服务调用它,这就是为什么您要面对这个问题,而不是您从控制器调用它,它应该对您工作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63129534

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档