Springboot redisマルチデータソースケース


以下はspringbootマルチデータソースのケースです.ここで共有して記録します.
pomファイル:


	4.0.0

	
		org.springframework.boot
		spring-boot-starter-parent
		2.3.0.RELEASE
	

	com.xinx
	redis-multi-data-source
	0.0.1-SNAPSHOT
	redis-multi-data-source
	Redis multi-data source demo project for Spring Boot

	
		UTF-8
		UTF-8
		1.8
	

	
		
			org.springframework.boot
			spring-boot-starter
		

		
			org.springframework.boot
			spring-boot-starter-data-redis
		

		
			org.springframework.boot
			spring-boot-starter-test
			test
			
				
					org.junit.vintage
					junit-vintage-engine
				
			
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	



プロジェクトプロファイル:
spring:
  redis:
    database: 0
    host-name: localhost
    port: 6379
  redis1:
    database: 1
    host-name: localhost
    port: 6379

マルチデータソースプロファイル:
package com.xinx.redismultidatasource.config.redis;

import org.springframework.beans.factory.annotation.Qualifier;
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.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;

/**
 * redis       
 *
 * @author xinx
 * @date 2020/9/11 17:21
 */
@Configuration
public class MultiResourceRedisConfig {

    /**
     *          
     *
     * @return
     */
    @Bean
    @ConfigurationProperties(prefix = "spring.redis")
    public RedisStandaloneConfiguration redisConfig() {
        return new RedisStandaloneConfiguration();
    }

    /**
     *         
     *
     * @return
     */
    @Bean
    @ConfigurationProperties(prefix = "spring.redis1")
    public RedisStandaloneConfiguration redisConfig1() {
        return new RedisStandaloneConfiguration();
    }

    /**
     *              
     *     :    @Primary   bean   ,              LettuceConnectionFactory
     *
     * @return
     */
    @Primary
    @Bean("factory")
    public LettuceConnectionFactory factory(@Qualifier("redisConfig") RedisStandaloneConfiguration redisConfig) {
        return new LettuceConnectionFactory(redisConfig);
    }

    @Bean("factory1")
    public LettuceConnectionFactory factory1(@Qualifier("redisConfig1")RedisStandaloneConfiguration redisConfig1) {
        return new LettuceConnectionFactory(redisConfig1);
    }

    @Primary
    @Bean("stringRedisTemplate")
    public StringRedisTemplate stringRedisTemplate(@Qualifier("factory") LettuceConnectionFactory factory) {
        StringRedisTemplate temple = new StringRedisTemplate();
        temple.setConnectionFactory(factory);
        return temple;
    }

    @Bean("stringRedisTemplate1")
    public StringRedisTemplate stringRedisTemplate1(@Qualifier("factory1") LettuceConnectionFactory factory1) {
        StringRedisTemplate temple = new StringRedisTemplate();
        temple.setConnectionFactory(factory1);
        return temple;
    }

}

テストクラス:
package com.xinx.redismultidatasource;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.concurrent.TimeUnit;

@SpringBootTest
class RedisMultiDataSourceApplicationTests {

    @Autowired
	@Qualifier("stringRedisTemplate")
    private StringRedisTemplate stringRedisTemplate;

    @Test
    void operateDb() {
        stringRedisTemplate.opsForValue().set("test0", "test", 1, TimeUnit.SECONDS);
    }

    @Autowired
	@Qualifier("stringRedisTemplate1")
    private StringRedisTemplate stringRedisTemplate1;

    @Test
    void operateDb1() {
        stringRedisTemplate1.opsForValue().set("test1", "test", 1, TimeUnit.SECONDS);
    }

}