springboot项目中多个Redis数据源配置

多个不同数据源的Redis配置类

需要引入jia

		<dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
        </dependency>

详细代码如下:

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import java.util.HashSet;
import java.util.Set;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;


/**
 * @author **********
 * Created on 2021/12/8.
 */
@Configuration
@EnableCaching
public class JedisClusterConfig {

	//nacos配置项取值
    @Value("${spring.redis.cluster.nodes}")
    String cluster1;

    @Value("${spring.redis1.cluster.nodes}")
    String cluster2;

    @Value("${spring.redis.password}")
    String password1;

    @Value("${spring.redis1.password}")
    String password2;

    @Value("${spring.redis.lettuce.pool.min-idle}")
    String minIdle;

    @Value("${spring.redis.lettuce.pool.max-idle}")
    String maxIdle;

    @Value("${spring.redis.lettuce.pool.max-active}")
    String active;

    @Value("${spring.redis.lettuce.pool.max-wait}")
    String wait;
    /**
     * 注意:
     * 这里返回的JedisCluster是单例的,并且可以直接注入到其他类中去使用
     * @return
     */
    @Bean(name = "name1")
    @Primary
    public JedisCluster getJedisCluster1() {
        String[] serverArray =cluster1.split(",");//获取服务器数组
        Set<HostAndPort> nodes = getHostAndPorts(serverArray);
        GenericObjectPoolConfig config = getGenericObjectPoolConfig();
        return new JedisCluster(nodes,10000,1000,1,password1,config);
    }

    /**
     * 注意:
     * 这里返回的JedisCluster是单例的,并且可以直接注入到其他类中去使用
     * @return
     */
    @Bean(name = "name2")
    public JedisCluster getJedisCluster2() {
        String[] serverArray =cluster2.split(",");//获取服务器数组
        Set<HostAndPort> nodes = getHostAndPorts(serverArray);
        GenericObjectPoolConfig config = getGenericObjectPoolConfig();
        return new JedisCluster(nodes,10000,1000,1,password2,config);
    }

    private Set<HostAndPort> getHostAndPorts(String[] serverArray) {
        Set<HostAndPort> nodes = new HashSet<>();
        for (String ipPort : serverArray) {
            String[] ipPortPair = ipPort.split(":");
            nodes.add(new HostAndPort(ipPortPair[0].trim(), Integer.parseInt(ipPortPair[1].trim())));
        }
        return nodes;
    }

    private GenericObjectPoolConfig getGenericObjectPoolConfig() {
        GenericObjectPoolConfig config = new GenericObjectPoolConfig();
        config.setMaxIdle(Integer.parseInt(maxIdle));
        config.setMaxTotal(Integer.parseInt(active));
        config.setMinIdle(Integer.parseInt(minIdle));
        config.setMaxWaitMillis(Long.parseLong(wait));
        return config;
    }


}

在其他类中注入哪个即用哪个

    @Resource(name = "name1")
    private JedisCluster jedisCluster;
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值