JAVAIARY

EUREKA Server Local 구축 및 서비스 추가 본문

lectureNote/SPRING

EUREKA Server Local 구축 및 서비스 추가

shiherlis 2023. 7. 20. 08:23

원래는 AWS에 올라가 있는 서버에 서비스를 추가하려고 했으나, 
그게 맘처럼 잘 안돼서 (...) 로컬로 해 보기로 했다. 

 

1. Eureka서버 로컬에서 구축하기

Eureka_local 이름으로 프로젝트를 하나 생성하고 build.gradle 수정

plugins {
    id 'java'
    id 'io.spring.dependency-management' version '1.1.0'
}
group = 'org.example'
version = '0.0.1-SNAPSHOT'

repositories {
    mavenCentral()
}
dependencies {

    implementation("org.springframework.cloud:spring-cloud-starter-netflix-eureka-server")
    implementation 'com.google.code.gson:gson:2.10'
}
dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:2021.0.5"
    }
}
test {
    useJUnitPlatform()
}

 

  • Eureka-server Dependency를 추가해 준다. 

그리고 application.yml에서 유레카 서버 관련 설정을 해준다.

 

spring:
  application:
    name: eureka_server
server:
  port: 8761
eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
  • eureka-client-defaultZone의 URL 로 유레카 서버가 생성이 된다. 
  • registerWithEureka : 서버 본인은 인스턴스에 등록할 필요가 없기 때문에 false로 둔다.
  • fetchRegistry : 상동

main 아래에 패키지를 하나 만들고 어플리케이션을 작성한다. 

package com.server.eureka;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

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

}
  • @EnableEurekaServer 어노테이션을 작성해 해당 어플리케이션이 Eureka Server임을 명시해준다. 

어플리케이션 실행 후 http://localhost:8761로 접속하면

유레카 서버를 확인할 수 있다 👍


2. Eureka에 서비스 추가하기

1) 서비스할 프로젝트의 build.gradle

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

eureka-client dependency 추가

 

2) application.yml

spring:
  application:
    name: kiosk_test
  profiles:
    active: TEST_KIOSK_API
---
spring:
  profiles: TEST_KIOSK_API
  cloud:
    discovery:
      enabled: true
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka
    register-with-eureka: true
    fetch-registry: true
  instance:
    prefer-ip-address: true

server:
  port: 9685
  • spring-application-name : 유레카에 추가 될 인스턴스명이 됨
  • 유레카 정보 추가

 

3) Application에 어노테이션 추가

@EnableDiscoveryClient

그리고 어플리케이션 실행을 해 준다.

지정한 포트로 인스턴스가 올라와 있는 것을 확인할 수 있다.