gpt4 book ai didi

spring - javanica @HystrixCommand 和 spring @Cacheable 执行顺序

转载 作者:行者123 更新时间:2023-12-01 03:28:58 25 4
gpt4 key购买 nike

在 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" />

感谢帮助

最佳答案

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

When two pieces of advice defined in different aspects both need to run at the same join point, unless you specify otherwise the order of execution is undefined. You can control the order of execution by specifying precedence. This is done in the normal Spring way by either implementing the org.springframework.core.Ordered interface in the aspect class or annotating it with the Order annotation. Given two aspects, the aspect returning the lower value from Ordered.getValue() (or the annotation value) has the higher precedence.



信用: spring annotation advice order

关于spring - javanica @HystrixCommand 和 spring @Cacheable 执行顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39376106/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com