【问题标题】:javanica @HystrixCommand and spring @Cacheable execution orderjavanica @HystrixCommand 和 spring @Cacheable 执行顺序
【发布时间】: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" />

感谢您的帮助

【问题讨论】:

  • &lt;cache:annotation-driven /&gt;&lt;aop:aspectj0-autoproxy /&gt; 上尝试 [CTRL]+[SPACE](代码完成)...找到 order 属性,将其设置为您想要的顺序。
  • 如果缓存建议在 Hystrix 建议之前执行,这意味着您可能正在缓存回退结果。我想你可能不想要这个。

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


【解决方案1】:

According to the docs,如果不指定顺序,就是未定义:

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

信用:spring annotation advice order

【讨论】:

    猜你喜欢
    • 2017-09-13
    • 2014-12-19
    • 2020-02-29
    • 2015-12-17
    • 1970-01-01
    • 1970-01-01
    • 2012-05-07
    • 2015-04-15
    • 1970-01-01
    相关资源
    最近更新 更多