溫馨提示×

springboot redis多數(shù)據(jù)源如何配置

小樊
81
2024-11-11 18:28:31
欄目: 云計算

在Spring Boot中配置Redis多數(shù)據(jù)源,你需要遵循以下步驟:

  1. 添加依賴

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

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
  1. 配置數(shù)據(jù)源

application.ymlapplication.properties文件中,配置兩個不同的數(shù)據(jù)源。例如:

# 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. 創(chuàng)建數(shù)據(jù)源配置類

為每個數(shù)據(jù)源創(chuàng)建一個配置類,繼承JedisConnectionFactory,并設(shè)置相應(yīng)的屬性。例如:

@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. 創(chuàng)建RedisTemplate

為每個數(shù)據(jù)源創(chuàng)建一個RedisTemplate,并設(shè)置相應(yīng)的數(shù)據(jù)源。例如:

@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

在你的服務(wù)類中,注入相應(yīng)的RedisTemplate,然后使用它們操作不同的數(shù)據(jù)源。例如:

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

現(xiàn)在你已經(jīng)成功配置了兩個不同的Redis數(shù)據(jù)源,并可以使用RedisTemplate操作它們。

0