在使用Spring Boot的Resilience4j弹性框架时出现了NoSuchMethodException异常。

3

我刚开始接触熔断器技术。我尝试使用eureka服务器实现这项技术,但卡在了回退方法的地方。

我的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.2.10.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.microservice</groupId>
<artifactId>inventory-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>inventory-client</name>
<description>Demo microservice project for Spring Boot</description>

<properties>
    <java.version>1.8</java.version>
    <spring-cloud.version>Hoxton.SR8</spring-cloud.version>
</properties>

<dependencies>
    <!-- <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency> -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>io.github.resilience4j</groupId>
        <artifactId>resilience4j-spring-boot2</artifactId>
        <version>1.3.1</version>
    </dependency>
    <dependency>
        <groupId>io.github.resilience4j</groupId>
        <artifactId>resilience4j-circuitbreaker</artifactId>
        <version>1.3.1</version>
    </dependency>
    

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </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.properties文件

spring.application.name=inventory-client-app
server.port=8081

spring.cloud.circuitbreaker.resilience4j.enabled=false

resilience4j.circuitbreaker.instances.itemTypesBreaker.registerHealthIndicator=true
resilience4j.circuitbreaker.instances.itemTypesBreaker.slidingWindowSize=10
resilience4j.circuitbreaker.instances.itemTypesBreaker.permittedNumberOfCallsInHalfOpenState=3
resilience4j.circuitbreaker.instances.itemTypesBreaker.slidingWindowType=TIME_BASED
resilience4j.circuitbreaker.instances.itemTypesBreaker.minimumNumberOfCalls=20
resilience4j.circuitbreaker.instances.itemTypesBreaker.waitDurationInOpenState=50s
resilience4j.circuitbreaker.instances.itemTypesBreaker.failureRateThreshold=50
resilience4j.circuitbreaker.instances.itemTypesBreaker.eventConsumerBufferSize=10

我的控制器

import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker;

@RestController
@RequestMapping("/inventory-client")
public class ItemTypeController {

@Autowired
RestTemplate restTemplate;


@GetMapping("/item-types")
@ResponseStatus(HttpStatus.OK)
@CircuitBreaker(name="itemTypesBreaker",fallbackMethod = "itemTypesFallBack")
public List<ItemType> getAllItemTypes(){
    List<ItemType> itemTypeList = (List<ItemType>) restTemplate.getForObject("http://inventory-catalog-app/inventory-catalog/item-types/", ItemType.class);
    return itemTypeList;
}

private String itemTypesFallBack(Throwable t){

    return "Item Catalog is down";
    
}

我不能使用备用方法调用此端点,请问我缺少哪些其他配置?谢谢。


我的猜测是,您使用的resilience4j版本与您使用的boot版本不兼容,但我不知道在哪里找到这些信息。 - spencergibb
3个回答

4

我找到了一个解决方案。回退方法的签名应该像这样。

private List<ItemType> itemTypesFallBack(Throwable t){
    System.out.println("Item Catalog is down");
    return null;

}


1
根据文档,回退方法应该放在同一个类中,并且必须具有相同的方法签名,只需添加一个额外的目标异常参数。
如果尝试从NumberFormatException中恢复,则将调用具有签名String fallback(String parameter, IllegalArgumentException exception)的方法。
通常我会创建带有RuntimeException额外参数的回退方法。在您的情况下,该方法看起来像:
private List<ItemType> itemTypesFallBack(RuntimeException e){
   ...
}

0
我正在尝试使用resilience4j和eureka server实现重试,但在fallback方法这一点上遇到了困难。我将fallback方法的签名与api相同,但仍然出现了异常。
java.lang.NoSuchMethodException: class org.springframework.http.ResponseEntity class com.user.service.UserService.controllers.UserController.ratingHotelFallback(class java.lang.String,class java.lang.Throwable)  at io.github.resilience4j.fallback.FallbackMethod.create(FallbackMethod.java:92) ~[resilience4j-spring-1.7.0.jar:1.7.0]

我的控制器
@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    private UserService userService;

    int retryCount = 1;

    @GetMapping("/{userId}")
    @Retry(name = "ratingHotelService", fallbackMethod = "ratingHotelFallback")
    public ResponseEntity<User> getSingleUser(@PathVariable String userId){
        System.out.println("Retry Count : " + retryCount);
        retryCount++;
    
        User user = userService.getUser(userId);
        return ResponseEntity.ok(user);
    }
    
    public ResponseEntity<User> ratingHotelFallBack(String userId, Exception ex){
        ex.printStackTrace();
    
        User user = User.builder()
                .userId("12345")
                .name("dummy")
                .email("dummy@gmail.com")
                .about("This user is created dummy because some service is down")
                .build();
    
        return ResponseEntity.ok(user);
    }

我的application.yml
server:
  port: 8081
  error:
    include-message: always

spring:
  application:
    name: USER-SERVICE

  datasource:
    url: jdbc:postgresql://localhost:5432/userdb
    username: postgres
    password: admin

  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true
    properties:
      hibernate:
        dialect: org.hibernate.dialect.PostgreSQLDialect
      format_sql: true

eureka:
  instance:
    prefer-ip-address: true

  client:
    fetch-registry: true
    register-with-eureka: true
    service-url:
      defaultZone: http://localhost:8080/eureka

management:
  health:
    circuitbreakers:
      enabled: true
  endpoints:
    web:
      exposure:
        include: health
  endpoint:
    health:
      show-details: always
resilience4j:
  retry:
    instances:
      ratingHotelService:
        max-attempts: 3
        wait-duration: 2s

我的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.7.10</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.user.service</groupId>
    <artifactId>UserService</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>UserService</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
        <spring-cloud.version>2021.0.6</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>io.github.resilience4j</groupId>
            <artifactId>resilience4j-spring-boot2</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </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>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

我有没有漏掉任何配置?谢谢。

你应该将问题以问题的形式发布,而不是作为答案。 - dcolazin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接