AWS Elastic Cache for Redis With Spring Boot

2023. 4. 15. 14:03·⚙️ Ops/AWS
목차
  1. 💡 ElasticCache for Redis
  2. 💡 사전 준비
  3. Elasticache Redis Cluster 생성
  4. EC2 Instance에 gcc설치후 redis-cli 컴파일
  5. 시스템 환경변수에 Redis Primary Node의 Endpoint를 넣고 application.yml에서 가져오기
  6. Spring Boot Project 내에 의존성 추가 & application.yml에 redis 정보 등록
  7. RedisConfig
  8. 💡 Redis in Local Server
  9. Local Server에서의 연동

💡 ElasticCache for Redis

Redis를 캐싱서버로 사용하는 이유

서버에 Refresh Token을 저장해야하는데 영구적으로 필요한 데이터가 아니기에, 리소스 절약하기 위해 사용

보통 휘발성 데이터들을 따로 빼기 위해 사용한다.


구현체
Lettuce, Jedis 중 Lettuce 사용


Lecttue - 비동기 처리, 성능up, 추가적인 의존성 필요X, 별도의 설정없이 Redis에 명령 가능
Jedis - Deprecated된 방법, 별도의 추가 의전송필요, None Thread-Safe


💡 사전 준비

Elasticache Redis Cluster 생성

AWS ElasticCache Redis Cluster 생성

img


EC2 Instance에 gcc설치후 redis-cli 컴파일

  • yum -y install gcc
  • wget http://download.redis.io/redis-stable.tar.gz && tar xvzf redis-stable.tar.gz && cd redis-stable && make
  • cp src/redis-cli /usr/bin/
  • redis-cli -h [Endpoint] -p 6379

img)img


시스템 환경변수에 Redis Primary Node의 Endpoint를 넣고 application.yml에서 가져오기

img


Spring Boot Project 내에 의존성 추가 & application.yml에 redis 정보 등록

implementation 'org.springframework.boot:spring-boot-starter-data-redis'


RedisConfig

/* Redis Template를 이용한 방식 */

@Configuration
@EnableRedisRepositories
@RequiredArgsConstructor
public class RedisConfig {

    @Value("${spring.redis.host}")
    private String redisHost;

    @Value("${spring.redis.port}")
    private int redisPort;

    private final RedisProperties redisProperties;

    /* RedisConnectionFactory 인터페이스를 통해 LettuceConnectionFactory 반환 */

    @Bean
    public RedisConnectionFactory factory() {
        RedisClusterConfiguration configuration = new RedisClusterConfiguration();
        configuration.clusterNode(redisHost, redisPort);

        LettuceClientConfiguration clientConfiguration = LettuceClientConfiguration.builder()
                .clientOptions(ClientOptions.builder()
                        .socketOptions(SocketOptions.builder()
                                .connectTimeout(Duration.ofMillis(redisProperties.getTimeout())).build())
                        .build())
                .commandTimeout(Duration.ofSeconds(redisProperties.getTimeout())).build();

        return new LettuceConnectionFactory(configuration, clientConfiguration);
    }

    @Bean
    public RedisTemplate<String, String> template() {
        /* RedisTemplate를 받아와 set, get, delete 사용 */
        RedisTemplate<String, String> template = new RedisTemplate<>();

        /* Key & Value Serializer, Factory 설정 */
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new StringRedisSerializer());
        template.setConnectionFactory(factory());

        return template;
    }
}

💡 Redis in Local Server

Local Server에서의 연동

  • Docker Compose Yml 작성후 Redis Container 생성 & 실행

img

  • 방화벽 포트 & 서비스 오픈

img

IntelliJ 내의 Docker 서비스와 로컬 개인서버 연결

img

연결 성공

img

  • Spring Boot Project 내에 의존성 추가 & application.yml에 redis 정보 등록

implementation 'org.springframework.boot:spring-boot-starter-data-redis'


