当我将Redis与springboot2.x集成时,我遇到了两个相互交织的问题:
在这里我简要地解释一下,DefaultJedisClientConfiguration
是JedisClientConfiguration
的实现类,而MutableJedisClientConfiguration
也是实现类,作为JedisConnectionFactory
中的静态内部类。 / p>
为什么要调用setTimeout()
中不赞成使用的方法JedisConnectionFactory
?此外,当我使用.yml
文件配置redis资源时,没有这样的错误,即不会调用该函数,而当使用.properties
文件时,它将被调用
/** @deprecated */ @Deprecated public void setTimeout(int timeout) { this.getMutableConfiguration().setReadTimeout(Duration.ofMillis((long)timeout)); this.getMutableConfiguration().setConnectTimeout(Duration.ofMillis((long)timeout)); }
//现在,将执行Assert.state()。
private JedisConnectionFactory.MutableJedisClientConfiguration getMutableConfiguration() { Assert.state(this.clientConfiguration instanceof JedisConnectionFactory.MutableJedisClientConfiguration, () -> { return String.format("Client configuration must be instance of MutableJedisClientConfiguration but is %s", new Object[]{ClassUtils.getShortName(this.clientConfiguration.getClass())}); }); return (JedisConnectionFactory.MutableJedisClientConfiguration)this.clientConfiguration; }
当我们传递poolConfig
时,JedisConnectionFactory
将生成MutableJedisClientConfiguration
,但是当我们自己构建JedisClientConfiguration
时,它就是生成的DefaultJedisClientConfiguration
我的配置Java代码:
@Configuration @PropertySource("classpath:config/redis.yml") // .yml @EnableAutoConfiguration class RedisConfig { @Value("${spring.redis.password:123456}") // can not read password, should set default value private String password; @Bean @ConfigurationProperties(prefix="spring.redis.pool") public JedisPoolConfig jedisPoolConfig(){ return new JedisPoolConfig(); } @Bean @ConfigurationProperties(prefix="spring.redis") public JedisConnectionFactory jedisConnectionFactory(){ RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration(); redisStandaloneConfiguration.setPassword(RedisPassword.of(password)); JedisConnectionFactory jedisConnectionFactory = null; JedisClientConfiguration.JedisClientConfigurationBuilder jedisClientConfiguration = JedisClientConfiguration.builder(); jedisConnectionFactory = new JedisConnectionFactory(redisStandaloneConfiguration, jedisClientConfiguration.build()); // build() -> DefaultJedisClientConfiguration return jedisConnectionFactory; } }
我的属性文件:
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.name=localhost_redis
spring.redis.timeout=5000
spring.redis.password=123456
spring.redis.minIdle=1
spring.redis.maxTotal=5000
spring.redis.numTestsPerEvictionRun=100
spring.redis.timeBetweenEvictionRunsMillis=6000
spring.redis.testWhileIdle=true
spring.redis.testOnBorrow=true
spring.redis.testOnReturn=true
spring.redis.pool.max-idle=10
spring.redis.pool.max-wait=5000
yml文件:
spring:
redis:
dbIndex: 0
hostName: localhost
password: 123456
port: 6379
timeout: 5000
pool:
maxIdle: 8
minIdle: 0
maxActive: 8
maxWait: -1
错误:
Description:
Failed to bind properties under 'spring.redis' to org.springframework.data.redis.connection.jedis.JedisConnectionFactory:
Property: spring.redis.timeout
Value: 5000
Origin: "spring.redis.timeout" from property source "class path resource [config/redis.properties]"
Reason: Client configuration must be instance of MutableJedisClientConfiguration but is DefaultJedisClientConfiguration
Action:
Update your application's configuration