3

我尝试在我的 spring 云网关中配置 Resilience4j,但没有成功。我找到的所有内容都是针对 Hystrix 或纯 Java 的。

我已将网关配置为在我的服务上传输请求,没关系。

但是不可能在上面配置resilience4j。我有一个很好的 R4J 响应式神器。

Spring Cloud API和网关中Resilience4j的配置不同?

查看我的配置文件。

spring:
  application:
    name: api-gateway
  cloud:
    gateway:
      routes:
        - id: helloworld_service
          uri: "https://localhost:8080"
          predicates:
            - Path=/helloworld/**
          filters:
            - RewritePath=/helloworld/(?<segment>.*), /$\{segment}
            - name: CircuitBreaker
              args:
                name: helloworld
      httpclient:
        ssl:
          useInsecureTrustManager: true

# RESILIENCE4J PROPERTIES
resilience4j:
  circuitbreaker:
    configs:
      default:
        #registerHealthIndicator: true
        ringBufferSizeInClosedState: 10
        ringBufferSizeInHalfOpenState: 3
        automaticTransitionFromOpenToHalfOpenEnabled: true
        waitDurationInOpenStateMillis: 2000
        failureRateThreshold: 50
        eventConsumerBufferSize: 10
    instances:
      helloworld:
        baseConfig: default
        ringBufferSizeInClosedState: 5

我的依赖:

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </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>

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

并产生错误:

reactor.core.Exceptions$ErrorCallbackNotImplemented: java.lang.IllegalArgumentException: Unable to find GatewayFilterFactory with name CircuitBreaker
Caused by: java.lang.IllegalArgumentException: Unable to find GatewayFilterFactory with name CircuitBreaker

非常感谢您的帮助。

4

4 回答 4

9

如果要为 Resuliience4j 断路器使用外部配置,则需要添加以下依赖项:

<dependency>
           <groupId>org.springframework.cloud</groupId>
           <artifactId>spring-cloud-starter-gateway</artifactId>
       </dependency>
       <dependency>
           <groupId>io.github.resilience4j</groupId>
           <artifactId>resilience4j-spring-boot2</artifactId>
           <version>1.1.0</version>
       </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-actuator</artifactId>
       </dependency>

然后在您的 spring 网关应用程序中,使用您在 spring 云工厂中的外部配置注入加载的 Circuitbreaker 注册表:

    @Bean
    public ReactiveResilience4JCircuitBreakerFactory reactiveResilience4JCircuitBreakerFactory(CircuitBreakerRegistry circuitBreakerRegistry) {
        ReactiveResilience4JCircuitBreakerFactory reactiveResilience4JCircuitBreakerFactory = new ReactiveResilience4JCircuitBreakerFactory();
        reactiveResilience4JCircuitBreakerFactory.configureCircuitBreakerRegistry(circuitBreakerRegistry);
        return reactiveResilience4JCircuitBreakerFactory;
    }

然后在 applciation yml 文件中添加您的外部断路器配置,例如:

esilience4j.circuitbreaker:
  configs:
    default:
      slidingWindowSize: 10
      minimumNumberOfCalls: 5
      permittedNumberOfCallsInHalfOpenState: 3
      automaticTransitionFromOpenToHalfOpenEnabled: true
      waitDurationInOpenState: 2s
      failureRateThreshold: 50
      eventConsumerBufferSize: 10
      recordExceptions:
        - org.springframework.web.client.HttpServerErrorException
        - java.io.IOException
      ignoreExceptions:
        - java.lang.IllegalStateException
    shared:
      slidingWindowSize: 100
      permittedNumberOfCallsInHalfOpenState: 30
      waitDurationInOpenState: 1s
      failureRateThreshold: 50
      eventConsumerBufferSize: 10
      ignoreExceptions:
        - java.lang.IllegalStateException
  instances:
    backendA:
      baseConfig: default

使用配置的断路器名称在您的路由中启用断路器过滤器,例如:

spring:
  application:
    name: gateway-service
  output.ansi.enabled: ALWAYS
  cloud:
    gateway:
      routes:
        - id: test-service-withResilient4j
          uri: http://localhost:8091
          predicates:
            - Path=/testService/**
          filters:
            - CircuitBreaker=backendA
            - RewritePath=/testService/(?<path>.*), /$\{path}

现在应该使用您的外部配置断路器对其进行保护,否则您可以使用自定义程序通过代码配置方法对其进行配置

于 2019-12-26T10:43:06.310 回答
1

当前 spring-cloud-gatewayHoxton.RELEASE自动配置存在问题。您可以使用以下配置解决它。

@Configuration
public class Resilience4JConfiguration {

    @Bean
    public FallbackHeadersGatewayFilterFactory fallbackHeadersGatewayFilterFactory() {
        return new FallbackHeadersGatewayFilterFactory();
    }

    @Bean
    public SpringCloudCircuitBreakerFilterFactory resilience4JCircuitBreakerFactory(
            ReactiveResilience4JCircuitBreakerFactory reactiveCircuitBreakerFactory,
            ObjectProvider<DispatcherHandler> dispatcherHandlers) {
        return new SpringCloudCircuitBreakerResilience4JFilterFactory(reactiveCircuitBreakerFactory, dispatcherHandlers);
    }

}
于 2019-11-30T15:43:31.030 回答
1

坦克比拉克为您的耐心。

我的 Resilience4j 配置:

@Configuration
public class Resilience4JConfiguration {

    /**
     * Default Resilience4j circuit breaker configuration
     */
    @Bean
    public Customizer<ReactiveResilience4JCircuitBreakerFactory> defaultCustomizer() {
        return factory -> factory.configureDefault(id -> new Resilience4JConfigBuilder(id)
                .circuitBreakerConfig(CircuitBreakerConfig.custom().minimumNumberOfCalls(5).failureRateThreshold(20).build())
                .build());
    }

    @Bean
    public Customizer<ReactiveResilience4JCircuitBreakerFactory> helloworldCircuitBreaker(){
        return factory -> {
          factory.configure(builder -> builder.circuitBreakerConfig(CircuitBreakerConfig.ofDefaults()).build()
                  .setCircuitBreakerConfig(CircuitBreakerConfig.custom().minimumNumberOfCalls(10).failureRateThreshold(20).build())
                  , "helloworldCircuitBreaker");
        };
    }

    @Bean
    public Customizer<ReactiveResilience4JCircuitBreakerFactory> accountCircuitBreaker(){
        return factory -> {
            factory.configure(builder -> builder.circuitBreakerConfig(CircuitBreakerConfig.ofDefaults()).build(), "accountCircuitBreaker");
        };
    }

    @Bean
    public FallbackHeadersGatewayFilterFactory fallbackHeadersGatewayFilterFactory() {
        return new FallbackHeadersGatewayFilterFactory();
    }

    @Bean
    public SpringCloudCircuitBreakerFilterFactory resilience4JCircuitBreakerFactory(
            ReactiveCircuitBreakerFactory reactiveCircuitBreakerFactory,
            ObjectProvider<DispatcherHandler> dispatcherHandlers) {
        return new SpringCloudCircuitBreakerResilience4JFilterFactory(reactiveCircuitBreakerFactory, dispatcherHandlers);
    }
}