Redis 서버 IP,Port 등록

img

Redis 서버와 정상 통신 가능

img

저작자표시

'⚙️ Ops > AWS' 카테고리의 다른 글

EC2 <-> ELB 연동 (Application LoadBalancer)  (0) 2023.06.08
AWS Parameter Store  (0) 2023.04.17
AWS EC2 Memory Swaing  (0) 2023.04.15
AWS RDS  (0) 2023.04.15
AWS Route 53  (0) 2023.04.15
  1. 💡 ElasticCache for Redis
  2. 💡 사전 준비
  3. Elasticache Redis Cluster 생성
  4. EC2 Instance에 gcc설치후 redis-cli 컴파일
  5. 시스템 환경변수에 Redis Primary Node의 Endpoint를 넣고 application.yml에서 가져오기
  6. Spring Boot Project 내에 의존성 추가 & application.yml에 redis 정보 등록
  7. RedisConfig
  8. 💡 Redis in Local Server
  9. Local Server에서의 연동
'⚙️ Ops/AWS' 카테고리의 다른 글
  • EC2 <-> ELB 연동 (Application LoadBalancer)
  • AWS Parameter Store
  • AWS EC2 Memory Swaing
  • AWS RDS
신건우
신건우
조용한 개발자
  • 신건우
    우주먼지
    신건우
  • 전체
    오늘
    어제
    • 분류 전체보기 (422)
      • 📘 Frontend (71)
        • Markup (1)
        • Style Sheet (2)
        • Dart (8)
        • Javascript (12)
        • TypeScript (1)
        • Vue (36)
        • React (2)
        • Flutter (9)
      • 📘 Backend (143)
        • Java (34)
        • Concurrency (19)
        • Reflection (1)
        • Kotlin (29)
        • Python (1)
        • Spring (42)
        • Spring Cloud (5)
        • Message Broker (5)
        • Streaming (2)
        • 기능 개발 (5)
      • 💻 Server (6)
        • Linux (6)
      • ❌ Error Handling (11)
      • 📦 Database (62)
        • SQL (31)
        • NoSQL (2)
        • JPQL (9)
        • QueryDSL (12)
        • Basic (4)
        • Firebase (4)
      • ⚙️ Ops (57)
        • CS (6)
        • AWS (9)
        • Docker (8)
        • Kubernetes (13)
        • MSA (1)
        • CI & CD (20)
      • 📚 Data Architect (48)
        • Data Structure (10)
        • Algorithm (8)
        • Programmers (17)
        • BaekJoon (5)
        • CodeUp (4)
        • Design Pattern (4)
        • AI (0)
      • ⚒️ Management & Tool (8)
        • Git (7)
        • IntelliJ (1)
      • 📄 Document (10)
        • Project 설계 (6)
        • Server Migration (3)
      • 📄 책읽기 (2)
        • 시작하세요! 도커 & 쿠버네티스 (2)
      • 🎮 Game (4)
        • Stardew Vally (1)
        • Path of Exile (3)
  • 블로그 메뉴

    • 링크

      • Github
    • 공지사항

    • 인기 글

    • 태그

      GStreamer #Pipeline
      React #Markdown
      Lock #Thread #Concurrency
    • 최근 댓글

    • 최근 글

    • hELLO· Designed By정상우.v4.10.0
    신건우
    AWS Elastic Cache for Redis With Spring Boot
    상단으로

    티스토리툴바

    단축키

    내 블로그

    내 블로그 - 관리자 홈 전환
    Q
    Q
    새 글 쓰기
    W
    W

    블로그 게시글

    글 수정 (권한 있는 경우)
    E
    E
    댓글 영역으로 이동
    C
    C

    모든 영역

    이 페이지의 URL 복사
    S
    S
    맨 위로 이동
    T
    T
    티스토리 홈 이동
    H
    H
    단축키 안내
    Shift + /
    ⇧ + /

    * 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.