gpt4 book ai didi

java - Spring Hystrix 未在内部方法上触发

转载 作者:行者123 更新时间:2023-12-02 20:57:16 29 4
gpt4 key购买 nike

我正在尝试将 Hystrix 断路器包含在 Spring Boot 应用程序中。我的应用程序是一个标准的 spring boot 1.4.1,带有 spring-cloud-hystrix v 1.2.0 应用程序,带有单个 Controller 类,该 Controller 类使用“聚合”方法调用服务类。此方法使用内部私有(private)方法在内部调用两个服务。

如果我用 @HystrixCommand 注释“聚合”方法,一切都可以正常工作,但是如果注释其他内部方法 Hystrix 似乎不会被触发

这是我的服务类别:

@Service
public class AggregationService {

@Autowired
Service1 service1Client;

@Autowired
Service2 service2Client;

private static final String QUERY = "query";
private static final Logger log = LogManager.getLogger();
private final ObjectMapper objectMapper = new ObjectMapper();

//@HystrixCommand(
// fallbackMethod = "emptyResult",
// groupKey = "aggregation-service",
// commandKey = "aggregate")
public AggregationResponse aggregate(final AggregationParams params)
throws ApiException, IOException {

final String query = queryExplain(params);
final WrapperQueryBuilder wrappedQuery = QueryBuilders.wrapperQuery(query);
final SearchResponse aggregationResult = searchAggregation(params, wrappedQuery);

return toDtoResponse(aggregationResult.getAggregations().get(params.getAggregationField().name().toLowerCase()));
}

@HystrixCommand(
fallbackMethod = "emptyAggregationResult",
groupKey = "aggregation-service",
commandKey = "searchAggregation")
private SearchResponse searchAggregation(final AggregationParams params, final WrapperQueryBuilder query) {

return ... do something with service 2 ....
}

// @HystrixCommand(
// fallbackMethod = "rethrowTimeoutException",
// groupKey = "aggregation-service",
// commandKey = "query-for-aggregation",
// ignoreExceptions = TimeoutException.class)
private String queryExplain(final AggregationParams params) throws ApiException, IOException {
final String queryAsString = ... do something with service 1 ....
}

private String rethrowTimeoutException(final AggregationParams params, final Throwable e) {
log.error("On Hystrix fallback because of ", e);
return null;
}

private SearchResponse emptyAggregationResult(final AggregationParams params, final WrapperQueryBuilder query, final Throwable e) {
log.error("On Hystrix fallback because of ", e);
return null;
}

}

我的 Controller 方法是:

 @GetMapping("{field}")
@ResponseStatus(HttpStatus.OK)
public AggregationResponse aggregate(... some params ...) throws ApiException, IOException {

... omissis ....

return aggregationService.aggregate(params);

我的配置类有这些注释:

@Configuration
@EnableHystrix

我的application.properties包含:

hystrix.command.searchAggregation.execution.isolation.thread.timeoutInMilliseconds=1
hystrix.command.searchAggregation.circuitBreaker.errorThresholdPercentage=10
hystrix.command.queryExplain.execution.isolation.thread.timeoutInMilliseconds=1
hystrix.command.queryExplain.circuitBreaker.errorThresholdPercentage=10
hystrix.command.default.execution.timeout.enabled=true

我特意将执行隔离超时设置为 1MS,以便捕获 hystrix 执行。

奇怪的是,只有放置在“aggergate”方法上的@HystrixCommand似乎被触发,而其他方法则不然,如果我注释顶部“aggregate”上的注释,则不会触发超时错误,如果我取消注释a触发“缺少后备异常”

我问我配置错误了什么? @HystrixCommand 必须仅放置在“被调用”方法上并且不能用于内部方法吗?

希望我的问题很清楚(因为对我来说非常模糊:))

提前致谢

最佳答案

Javanica 使用 HystrixCommandAspect方面来检测使用 HystrixCommand 注释进行注释的方法,并且似乎 pointcut定义仅影响公共(public)方法。

更新:有一个 bug report与此特定问题相关。

关于java - Spring Hystrix 未在内部方法上触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39838173/

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