我的应用程序.yaml:

spring:
  application:
    name: api-gateway
  cloud:
    gateway:
      routes:
        - id: helloworld_service
          uri: "https://localhost:8080"
          predicates:
            - Path=/helloworldservice/**
          filters:
            - CircuitBreaker

有了这个配置,它使用的默认配置就正确了。

但是,如果我尝试精确指定特定的断路器,则不使用任何配置(没有默认值,也没有 helloworldCircuitBreaker)

带有特定断路器的配置:

spring:
  application:
    name: api-gateway
  cloud:
    gateway:
      routes:
        - id: helloworld_service
          uri: "https://localhost:8080"
          predicates:
            - Path=/helloworldservice/**
          filters:
            - name: CircuitBreaker
            args:
              name: helloworldCircuitBreaker
于 2019-12-04T19:57:23.410 回答
0

确保添加了以下依赖项:

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

现在在您的 spring 网关应用程序中,创建 bean:

@Bean
public Customizer<ReactiveResilience4JCircuitBreakerFactory> defaultCustomizer() {
    return factory -> factory.configureDefault(id -> new Resilience4JConfigBuilder(id)
            .circuitBreakerConfig(CircuitBreakerConfig.ofDefaults())
            .timeLimiterConfig(TimeLimiterConfig.custom().timeoutDuration(Duration.ofSeconds(4)).build()).build());
}

现在,application.yml 应该看起来像

spring:
application:
  name: CLOUD-GATEWAY
cloud:
  gateway:
    routes:
    - id: order-service
      uri: lb://ORDER-SERVICE
      predicates:
      - Path=/orders**
      filters:
        - name: CircuitBreaker
          args:
            name: order-service
            fallbackuri: forward:/fallback/orders
于 2021-11-14T18:36:26.907 回答