【发布时间】:2017-01-15 12:15:48
【问题描述】:
在 Spring 应用程序(不是 spring-boot)中,我在同一个 bean 方法上使用 javanica @HystrixCommand 注释和 spring cache @Cacheable 注释,Spring 在 Hystrix 建议之前执行缓存建议。 这是我正在等待的,但对我来说,没有任何配置的缓存建议和 hystrix 建议在 spring 中具有相同的顺序: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() {
...
}
...
}
有了这个 spring 配置:
<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" />
感谢您的帮助
【问题讨论】:
-
在
<cache:annotation-driven />或<aop:aspectj0-autoproxy />上尝试 [CTRL]+[SPACE](代码完成)...找到 order 属性,将其设置为您想要的顺序。 -
如果缓存建议在 Hystrix 建议之前执行,这意味着您可能正在缓存回退结果。我想你可能不想要这个。
标签: spring hystrix spring-cache spring-cloud-netflix