2016-10-03 25 views
0

我試圖在春季啓動應用程序中包含Hystrix斷路器。 我的應用程序是一個標準的spring boot 1.4.1,帶有spring-cloud-hystrix v 1.2.0應用程序,帶有一個控制器類,它使用「聚合」方法調用服務類。 此方法使用內部私有方法在內部調用兩個服務。春天Hystrix沒有觸發內部方法

如果我註釋「彙總」法@HystrixCommand everithing工作正常,但如果一個註釋其他內部方法蝟似乎沒有被觸發

這是我的服務類:

@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); 

我的配置類具有這些註解:

@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爲了趕錐執行。

奇怪的是,只有放置在「aggergate」方法上的@HystrixCommand似乎被觸發,而其他人不是這樣,如果我在頂部「聚合」上註釋註釋,沒有超時錯誤被觸發,如果我取消註釋一個「缺失後備免除」被觸發

我在問我錯過了什麼配置?必須將@HystrixCommand放在「調用」方法上,不能用於內部方法?

希望我的問題是明確的(因爲對我來說是很模糊的:))

在此先感謝

回答

1

鴉膽子使用HystrixCommandAspect方面檢測與HystrixCommand註解方法,並且似乎所定義的pointcut隻影響公共方法。

更新:有一個bug report與此特定問題相關。

+0

感謝您的問題!我無法找到它。我也嘗試將方法可見性更改爲public,但行爲相同,如果方法未直接調用,hystrix不會被觸發。同樣的環境被提及到喜歡的問題,但沒有提供解決方案(據我所知),我想知道是否有可能儀器方法沒有HystrixCommand註釋谷歌我沒有找到任何指南或例子。 現在我已經把我的課程分成了三部分,儀器化的hystrix和一切工作正常,但我仍然認爲這種方法對我的應用程序是有點過於工程 – IlTera

+0

HystrixCommand註釋只是由Javanica引入的高級簡化。如果你需要更多的靈活性,你可以用標準的方式實現你的命令,例如通過繼承HystrixCommand類https://github.com/Netflix/Hystrix/wiki/Getting-Started#hello-world –

+0

@IlTera,你是什麼意思通過「不直接呼叫」?何時不會直接調用某個方法? – jrahhali