Springboot配置多个redis连接

在这里插入图片描述

1. 添加Redis依赖

	首先,打开您的Spring Boot 项目的 pom.xml 文件并添加以下 Redis 相关依赖:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId></dependency><dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>                        
</dependency>

2. 配置多个 Redis 连接

接下来在 application.properties 文件中添加 Redis 连接的配置。
假设我们要添加两个 Redis 连接:分别为 Redis1 和 Redis2 添加了配置。
# Redis1 Configuration
spring.redis1.host=localhost
spring.redis1.port=6379                          
spring.redis1.password=                          
spring.redis1.pool.max-active=8                      
spring.redis1.pool.max-wait=-1                    
spring.redis1.pool.max-idle=8                   
spring.redis1.pool.min-idle=0
# Redis2 Configuration
spring.redis2.host=localhost
spring.redis2.port=6380                         
spring.redis2.password=                          
spring.redis2.pool.max-active=8                  
spring.redis2.pool.max-wait=-1                  
spring.redis2.pool.max-idle=8                    
spring.redis2.pool.min-idle=0

3. 在代码中使用多个 Redis 连接

为了在代码中使用多个 Redis 连接,我们首先需要创建一个配置类 RedisConfiguration
在该类中进行多个 Redis 连接的配置。
注意,这里的 @Primary 注解用于指定默认的 Redis 连接。
@Configuration
public class RedisConfiguration {

    @Bean(name = "redis1Template")
    @Primary
    public RedisTemplate<String, String> redis1Template() {
        RedisTemplate<String, String> template = new RedisTemplate<>();
        template.setConnectionFactory(redis1ConnectionFactory());
        return template;
    }

    @Bean(name = "redis2Template")
    public RedisTemplate<String, String> redis2Template() {
        RedisTemplate<String, String> template = new RedisTemplate<>();
        template.setConnectionFactory(redis2ConnectionFactory());
        return template;
    }

    @Bean
    @Primary
    public RedisConnectionFactory redis1ConnectionFactory() {
        return new JedisConnectionFactory(new RedisStandaloneConfiguration("localhost", 6379));
    }

    @Bean
    public RedisConnectionFactory redis2ConnectionFactory() {
        return new JedisConnectionFactory(new RedisStandaloneConfiguration("localhost", 6380));
    }
}
在上述代码中,我们为每个 Redis 连接定义了一个 Redistemplate 
并创建了两个 RedisConnectionFactory 类。
接下来,在我们的代码中我们可以使用这些对象来访问不同的 Redis 实例。示例如下:
@Autowired
@Qualifier("redis1Template")
RedisTemplate<String, String> redis1Template;

@Autowired
@Qualifier("redis2Template")
RedisTemplate<String, String> redis2Template;

// 访问redis1
redis1Template.opsForValue().set("key1", "value1");
String value1 = redis1Template.opsForValue().get("key1");

// 访问redis2
redis2Template.opsForValue().set("key2", "value2");
String value2 = redis2Template.opsForValue().get("key2");
上述代码在注入时指定了不同的 Qualifier,以便区分两个不同的 Redis 实例。​

在这里插入图片描述

