有时候我们需要连接多个不同的redis来进行读写,下面是一个Springboot的多数据源配置demo;配置比较简单,我使用的Springboot的版本是:2.1.0.RELEASE,Springboot默认使用的连接池是:Lettuce
代码清单:
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
配置文件:RedisConfig.java
@Configuration
public class RedisConfig {
@Bean(name = "redisTemplate")
@Primary
public RedisTemplate<String, Object> redisTemplate(@Value("${spring.redis.host}") String host,
@Value("${spring.redis.port}") int port,
@Value("${spring.redis.password}") String password,
@Value("${spring.redis.database}") int database,
@Value("${spring.redis.timeout}") long timeout,

本文展示了如何在Springboot项目中配置多个Redis数据源。使用的是Springboot 2.1.0.RELEASE版本,连接池为Lettuce。通过代码清单,包括pom.xml配置和RedisConfig.java配置文件,详细解释了配置过程。测试验证了多数据源的有效性,还提供了Github代码示例链接供参考。
最低0.47元/天 解锁文章
6033

被折叠的 条评论
为什么被折叠?



