我正在使用具有功能端点的Spring WebFlux来创建API.为了提供我想要的结果,我需要使用外部RESTful API,并以异步方式执行此操作,我正在使用WebClient实现.效果很好,就像这样:
public WeatherWebClient() {
this.weatherWebClient = WebClient.create("http://api.openweathermap.org/data/2.5/weather");
}
public Mono<WeatherApiResponse> getWeatherByCityName(String cityName) {
return weatherWebClient
.get()
.uri(uriBuilder -> uriBuilder
.queryParam("q", cityName)
.queryParam("units", "metric")
.queryParam("appid", API_KEY)
.build())
.accept(APPLICATION_JSON)
.retrieve()
.bodyToMono(WeatherApiResponse.class);
}
Run Code Online (Sandbox Code Playgroud)
由于这会执行网络访问,因此它是NetFlix OSS Hystrix的一个很好的用例.我已经尝试过使用spring-cloud-starter-netflix-hystrix,将@HystrixCommand添加到上面的方法中,但即使我设置了错误的URL(404)或错误的API_KEY(401),也无法使其跳闸. .
我认为这可能是与WebFlux本身兼容的问题,但设置属性@HystrixProperty(name ="circuitBreaker.forceOpen",value ="true")确实迫使回退方法运行.
我错过了什么吗?这种方法是否与Spring WebClients不兼容?
谢谢!
我有以下配置,用于在运行时创建断路器:
@Configuration
public class CircuitBreakerConfiguration
{
public final static String DEFAULT_CIRCUIT_BREAKER_REGISTRY = "DEFAULT_CIRCUIT_BREAKER_REGISTRY";
private CircuitBreakerConfig getCircuitBreakerConfig()
{
return CircuitBreakerConfig.custom()
.failureRateThreshold(10)
.waitDurationInOpenState(Duration.ofMillis(30000))
.permittedNumberOfCallsInHalfOpenState(2)
.slidingWindowType(CircuitBreakerConfig.SlidingWindowType.COUNT_BASED)
.slidingWindowSize(5)
.automaticTransitionFromOpenToHalfOpenEnabled(true)
.recordExceptions(CheckAvailabilityException.class)
.build();
}
@Bean
@Qualifier(DEFAULT_CIRCUIT_BREAKER_REGISTRY)
public CircuitBreakerRegistry getCircuitBreakerRegistry()
{
return CircuitBreakerRegistry.of(getCircuitBreakerConfig());
}
}
Run Code Online (Sandbox Code Playgroud)
我想将这些配置移至我的application.properties
文件中。
我尝试了以下方法来覆盖默认配置:
resilience4j.circuitbreaker.configs.default.sliding-window-size=10
resilience4j.circuitbreaker.configs.default.sliding-window-type=COUNT_BASED
resilience4j.circuitbreaker.configs.default.failure-rate-threshold=50
resilience4j.circuitbreaker.configs.default.wait-duration-in-open-state=30s
resilience4j.circuitbreaker.configs.default.permitted-number-of-calls-in-half-open-state=2
resilience4j.circuitbreaker.configs.default.automatic-transition-from-open-to-half-open-enabled=true
resilience4j.circuitbreaker.configs.default.record-exceptions=com.example.web.domain.checkavailability.exceptions.CheckAvailabilityException
Run Code Online (Sandbox Code Playgroud)
但是,这似乎也没有覆盖默认配置。
我想测试以下场景:
hystrix.command.default.execution.isolation.thread.timeoutInMillisecond
值设置为较低的值,并查看我的应用程序的行为方式.请有人可以提供样品链接.
我们拥有一个拥有自己的API网关,服务发现和负载平衡的基础架构.但是出于弹性目的,我需要使用Hystrix.
我正在尝试了解 Hystrix。我了解当服务调用 3rd 方服务并且该服务没有响应并且阈值已超过配置时,电路将被打开并且继续呼叫将被短路。
但我无法理解电路是如何再次闭合的。让我们假设我们的服务正在调用 3rd 方服务,并且该服务无法正常工作,因此电路已打开。5 分钟后,该服务开始正常工作,现在电路应该关闭。调用服务如何知道第 3 方服务已经开始正常运行,现在应该关闭?
鉴于 Hystrix 进入维护模式,我一直致力于将(相当大的)代码库迁移到 Resilience4j。
我在 Hystrix 中大量使用以下模式:
new HystrixCommand<SomeReturnValue>(DependencyKeys.DEPENDENCY) {
@Override
protected SomeReturnValue run() {
return someExpensiveCall();
}
}
.observe()
Run Code Online (Sandbox Code Playgroud)
我想用 Resilience4j 复制 Hystrix 的一些功能。
到目前为止,我有以下语法来连接外部调用:
resilience.single(DependencyKeys.DEPENDENCY, this::someExpensiveCall);
Run Code Online (Sandbox Code Playgroud)
其中Resilience
类提供single
方法:
public <T> Single<T> single(ResilienceKey key, Callable<T> callable) {
return Completable.complete()
.subscribeOn(Schedulers.computation())
.observeOn(configuration.scheduler(key))
.andThen(Single.defer(() -> Single.fromCallable(callable)
.lift(CircuitBreakerOperator.of(configuration.circuitBreaker(key)))
.lift(RateLimiterOperator.of(configuration.rateLimiter(key)))
.lift(BulkheadOperator.of(configuration.bulkhead(key)))
))
.observeOn(Schedulers.computation());
}
Run Code Online (Sandbox Code Playgroud)
在断路和在不同线程池上运行代码方面,这看起来如何更好地类似于 Hystrix,但以更理智的方式。我真的不喜欢用 just 来启动链,这样我就可以在实际的可调用对象被包装之前Completable.complete()
强制使用 a 。observeOn
我正在尝试使用 eureka 和 zuul 进行微服务。并且所有请求都存在问题,耗时超过 1 秒。据我了解,1 秒是默认的 hystrix 超时,为了在 Zuul 中配置超时,我必须配置这些属性:
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds
ribbon.ConnectTimeout
ribbon.ReadTimeout
但是当我设置它们时,Intelije Idea 中的每一个都有“无法解析配置属性......”警告。而且,它们似乎没有被应用,也不起作用。
spring-boot hystrix netflix-eureka netflix-zuul netflix-ribbon
Netflix 将 Hystrix 正式置于维护模式(https://github.com/Netflix/Hystrix#hystrix-status),我开始寻找替代方案。当涉及到断路器、隔板、重试等模式时,有一些不错的库,例如resilience4j,但我找不到 Hystrix 可以做的请求崩溃的替代方案。
有人知道可以提供此类功能的库吗?
谢谢,本杰明
我的 SpringBoot 应用程序启用了 Hystrix,并为某些 Feign 客户端定义了回退,其余客户端未定义。
现在,我想为尚未定义回退的那些禁用 Hystrix。因此,我按照 [第 7.4 段] https://cloud.spring.io/spring-cloud-netflix/multi/multi_spring-cloud-feign.html 中列出的步骤操作,即使用 vanilla Feign.Builder 创建单独的 Feign 配置. 但是,添加新的 @Bean Feign.Builder 会禁用我不想要的所有 Feign 客户端的 Hystrix 功能。如果我删除@Bean Feign.Builder,Hystrix 回退会像往常一样在 myhystrixclient 中启动。一个类似的问题在这里如何在多个假客户端之一中禁用 hystrix仍然是开放的。我究竟做错了什么?
public class MyFeignClientConfiguration {
@Bean
public FeignErrorDecoder feignErrorDecoder() {
return new FeignErrorDecoder();
}
@Bean
@Scope("prototype")
public Feign.Builder feignBuilder() {
return Feign.builder();
}
}
Run Code Online (Sandbox Code Playgroud)
我的 Feign 客户端如下所示:
@FeignClient(name = "myregularclient", configuration = MyFeignClientConfiguration.class)
public interface MyRegularClient {
//my APIs here
}
Run Code Online (Sandbox Code Playgroud)
我的 Hystrix Feign 配置如下所示:
public class …
Run Code Online (Sandbox Code Playgroud) 我正在为断路器编写一个小程序,运行该应用程序时会抛出异常。
springboot 版本 2.5.4,Hystrix 版本使用2.2.6
BeanCreationException:创建类路径资源 [org/springframework/cloud/autoconfigure/ConfigurationPropertiesRebinderAutoConfiguration.class] 中定义的名为“configurationPropertiesBeans”的 bean 时出错:合并 bean 定义的后处理失败;嵌套异常是 java.lang.IllegalStateException:无法从 ClassLoader [jdk.internal.loader.ClassLoaders$AppClassLoader@659e0bfd] 内省类 [org.springframework.cloud.context.properties.ConfigurationPropertiesBeans]
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.4</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.ramgovindhare</groupId>
<artifactId>cricuitbreakerhystrix</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>CricuitBreakerHystrix</name>
<description>firstMicroserviceProject</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-hystrix -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>2.2.8.RELEASE</version> <--- **See this**
</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</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> …
Run Code Online (Sandbox Code Playgroud) hystrix ×10
java ×4
spring-boot ×4
resilience4j ×3
cloud ×2
spring ×2
architecture ×1
fallback ×1
netflix ×1
netflix-zuul ×1
resiliency ×1
rx-java2 ×1
spring-cloud ×1
turbine ×1