Spring Boot Framework
[Spring Cloud Netflix] Eureka Gateway 샘플
헤르메스의날개
2024. 3. 28. 11:12
728x90
아래와 같은 환경을 구축하기 위해 Microservice 가 1개 이상 있어야 합니다.
Spring Cloud 아키텍처 관계도
개발환경
JDK : Zulu JDK 17.0.10
SpringBoot 3.2.3
SpringBoot Eureka Client ( 현재 최신 버전 :4.1.0 )
Build Tools : Gradle
1. 프로젝트 생성
2. Eureka Gateway
- 간단한 Application 입니다.
build.gradle
plugins {
id 'java'
id 'org.springframework.boot' version '3.2.4'
id 'io.spring.dependency-management' version '1.1.4'
}
group = 'octopus'
version = '0.0.1-SNAPSHOT'
java {
sourceCompatibility = '17'
}
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'
// Default :: 1.18.30
compileOnly 'org.projectlombok:lombok'
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()
}
application.yml
server:
port: 8080
eureka:
client:
fetch-registry: false
register-with-eureka: false
service-url:
#defaultZone: http://localhost:8761/eureka/ # Eureka Server 경로
defaultZone: ${EUREKA_URL:http://localhost:8761/eureka/} # http://localhost:8761/eureka/ 와 같은 설정임.
spring:
application:
name: Eureka-Gateway
cloud:
gateway:
routes:
- id: service01
uri: http://localhost:8081
predicates:
- Path=/service01/** # "Path" 반드시 첫글자 대문자
- id: service02
uri: http://localhost:8082
predicates:
- Path=/service02/** # "Path" 반드시 첫글자 대문자
FilterConfig.java
package octopus.config;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FilterConfig {
@Bean
public RouteLocator gatewayRoutes(RouteLocatorBuilder builder) {
return builder.routes()
.route(r -> r.path("/service01/**")
.filters(f -> f.addRequestHeader("first-request", "first-request-header")
.addResponseHeader("first-response", "first-response-header"))
.uri("http://localhost:8081"))
.route(r -> r.path("/service02/**")
.filters(f -> f.addRequestHeader("first-request", "second-request-header")
.addResponseHeader("first-response", "second-response-header"))
.uri("http://localhost:8082"))
.build();
}
}
3. Eureka Gateway를 통한 Uri 호출
http://localhost:8080/service01/welcome
- http://localhost:8081/service01/welcome 를 Gateway를 통해서 http://localhost:8080/service01/welcome 를 접속해도 같은 Uri로 이동할 수 있게 됩니다.
http://localhost:8080/service02/welcome
- http://localhost:8081/service01/welcome 를 Gateway를 통해서 http://localhost:8080/service01/welcome 를 접속해도 같은 Uri로 이동할 수 있게 됩니다.
https://hermeslog.tistory.com/755
https://hermeslog.tistory.com/760
https://hermeslog.tistory.com/762
728x90