温馨提示×

springboot redis多数据源如何配置

小樊
81
2024-11-11 18:28:31
栏目: 云计算

在Spring Boot中配置Redis多数据源,你需要遵循以下步骤:

  1. 添加依赖

在你的pom.xml文件中,添加Spring Boot Redis的starter依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
  1. 配置数据源

application.ymlapplication.properties文件中,配置两个不同的数据源。例如:

# application.yml
spring:
  redis:
    datasource-one:
      host: localhost
      port: 6379
      password: your_password
      database: 0
    datasource-two:
      host: localhost
      port: 6380
      password: your_password
      database: 1

或者

# application.properties
spring.redis.datasource-one.host=localhost
spring.redis.datasource-one.port=6379
spring.redis.datasource-one.password=your_password
spring.redis.datasource-one.database=0

spring.redis.datasource-two.host=localhost
spring.redis.datasource-two.port=6380
spring.redis.datasource-two.password=your_password
spring.redis.datasource-two.database=1
  1. 创建数据源配置类

为每个数据源创建一个配置类,继承JedisConnectionFactory,并设置相应的属性。例如:

@Configuration
public class RedisDataSourceOneConfig {

    @Bean
    public JedisConnectionFactory jedisConnectionFactory() {
        RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();
        config.setHostName("localhost");
        config.setPort(6379);
        config.setPassword("your_password");
        config.setDatabase(0);
        return new JedisConnectionFactory(config);
    }
}
@Configuration
public class RedisDataSourceTwoConfig {

    @Bean
    public JedisConnectionFactory jedisConnectionFactory() {
        RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();
        config.setHostName("localhost");
        config.setPort(6380);
        config.setPassword("your_password");
        config.setDatabase(1);
        return new JedisConnectionFactory(config);
    }
}
  1. 创建RedisTemplate

为每个数据源创建一个RedisTemplate,并设置相应的数据源。例如:

@Configuration
public class RedisTemplateConfig {

    @Autowired
    private JedisConnectionFactory jedisConnectionFactoryOne;

    @Bean
    public RedisTemplate<String, Object> redisTemplateOne() {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(jedisConnectionFactoryOne);
        return template;
    }
}
@Configuration
public class RedisTemplateConfig {

    @Autowired
    private JedisConnectionFactory jedisConnectionFactoryTwo;

    @Bean
    public RedisTemplate<String, Object> redisTemplateTwo() {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(jedisConnectionFactoryTwo);
        return template;
    }
}
  1. 使用RedisTemplate

在你的服务类中,注入相应的RedisTemplate,然后使用它们操作不同的数据源。例如:

@Service
public class MyService {

    @Autowired
    private RedisTemplate<String, Object> redisTemplateOne;

    @Autowired
    private RedisTemplate<String, Object> redisTemplateTwo;

    public void saveDataToDataSourceOne(String key, Object value) {
        redisTemplateOne.opsForValue().set(key, value);
    }

    public Object getDataFromDataSourceOne(String key) {
        return redisTemplateOne.opsForValue().get(key);
    }

    public void saveDataToDataSourceTwo(String key, Object value) {
        redisTemplateTwo.opsForValue().set(key, value);
    }

    public Object getDataFromDataSourceTwo(String key) {
        return redisTemplateTwo.opsForValue().get(key);
    }
}

现在你已经成功配置了两个不同的Redis数据源,并可以使用RedisTemplate操作它们。

0