springboot(23)自定义RedisConnectionFactory

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
@Value("${spring.redis.host}")
private String redisHost;

@Value("${spring.redis.port}")
private int redisPort;

@Value("${spring.redis.timeout}")
private int redisTimeout;

@Value("${spring.redis.password}")
private String redisAuth;

@Value("${spring.redis.database}")
private int redisDb;

@Value("${spring.redis.jedis.pool.max-active}")
private int maxActive;

@Value("${spring.redis.jedis.pool.max-wait}")
private int maxWait;

@Value("${spring.redis.jedis.pool.max-idle}")
private int maxIdle;

@Value("${spring.redis.jedis.pool.min-idle}")
private int minIdle;

@Bean
public RedisConnectionFactory connectionFactory() {
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxTotal(maxActive);
poolConfig.setMaxIdle(maxIdle);
poolConfig.setMaxWaitMillis(maxWait);
poolConfig.setMinIdle(minIdle);
poolConfig.setTestOnBorrow(true);
poolConfig.setTestOnReturn(false);
poolConfig.setTestWhileIdle(true);
JedisClientConfiguration clientConfig = JedisClientConfiguration.builder()
.usePooling().poolConfig(poolConfig).and().readTimeout(Duration.ofMillis(redisTimeout)).build();

// 单点redis
RedisStandaloneConfiguration redisConfig = new RedisStandaloneConfiguration();
// 哨兵redis
// RedisSentinelConfiguration redisConfig = new RedisSentinelConfiguration();
// 集群redis
// RedisClusterConfiguration redisConfig = new RedisClusterConfiguration();
redisConfig.setHostName(redisHost);
redisConfig.setPassword(RedisPassword.of(redisAuth));
redisConfig.setPort(redisPort);
redisConfig.setDatabase(redisDb);

return new JedisConnectionFactory(redisConfig,clientConfig);
}
>