美文网首页
Springboot2.x 之redis 多数据源配置

Springboot2.x 之redis 多数据源配置

作者: 骑蚂蚁上高速_jun | 来源:发表于2020-08-28 00:20 被阅读0次
  1. 添加 pom.xml 坐标
<!-- redis  -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency> <!--配置redis连接池-->
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
</dependency>
  1. 连接属性在 application.yaml 中配置
spring:
  # 配置源 1
  redis1:
    database: 0
    host: 127.0.0.1
    port: 6379
    password: ""
    lettuce:
      pool:
        max-active: 8
        max-wait: 1
        max-idle: 8
        min-idle: 0 #
  redis2:
    database: 0
    host: 127.0.0.1
    port: 6379
    password: ""
    lettuce:
      pool:
        max-active: 8
        max-wait: 1
        max-idle: 8
        min-idle: 0 #
  1. 创建 Configuration 类 接管默认配置
package cn.waimaolang.demo.configura;

import cn.waimaolang.demo.command.MyMessageListenCommand;
import cn.waimaolang.demo.service.MessageConsumerService;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
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.DefaultLettucePool;
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.listener.ChannelTopic;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.io.Serializable;


@Configuration
public class RedisConfigura {

    @Value("${spring.redis1.host}")
    private String host;
    @Value("${spring.redis1.port}")
    private int port;
    @Value("${spring.redis1.password}")
    private String password;

    /**
     * 配置数据源1 基本连接属性
     * @return
     */
    private RedisStandaloneConfiguration getRedisConfig(){
        RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(host,port);
        config.setPassword(password);
        config.setDatabase(0);
        return config;
    }

    /**
     * 配置数据源1 连接池配置
     * @return
     */
    private LettuceClientConfiguration getLettuceConfig(){
        GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
        poolConfig.setMaxTotal(8);
        poolConfig.setMaxIdle(8);
        poolConfig.setMinIdle(1);
        poolConfig.setMaxWaitMillis(8);
        return  LettucePoolingClientConfiguration.builder().poolConfig(poolConfig).build();
    }

    /**
     * 实例配置
     * @return RedisTemplate
     */
    @Bean("redis_1")
    public RedisTemplate<String, Serializable> redisTemplate() {
        RedisTemplate<String, Serializable> redisTemplate = new RedisTemplate<>();
        redisTemplate.setKeySerializer(new StringRedisSerializer()); // redis 序列化数据key的方式
/* 能与其他语言 相互编码缓存 value编码方式,如果与其他语言混合开发项目,需要获取相同的缓存,
        则使用此种方式编码 */
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);  

        // 另外一种序列化方式
        //redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        LettuceConnectionFactory factory = new LettuceConnectionFactory(getRedisConfig(), getLettuceConfig());
        factory.afterPropertiesSet();
        redisTemplate.setConnectionFactory(factory);
        return redisTemplate;
    }

    /**
     * 发布/订阅
     * RedisMessageListenerContainer提供订阅消息的多路分发,这样多个订阅可以共享同一个Redis连接.
     */
    @Bean
    public RedisMessageListenerContainer redisContainer(LettuceConnectionFactory connectionFactory) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.addMessageListener(new MyMessageListenCommand(), new ChannelTopic("tenmao.blog.channel"));
        return container;
    }

}
# 以上只配置了数据源1 ; 数据源2的配置方法同理, 多写几个配置方法就行

3 . 调用

@Autowired
    @Qualifier("redis_1") // 引用redis1 实例
    RedisTemplate redisTemplate;

public void services(){
      redisTemplate.opsForValue().set("name","xxxxx");
}

相关文章

网友评论

      本文标题:Springboot2.x 之redis 多数据源配置

      本文链接:https://www.haomeiwen.com/subject/iycksktx.html