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);
    }

}

 

你可能感兴趣的:(spring,boot,redis,redis)