使用的springboot版本为2.2.0.RELEASE
jedis版本2.9.0,报错:
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'redisHealthContributor' defined in class path resource [org/springframework/boot/actuate/autoconfigure/redis/RedisHealthContributorAutoConfiguration.class]: Unsatisfied dependency expressed through method 'redisHealthContributor' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'taskConnectionFactory' defined in class path resource [com/mr/web/config/RedisServiceConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.redis.connection.RedisConnectionFactory]: Factory method 'taskConnectionFactory' threw exception; nested exception is java.lang.NoClassDefFoundError: redis/clients/jedis/util/SafeEncoderat org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:787)at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:528)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1338)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1177)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:557)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276)at org.springframework.beans.factory.support.DefaultListableBeanFactory.addCandidateEntry(DefaultListableBeanFactory.java:1503)at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1467)at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveMultipleBeans(DefaultListableBeanFactory.java:1386)at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1245)at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1207)at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:874)at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:778)... 95 common frames omitted
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'taskConnectionFactory' defined in class path resource [com/mr/web/config/RedisServiceConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.redis.connection.RedisConnectionFactory]: Factory method 'taskConnectionFactory' threw exception; nested exception is java.lang.NoClassDefFoundError: redis/clients/jedis/util/SafeEncoderat org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:645)at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:475)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1338)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1177)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:557)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276)at org.springframework.beans.factory.support.DefaultListableBeanFactory.addCandidateEntry(DefaultListableBeanFactory.java:1503)at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1467)at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveMultipleBeans(DefaultListableBeanFactory.java:1386)at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1245)at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1207)at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:874)at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:778)... 112 common frames omitted
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.redis.connection.RedisConnectionFactory]: Factory method 'taskConnectionFactory' threw exception; nested exception is java.lang.NoClassDefFoundError: redis/clients/jedis/util/SafeEncoderat org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:640)... 129 common frames omitted
Caused by: java.lang.NoClassDefFoundError: redis/clients/jedis/util/SafeEncoderat org.springframework.data.redis.connection.jedis.JedisConverters.lambda$static$1(JedisConverters.java:129)at org.springframework.data.redis.connection.jedis.JedisConverters.toBytes(JedisConverters.java:344)at org.springframework.data.redis.connection.jedis.JedisConverters.<clinit>(JedisConverters.java:136)at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.<clinit>(JedisConnectionFactory.java:85)at com.mr.web.config.RedisServiceConfig.taskConnectionFactory(RedisServiceConfig.java:48)at com.mr.web.config.RedisServiceConfig$$EnhancerBySpringCGLIB$$879aed86.CGLIB$taskConnectionFactory$1(<generated>)at com.mr.web.config.RedisServiceConfig$$EnhancerBySpringCGLIB$$879aed86$$FastClassBySpringCGLIB$$d00e144b.invoke(<generated>)at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244)at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:363)at com.mr.web.config.RedisServiceConfig$$EnhancerBySpringCGLIB$$879aed86.taskConnectionFactory(<generated>)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:498)at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)... 130 common frames omitted
Caused by: java.lang.ClassNotFoundException: redis.clients.jedis.util.SafeEncoderat java.net.URLClassLoader.findClass(URLClassLoader.java:381)at java.lang.ClassLoader.loadClass(ClassLoader.java:424)at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)at java.lang.ClassLoader.loadClass(ClassLoader.java:357)... 145 common frames omitted
Disconnected from the target VM, address: '127.0.0.1:55180', transport: 'socket'
使用的springboot-start-redis的时候,想自己生成RedisConnectionFactory,
@Configuration
public class RedisServiceConfig {
@Value("${redis.task.host}")private String redisHost;@Value("${redis.task.port}")private int redisPort;@Value("${redis.task.pass}")private String redisPass;@Value("${redis.task.db}")private int redisDb;@Value("${redis.task.config.timeout}")private String timeout;@Value("${redis.task.config.maxTotal}")private int maxTotal;@Value("${redis.task.config.maxIdle}")private int maxIdle;@Value("${redis.task.config.maxWaitMillis}")private int maxWaitMillis;@Value("${redis.task.config.minEvictableIdleTimeMillis}")private String minEvictableIdleTimeMillis;@Value("${redis.task.config.numTestsPerEvictionRun}")private int numTestsPerEvictionRun;@Value("${redis.task.config.timeBetweenEvictionRunsMillis}")private String timeBetweenEvictionRunsMillis;@Value("${redis.task.config.testOnBorrow}")private Boolean testOnBorrow;@Value("${redis.task.config.testWhileIdle}")private Boolean testWhileIdle;@Bean@Primarypublic RedisConnectionFactory taskConnectionFactory() {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();jedisPoolConfig.setMaxIdle(maxIdle);jedisPoolConfig.setMaxTotal(maxTotal);jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);jedisPoolConfig.setMinEvictableIdleTimeMillis(Integer.parseInt(minEvictableIdleTimeMillis));jedisPoolConfig.setNumTestsPerEvictionRun(numTestsPerEvictionRun);jedisPoolConfig.setTimeBetweenEvictionRunsMillis(Integer.parseInt(timeBetweenEvictionRunsMillis));jedisPoolConfig.setTestOnBorrow(testOnBorrow);jedisPoolConfig.setTestWhileIdle(testWhileIdle);JedisConnectionFactory connectionFactory = new JedisConnectionFactory(jedisPoolConfig);connectionFactory.setPort(redisPort);connectionFactory.setHostName(redisHost);connectionFactory.setDatabase(redisDb);
// connectionFactory.setPassword(redisPass);//配置连接池属性connectionFactory.setTimeout(Integer.parseInt(timeout));return connectionFactory;}@Beanpublic StringRedisTemplate taskRedisTemplate() {
StringRedisTemplate template = new StringRedisTemplate();template.setConnectionFactory(taskConnectionFactory());return template;}}
配置文件:
redis:task:host: 127.0.0.1port: 6379pass:db: 2config:
# 超时timeout: 10000
# 控制一个pool可分配多少个jedis实例maxTotal: 100
# 最大空闲数maxIdle: 10
# 最大等待时间maxWaitMillis: 100000
# 连接的最小空闲时间 默认1800000毫秒(30分钟)minEvictableIdleTimeMillis: 300000
# 每次释放连接的最大数目,默认3numTestsPerEvictionRun: 1024
# 逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程, 默认-1timeBetweenEvictionRunsMillis: 30000
# 是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个testOnBorrow: true
# 在空闲时检查有效性, 默认falsetestWhileIdle: true
使用2.9.0版本一直报错,然后将版本升级到,就不报错了…