alibaba / jetcache

JetCache is a Java cache framework.
Apache License 2.0
5.15k stars 1.06k forks source link

springboot(已整合redis)整合JetCache2.7版本,CacheManager注入失败 #705

Open chuyuanlinzi opened 2 years ago

chuyuanlinzi commented 2 years ago

打扰,请教个问题: springboot(之前整合过redis)整合2.7.0.RC3版本lettuce pom:

        <dependency>
            <groupId>com.alicp.jetcache</groupId>
            <artifactId>jetcache-redis-lettuce</artifactId>
            <version>${jetcache.latest.version}</version>
        </dependency>
        <dependency>
            <groupId>com.alicp.jetcache</groupId>
            <artifactId>jetcache-starter-redis-lettuce</artifactId>
            <version>${jetcache.latest.version}</version>
        </dependency>

启动服务异常: The bean 'cacheManager', defined in class path resource [org/springframework/boot/autoconfigure/cache/JCacheCacheConfiguration.class], could not be registered. A bean with that name has already been defined in class path resource [com/alicp/jetcache/autoconfigure/JetCacheAutoConfiguration.class] and overriding is disabled.

Action:

Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true

应该是引入springboot的redis依赖导致:

         <!--redis-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

这个该如何处理?

areyouok commented 2 years ago

和spring cache名字冲突了是吗,我看看啊

areyouok commented 2 years ago

我有个提交改了bean名字,你clone下来用master分支自己build一个试试?版本是2.7.0-SNAPSHOT

mvn -DskipTests clean install

herodotus-ecosystem commented 2 years ago

用了 2.7.0.RC3 一段时间了,没有发现 CacheManager 注入失败问题,用得好好的。感觉 CacheManager 注入失败相关的ISSUE,大多都和自身的环境有关系。依赖其它的starter,自身环境的模块逻辑以及Bean的注入顺序和逻辑都会有影响。

我的代码供参考:

@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(CacheProperties.class)
@Import({CaffeineConfiguration.class, RedisConfiguration.class})
@AutoConfigureAfter(JetCacheAutoConfiguration.class)
public class JetCacheConfiguration {

    private static final Logger log = LoggerFactory.getLogger(JetCacheConfiguration.class);

    @PostConstruct
    public void postConstruct() {
        log.debug("[Herodotus] |- SDK [Engine Cache JetCache] Auto Configure.");
    }

    @Bean
    @ConditionalOnClass(CacheManager.class)
    public JetCacheCreateCacheFactory jetCacheCreateCacheFactory(CacheManager cacheManager) {
        JetCacheCreateCacheFactory factory = new JetCacheCreateCacheFactory(cacheManager);
        JetCacheUtils.setJetCacheCreateCacheFactory(factory);
        log.trace("[Herodotus] |- Bean [Jet Cache Create Cache Factory] Auto Configure.");
        return factory;
    }

    @Bean
    @Primary
    @ConditionalOnMissingBean
    public HerodotusCacheManager herodotusCacheManager(JetCacheCreateCacheFactory jetCacheCreateCacheFactory, CacheProperties cacheProperties) {
        HerodotusCacheManager herodotusCacheManager = new HerodotusCacheManager(jetCacheCreateCacheFactory, cacheProperties);
        herodotusCacheManager.setAllowNullValues(cacheProperties.getAllowNullValues());
        log.trace("[Herodotus] |- Bean [Jet Cache Herodotus Cache Manager] Auto Configure.");
        return herodotusCacheManager;
    }
}
chuyuanlinzi commented 2 years ago

用了 2.7.0.RC3 一段时间了,没有发现 CacheManager 注入失败问题,用得好好的。感觉 CacheManager 注入失败相关的ISSUE,大多都和自身的环境有关系。依赖其它的starter,自身环境的模块逻辑以及Bean的注入顺序和逻辑都会有影响。

我的代码供参考:

@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(CacheProperties.class)
@Import({CaffeineConfiguration.class, RedisConfiguration.class})
@AutoConfigureAfter(JetCacheAutoConfiguration.class)
public class JetCacheConfiguration {

    private static final Logger log = LoggerFactory.getLogger(JetCacheConfiguration.class);

    @PostConstruct
    public void postConstruct() {
        log.debug("[Herodotus] |- SDK [Engine Cache JetCache] Auto Configure.");
    }

    @Bean
    @ConditionalOnClass(CacheManager.class)
    public JetCacheCreateCacheFactory jetCacheCreateCacheFactory(CacheManager cacheManager) {
        JetCacheCreateCacheFactory factory = new JetCacheCreateCacheFactory(cacheManager);
        JetCacheUtils.setJetCacheCreateCacheFactory(factory);
        log.trace("[Herodotus] |- Bean [Jet Cache Create Cache Factory] Auto Configure.");
        return factory;
    }

