프로젝트 (Java)/예약마켓

[프로젝트] 40. Eureka Server 추가

hihyuk 2024. 2. 13. 09:20

Eureka Server의 추가는 마이크로서비스 아키텍처에서 서비스 디스커버리를 관리하는 핵심 요소입니다. Eureka Server를 사용하면 각 마이크로서비스가 실행될 때 자신의 정보(예: IP 주소, 포트)를 Eureka Server에 등록하고, 다른 서비스의 위치 정보를 조회할 수 있습니다. 이를 통해 서비스 간의 탄탄한 연결과 유연한 서비스 관리가 가능해집니다.

 

Eureka Server 설정

Eureka Server를 설정하기 위해 build.gradle 파일에 필요한 의존성을 추가하고, EurekaServerApplication 클래스에 @EnableEurekaServer 어노테이션을 사용하여 Eureka Server로의 기능을 활성화합니다.

build.gradle

plugins {
    id 'java'
}

group = 'org.example'
version = 'unspecified'

repositories {
    mavenCentral()
}

ext {
    set('springCloudVersion', "2021.0.4")
}

dependencies {
    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server'
    testImplementation platform('org.junit:junit-bom:5.9.1')
    testImplementation 'org.junit.jupiter:junit-jupiter'
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}

test {
    useJUnitPlatform()
}

EurekaServerApplication

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

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

 

application.yml 파일에서는 Eureka Server의 포트 번호를 설정하고, Eureka 자체의 등록을 방지하기 위해 register-with-eurekafetch-registry 옵션을 false로 설정합니다. 또한, Spring Security를 사용하여 Eureka Dashboard에 대한 기본 인증을 구성할 수 있습니다.

application.yml

server:
  port: 8761 // 유레카 서버 기본 port 

eureka:
  client:
    register-with-eureka: false // 유레카 서버에 등록할 때 true로 바꾸어줌
    fetch-registry: false       // 자신을 등록할 필요가 없으므로 false
    service-url:
      defaultZone: http://localhost:8761/eureka

spring:
  autoconfigure:
    exclude: org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration // db 설정 제외
  application:
    name: eureka-server
  security:
    user:	
      name: admin		
      password: admin	// 시큐리티 아이디 비번 설정

 

서비스의 Eureka Client 설정

각 마이크로서비스에서 Eureka Server에 자신을 등록하기 위해, 서비스의 build.gradle 파일에 Eureka Client 의존성을 추가합니다.

ext {
    set('springCloudVersion', "2021.0.4")
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-security'
    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
    implementation project(':myshop-core')
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}

그리고 각 서비스의 application.yml 파일에서는 Eureka Server의 URL과 함께 해당 서비스를 Eureka Server에 등록하도록 설정합니다. admin:admin은 Eureka Dashboard에 접근하기 위해 설정한 기본 인증 정보입니다.

 

application.yml

  application:
    name: 000-service
  eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://admin:admin@localhost:8761/eureka

마지막으로, 각 서비스의 메인 애플리케이션 클래스에 @EnableDiscoveryClient 어노테이션을 추가하여 Eureka Client로의 기능을 활성화합니다.

@Import(JpaAuditingConfig.class)
@SpringBootApplication
@EnableDiscoveryClient
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

 

Eureka Server

이렇게 설정하면, 각 마이크로서비스는 Eureka Server에 자동으로 등록되고, 서비스 간의 상호작용이 Eureka를 통해 관리됩니다. 또한, API Gateway를 포함하여 모든 서비스가 Eureka Server를 통해 서비스 디스커버리를 할 수 있게 되어, 서비스 간의 연결과 통신이 보다 유연하고 관리가 용이해집니다.