采用的Spring-boot-starter-data中的redis,通过lettuce创建工厂类进行创建bean
第一步 引入依赖
PS:采用的是Spring Boot 2.3.4.RELEASE版本
复制代码
1
2
3
4
5
6
7
8
9
10<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
第二步 配置yml
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25redis: host: r-2.redis.rds.aliyuncs.com port: 6379 password: 666 timeout: 5000 lettuce: pool: max-active: 32 max-wait: 300 max-idle: 16 min-idle: 8 database: 0 redis-one: host: r-2.redis.rds.aliyuncs.com port: 6379 password: 666 timeout: 5000 lettuce: pool: max-active: 32 max-wait: 300 max-idle: 16 min-idle: 8 database: 1
第三步 自定义配置了连接池对象和Redis的Standalone模式的配置(为了简化代码)
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13package net.zhongjunkeji.common.config.redis; import org.springframework.boot.autoconfigure.data.redis.RedisProperties; import org.springframework.data.redis.connection.RedisPassword; import org.springframework.data.redis.connection.RedisStandaloneConfiguration; public class UserRedisStandaloneConfiguration extends RedisStandaloneConfiguration { public UserRedisStandaloneConfiguration(RedisProperties redisProperties) { super.setHostName(redisProperties.getHost()); super.setPassword(RedisPassword.of(redisProperties.getPassword())); super.setPort(redisProperties.getPort()); super.setDatabase(redisProperties.getDatabase()); } }
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23package net.zhongjunkeji.common.config.redis; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import org.springframework.boot.autoconfigure.data.redis.RedisProperties; /** * <p> * 描述: 创建对象池() * </p> * * @author GHQ·阿甘 * @version 1.0.0 * @since 2021/7/6 23:34 **/ public class UserGenericObjectPoolConfig extends GenericObjectPoolConfig{ public UserGenericObjectPoolConfig(RedisProperties redisProperties){ this.setMaxTotal(redisProperties.getLettuce().getPool().getMaxActive()); this.setMaxIdle(redisProperties.getLettuce().getPool().getMaxIdle()); this.setMinIdle(redisProperties.getLettuce().getPool().getMinIdle()); this.setMaxWaitMillis(redisProperties.getLettuce().getPool().getMaxWait().toMillis()); } }
第四步 创建Lettuce工厂类和RedisTemlateBean
复制代码
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132package net.zhongjunkeji.common.config.redis; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.data.redis.RedisProperties; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.data.redis.connection.RedisStandaloneConfiguration; import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration; import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration; import org.springframework.data.redis.core.RedisTemplate; import java.time.Duration; /** * <p> * 描述: TODO * </p> * * @author GHQ·阿甘 * @version 1.0.0 * @since 2021-07-06 **/ @Configuration public class RedisMoreDataSourceConfig { /** * <p>描述: 默认(主0)Lettuce工厂类 * <p>开发者: GHQ·阿甘 * <p>时间: 2021/7/6 23:11 * * @param defaultRedisConfig * @param defaultPoolConfig * @return org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory * @throws **/ @Bean @Primary public LettuceConnectionFactory defaultLettuceConnectionFactory(RedisStandaloneConfiguration defaultRedisConfig, GenericObjectPoolConfig defaultPoolConfig) { LettuceClientConfiguration clientConfig = LettucePoolingClientConfiguration.builder().commandTimeout(Duration.ofMillis(100)) .poolConfig(defaultPoolConfig).build(); return new LettuceConnectionFactory(defaultRedisConfig, clientConfig); } /** * <p>描述: one Lettuce工厂类 * <p>开发者: GHQ·阿甘 * <p>时间: 2021/7/7 0:20 * * @param oneRedisConfig * @param onePoolConfig * @return org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory * @throws **/ @Bean("oneLettuceConnectionFactory") @ConditionalOnBean(name = "oneRedisConfig") public LettuceConnectionFactory oneLettuceConnectionFactory(@Qualifier("oneRedisConfig") RedisStandaloneConfiguration oneRedisConfig, @Qualifier("onePoolConfig") GenericObjectPoolConfig onePoolConfig) { LettuceClientConfiguration clientConfig = LettucePoolingClientConfiguration.builder().commandTimeout(Duration.ofMillis(100)).poolConfig(onePoolConfig).build(); return new LettuceConnectionFactory(oneRedisConfig, clientConfig); } /** * <p>描述: 主默认(0)RedisTemplate * <p>开发者: GHQ·阿甘 * <p>时间: 2021/7/7 0:26 * * @param defaultLettuceConnectionFactory * @return org.springframework.data.redis.core.RedisTemplate<java.lang.String, java.lang.String> * @throws **/ @Bean @Primary public RedisTemplate<String, String> defaultRedisTemplate( LettuceConnectionFactory defaultLettuceConnectionFactory) { RedisTemplate<String, String> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(defaultLettuceConnectionFactory); redisTemplate.afterPropertiesSet(); return redisTemplate; } /** * <p>描述: (1)RedisTemplate * <p>开发者: GHQ·阿甘 * <p>时间: 2021/7/7 0:26 * * @param localLettuceConnectionFactory * @return org.springframework.data.redis.core.RedisTemplate<java.lang.String, java.lang.String> * @throws **/ @Bean(name = "oneRedisTemplate") @ConditionalOnBean(name = "oneLettuceConnectionFactory") public RedisTemplate<String, String> oneRedisTemplate(@Qualifier("oneLettuceConnectionFactory") LettuceConnectionFactory localLettuceConnectionFactory) { RedisTemplate<String, String> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(localLettuceConnectionFactory); redisTemplate.afterPropertiesSet(); return redisTemplate; } @Configuration @ConfigurationProperties(prefix = "spring.redis-one") public class OneRedisConfig extends RedisProperties { @Bean("onePoolConfig") public GenericObjectPoolConfig onePoolConfig() { return new UserGenericObjectPoolConfig(this); } @Bean("oneRedisConfig") public RedisStandaloneConfiguration oneRedisConfig() { return new UserRedisStandaloneConfiguration(this); } } @Configuration @ConfigurationProperties(prefix = "spring.redis") public class DefaultRedisConfig extends RedisProperties { @Bean @Primary public GenericObjectPoolConfig defaultPoolConfig() { return new UserGenericObjectPoolConfig(this); } @Bean @Primary public RedisStandaloneConfiguration defaultRedisConfig() { return new UserRedisStandaloneConfiguration(this); } } }
最后
以上就是瘦瘦黑米最近收集整理的关于SpringBoot+RedisTemplate多数据源的全部内容,更多相关SpringBoot+RedisTemplate多数据源内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复