javanica @HystrixCommand和spring @Cacheable执行顺序

时间:2016-09-07 17:35:03

标签: spring hystrix spring-cache spring-cloud-netflix

在Spring应用程序(不是spring-boot)中,我在同一个bean方法上使用javanica @HystrixCommand注释和spring cache @Cacheable注释,Spring在Hystrix建议之前执行缓存建议。 这就是我正在等待的东西,但对我来说,没有任何配置的缓存建议和hystrix建议在春天有相同的顺序:LOWEST_PRECEDENCE。

我不知道是什么使这个订单:它是在某个地方定义的还是这个定义不明确的订单而且我很幸运能得到良好的订单? 必须采取哪些措施来确保在Hystrix建议之前执行缓存建议(在缓存上设置顺序:在LOWEST_PRECEDENCE之前驱动注释?)

这是我的代码示例:

...
import org.springframework.cache.annotation.CacheConfig;

import org.springframework.cache.annotation.Cacheable;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
...


@Service
@Slf4j
@CacheConfig(cacheManager="myCacheManager")
public class MyRessourcesImpl implements MyRessources {
...
    @Override
    @HystrixCommand(commandProperties = {
            @HystrixProperty(name = "execution.isolation.strategy", value = "SEMAPHORE"),
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "10000") })
    @Cacheable("cacheName")  //from spring cache
    public Map<String, String> getTheMap() {
        ...
    }
...    
}

使用此弹簧配置:

<bean id="myCache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" >
    <property name="configLocation" value="classpath:/META-INF/...."  />
</bean>

<bean id="myCacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
    <property name="cacheManager" ref="myCache" />
</bean>

<cache:annotation-driven cache-manager="myCacheManager" />

<aop:aspectj-autoproxy/>

<bean id="hystrixAspect" class="com.netflix.hystrix.contrib.javanica.aop.aspectj.HystrixCommandAspect" />

感谢您的帮助

1 个答案:

答案 0 :(得分:2)

According to the docs,如果您没有指定订单,则未定义:

  

当在不同方面定义的两条建议都需要在同一个连接点运行时,除非您另行指定,否则执行顺序是未定义的。您可以通过指定优先级来控制执行顺序。这是通过在方法类中实现org.springframework.core.Ordered接口或使用Order注释对其进行注释来以常规Spring方式完成的。给定两个方面,从Ordered.getValue()(或注释值)返回较低值的方面具有较高的优先级。

信用:spring annotation advice order