0

我正在尝试在我的 Spring Boot(使用 Swagger)中构建一个简单的 CircuitBreaker。

这个想法是第一个应用程序调用另一个可以是 Up 或 Down 的应用程序,我想实现一个 CircuitBreaker,如果第二个应用程序关闭,它会给出错误消息。

主要问题是我尝试使用@CircuitBreaker 注释,表示它无法解析为类型。

import...
import io.github.resilience4j.*;

public class AccountServiceImpl implements AccountService {

 Autowired private RestTemplate restTemplate;

  @CircuitBreaker( name = "test", fallbackMethod = "fallback")   
  public void createAccount(ConsentRequestDTO consent) {
    //code which makes the post
    //
  }

  public String fallback() {
    return "fallback-exceptiont";
  }
}

我的 pom.xml 是

<dependency>
    <groupId>io.github.resilience4j</groupId>
    <artifactId>resilience4j-spring-boot2</artifactId>
    <version>1.7.0</version>
  </dependency>

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-circuitbreaker-reactor-resilience4j</artifactId>
    <version>2.0.1</version>
</dependency>

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-aop</artifactId>
  <version>2.3.1.RELEASE</version>
</dependency>

<!-- https://mvnrepository.com/artifact/io.github.resilience4j/resilience4j-retry -->
    <dependency>
      <groupId>io.github.resilience4j</groupId>
      <artifactId>resilience4j-circuitbreaker</artifactId>
      <version>1.7.0</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/io.github.resilience4j/resilience4j-core -->
    <dependency>
      <groupId>io.github.resilience4j</groupId>
      <artifactId>resilience4j-core</artifactId>
      <version>1.7.0</version>
    </dependency>

    <dependency>
      <groupId>io.github.resilience4j</groupId>
      <artifactId>resilience4j-all</artifactId>
      <version>1.7.0</version>
    </dependency>

我不知道如何解决错误。也许它的依赖关系?我试过没有注释,使用

CircuitBreakerConfig config = CircuitBreakerConfig.ofDefaults()

但它给出了同样的错误,“无法解决”。

4

1 回答 1

2

基于您不需要的演示断路器

<dependency>
    <groupId>io.github.resilience4j</groupId>
    <artifactId>resilience4j-spring-boot2</artifactId>
    <version>1.7.0</version>
  </dependency>

因为你已经有了spring-cloud-starter-circuitbreaker-reactor-resilience4j。使用这些导入可能会解决您的问题

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-client</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.inject</groupId>
            <artifactId>jersey-hk2</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.ext.rx</groupId>
            <artifactId>jersey-rx-client-rxjava</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.ext.rx</groupId>
            <artifactId>jersey-rx-client-rxjava2</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-jackson</artifactId>
            <version>${jersey.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
        <dependency>
            <groupId>io.projectreactor.addons</groupId>
            <artifactId>reactor-adapter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-circuitbreaker-reactor-resilience4j</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
于 2021-09-19T20:52:34.267 回答