ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Spring boot] Eureka (Spring Cloud Discovery)
    카테고리 없음 2024. 5. 27. 01:03

    유레카 서버는 각각 분산된 서버를 등록하고 관리해주는 미들웨어이다. 각 서버가 시작될 때 유레카 서버에 등록된다. 각 서버에서 다른 서버로 요청할 때는 유레카 서버에서 그 정보를 받아서 요청한다.

     

    Eureka Server 생성하기

    1. Eureka 서버의 build.gradle에 Dependency를 등록한다.

    compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-server')

     

    2. application.properties에 필요한 정보를 추가한다.

    server.port=12300
    
    spring.application.name=eureka-server
    
    eureka.client.service-url.defaultZone=http://localhost:12300/eureka/
    eureka.client.register-with-eureka=false
    eureka.client.fetch-registry=false

     

    3. Application.java에 @EnableEurekaServer 어노테이션을 추가한다.

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

    이렇게 Eureka Server의 생성이 완료되었다. 방금 생성한 어플리케이션을 동작 시키고 브라우저에서 http://localhost:12300에 접속하면 Eureka Server가 제공하는 Page를 확인할 수 있다.

     

    Eureka Client 등록하기

    1. 해당 프로젝트의 build.gradle 파일에 Dependency를 등록한다.

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

     

    2. DependencyManagement를 추가한다.

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

     

    3. application.properties에 필요한 정보를 추가한다.

    server.port=8086
    
    eureka.client.service-url.defaultZone=http://localhost:12300/eureka/
    eureka.client.register-with-eureka=true
    eureka.client.fetch-registry=true

     

    4. Application.java에 @EnableDiscoveryClient 어노테이션을 추가한다.

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

     

    댓글

Designed by Tistory.