Spring Cloud Eureka 사용해보기

2024. 5. 20. 05:09Spring Cloud

반응형

들어가며

Spring Cloud Eureka는 마이크로서비스 구조에서 서비스 간 연결 설정을 돕기 위한 라이브러리이다.

서비스 발견(Service Discovery)는 서비스간에 서로의 주소를 하드코딩하지 않고 연결, 통신하게 해주는 기술이다.

Eureka는 각 서비스를 레지스트리에 등록해두고 검색할 수 있는 기능을 제공하며, Feign 등 REST Client와의 연동도 가능하다.

Spring Cloud Eureka 사용해보기

Eureka Server

  • build.gradle
dependencies {
    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server'
}
  • @EnableEurekaServer 어노테이션으로 Eureka Server 활성화
@SpringBootApplication
@EnableEurekaServer
public class EurekaserversampleApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaserversampleApplication.class, args);
    }
}
  • 서버 설정 - application.yaml
    • 포트를 8761번으로 설정
server:
  port: 8761
eureka:
  client:
    register-with-eureka: false
    fetch-registry: false
  • 서버를 실행하고 localhost:8761로 접속하면 Eureka 대시보드를 확인할 수 있다.

Eureka Client

  • 의존성 설정 - build.gradle
dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-web'
	implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
}
  • 서버 설정 - application.yaml
    • localhost:8761 을 eureka 서버의 기본 경로로 설정한다.
    • 이 경우 서버에서 동적으로 클라이언트를 추가한다.
spring:
  application:
    name: eurekaclientsample
server:
  port: 0
eureka:
  client:
    serviceUrl:
      defaultZone: ${EUREKA_URI:<http://localhost:8761/eureka>}
  instance:
    preferIpAddress: true

Resources

반응형