在Spring Boot中配置Redis多數(shù)據(jù)源,你需要遵循以下步驟:
在你的pom.xml
文件中,添加Spring Boot Redis的starter依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
在application.yml
或application.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
為每個數(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);
}
}
為每個數(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;
}
}
在你的服務(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
操作它們。