你可以使用resilience4j-spring-boot2依赖项和org.springframework.cache.annotation.CachingConfigurer。
这个解决方案适用于最新的Spring-boot版本6.x。
对我来说有效。
以下是代码片段:
Pom.xml:
<dependency>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-spring-boot2</artifactId>
<version>2.1.0</version>
</dependency>
缓存配置类:
package com.redis.sample;
import org.springframework.cache.annotation.CachingConfigurer;
import org.springframework.cache.interceptor.CacheErrorHandler;
import org.springframework.context.annotation.Configuration;
@Configuration
public class CachingConfiguration implements CachingConfigurer {
@Override
public CacheErrorHandler errorHandler() {
return new CustomCacheErrorHandler();
}
}
CustomCacheErrorHandler类:
package com.redis.sample;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.cache.Cache;
import org.springframework.cache.interceptor.CacheErrorHandler;
import org.springframework.stereotype.Component;
@Component
public class CustomCacheErrorHandler implements CacheErrorHandler {
@Override
public void handleCacheGetError(RuntimeException exception, Cache cache, Object key) {
}
@Override
public void handleCachePutError(RuntimeException exception, Cache cache, Object key, Object value) {
}
@Override
public void handleCacheEvictError(RuntimeException exception, Cache cache, Object key) {
}
@Override
public void handleCacheClearError(RuntimeException exception, Cache cache) {
}
}
CircuitBreakerConfig.java配置类:
package com.redis.sample;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import io.github.resilience4j.circuitbreaker.CircuitBreaker;
import io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry;
@Configuration
public class CircuitBreakerConfig {
@Bean
public CircuitBreakerRegistry circuitBreakerRegistry() {
io.github.resilience4j.circuitbreaker.CircuitBreakerConfig config = io.github.resilience4j.circuitbreaker.CircuitBreakerConfig.custom()
.failureRateThreshold(6)
.permittedNumberOfCallsInHalfOpenState(2)
.slidingWindowSize(5)
.minimumNumberOfCalls(5)
.build();
return CircuitBreakerRegistry.of(config);
}
@Bean
public CircuitBreaker defaultCircuitBreaker() {
io.github.resilience4j.circuitbreaker.CircuitBreakerConfig config = io.github.resilience4j.circuitbreaker.CircuitBreakerConfig.custom()
.minimumNumberOfCalls(2)
.build();
return CircuitBreaker.of("default", config);
}
}
application-dev.properties
ж–Ү件дёӯж·»еҠspring.cache.type=NONE
гҖӮ - M. Deinumspring.cache.type=NONE
并不是关闭缓存,而是阻止缓存的内容。也就是说,它仍然会向您的程序添加27层AOP/拦截器堆栈,只是不进行缓存。这取决于他所说的"关闭所有"的含义。 - David NewcombNoOpCacheManager
bean设置为主要bean。https://dev59.com/hFsV5IYBdhLWcg3wsQnf#41586253 - Amir Azizkhani