Skip to content

Commit

Permalink
[feat] Redis 비밀번호 설정 - #271
Browse files Browse the repository at this point in the history
  • Loading branch information
rlarlgnszx committed Sep 9, 2024
1 parent 4449255 commit bfde5e8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,22 @@
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
public class RedisClusterConfig {

@Value("${aws.ip}")
private String host;
@Value("${spring.data.redis.cluster.password}")
private String password;

private RedisConnectionFactory redisConnectionFactory;
@Bean
Expand All @@ -46,6 +50,7 @@ public RedisConnectionFactory redisConnectionFactoryForCluster() {
.clusterNode(host, 7004)
.clusterNode(host, 7005)
.clusterNode(host, 7006);
clusterConfig.setPassword(RedisPassword.of(password));
SocketOptions socketOptions = SocketOptions.builder()
.connectTimeout(Duration.ofSeconds(3L))
.tcpNoDelay(true)
Expand All @@ -56,7 +61,7 @@ public RedisConnectionFactory redisConnectionFactoryForCluster() {
.builder()
.dynamicRefreshSources(true)
.enableAllAdaptiveRefreshTriggers()
.enablePeriodicRefresh(Duration.ofSeconds(30))
.enablePeriodicRefresh(Duration.ofHours(1L))
.build();

ClusterClientOptions clusterClientOptions = ClusterClientOptions
Expand Down Expand Up @@ -96,6 +101,7 @@ public RedisTemplate<String, String> redistemplateForCluster() {
redisTemplate.setValueSerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new StringRedisSerializer());
// redisTemplate.setEnableTransactionSupport(true);
return redisTemplate;
}

Expand Down
12 changes: 8 additions & 4 deletions redis-cluster.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ if command -v hostname >/dev/null 2>&1; then
else
REDIS_IP=$(hostname -I | awk '{print $1}') # Linux
fi
export REDISCLI_AUTH=$(echo $REDISCLI_AUTH)
else
echo "호스트 이름 명령어를 찾을 수 없습니다."
exit 1
fi

# 환경변수 설정
export REDIS_IP

export REDISCLI_AUTH
# Redis 설정 파일을 저장할 디렉토리 생성
mkdir -p redis_conf

Expand All @@ -31,7 +32,8 @@ cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 3000
appendonly yes
# requirepass xxxx
requirepass ${REDISCLI_AUTH}
masterauth ${REDISCLI_AUTH}
protected-mode no
bind 0.0.0.0
cluster-announce-ip ${REDIS_IP}
Expand All @@ -47,7 +49,8 @@ cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 3000
appendonly yes
# requirepass xxxx
requirepass ${REDISCLI_AUTH}
masterauth ${REDISCLI_AUTH}
protected-mode no
bind 0.0.0.0
cluster-announce-ip ${REDIS_IP}
Expand All @@ -56,6 +59,7 @@ cluster-announce-bus-port $((17001 + ${slave_ports[$i]} - 7001))
EOL
done


# Docker Compose로 Redis 노드들을 백그라운드에서 실행
docker compose -f docker-compose-local.yml up -d redis-master-1 redis-master-2 redis-master-3 redis-slave-1 redis-slave-2 redis-slave-3 redis
echo "REDIS IP : ${REDIS_IP}"
Expand All @@ -70,7 +74,7 @@ docker exec -it redis-master-1 redis-cli --cluster create \
redis-slave-1:7004 \
redis-slave-2:7005 \
redis-slave-3:7006 \
--cluster-replicas 1 --cluster-yes
--cluster-replicas 1 -a ${REDISCLI_AUTH} --cluster-yes

# 성공 메시지 출력
echo "Redis cluster has been created successfully."

0 comments on commit bfde5e8

Please sign in to comment.