Config Server와 다른 마이크로서비스들이 Docker 환경에서 올바르게 시작되고 실행될 수 있도록 설정하는 방법을 다룹니다. 다음은 각 과정별로 상세히 정리한 내용입니다.

1. Config Server의 Liveness와 Readiness 설정

  • Liveness와 Readiness는 애플리케이션이 정상적으로 작동 중인지, 요청을 처리할 준비가 되었는지를 나타냅니다.
  • Config Server에서 Actuator를 사용하여 /actuator/health/liveness/actuator/health/readiness API를 통해 liveness와 readiness 상태를 노출합니다.
  • 이러한 상태 정보는 Docker가 Config Server가 올바르게 시작되었는지 확인하는 데 사용됩니다.

2. Docker Compose 파일에 Health Check 추가

  • Docker Compose 파일에서 healthcheck 요소를 사용하여 Docker가 Config Server의 건강 상태를 평가하도록 설정합니다.

  • test 명령을 통해 curl을 사용하여 Config Server의 /actuator/health/readiness API를 호출합니다.

  • 성공 조건grep 명령으로 응답에 "UP" 문자열이 포함되어 있는지 확인하는 것입니다. 그렇지 않으면 exit 명령을 통해 Docker Compose가 이 서비스를 비정상으로 간주합니다.

  • 추가로 interval, timeout, retries, start_period 설정을 통해 health check를 반복할 간격, 타임아웃 시간, 재시도 횟수, 서비스 시작 후 체크 시작까지의 대기 시간을 설정합니다.

    코드 예시:

    config-server:
      image: eazybytes/configserver:S6
      container_name: configserver-ms
      ports:
        - "8071:8071"
      networks:
        - eazybank
      healthcheck:
        test: ["CMD-SHELL", "curl -f http://localhost:8071/actuator/health/readiness | grep 'UP'"]
        interval: 10s
        timeout: 5s
        retries: 10
        start_period: 10s

3. 마이크로서비스와 Config Server 간의 의존성 설정

  • 각 마이크로서비스(accounts, loans, cards)에서 depends_on 요소를 사용하여 Config Server가 완전히 시작되고 건강 상태가 양호한지 확인한 후에 마이크로서비스가 시작되도록 설정합니다.

  • depends_on 요소 내에서 condition: service_healthy를 설정하여 Config Server의 health check가 성공적으로 통과되었는지 확인 후 해당 마이크로서비스를 시작합니다.

    코드 예시:

    accounts:
      image: eazybytes/accounts:S6
      container_name: accounts-ms
      ports:
        - "8080:8080"
      networks:
        - eazybank
      depends_on:
        config-server:
          condition: service_healthy

4. RabbitMQ 서비스 추가

  • RabbitMQ는 Spring Cloud Bus가 올바르게 동작하기 위해 필요한 메시지 브로커입니다.

  • RabbitMQ의 포트(567215672)를 매핑하고, RabbitMQ가 제대로 시작되었는지 확인하기 위한 healthcheck 요소를 설정합니다.

  • RabbitMQ가 올바르게 시작되었는지 확인하기 위한 명령은 RabbitMQ 공식 문서에서 제공되는 테스트 명령을 사용합니다.

    코드 예시:

    rabbitmq:
      image: "rabbitmq:management"
      container_name: rabbitmq
      hostname: rabbitmq
      ports:
        - "5672:5672"
        - "15672:15672"
      networks:
        - eazybank
      healthcheck:
        test: ["CMD-SHELL", "rabbitmq-diagnostics -q ping"]
        interval: 10s
        timeout: 5s
        retries: 10
        start_period: 10s

