simDev1234
심플하고 차분하게
simDev1234
전체 방문자
오늘
어제
  • 분류 전체보기
    • Computer Science
      • Basic Math
      • Data Structure
      • Algorithm
      • Database
      • OS
    • Language
      • Java
      • Kotlin
      • SQL
    • Framework
      • Spring
      • Orm&Mapper
      • 프로젝트로 스프링 이해하기
      • 스프링 라이브러리
    • Infra
      • Cloud
      • Docker
      • Redis
      • AWS, Azure
      • Device
    • Etc
      • CleanCoding
    • Git,Github

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • JVM메모리구조
  • 스프링
  • controllerTest
  • null
  • 자바프로그램
  • 자바메모리구조
  • 컨트롤러
  • 참조타입
  • 404
  • 자바프로그래밍
  • scanner #next() #nextLine()
  • 참조변수
  • 자바

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
simDev1234

심플하고 차분하게

[Spring Cloud & MSA] Spring Cloud Netflix Eureka
Framework/Spring

[Spring Cloud & MSA] Spring Cloud Netflix Eureka

2024. 1. 10. 01:08

 

1. Spring Cloud Netflix Eureka 

- 유레카는 Service Discovery 중 하나이다.

- Service Discovery는 말 그대로 서비스를 찾아주는 것으로써, 어느 위치에 어떤 서버가 있는지를 찾아준다.

  rf. key / value 형태로 서비스를 등록하고 검색할 수 있도록 해준다.

- 넷플릭스 자사의 기술들을 Java Spring 재단에 기부를 하면서 만들어진 것이 Eureka이다. 

- Eureka를 쓰려면 먼저 서비스를 등록해야 한다. 

- Client에서 요청이 Load Banlancer로 들어오면 Service Discovery가 서비스의 위치를 찾아준다.

 

2. 프로젝트 환경 구성

[1] 프로젝트 생성

- 마인드챗이라고, 내 마음 속 이야기를 채팅창에 적을 수 있는 플젝을 한 번 만들어보고 싶어서 아래처럼 만들어봤다.

 

[2] 의존성에서 유레카 서버 하나만 먼저 넣어준다. 

- build.gradle의 모습

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

group = 'com.example'
version = '0.0.1-SNAPSHOT'

java {
    sourceCompatibility = '17'
}

repositories {
    mavenCentral()
}

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

dependencies {
    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

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

tasks.named('test') {
    useJUnitPlatform()
}

 

[3] 메인 클래스에 유레카 서버를 활성화하는 Annotation 추가

@SpringBootApplication
@EnableEurekaServer
public class MindchatApplication {

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

}

 

[4] application.yml 작성

# service port (Eureka 서버 포트)
server:
  port: 8761

# MSA 각 서비스의 고유 id 부여
spring:
  application:
    name: discoveryservice

# 마이크로 서비스를 등록하는 역할을 한다.
# Eureka는 서버로서 구동만 하면 되기 때문에 아래 사항을 false로 입력
eureka:
  client:
    register-with-eureka: false
    fetch-registry: false

- 실행 시, 로그에 8761 포트 뜸

main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port 8761 (http)

- http://localhost:8761 로 들어오면 Eureka Dashboard 화면 보임 

- 아래가 등록된 마이크로 서비스 목록을 볼 수 있는 곳임

 

3. 마이크로 서비스 모듈 만들어보기 

- [File] - [new project] 로 모듈을 만든다.

- 중요한 건 이번에는 Eureka Discovery Client 를 추가한다.

- build.gradle 

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

group = 'com.example'
version = '0.0.1-SNAPSHOT'

java {
	sourceCompatibility = '17'
}

configurations {
	compileOnly {
		extendsFrom annotationProcessor
	}
}

repositories {
	mavenCentral()
}

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

dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
	implementation 'org.springframework.boot:spring-boot-starter-validation'
	implementation 'org.springframework.boot:spring-boot-starter-web'
	implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
	compileOnly 'org.projectlombok:lombok'
	developmentOnly 'org.springframework.boot:spring-boot-devtools'
	annotationProcessor 'org.projectlombok:lombok'
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

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

tasks.named('test') {
	useJUnitPlatform()
}

 

- Main 클래스에 아래와 같이 어노테이션 추가

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

}

- yml 설정

server:
  port: 9001

spring:
  application:
    name: userservice

eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://127.0.0.1:8761/eureka

- 유레카 확인

 

[ 출처 ] 인프런 - Spring Cloud로 개발하는 마이크로서비스 애플리케이션(MSA)

[ 참고 ]

https://bcho.tistory.com/1252

 

'Framework > Spring' 카테고리의 다른 글

Spring Cloud - Netflix Eureka, Spring Gateway  (1) 2024.04.27
[Spring Cloud & MSA] MSA와 Spring Cloud  (1) 2024.01.09
[스프링] Build.Gradle & application.yml 관련 메모  (0) 2022.10.28
[스프링] @AutoWired 동작 원리 및 DI injection 관련 설명 모음  (0) 2022.10.20
[HTTP] @RequestParam vs @RequestBody  (0) 2022.10.19
    'Framework/Spring' 카테고리의 다른 글
    • Spring Cloud - Netflix Eureka, Spring Gateway
    • [Spring Cloud & MSA] MSA와 Spring Cloud
    • [스프링] Build.Gradle & application.yml 관련 메모
    • [스프링] @AutoWired 동작 원리 및 DI injection 관련 설명 모음
    simDev1234
    simDev1234
    TIL용 블로그. * 저작권 이슈가 있는 부분이 있다면 댓글 부탁드립니다.

    티스토리툴바