javanica @HystrixCommand和spring @Cacheable执行命令
作者:互联网
在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() {
...
}
...
}
有了这个春天配置:
<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,hystrix,spring-cloud-netflix,spring-cache 来源: https://codeday.me/bug/20190702/1352879.html