DB/Redis
SpringBoot와 Redis Master-Slave 연동
잔망루피
2024. 4. 2. 17:30
🐟 RedisConfig 클래스에 아래 내용 추가
@Value("${redis.master.host}")
private String redisHost;
@Value("${redis.master.port}")
private int redisPort;
@Bean
public RedisConnectionFactory redisConnectionFactory() {
LettuceClientConfiguration clientConfig =
LettuceClientConfiguration.builder()
.readFrom(
ReadFrom.REPLICA_PREFERRED) // replica에서 우선적으로 읽지만 replica에서 읽어오지 못할
// 경우 Master에서 읽어옴
.build();
RedisStaticMasterReplicaConfiguration slaveConfig =
new RedisStaticMasterReplicaConfiguration(redisHost, redisPort);
return new LettuceConnectionFactory(slaveConfig, clientConfig);
}
🥕 yml
redis:
master:
host: 172.23.0.1
port: 27890
slaves:
- host: 172.23.0.1
port: 27891
- host: 172.23.0.1
port: 27892
🎀 스프링부트와 연동 완료
참고 👇👇👇
https://velog.io/@ililil9482/Redis-Spring-Boot-Master-Slave-%EC%A0%81%EC%9A%A9%ED%95%98%EA%B8%B0
반응형