欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Spring boot如何快速的配置多个Redis数据源

程序员文章站 2022-05-26 09:59:44
简介redis 多数据源主要的运用场景是在需要使用多个redis服务器或者使用多个redis库,本文采用的是fastdep依赖集成框架,快速集成redis多数据源并集成lettuce连接池,只需引入依...

简介

redis 多数据源主要的运用场景是在需要使用多个redis服务器或者使用多个redis库,本文采用的是fastdep依赖集成框架,快速集成redis多数据源并集成lettuce连接池,只需引入依赖后在yaml文件中配置多数据源连接信息即可。

源码地址
希望大家可以star支持一下,后续还会加入其它依赖的简易整合。

引入依赖

maven

<dependency>
  <groupid>com.louislivi.fastdep</groupid>
  <artifactid>fastdep-redis</artifactid>
  <version>1.0.1</version>
</dependency>

gradle

compile group: 'com.louislivi.fastdep', name: 'fastdep-redis', version: '1.0.1'

配置文件

fastdep:
  redis:
   redis1: #连接名称
    database: 0
    host: 192.168.12.88
    port: 6379
    lettuce: #下面为连接池的补充设置
     shutdown-timeout: 100 # 关闭超时时间
     pool:
      max-active: 18 # 连接池最大连接数(使用负值表示没有限制)
      max-idle: 8 # 连接池中的最大空闲连接
      max-wait: 30 # 连接池最大阻塞等待时间(使用负值表示没有限制)
      min-idle: 0 # 连接池中的最小空闲连接
   redis2: #连接名称
    database: 1
    host: 192.168.12.88
    port: 6379
    lettuce: #下面为连接池的补充设置
     shutdown-timeout: 100 # 关闭超时时间
     pool:
      max-active: 18 # 连接池最大连接数(使用负值表示没有限制)
      max-idle: 8 # 连接池中的最大空闲连接
      max-wait: 30 # 连接池最大阻塞等待时间(使用负值表示没有限制)
      min-idle: 0 # 连接池中的最小空闲连接

运用

@autowired
private stringredistemplate redis1stringredistemplate;
// 注入时 redis1 代表配置文件中的连接名称 stringredistemplate 为固定注入redis对象类型,
// 会自动根据注入的变量名进行匹配

@autowired
private stringredistemplate redis2stringredistemplate;


@getmapping("redis")
public void redis() {
  system.out.println(redis1stringredistemplate.opsforvalue().get("test"));
  system.out.println(redis2stringredistemplate.opsforvalue().get("test"));
}

扩展

有时候需要自定义redistemplate序列化和增加一些额外的配置,这时候我们可以封装一个redis工具类来实现

package com.louislivi.fastdep.test.utils;

import org.springframework.beans.factory.annotation.autowired;
import org.springframework.data.redis.core.redistemplate;
import org.springframework.data.redis.core.stringredistemplate;
import org.springframework.data.redis.serializer.stringredisserializer;
import org.springframework.stereotype.component;

/**
 * redisutil
 * 
 * @author : louislivi
 */
@component
public class redisutil {
  @autowired
  private stringredistemplate redis1stringredistemplate;

  @autowired
  private stringredistemplate redis2stringredistemplate;

  @autowired
  private redistemplate redis2redistemplate;

  @autowired
  private redistemplate redis1redistemplate;

  public redistemplate redistemplate(string name) {
    redistemplate redistemplate;
    switch (name) {
      case "redis2":
        redistemplate = redis2redistemplate;
        break;
      default:
        redistemplate = redis1redistemplate;
        break;
    }
    stringredisserializer stringredisserializer = new stringredisserializer();
    redistemplate.setkeyserializer(stringredisserializer);
    redistemplate.setvalueserializer(stringredisserializer);
    redistemplate.sethashkeyserializer(stringredisserializer);
    redistemplate.sethashvalueserializer(stringredisserializer);
    return redistemplate;
  }

  public stringredistemplate stringredistemplate(string name) {
    stringredistemplate stringredistemplate;
    switch (name) {
      case "redis2":
        stringredistemplate = redis2stringredistemplate;
        break;
      default:
        stringredistemplate = redis1stringredistemplate;
        break;
    }
    stringredistemplate.setenabletransactionsupport(true);
    return stringredistemplate;
  }
}
@autowired
private redisutil redisutil;


@getmapping("redis")
public void redis() {
  system.out.println(redisutil.redistemplate("redis1").opsforvalue().get("test"));
  system.out.println(redisutil.stringredistemplate("redis2").opsforvalue().get("test"));
}

原理

使用importbeandefinitionregistrar beandefinitionbuilder.genericbeandefinition动态注入bean其实很简单有兴趣可以去看看源码,这样的依赖集成是不是简单了很多呢?

希望大家能够支持开源,给个小星星,后续还会继续开发其他依赖的整合,甚至兼容其他框架使用。fastdep让java整合依赖更简单。在此也招募有志同道合的coder共同完善这个项目。

到此这篇关于spring boot如何快速的配置多个redis数据源的文章就介绍到这了,更多相关spring boot多个redis数据源内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!