springboot redis主从连法
时间: 2023-11-10 07:03:48 浏览: 168
springboot项目链接redis哨兵集群使用
在Spring Boot中使用Redis主从复制,需要在配置文件中指定主从节点的信息。具体步骤如下:
1. 在pom.xml文件中添加Redis和Lettuce的依赖:
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</dependency>
```
2. 在application.properties文件中配置Redis主从节点的信息:
```properties
# Redis主节点
spring.redis.host=192.168.0.1
spring.redis.port=6379
# Redis从节点
spring.redis.cluster.nodes=192.168.0.2:6379,192.168.0.3:6379
```
3. 在Redis配置类中创建RedisConnectionFactory对象,并设置主从节点信息:
```java
@Configuration
public class RedisConfig {
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private int port;
@Value("${spring.redis.cluster.nodes}")
private String clusterNodes;
@Bean
public RedisConnectionFactory redisConnectionFactory() {
RedisStandaloneConfiguration standaloneConfig = new RedisStandaloneConfiguration(host, port);
RedisClusterConfiguration clusterConfig = new RedisClusterConfiguration(Arrays.asList(clusterNodes.split(",")));
LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(clusterConfig, standaloneConfig);
return connectionFactory;
}
}
```
阅读全文