02.Spring Cloud Eureka
Spring Cloud Eureka
- Eureka 서버
- Eureka 서버는 마이크로 서비스에서 주소록 역할을 한다
- 기존의 레거시 프로젝트의 경우에는 한개의 서버에서 거대한 모놀리스 어플리케이션을 구동하기 때문에 마이크로 서비스의 주소록이 필요없었다
- 하지만 컨테이너 및 쿠버네티스에서 서비스를 운영하게 되면서 서버의 IP가 수시적으로 변경되기 때문에 이러한 Service Discovery 역할을 하는 Eureka 서버가 필요하게 되었다.
- Spring Initializer를 통해 프로젝트를 처음 생성할 때 Eureka Server를 Dependency 해준다.
- EurekaServerApplication.java
1
2
3
4
5
6
7
8
9
10
11
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
- src/main/resources/application.properties를 application.yml로 변환
1
2
3
4
5
6
7
8
9
10
11
12
13
server:
port: 8761
spring:
application:
name: eureka-server
eureka:
client:
register-with-eureka: false # 유레카 registry에 자신을 등록할지 여부
fetch-registry: false # registry에 있는 정보를 가져올지 여부를 결정
server:
wait-time-in-ms-when-sync-empty: 5 # Heartbeat
enable-self-preservation: true
- Eureka 클라이언트
- 주소록에 기록될 Client 서버이다
- spring initializer를 통해 프로젝트 생성시 Eureka Discovery Client를 Dependency해준다.
- Gateway 클라이언트 설정
- GatewayApplication.java
1
2
3
4
5
6
7
8
9
10
11
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
}
- application.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
server:
port: 8000
eureka:
client:
fetch-registry: true # registry에 있는 정보를 가져올지 여부를 결정
register-with-eureka: true # 유레카 registry에 자신을 등록할지 여부
service-url:
defaultZone: http://localhost:8761/eureka/ # 유레카 서버를 바라보도록 설정
spring:
application:
name: gateway-service
config:
import: "optional:configserver:"
cloud:
gateway:
routes:
- id: board
uri: http://localhost:8080/
predicates:
- Path=/api/boards/**
# http://localservice:8000/api/boards/** 요청이 들어오면 라우터를 설정한대로 http://localhost:8080/로 요청을 전달한다
- build.gradle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
plugins {
id 'java'
id 'org.springframework.boot' version '3.2.2'
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.cloud:spring-cloud-starter-gateway'
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'org.springframework.boot:spring-boot-starter-webflux' // webflux
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
tasks.named('test') {
useJUnitPlatform()
}
- Board 클라이언트 설정
- Board의 application.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
server:
port: 8080
spring:
application:
name: board-service
datasource:
driver-class-name: org.mariadb.jdbc.Driver
url: jdbc:mariadb://localhost:3306/msaPractice
username: root
password: 1234
jpa:
properties:
hibernate:
ddl-auto: create
format_sql: true
show_sql: true
generate-ddl: true
eureka:
instance:
prefer-ip-address: true
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:8761/eureka/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
plugins {
id 'java'
id 'org.springframework.boot' version '3.2.2'
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.cloud:spring-cloud-starter-netflix-eureka-client'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
runtimeOnly 'org.mariadb.jdbc:mariadb-java-client'
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.1.0' // swagger. 요구사항정의서 자동화도구
implementation 'org.springframework.boot:spring-boot-starter-webflux'
implementation 'com.fasterxml.jackson.core:jackson-databind' // json 라이브러리 설정
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
tasks.named('test') {
useJUnitPlatform()
}