Spring 内部方法未触发 Hystrix
Spring Hystrix not triggered on inner methods
我正在尝试在 spring 引导应用程序中包含 Hystrix 断路器。
我的应用程序是一个标准的 spring boot 1.4.1,带有 spring-cloud-hystrix v 1.2.0 应用程序,带有单个控制器 class,它调用服务 class "aggregate"方法。
此方法使用内部私有方法在内部调用两个服务。
如果我用@HystrixCommand 注释 "aggregate" 方法一切正常,但如果注释其他内部方法 Hystrix 似乎不会被触发
这是我的服务class:
@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;
}
}
我的控制器方法是:
@GetMapping("{field}")
@ResponseStatus(HttpStatus.OK)
public AggregationResponse aggregate(... some params ...) throws ApiException, IOException {
... omissis ....
return aggregationService.aggregate(params);
我的配置 class 有这些注释:
@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
为了捕捉 hystrix 的执行,我特意将执行隔离的超时设置为 1MS。
奇怪的是,只有放在 "aggergate" 方法上的 @HystrixCommand 似乎被触发,而其他的则没有,所以如果我在顶部注释注释 "aggregate" 没有触发超时错误,如果我取消注释 "missing fallback exeption" 被触发
我在问我配置有误吗? @HystrixCommand 必须只放在 "called" 方法上,不能用于内部方法吗?
希望我的问题很清楚(因为对我来说很晦涩:))
提前致谢
Javanica 使用 HystrixCommandAspect aspect to detect methods annotated with the HystrixCommand
annotation, and it seems that the pointcut 定义仅影响 public 方法。
更新:有一个 bug report 与此特定问题相关。
我正在尝试在 spring 引导应用程序中包含 Hystrix 断路器。 我的应用程序是一个标准的 spring boot 1.4.1,带有 spring-cloud-hystrix v 1.2.0 应用程序,带有单个控制器 class,它调用服务 class "aggregate"方法。 此方法使用内部私有方法在内部调用两个服务。
如果我用@HystrixCommand 注释 "aggregate" 方法一切正常,但如果注释其他内部方法 Hystrix 似乎不会被触发
这是我的服务class:
@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;
}
}
我的控制器方法是:
@GetMapping("{field}")
@ResponseStatus(HttpStatus.OK)
public AggregationResponse aggregate(... some params ...) throws ApiException, IOException {
... omissis ....
return aggregationService.aggregate(params);
我的配置 class 有这些注释:
@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
为了捕捉 hystrix 的执行,我特意将执行隔离的超时设置为 1MS。
奇怪的是,只有放在 "aggergate" 方法上的 @HystrixCommand 似乎被触发,而其他的则没有,所以如果我在顶部注释注释 "aggregate" 没有触发超时错误,如果我取消注释 "missing fallback exeption" 被触发
我在问我配置有误吗? @HystrixCommand 必须只放在 "called" 方法上,不能用于内部方法吗?
希望我的问题很清楚(因为对我来说很晦涩:))
提前致谢
Javanica 使用 HystrixCommandAspect aspect to detect methods annotated with the HystrixCommand
annotation, and it seems that the pointcut 定义仅影响 public 方法。
更新:有一个 bug report 与此特定问题相关。