更多学习资讯,学习源码,请在公众号耶宇星球领取
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
# 简介 springboot-skeleton 是一个基于SpringBoot,快速构建微服务项目的脚手架。内置mybatis-plus、alibaba-druid、xxl-job、redis、rocketMq等基础组件,并且包括了这些基础组件的使用demo; 本项目master分支作为基础脚手架,包括大家平时常用的基础依赖包,非常用包例如分库分表、多数据源等场景会通过特色分支来建设 # 功能特点 - [x] 基于Eureka注册中心 - [x] swagger 接口文档生成 - [x] 基于xxl-job的分布式任务调度器 - [x] ORM代码自动生成 - [x] 参数优雅验证 - [x] druid数据库连接池; - [x] 连接池监控UI; - [x] sharding-jdbc 分库分表中间件 - [x] 链路追踪组件 - [x] 配置中心 - [x] 分布式锁 redission # 核心组件 - [x] Skywalking - [x] Swagger-Ui - [x] Mybatis-plus - [x] Druid - [x] Eureka - [x] Xxl-job - [x] Sharding-jdbc - [x] hutool - [x] Redis - [ ] Nacos - [ ] RocketMq - [ ] Easypoi - [ ] OSS # 使用方式 待完善 ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。
【资源说明】 1、基于SpringBoot+Vue搭建的多功能体育场地智能管理系统源码+数据库+项目说明.zip 2、该资源包括项目的全部源码,下载可以直接使用! 3、本项目适合作为计算机、数学、电子信息等专业的课程设计、期末大作业和毕设项目,作为参考资料学习借鉴。 4、本资源作为“参考资料”如果需要实现其他功能,需要能看懂代码,并且热爱钻研,自行调试。 基于SpringBoot+Vue搭建的多功能体育场地智能管理系统源码+数据库+项目说明.zip # Stadium-management-system---体育馆场地管理系统-2.0 ## 项目说明 该项目为体育馆场地管理系统2.0版本,使用SpringBoot+Vue搭建,MyBatis+RabbitMq+Spring Security+锁+邮件服务+定时任务等技术。 * 该项目在1.0的基础上进行的改进,前后端采用json数据格式交互,实现前后端分离 * 并不是完全的前后端分离,只是用了vue做模板引擎,jquery+vue * 该项目使用了Thymeleaf,只是为了跳转更方便 * 该项目分为两个部分,都是对上个版本的重构,使用了两个端口,只是操作的同一个数据库 * 这里订单ID生成使用了雪花算法,uuid也行 --- ## 项目截图 >前台用户部分 ![image](https://github.com/Suarge/gms-2.0/blob/master/readm_photo/1.png) ![image](https://github.com/Suarge/gms-2.0/blob/master/readm_photo/2.png) ![image](https://github.com/Suarge/gms-2.0/blob/master/readm_photo/3.png) ![image](https://github.com/Suarge/gms-2.0/blob/master/readm_photo/4.png) >后台管理员部分 ![image](https://github.com/Suarge/gms-1.0/blob/master/readme_photo/6.png) ![image](https://github.com/Suarge/gms-1.0/blob/master/readme_photo/7.png) ![image](https://github.com/Suarge/gms-1.0/blob/master/readme_photo/8.png) ## 功能 用户部分: * 登录、注册、修改密码、注销登录 * 首页各种加载、场馆类型展示、通知信息展示、场馆详情展示 * 模糊查询订单 * 打印订单 新增: * 异步邮件发送 * 事务控制 * 双重检验锁控制线程安全 * 异步邮件发送 管理员部分: * 登录、注销 * 总览 * 场地管理 * 预约查询 * 通知发布 * 情况分析 ## 技术栈 * SpringBoot+Vue搭建 * MyBatis+mysql+durid * 消息队列RabbitMq * 定时任务 * Spring Security * 线程同步锁 * apache的poi文件导出api ## 改进 * 由于前后端分离,项目加载速度快了很多,如果页面加载增加redis作为缓存会更好 * 使用了更好地框架进行搭建,更方便简单 * 项目的主要业务逻辑增加,主要是加锁和异步邮件,以及事务控制的问题(这里都在一个代码里面) * 主要的改造思路在项目中的**改造思路**文件夹中 ## 安装 ### 1、下载项目到本地 ```xml git clone https://github.com/Suarge/gms-2.0.git ``` ### 2. 导入项目 该项目是用idea创建的,可以直接导入到idea中 ### 3.设置durid连接数据库 * 将`gms.sql`中的sql文件运行,并修改对应的配置文件durid * 具体设置在`application.properties`里修改,如果出现乱码请修改编码 ### 4.修改application.properties * 在`application.properties`配置文件中修改成自己的qq和stmp的密码 * 其他信息可以根据自己需要修改 ### 5.下载rabbitmq消息队列 我使用的是**rabbitmq3.6.9** 和**erlang8.3**直接在windows上安装的 ### 4.启动项目 * 因为前端是ajax访问的,所以日期不对的话下面的场馆表格是不会加载的,gms.sq
Spring Boot可以通过配置多个Redis连接池来实现集成多个Redis。具体步骤如下: 1. 在`application.properties`文件中添加多个Redis连接配置,如下所示: ``` # Redis1 spring.redis.host=127.0.0.1 spring.redis.port=6379 spring.redis.password= # Redis2 spring.redis2.host=127.0.0.1 spring.redis2.port=6380 spring.redis2.password= ``` 2. 在`@Configuration`类中创建多个`RedisConnectionFactory`对象,如下所示: ``` @Configuration public class RedisConfig { @Autowired private Environment environment; @Bean(name = "redis1ConnectionFactory") public RedisConnectionFactory redis1ConnectionFactory() { JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(); jedisConnectionFactory.setHostName(environment.getProperty("spring.redis.host")); jedisConnectionFactory.setPort(Integer.parseInt(environment.getProperty("spring.redis.port"))); jedisConnectionFactory.setPassword(environment.getProperty("spring.redis.password")); return jedisConnectionFactory; } @Bean(name = "redis2ConnectionFactory") public RedisConnectionFactory redis2ConnectionFactory() { JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(); jedisConnectionFactory.setHostName(environment.getProperty("spring.redis2.host")); jedisConnectionFactory.setPort(Integer.parseInt(environment.getProperty("spring.redis2.port"))); jedisConnectionFactory.setPassword(environment.getProperty("spring.redis2.password")); return jedisConnectionFactory; } } ``` 3. 在需要使用Redis的地方使用`@Qualifier`注解指定使用哪个`RedisConnectionFactory`,如下所示: ``` @Autowired @Qualifier("redis1ConnectionFactory") private RedisConnectionFactory redis1ConnectionFactory; @Autowired @Qualifier("redis2ConnectionFactory") private RedisConnectionFactory redis2ConnectionFactory; ``` 4. 使用`redis1ConnectionFactory`和`redis2ConnectionFactory`创建`RedisTemplate`对象,并使用`@Qualifier`注解指定使用哪个`RedisConnectionFactory`,如下所示: ``` @Bean(name = "redis1Template") public RedisTemplate redis1Template() { RedisTemplate redisTemplate = new RedisTemplate(); redisTemplate.setConnectionFactory(redis1ConnectionFactory); return redisTemplate; } @Bean(name = "redis2Template") public RedisTemplate redis2Template() { RedisTemplate redisTemplate = new RedisTemplate(); redisTemplate.setConnectionFactory(redis2ConnectionFactory); return redisTemplate; } ``` 5. 在需要使用Redis的地方使用`@Qualifier`注解指定使用哪个`RedisTemplate`,如下所示: ``` @Autowired @Qualifier("redis1Template") private RedisTemplate redis1Template; @Autowired @Qualifier("redis2Template") private RedisTemplate redis2Template; ``` 至此,Spring Boot项目就成功集成了多个Redis。在使用时,只需要使用对应的`RedisTemplate`即可。例如,使用`redis1Template`操作Redis1,使用`redis2Template`操作Redis2。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

北顾丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值