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

springboot redis使用lettuce配置多数据源的实现

程序员文章站 2024-01-19 14:11:58
目前项目上需要连接两个redis数据源,一个redis数据源是单机模式,一个redis数据源是分片集群模式,这里将具体配置列一下。项目用的springboot版本为 ...

目前项目上需要连接两个redis数据源,一个redis数据源是单机模式,一个redis数据源是分片集群模式,这里将具体配置列一下。

项目用的springboot版本为

    <parent>
        <groupid>org.springframework.boot</groupid>
        <artifactid>spring-boot-starter-parent</artifactid>
        <version>2.2.1.release</version>
        <relativepath/> <!-- lookup parent from repository -->
    </parent>

一、在yml中配置redis数据源信息

redis:
    cluster:
      nodes: 127.0.0.1:9001
    lettuce:
      #连接池配置
      pool:
        #连接池最大连接数
        max-active: 20
        #连接池最大等待时间,负数表示不做限制
        max-wait: -1
        #最大空闲连接
        max-idle: 9
        #最小空闲连接
        min-idle: 0
    timeout: 500000
  redis2:
    host: 127.0.0.1
    port: 6385
    lettuce:
      pool:
        max-active: 20
        max-idle: 8
        max-wait: -1
        min-idle: 0
    timeout: 500000

(这里的redis都没有配置密码)

二、添加redis配置类

package com.cq.config;
 
import cn.hutool.core.convert.convert;
import org.apache.commons.pool2.impl.genericobjectpoolconfig;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.beans.factory.annotation.qualifier;
import org.springframework.beans.factory.annotation.value;
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.core.env.environment;
import org.springframework.core.env.mappropertysource;
import org.springframework.data.redis.connection.redisclusterconfiguration;
import org.springframework.data.redis.connection.redisconnectionfactory;
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 org.springframework.data.redis.serializer.redisserializer;
import org.springframework.data.redis.serializer.stringredisserializer;
 
import java.io.serializable;
import java.util.hashmap;
import java.util.map;
 
/**
 * @author cccccloud on 2020/11/16 17:16
 */
@configuration
public class redisconfig {
 
    @autowired
    private environment environment;
 
    @value("${spring.redis2.host}")
    private string host;
    @value("${spring.redis2.port}")
    private string port;
    @value("${spring.redis2.lettuce.pool.max-active}")
    private string max_active;
    @value("${spring.redis2.lettuce.pool.max-idle}")
    private string max_idle;
    @value("${spring.redis2.lettuce.pool.max-wait}")
    private string max_wait;
    @value("${spring.redis2.lettuce.pool.min-idle}")
    private string min_idle;
 
    /**
     * 配置lettuce连接池
     *
     * @return
     */
    @bean
    @primary
    @configurationproperties(prefix = "spring.redis.cluster.lettuce.pool")
    public genericobjectpoolconfig redispool() {
        return new genericobjectpoolconfig();
    }
 
    /**
     * 配置第一个数据源的
     *
     * @return
     */
    @bean("redisclusterconfig")
    @primary
    public redisclusterconfiguration redisclusterconfig() {
 
        map<string, object> source = new hashmap<>(8);
        source.put("spring.redis.cluster.nodes", environment.getproperty("spring.redis.cluster.nodes"));
        redisclusterconfiguration redisclusterconfiguration;
        redisclusterconfiguration = new redisclusterconfiguration(new mappropertysource("redisclusterconfiguration", source));
        redisclusterconfiguration.setpassword(environment.getproperty("spring.redis.password"));
        return redisclusterconfiguration;
 
    }
 
    /**
     * 配置第一个数据源的连接工厂
     * 这里注意:需要添加@primary 指定bean的名称,目的是为了创建两个不同名称的lettuceconnectionfactory
     *
     * @param redispool
     * @param redisclusterconfig
     * @return
     */
    @bean("lettuceconnectionfactory")
    @primary
    public lettuceconnectionfactory lettuceconnectionfactory(genericobjectpoolconfig redispool, @qualifier("redisclusterconfig") redisclusterconfiguration redisclusterconfig) {
        lettuceclientconfiguration clientconfiguration = lettucepoolingclientconfiguration.builder().poolconfig(redispool).build();
        return new lettuceconnectionfactory(redisclusterconfig, clientconfiguration);
    }
 
    /**
     * 配置第一个数据源的redistemplate
     * 注意:这里指定使用名称=factory 的 redisconnectionfactory
     * 并且标识第一个数据源是默认数据源 @primary
     *
     * @param redisconnectionfactory
     * @return
     */
    @bean("redistemplate")
    @primary
    public redistemplate redistemplate(@qualifier("lettuceconnectionfactory") redisconnectionfactory redisconnectionfactory) {
        redistemplate<string, object> template = new redistemplate<>();
        template.setconnectionfactory(redisconnectionfactory);
 
        stringredisserializer stringredisserializer = new stringredisserializer();
        // key采用string的序列化方式
        template.setkeyserializer(stringredisserializer);
        // hash的key也采用string的序列化方式
        template.sethashkeyserializer(stringredisserializer);
        // value序列化方式采用jackson
        template.setvalueserializer(stringredisserializer);
        // hash的value序列化方式采用jackson
        template.sethashvalueserializer(stringredisserializer);
        template.afterpropertiesset();
 
        return template;
    }
 
    @bean
    public genericobjectpoolconfig redispool2() {
        genericobjectpoolconfig config = new genericobjectpoolconfig();
        config.setminidle(convert.toint(min_idle));
        config.setmaxidle(convert.toint(max_idle));
        config.setmaxtotal(convert.toint(max_active));
        config.setmaxwaitmillis(convert.toint(max_wait));
        return config;
    }
 
    @bean
    public redisstandaloneconfiguration redisconfig2() {
        redisstandaloneconfiguration redisconfig = new redisstandaloneconfiguration(host,convert.toint(port));
        return redisconfig;
    }
 
    @bean("factory2")
    public lettuceconnectionfactory factory2(@qualifier("redispool2") genericobjectpoolconfig config,
                                             @qualifier("redisconfig2") redisstandaloneconfiguration redisconfig) {//注意传入的对象名和类型redisstandaloneconfiguration
        lettuceclientconfiguration clientconfiguration = lettucepoolingclientconfiguration.builder().poolconfig(config).build();
        return new lettuceconnectionfactory(redisconfig, clientconfiguration);
    }
 
 
    /**
     * 单实例redis数据源
     *
     * @param connectionfactory
     * @return
     */
    @bean("redistemplatesingle")
    public redistemplate<string, object> redistemplatesingle(@qualifier("factory2")lettuceconnectionfactory connectionfactory) {//注意传入的对象名
        redistemplate<string, object> redistemplate = new redistemplate<>();
        redistemplate.setconnectionfactory(connectionfactory);
 
        redisserializer<string> redisserializer = new stringredisserializer();
        redistemplate.setkeyserializer(redisserializer);
        redistemplate.setvalueserializer(redisserializer);
        redistemplate.sethashkeyserializer(redisserializer);
        redistemplate.sethashvalueserializer(redisserializer);
        return redistemplate;
    }
}

三、使用redis

使用单实例redis

    /**
     * redis 单节点
     */
    @resource(name = "redistemplatesingle")
    private redistemplate redistemplatesingle;

使用redis集群

    /**
     * redis 集群
     */
    @resource(name = "redistemplate")
    private redistemplate redistemplate;
 

到此这篇关于springboot redis使用lettuce配置多数据源的实现的文章就介绍到这了,更多相关springboot lettuce多数据源内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!