Spring Cloud 마이크로서비스 - Eureka와 Multi-Module 프로젝트

Published: (December 30, 2025 at 11:19 PM EST)
4 min read
Source: Dev.to

Source: Dev.to

마이크로서비스와 Spring Cloud

마이크로서비스 아키텍처는 대규모 애플리케이션을 작은 서비스 단위로 분리하여 독립적으로 개발, 배포, 확장할 수 있게 합니다. Spring Cloud는 이러한 마이크로서비스 구축을 위한 다양한 도구를 제공합니다.

Eureka는 Netflix OSS의 서비스 디스커버리 솔루션으로, 마이크로서비스들의 위치를 관리합니다. 서비스 인스턴스가 동적으로 생성·삭제될 때, Eureka가 IP와 Port 정보를 자동으로 관리합니다.

[Service A] ----→ [Eureka Server] ←---- [Service B]

              서비스 위치 정보 관리

Eureka Server 설정

의존성

implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server'

메인 클래스

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}

application.yml

server:
  port: 8761

eureka:
  client:
    register-with-eureka: false
    fetch-registry: false
  server:
    wait-time-in-ms-when-sync-empty: 0

Eureka Client 설정

의존성

implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'

application.yml

spring:
  application:
    name: user-service

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

서비스 간 호출

RestTemplate 사용

@Configuration
public class RestTemplateConfig {

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}
@Service
public class OrderService {

    @Autowired
    private RestTemplate restTemplate;

    public User getUser(Long userId) {
        // 서비스 이름으로 호출 (Eureka가 실제 주소로 변환)
        return restTemplate.getForObject(
            "http://user-service/users/" + userId,
            User.class
        );
    }
}

OpenFeign 사용 (권장)

@FeignClient(name = "user-service")
public interface UserClient {

    @GetMapping("/users/{id}")
    User getUser(@PathVariable Long id);
}

Multi‑Module 프로젝트 구성

프로젝트를 여러 모듈로 분리하면 코드 재사용성과 관리 효율이 높아집니다.

my-project/
├── build.gradle
├── settings.gradle
├── library/
│   ├── build.gradle
│   └── src/
├── application/
│   ├── build.gradle
│   └── src/
└── common/
    ├── build.gradle
    └── src/

settings.gradle

rootProject.name = 'my-project'

include 'library'
include 'application'
include 'common'

공통 플러그인 및 설정 (build.gradle)

plugins {
    id 'java'
    id 'org.springframework.boot' version '3.2.0'
    id 'io.spring.dependency-management' version '1.1.4'
}

subprojects {
    apply plugin: 'java'
    apply plugin: 'org.springframework.boot'
    apply plugin: 'io.spring.dependency-management'

    group = 'com.example'
    version = '1.0.0'

    java {
        sourceCompatibility = '17'
    }

    repositories {
        mavenCentral()
    }

    dependencies {
        implementation 'org.springframework.boot:spring-boot-starter'
        testImplementation 'org.springframework.boot:spring-boot-starter-test'
    }
}

application/build.gradle

dependencies {
    implementation project(':library')
    implementation project(':common')
    implementation 'org.springframework.boot:spring-boot-starter-web'
}

라이브러리 모듈 (library/build.gradle)

bootJar {
    enabled = false
}

jar {
    enabled = true
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
}

API 버전 관리 예시

@RestController
@RequestMapping("/api/v1/users")
public class UserControllerV1 {
    // v1 API
}
@RestController
@RequestMapping("/api/v2/users")
public class UserControllerV2 {
    // v2 API
}

서킷 브레이커 적용

@Service
public class UserService {

    @CircuitBreaker(name = "user-service", fallbackMethod = "getUserFallback")
    public User getUser(Long id) {
        return userClient.getUser(id);
    }

    public User getUserFallback(Long id, Exception e) {
        return new User(id, "Unknown", "default@example.com");
    }
}

Config Server 연동 (bootstrap.yml)

spring:
  application:
    name: user-service
  cloud:
    config:
      uri: http://config-server:8888

결론

Spring Cloud와 Eureka를 활용하면 확장 가능한 마이크로서비스 아키텍처를 구축할 수 있습니다. Multi‑Module 프로젝트 구성은 코드 재사용성을 높이고 관리 편의성을 제공합니다. 서비스 디스커버리, 로드 밸런싱, 서킷 브레이커 등 다양한 패턴을 적용해 안정적인 시스템을 설계하세요.

참고 자료

Back to Blog

Related posts

Read more »

Spring Cloud Gateway: Basic Example

!Cover image for Spring Cloud Gateway: Basic Examplehttps://media2.dev.to/dynamic/image/width=1000,height=420,fit=cover,gravity=auto,format=auto/https%3A%2F%2Fd...