Mic*_*gme 7 project-reactor hystrix spring-cloud-netflix spring-webflux
我正在使用具有功能端点的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不兼容?
谢谢!
小智 14
@HystrixCommand不会真正起作用,因为Hystrix不会威胁Mono/Flux与Java原语有任何不同.
Hystrix不监控Mono的内容,只监控呼叫的结果public Mono<WeatherApiResponse> getWeatherByCityName(String cityName).
这个结果总是好的,因为反应式调用链的创建总是会成功的.
您需要的是以不同的方式使Hystrix威胁Mono/Flux.在Spring Cloud中,有一个构建器,用于使用HystrixCommand包装Mono/Flux.
Mono<WeatherApiResponse> call = this.getWeatherByCityName(String cityName);
Mono<WeatherApiResponse> callWrappedWithHystrix = HystrixCommands
.from(call)
.fallback(Mono.just(WeatherApiResponse.EMPTY))
.commandName("getWeatherByCityName")
.toMono();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3348 次 |
| 最近记录: |