在springboot1.2.3中集成elasticcache(集群模式开启)

dy2hfwbg  于 2021-07-03  发布在  Java
关注(0)|答案(0)|浏览(140)

我有一个弹性缓存redis cluster->shard-2,replica-1。我正在尝试将它与我的spring boot应用程序集成 (version-1.2.3) . 默认的spring data redis版本1.4.1不支持redis集群配置。将spring数据redis版本升级到更高版本->1.8时。绝地武士 version:-2.9.0 我面临以下问题issue:-

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cacheManager' defined in class path resource [mobi/fareye/config/RedisConfiguration.class]: Post-processing failed of bean type [class org.springframework.data.redis.cache.RedisCacheManager] failed; nested exception is java.lang.IllegalStateException: Failed to introspect bean class [org.springframework.data.redis.cache.RedisCacheManager] for persistence metadata: could not find class that it depends on
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyMergedBeanDefinitionPostProcessors(AbstractAutowireCapableBeanFactory.java:936)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:512)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
    at mobi.fareye.Application.main(Application.java:71)
Caused by: java.lang.IllegalStateException: Failed to introspect bean class [org.springframework.data.redis.cache.RedisCacheManager] for persistence metadata: could not find class that it depends on
    at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findPersistenceMetadata(PersistenceAnnotationBeanPostProcessor.java:396)
    at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessMergedBeanDefinition(PersistenceAnnotationBeanPostProcessor.java:333)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyMergedBeanDefinitionPostProcessors(AbstractAutowireCapableBeanFactory.java:931)
    ... 13 more
Caused by: java.lang.NoClassDefFoundError: org/springframework/cache/support/AbstractValueAdaptingCache
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:756)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:468)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:74)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:369)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:363)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:362)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
    at java.lang.Class.getDeclaredMethods(Class.java:1975)
    at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.buildPersistenceMetadata(PersistenceAnnotationBeanPostProcessor.java:420)
    at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findPersistenceMetadata(PersistenceAnnotationBeanPostProcessor.java:392)
    ... 15 more
Caused by: java.lang.ClassNotFoundException: org.springframework.cache.support.AbstractValueAdaptingCache
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
    ... 32 more

我的配置类:-

@Configuration
public class RedisConfiguration {

    private final Integer maxInactiveIntervalInSeconds = 60*60*24;

    @Value("${spring.redis.host}")
    private String redisConnectionHostName;

    @Value("${spring.redis.port}")
    private Integer redisConnectionPort;

    @Value("${spring.redis.maxIdle:8}")
    private Integer redisConnectionMaxIdle;

    @Value("${spring.redis.maxTotal:8}")
    private Integer redisConnectionMaxTotal;

    @Value("${spring.redis.timeout:50}")
    private Integer redisConnectionTimeout;

    @Value("${app.REDIS_CACHE_DEFAULT_EXPIRY_TIME_IN_SECONDS}")
    private Long REDIS_CACHE_DEFAULT_EXPIRY_TIME_IN_SECONDS;

    @Bean
    public RedisConnectionFactory redisConnectionFactory() {
        List<String> nodes = Collections.singletonList(redisConnectionHostName+":redisConnectionPort);
        RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration(nodes);
        JedisConnectionFactory redisConnectionFactory = new JedisConnectionFactory(redisClusterConfiguration);
        if(redisConnectionHostName == null) {
            throw new BeanCreationException("Missing yml configuration : \"spring.redis.host\". " +
                "Unable to create bean for RedisConnectionFactory.");
        } else if(redisConnectionPort == null) {
            throw new BeanCreationException("Missing yml configuration : \"spring.redis.port\". " +
                "Unable to create bean for RedisConnectionFactory.");
        }
//        redisConnectionFactory.setHostName(redisConnectionHostName);
//        redisConnectionFactory.setPort(redisConnectionPort);
//        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
//        jedisPoolConfig.setMaxTotal(redisConnectionMaxTotal);
//        jedisPoolConfig.setMaxIdle(redisConnectionMaxIdle);
//        jedisPoolConfig.setMaxWaitMillis(redisConnectionTimeout);
//        redisConnectionFactory.setPoolConfig(jedisPoolConfig);
        return redisConnectionFactory;
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate() {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory());
        return template;
    }

    @Bean(name = "stringRedisTemplate")
    public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
        StringRedisTemplate redisTemplate = new StringRedisTemplate();
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        return redisTemplate;
    }

    @Bean(name = "objectRedisTemplate")
    public RedisTemplate<String, Object> objectRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        return redisTemplate;
    }

    @Bean
    public CacheManager cacheManager() {

        RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate());
        cacheManager.setDefaultExpiration(REDIS_CACHE_DEFAULT_EXPIRY_TIME_IN_SECONDS);
        cacheManager.setUsePrefix(true);
        return cacheManager;

    }
}

根据我的发现,这个错误与springdataredis所需的spring上下文的不同版本有关。我想避免spring版本升级。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题