이전 게시글 - Jwt 인증 + Redis BlackList
replicaof master 6379
redis_master:
container_name: master
image: redis
ports:
- 6379:6379
networks:
- aiary
redis_slave-a:
container_name: slave-a
image: redis
ports:
- 7000:6379
volumes:
- ./redis:/usr/local/etc/redis/
command: redis-server /usr/local/etc/redis/redis.conf
networks:
- aiary
redis_slave-b:
container_name: slave-b
image: redis
ports:
- 7001:6379
volumes:
- ./redis:/usr/local/etc/redis/
command: redis-server /usr/local/etc/redis/redis.conf
networks:
- aiary
networks:
aiary:
driver: bridge
→ Replica 성공
→ redis-cli
로 접속하고 데이터 저장
→ get key1 확인하니깐 slave redis에서도 hello redis가 나옴
→ 만약 replication에서 insert를 한다면 replica 옵션으로 master를 복제하고 있는 redis에서는 READONLY 옵션이 걸려있으므로 insert할 수 없다!
redis:
master:
host: localhost
port: 6379
slaves:
- host: localhost
port: 7000
- host: localhost
port: 7001
@Getter
@Setter
@NoArgsConstructor
@ConfigurationProperties(prefix = "spring.redis")
@Configuration
public class RedisInfo {
private String host;
private int port;
private RedisInfo master;
private List<RedisInfo> slaves;
}
@ConfigurationProperties(prefix = "spring.redis")
을 통해 yml 에서 설정한 redis 설정 값들을 가져온다.@RequiredArgsConstructor
@Configuration
@EnableRedisRepositories
public class RedisRepositoryConfig {
private final RedisInfo redisInfo;
@Bean
public LettuceConnectionFactory redisConnectionFactory() {
LettuceClientConfiguration clientConfig = LettuceClientConfiguration.builder()
.readFrom(ReadFrom.REPLICA_PREFERRED) // replica에서 우선적으로 읽지만 replica에서 읽어오지 못할 경우 Master에서 읽어옴
.build();
// replica 설정
RedisStaticMasterReplicaConfiguration slaveConfig = new RedisStaticMasterReplicaConfiguration(
redisInfo.getMaster().getHost(), redisInfo.getMaster().getPort());
// 설정에 slave 설정 값 추가
redisInfo.getSlaves().forEach(slave -> slaveConfig.addNode(slave.getHost(), slave.getPort()));
return new LettuceConnectionFactory(slaveConfig, clientConfig);
}
@Bean
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
redisTemplate.setConnectionFactory(redisConnectionFactory());
return redisTemplate;
}
}
public void logout(UserTokenReq userTokenReq) {
// Access 토큰에서 User 정보를 가져온다
Authentication authentication = jwtTokenProvider.getAuthentication(
userTokenReq.getAccessToken());
redisTemplate.opsForValue().get("RT:" + authentication.getName());
}