SpringBoot配置多個Redis資料來源

2021-10-05 13:36:57 字數 3495 閱讀 8158

一. 新增依賴

org.springframework.boot

spring-boot-starter-data-redis

redis.clients

jedis

spring:

redis:

onedb:

host:

port: 6379

password:

database: 10

timeout: 5000

threedb:

host:

port: 6379

password:

database: 14

timeout: 5000

sixdb:

host:

port: 6379

password:

database: 17

timeout: 5000

三. redistemplateconfig

package com.ly.backstage.config.database;

import org.springframework.beans.factory.annotation.value;

import org.springframework.context.annotation.bean;

import org.springframework.context.annotation.configuration;

import org.springframework.data.redis.connection.redisconnectionfactory;

import org.springframework.data.redis.connection.jedis.jedisconnectionfactory;

import org.springframework.data.redis.core.stringredistemplate;

import org.springframework.util.stringutils;

import redis.clients.jedis.jedispoolconfig;

/** * @author: willis

* @date: 2020/4/28 10:57

*/@configuration

public class redistemplateconfig ")

private string onehost;

@value("$")

private integer oneport;

@value("$")

private string onepassword;

@value("$")

private integer onedatabase;

//threedb

@value("$")

private string threehost;

@value("$")

private integer threeport;

@value("$")

private string threepassword;

@value("$")

private integer threedatabase;

//sixdb

@value("$")

private string sixhost;

@value("$")

private integer sixport;

@value("$")

private string sixpassword;

@value("$")

private integer sixdatabase;

private static final int max_idle = 200; //最大空閒連線數

private static final int max_total = 1024; //最大連線數

private static final long max_wait_millis = 10000; //建立連線最長等待時間

//配置工廠

public redisconnectionfactory connectionfactory(string host, int port, string password, int maxidle,

int maxtotal, long maxwaitmillis, int index)

if (index != 0)

jedisconnectionfactory.setpoolconfig(poolconfig(maxidle, maxtotal, maxwaitmillis, false));

jedisconnectionfactory.afterpropertiesset();

return jedisconnectionfactory;

}//連線池配置

public jedispoolconfig poolconfig(int maxidle, int maxtotal, long maxwaitmillis, boolean testonborrow)

@bean(name = "redistemplate1")

public stringredistemplate redistemplate1()

@bean(name = "redistemplate3")

public stringredistemplate redistemplate3()

@bean(name = "redistemplate6")

public stringredistemplate redistemplate6()

}

四. 分別呼叫

@autowired

@resource(name = "redistemplate1")

stringredistemplate oneredis;

@autowired

@resource(name = "redistemplate3")

stringredistemplate threeredis;

@autowired

@resource(name = "redistemplate6")

stringredistemplate sixredis;

五. 額外:當儲存資料的key value為json格式時(未驗證)

引入 fastjson 依賴

com.alibaba

fastjson

1.2.58

在 redistemplateconfig 中,新增序列化方法. 需序列化時呼叫

public void redisserializeconfig(stringredistemplate template)

Spring Boot配置多個DataSource

廖雪峰 程式設計 1 13 10 11 閱讀 14041 使用spring boot時,預設情況下,配置datasource非常容易。spring boot會自動為我們配置好乙個datasource。org.hsqldbgroupid hsqldbartifactid runtimescope de...

springboot 多個redis配置

configurationproperties 註解會把配置檔案裡spring.redis開頭的屬性,賦值給bean裡對應的物件.configuration conditionalonproperty import public class redisconfig bean configuratio...

SpringBoot配置多個mysql資料來源

當我們在進行資料庫分庫分表操作是可能會需要到多個資料庫,那麼我們就需要對多個資料庫的資料來源進行配置.整理一下,今天在springboot框架下多個資料來源的配置過程 兩個為例 1.配置資料庫資訊 在yml配置檔案中配置需要的資料庫資訊 spring datasource 1 配置第乙個資料庫的位址...