    @Bean
    @Primary
    @ConditionalOnMissingBean
    public HerodotusCacheManager herodotusCacheManager(JetCacheCreateCacheFactory jetCacheCreateCacheFactory, CacheProperties cacheProperties) {
        HerodotusCacheManager herodotusCacheManager = new HerodotusCacheManager(jetCacheCreateCacheFactory, cacheProperties);
        herodotusCacheManager.setAllowNullValues(cacheProperties.getAllowNullValues());
        log.trace("[Herodotus] |- Bean [Jet Cache Herodotus Cache Manager] Auto Configure.");
        return herodotusCacheManager;
    }
}

方便发下jetcache的依赖吗,我正在验证你的方法,但发现JetCacheCreateCacheFactory和JetCacheUtils未找到对应依赖

chuyuanlinzi commented 2 years ago

我有个提交改了bean名字,你clone下来用master分支自己build一个试试?版本是2.7.0-SNAPSHOT

mvn -DskipTests clean install

重新依赖修改后的SNAPSHOT版本解决了,应该是Springboot中CacheManager和JetCache中CacheManager重名导致

chuyuanlinzi commented 2 years ago

我有个提交改了bean名字,你clone下来用master分支自己build一个试试?版本是2.7.0-SNAPSHOT

mvn -DskipTests clean install

非常感谢,希望合并到后续的release版本中,方便无缝升级

areyouok commented 2 years ago

试试2.7.0吧

herodotus-ecosystem commented 2 years ago

用了 2.7.0.RC3 一段时间了,没有发现 CacheManager 注入失败问题,用得好好的。感觉 CacheManager 注入失败相关的ISSUE,大多都和自身的环境有关系。依赖其它的starter,自身环境的模块逻辑以及Bean的注入顺序和逻辑都会有影响。 我的代码供参考:

@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(CacheProperties.class)
@Import({CaffeineConfiguration.class, RedisConfiguration.class})
@AutoConfigureAfter(JetCacheAutoConfiguration.class)
public class JetCacheConfiguration {

    private static final Logger log = LoggerFactory.getLogger(JetCacheConfiguration.class);

    @PostConstruct
    public void postConstruct() {
        log.debug("[Herodotus] |- SDK [Engine Cache JetCache] Auto Configure.");
    }

    @Bean
    @ConditionalOnClass(CacheManager.class)
    public JetCacheCreateCacheFactory jetCacheCreateCacheFactory(CacheManager cacheManager) {
        JetCacheCreateCacheFactory factory = new JetCacheCreateCacheFactory(cacheManager);
        JetCacheUtils.setJetCacheCreateCacheFactory(factory);
        log.trace("[Herodotus] |- Bean [Jet Cache Create Cache Factory] Auto Configure.");
        return factory;
    }

    @Bean
    @Primary
    @ConditionalOnMissingBean
    public HerodotusCacheManager herodotusCacheManager(JetCacheCreateCacheFactory jetCacheCreateCacheFactory, CacheProperties cacheProperties) {
        HerodotusCacheManager herodotusCacheManager = new HerodotusCacheManager(jetCacheCreateCacheFactory, cacheProperties);
        herodotusCacheManager.setAllowNullValues(cacheProperties.getAllowNullValues());
        log.trace("[Herodotus] |- Bean [Jet Cache Herodotus Cache Manager] Auto Configure.");
        return herodotusCacheManager;
    }
}

方便发下jetcache的依赖吗,我正在验证你的方法,但发现JetCacheCreateCacheFactory和JetCacheUtils未找到对应依赖

这两个类是方便自己使用jetcache,用于实现使用jetcache作为JPA的二级缓存,就是用jetcache 2.7.0 QuickConfig 方式构造缓存。从使用jetcache的角度可以忽略不计,这里只是为了说明注入 jetcache CacheManager我这里是没有问题的。

我的代码中就仅是依赖了:

        <dependency>
            <groupId>com.alicp.jetcache</groupId>
            <artifactId>jetcache-starter-redis-lettuce</artifactId>
        </dependency>

相关代码在:https://gitee.com/herodotus/dante-engine 工程的,engine-cache --> cache-sdk-jetcache 的包中,感兴趣可以看一下。

JanYork commented 1 year ago

请问解决了吗?我在boot2.7.0并且也使用了spring-data-redis的项目中整合jetcache启动显示redis冲突了,求一份解决方案

yangxing19930210 commented 2 months ago

用@Autowired吧,优先根据类型装配,如果是用的@Resource,会根据bean name装配,就会注入失败