5. Config Server와 RabbitMQ 간의 의존성 설정

  • Config Server가 RabbitMQ에 의존하므로, depends_on 요소를 사용하여 RabbitMQ가 시작된 후에 Config Server가 시작되도록 설정합니다.

  • 이 설정으로 RabbitMQ가 완전히 시작되고 health check를 통과해야만 Config Server가 시작됩니다.

    코드 예시:

    config-server:
      image: eazybytes/configserver:S6
      container_name: configserver-ms
      ports:
        - "8071:8071"
      networks:
        - eazybank
      depends_on:
        rabbitmq:
          condition: service_healthy
      healthcheck:
        test: ["CMD-SHELL", "curl -f http://localhost:8071/actuator/health/readiness | grep 'UP'"]
        interval: 10s
        timeout: 5s
        retries: 10
        start_period: 10s

6. 전체 Docker Compose 파일 구조

  • RabbitMQ, Config Server, Accounts, Loans, Cards 마이크로서비스가 정의된 Docker Compose 파일을 작성합니다.

  • 각 마이크로서비스는 Config Server가 완전히 시작된 후에 시작되도록 설정됩니다.

  • RabbitMQ는 Config Server와 다른 마이크로서비스들이 시작되기 전에 시작됩니다.

    최종 Docker Compose 파일:

    version: '3.7'
    
    services:
      rabbitmq:
        image: "rabbitmq:management"
        container_name: rabbitmq
        hostname: rabbitmq
        ports:
          - "5672:5672"
          - "15672:15672"
        networks:
          - eazybank
        healthcheck:
          test: ["CMD-SHELL", "rabbitmq-diagnostics -q ping"]
          interval: 10s
          timeout: 5s
          retries: 10
          start_period: 10s
    
      config-server:
        image: eazybytes/configserver:S6
        container_name: configserver-ms
        ports:
          - "8071:8071"
        networks:
          - eazybank
        depends_on:
          rabbitmq:
            condition: service_healthy
        healthcheck:
          test: ["CMD-SHELL", "curl -f http://localhost:8071/actuator/health/readiness | grep 'UP'"]
          interval: 10s
          timeout: 5s
          retries: 10
          start_period: 10s
    
      accounts:
        image: eazybytes/accounts:S6
        container_name: accounts-ms
        ports:
          - "8080:8080"
        networks:
          - eazybank
        depends_on:
          config-server:
            condition: service_healthy
        environment:
          - SPRING_CONFIG_IMPORT=configserver:http://config-server:8071
          - SPRING_PROFILES_ACTIVE=default
          - SPRING_APPLICATION_NAME=accounts
    
      loans:
        image: eazybytes/loans:S6
        container_name: loans-ms
        ports:
          - "8090:8090"
        networks:
          - eazybank
        depends_on:
          config-server:
            condition: service_healthy
        environment:
          - SPRING_CONFIG_IMPORT=configserver:http://config-server:8071
          - SPRING_PROFILES_ACTIVE=default
          - SPRING_APPLICATION_NAME=loans
    
      cards:
        image: eazybytes/cards:S6
        container_name: cards-ms
        ports:
          - "9000:9000"
        networks:
          - eazybank
        depends_on:
          config-server:
            condition: service_healthy
        environment:
          - SPRING_CONFIG_IMPORT=configserver:http://config-server:8071
          - SPRING_PROFILES_ACTIVE=default
          - SPRING_APPLICATION_NAME=cards
    
    networks:
      eazybank:
        driver: bridge

7. 향후 작업: Docker Compose 최적화

  • 현재 Docker Compose 파일에 중복된 설정을 제거하고, 공통 설정을 별도의 파일로 이동시켜 유지 관리의 편리성을 높입니다.
  • 이후 Docker Compose를 사용하여 모든 마이크로서비스를 시작하고 테스트합니다.

이와 같은 방식으로 Docker Compose 파일을 작성하고 최적화하여, 모든 마이크로서비스가 올바르게 시작될 수 있도록 설정할 수 있습니다.

profile
무슨 생각하며 사니

0개의 댓글

관련 채용 정보

Powered by GraphCDN, the GraphQL CDN