Spring Cloud Config 서비스와 라이선싱 서비스 연동 설정

2025. 3. 7. 16:17Spring Microservice

🎯 Licensing Service의 Spring Cloud Config 연동 정리 🎯

🚀 1. Spring Cloud Config 연동을 위한 기본 설정

Licensing Service가 Spring Cloud Config 서버에서 설정을 가져오려면 몇 가지 설정이 필요합니다.

📌 1) Maven 의존성 추가 (pom.xml)

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
</dependency>

설명

  • 🏗️ spring-cloud-starter-config: Spring Cloud Config 서버와 통신하는 기능을 제공
  • 💾 spring-boot-starter-data-jpa: JPA를 사용하기 위한 Spring Boot 의존성
  • 🛢️ postgresql: PostgreSQL 데이터베이스 드라이버

🌍 2. Licensing Service에서 Spring Cloud Config 서버 연결

Spring Cloud Config 서버에서 설정을 가져오려면 bootstrap.yml을 사용해야 합니다.

🔧 1) bootstrap.yml 설정

spring:
  application:
    name: licensing-service
  profiles:
    active: dev
  cloud:
    config:
      uri: http://localhost:8071

설명

  • 📛 spring.application.name: Licensing Service의 이름(이름과 동일한 설정 파일을 Config 서버에서 찾음)
  • 🌎 spring.profiles.active: 현재 실행할 환경을 dev로 설정
  • 🔗 spring.cloud.config.uri: Spring Cloud Config 서버 주소 설정(http://localhost:8071)

bootstrap.yml은 Spring Boot 애플리케이션이 실행될 때 가장 먼저 로드되는 설정 파일로, Config 서버에서 설정을 가져오는 역할을 합니다.

🏃 3. Licensing Service 실행 방법

▶️ 1) 기본 실행

Spring Cloud Config 서버를 먼저 실행한 후, Licensing Service를 실행합니다.

mvn spring-boot:run

설명

  • bootstrap.yml 설정을 기반으로 Config 서버에서 설정 정보를 가져와 자동으로 적용 💡

🔄 2) 커맨드를 사용해 실행 중 환경 설정 변경

Spring Boot 실행 시 -D 옵션을 사용하여 설정을 변경할 수 있습니다.

java -Dspring.cloud.config.uri=http://localhost:8071 \
     -Dspring.profiles.active=dev \
     -jar target/licensing-service-0.0.1-SNAPSHOT.jar

설명

  • 🔗 spring.cloud.config.uri: Spring Cloud Config 서버 주소 직접 지정
  • 🔄 spring.profiles.active: 실행할 프로필을 dev로 지정

🐳 4. Docker Compose를 이용한 실행

Docker 환경에서 실행할 경우, docker-compose.yml을 활용할 수 있습니다.

📜 1) docker-compose.yml 예제

licensingservice:
    image: ostock/licensing-service:0.0.1-SNAPSHOT
    ports:
      - "8080:8080"

    environment:
     SPRING_PROFILES_ACTIVE: "dev"
     SPRING_CLOUD_CONFIG_URI: http://configserver:8071

설명

  • 🌎 SPRING_PROFILES_ACTIVE: 실행할 환경을 dev로 설정
  • 🔗 SPRING_CLOUD_CONFIG_URI: Spring Cloud Config 서버 주소 지정

▶️ 2) 도커 컴포우즈 실행 커맨드

docker-compose up

설명

  • 각각의 Docker 컨테이너에서 Config 서버와 Licensing Service 실행 🚀

🔍 5. Actuator를 활용한 설정 확인

Spring Boot Actuator의 /actuator/env 엔드포인트를 호출하면 Config 서버에서 가져온 환경 정보를 확인할 수 있습니다.

🖥️ 1) /actuator/env 호출

curl http://localhost:8080/actuator/env

설명

  • 현재 애플리케이션의 환경 변수 JSON 형태로 출력 📜

📜 2) /actuator/env 응답 예시

{
    "activeProfiles": [
        "dev"
    ],
    "propertySources": [
        {
            "name": "bootstrapProperties-classpath:/config/licensing-service-dev.properties",
            "properties": {
                "spring.datasource.username": { "value": "postgres" },
                "spring.datasource.url": { "value": "jdbc:postgresql://localhost:5433/ostock_dev" },
                "example.property": { "value": "I AM DEV" }
            }
        },
        {
            "name": "bootstrapProperties-classpath:/config/licensing-service.properties",
            "properties": {
                "management.endpoints.web.exposure.include": { "value": "*" },
                "spring.jpa.properties.hibernate.dialect": { "value": "org.hibernate.dialect.PostgreSQLDialect" }
            }
        }
    ]
}

설명

  • 🎭 activeProfiles: 현재 활성화된 프로필(이 경우 dev)
  • 📂 bootstrapProperties-classpath:/config/licensing-service-dev.properties: dev 프로필용 설정 파일
  • 📂 bootstrapProperties-classpath:/config/licensing-service.properties: 기본 설정 파일

🔐 6. 보안 고려 사항

Spring Boot Actuator의 /actuator/env 엔드포인트는 보안 위험이 존재 ⚠️
👉 민감한 정보가 노출되지 않도록 보안 설정을 적용하는 것이 중요!

보안 조치 방법

  1. Actuator 엔드포인트 비활성화
    management.endpoints.web.exposure.include=none
  2. 민감한 정보 제한
    management.endpoint.env.show-values=when_authorized
  3. Spring Security 적용 🔐
    • 인증된 사용자만 접근 가능하도록 설정

📌 7. 전체 정리

단계 설명
1️⃣. 의존성 추가 spring-cloud-starter-config 포함
2️⃣. 설정 파일 작성 bootstrap.yml에서 Config 서버 주소 및 프로필 지정
3️⃣. 애플리케이션 실행 mvn spring-boot:run 또는 java -jar 실행
4️⃣. Docker Compose 실행 docker-compose.yml에서 환경 변수 설정 후 실행
5️⃣. 설정 확인 /actuator/env 엔드포인트 호출
6️⃣. 보안 조치 Actuator 설정 제한 및 인증 적용

🎯 핵심 요약

✅ Spring Cloud Config 서버와 연결하여 중앙에서 설정을 관리 🌍
bootstrap.yml에서 Config 서버 주소 및 프로필을 설정 ⚙️
✅ Docker Compose를 활용하여 컨테이너 환경에서도 쉽게 실행 가능 🐳
/actuator/env를 활용하여 설정이 정상적으로 반영되었는지 확인 🔍
보안 설정을 강화하여 불필요한 정보 노출 방지 🔐