How can spring boot quickly configure multiple redis data sources

Time:2021-12-5

brief introduction

The main application scenario of redis multiple data sources is that multiple data sources need to be usedredisServer or use multipleredisLibrary, this paper usesfastdepRely on the integration framework for rapid integrationRedisMultiple data sources and integrationlettuceThe connection pool only needs to be created after the dependency is introducedyamlConfigure the connection information of multiple data sources in the file.

Source address

I hope you can support star and add other dependent simple integration in the future.

https://github.com/louislivi/fastdep

Introduce dependency

  • Maven
    <dependency>
    <groupId>com.louislivi.fastdep</groupId>
    <artifactId>fastdep-redis</artifactId>
    <version>1.0.1</version>
    </dependency>
  • Gradle
    compile group: 'com.louislivi.fastdep', name: 'fastdep-redis', version: '1.0.1'

configuration file

fastdep:

application

@Autowired

extend

Sometimes it needs to be customizedredisTemplateSerialize and add some additional configuration, at this time we can encapsulate oneredisTool class

package com.louislivi.fastdep.test.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;

/**
 * RedisUtil
 * 
 * @author : louislivi
 */
@Component
public class RedisUtil {
    @Autowired
    private StringRedisTemplate redis1StringRedisTemplate;

    @Autowired
    private StringRedisTemplate redis2StringRedisTemplate;

    @Autowired
    private RedisTemplate redis2RedisTemplate;

    @Autowired
    private RedisTemplate redis1RedisTemplate;

    public RedisTemplate redisTemplate(String name) {
        RedisTemplate redisTemplate;
        switch (name) {
            case "redis2":
                redisTemplate = redis2RedisTemplate;
                break;
            default:
                redisTemplate = redis1RedisTemplate;
                break;
        }
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        redisTemplate.setKeySerializer(stringRedisSerializer);
        redisTemplate.setValueSerializer(stringRedisSerializer);
        redisTemplate.setHashKeySerializer(stringRedisSerializer);
        redisTemplate.setHashValueSerializer(stringRedisSerializer);
        return redisTemplate;
    }

    public StringRedisTemplate stringRedisTemplate(String name) {
        StringRedisTemplate stringRedisTemplate;
        switch (name) {
            case "redis2":
                stringRedisTemplate = redis2StringRedisTemplate;
                break;
            default:
                stringRedisTemplate = redis1StringRedisTemplate;
                break;
        }
        stringRedisTemplate.setEnableTransactionSupport(true);
        return stringRedisTemplate;
    }
}
@Autowired
private RedisUtil redisUtil;

@GetMapping("redis")
public void redis() {
    System.out.println(redisUtil.redisTemplate("redis1").opsForValue().get("test"));
    System.out.println(redisUtil.stringRedisTemplate("redis2").opsForValue().get("test"));
}

principle

useImportBeanDefinitionRegistrar BeanDefinitionBuilder.genericBeanDefinitionDynamic injectionBeanIn fact, it is very simple. If you are interested, you can look at the source code. Is this dependency integration much simpler?

I hope you can support open source and give it a little star. In the future, you will continue to develop the integration of other dependencies and even be compatible with other frameworks.fastdepGive WayjavaIntegration dependency is simpler. We also recruit like-minded people herecoderWork together to improve the project.

This work adoptsCC agreement, reprint must indicate the author and the link to this article