Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT] 단일 Redis -> Cluster Migration - #233 #234

Merged
merged 2 commits into from
Aug 31, 2024
Merged

Conversation

rlarlgnszx
Copy link
Member

🔥Pull requests

⛳️ 작업한 브랜치

👷 작업한 내용

-2가지의 Redis ConnectionFactory를 사용합니다.

  1. ElasticCache Redis와 연결하는 RedisConnectionFactory -> 단일포트연결
  2. Ec2 내부 RedisCluster와 연결하는 RedisConnectionFactory -> 6개 포트 연결
  3. 따라서 그에 따라 For + [ Cache , Cluster ] 로 명명했습니다.
  • 각각의 Connection을 통해 RedisTemplate 객체를 통해 알맞는 연결에 따른 객체를 보낼수 있습니다.
  • 단일 Redis 연결
  • public class RedisCacheConfig {
    
      @Value("${spring.data.redis.host}")
      private String host;
      @Value("${spring.data.redis.port}")
      private int port;
    
      @Bean
      public RedisConnectionFactory redisConnectionFactoryForCache() {
          return new LettuceConnectionFactory(new RedisStandaloneConfiguration(host, port));
      }
    
      @Bean
      public RedisTemplate<String, Object> redisTemplateForCache() {
          RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
          redisTemplate.setConnectionFactory(redisConnectionFactoryForCache());
          redisTemplate.setKeySerializer(new StringRedisSerializer());
          redisTemplate.setValueSerializer(new StringRedisSerializer());
          return redisTemplate;
      }
  • Cluster Redis 연결
    @Configuration
    public class RedisConfig {

    @Bean
    public RedisConnectionFactory redisConnectionFactoryForCluster() {
        RedisClusterConfiguration clusterConfig = new RedisClusterConfiguration()
                .clusterNode("127.0.0.1", 7000)
                .clusterNode("127.0.0.1", 7001)
                .clusterNode("127.0.0.1", 7002)
                .clusterNode("127.0.0.1", 7003)
                .clusterNode("127.0.0.1", 7004)
                .clusterNode("127.0.0.1", 7005);
        // 클러스터 모드로 LettuceConnectionFactory 설정
        LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory(clusterConfig);
        lettuceConnectionFactory.setShareNativeConnection(false); // 클러스터 모드에서 필요에 따라 사용할 수 있음
        return lettuceConnectionFactory;
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate() {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setEnableTransactionSupport(true);
        redisTemplate.setConnectionFactory(redisConnectionFactoryForCluster());
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new StringRedisSerializer());
        return redisTemplate;
    }

 }

🚨 참고 사항

  • EC2 내부 변경파일
  • deploy.sh : 기존 spring은 그대로 + cluster 생성로직 추가
  • redis_conf : redis 설정파일

📟 관련 이슈

Copy link
Contributor

@gardening-y gardening-y left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

수고하셨습니다!! pr에 왜이런 기술을 쓰게 되었는지 같이 작성해봐도 좋을 것 같아요!

@rlarlgnszx rlarlgnszx merged commit 4e0cd36 into develop Aug 31, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[FEAT] 단일 Redis -> Cluster Migration
2 participants