gpt4 book ai didi

spring-cloud-gateway - 在 Spring Cloud Gateway 中使用 Resilience4j 为路由配置特定的断路器

转载 作者:行者123 更新时间:2023-12-03 23:27:58 26 4
gpt4 key购买 nike

我尝试在我的 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

非常感谢您的帮助。

最佳答案

如果要对 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 云工厂中的外部配置注入(inject)加载的 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}

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

关于spring-cloud-gateway - 在 Spring Cloud Gateway 中使用 Resilience4j 为路由配置特定的断路器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58879226/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com