iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >springboot redis使用lettuce配置多数据源的实现
  • 494
分享到

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

2024-04-02 19:04:59 494人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

目前项目上需要连接两个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;
 

@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;
 
    
    @Bean
    @Primary
    @ConfigurationProperties(prefix = "spring.redis.cluster.lettuce.pool")
    public GenericObjectPoolConfig redisPool() {
        return new GenericObjectPoolConfig();
    }
 
    
    @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;
 
    }
 
    
    @Bean("lettuceConnectionFactory")
    @Primary
    public LettuceConnectionFactory lettuceConnectionFactory(GenericObjectPoolConfig redisPool, @Qualifier("redisClusterConfig") RedisClusterConfiguration redisClusterConfig) {
        LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder().poolConfig(redisPool).build();
        return new LettuceConnectionFactory(redisClusterConfig, clientConfiguration);
    }
 
    
    @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);
    }
 
 
    
    @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


    
    @Resource(name = "redisTemplateSingle")
    private RedisTemplate redisTemplateSingle;

使用redis集群


    
    @Resource(name = "redisTemplate")
    private RedisTemplate redisTemplate;
 

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

--结束END--

本文标题: springboot redis使用lettuce配置多数据源的实现

本文链接: https://www.lsjlt.com/news/124953.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作