0. 이 글을 작성하는 이유
Spring API Gateway를 사용하며 정리하기 위해
1. 이전 글에서 좀 달라진 점
이전 글 : https://developer-youn.tistory.com/174
Spring Cloud Netflix Zuul을 바라보며
0. 이 글을 작성하는 이유 회사에서 API Gateway에 Spring Cloud Netflix Zuul이 적용되어 있는데 이게 뭐 하는 놈인지 간단하게 알고 가기 위함(추후 더 자세히 작성되는 글이 올라올 수도..) 1. Spring Cloud와
developer-youn.tistory.com
토이프로젝트에서 kotlin + jdk17 + spring boot3을 사용하며 API Gateway를 사용하려고 했는데 netflix-zuul의 경우 더 이상 기능 개발도 안 한다고 해서 cloud-gateway로 넘어가라고 했다.
https://github.com/spring-cloud/spring-cloud-netflix/issues/4158
spring-cloud-starter-netflix-zuul not work with spring-boot 3.0 and spring 6.0 · Issue #4158 · spring-cloud/spring-cloud-netfl
Hi. I see that spring-cloud-starter-netflix-zuul does not support working with spring-boot 3.0. For example, ZuulHandlerMapping use javax.servlet.http.HttpServletRequest, but in new version spring ...
github.com
2. 그래서 내 Gradle은
이렇게 나왔다.
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id("org.springframework.boot") version "3.0.6"
id("io.spring.dependency-management") version "1.1.4"
kotlin("jvm") version "1.9.20"
kotlin("plugin.spring") version "1.9.20"
}
extra["springCloudVersion"] = "2022.0.3"
group = "com.cooffee"
version = "0.0.1-SNAPSHOT"
java {
sourceCompatibility = JavaVersion.VERSION_17
}
repositories {
mavenCentral()
}
dependencyManagement {
imports {
mavenBom("org.springframework.cloud:spring-cloud-dependencies:${property("springCloudVersion")}")
}
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("org.springframework.cloud:spring-cloud-starter-gateway")
implementation("io.netty:netty-resolver-dns-native-macos:4.1.68.Final:osx-aarch_64")
testImplementation("org.springframework.boot:spring-boot-starter-test")
}
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs += "-Xjsr305=strict"
jvmTarget = "17"
}
}
tasks.withType<Test> {
useJUnitPlatform()
}
3. Routing전략(with application.yml)
토이 프로젝트는 MSA환경으로 끌고 갈 생각이었다.
API Gateway는 18080포트로, 그 이하는 18081, 18082로 넘기는 걸 예시로 했다.
cloud.gateway.routes를 통해 어디로 routing해줄지 정해준다. id로 구분을 하며 Predicates를 통해 어떤 경로가 들어왔을 경우인지에 대해 힌트를 제공해 준다. 여기 있는 작업들은 java/kotlin 코드로도 작성이 가능하다.
그리고 rewrtie path옵션을 달아두었다.
Rewrite를 하는 이유는 우리가 요청받은 path는 account의 경우 /account/login 같이 이루어질 텐데 account의 경우는 routing이 되면 필요가 없어 login만 던지기 위함이다.
server:
port: 18080
spring:
main:
web-application-type: reactive
application:
name: user-api-gateway
cloud:
gateway:
routes:
- id: account-service
uri: http://localhost:18081
predicates:
- Path=/account/**
filters:
- RewritePath=/account/(?<segment>.*), /$\{segment}
- id: shopping
uri: http://localhost:18082
predicates:
- Path=/shopping/**
filter:
rewrite-path:
enabled: true
4. Account service
이제 간단하게 account service를 만들어보자.
yml파일에서는 간단하게 서버 포트만 지정해 주었다.
server:
port: 18081
routing에 대한 테스트를 위해 간단한 컨트롤러를 하나 붙였다.
@RestController
class TestController {
@GetMapping("/abc")
fun foo(): String{
println("call!!")
return ""
}
}
5. Test(with postman)
ref
https://spring.io/projects/spring-cloud-gateway
Spring Cloud Gateway
This project provides a libraries for building an API Gateway on top of Spring WebFlux or Spring WebMVC. Spring Cloud Gateway aims to provide a simple, yet effective way to route to APIs and provide cross cutting concerns to them such as: security, monitor
spring.io
https://ykh6242.tistory.com/entry/Spring-Cloud-Gateway가-netty-기반-reactive-web-application으로-구동되는-이유
Spring Cloud Gateway가 netty 기반 reactive web application으로 구동되는 이유
개요 Spring Cloud Gateway 애플리케이션을 구동하게 되면 기존의 임베디드 톰켓 기반의 Spring Boot Web 애플리케이션과는 다르게 Netty 기반의 비동기 통신을 지원하는 형태의 웹 애플리케이션으로 실행
ykh6242.tistory.com
'JAVA > spring' 카테고리의 다른 글
gRPC 붙이기, submodule을 곁들이고 github 멀티 계정의 귀찮음을 같이 처리하기 위한 스크립트 (0) | 2024.01.01 |
---|---|
[Test] h2대신 postgresql을 DB로 대체해본.ssul (1) | 2023.11.04 |
Spring Cloud Netflix Zuul을 바라보며 (1) | 2023.10.28 |
Spring Security - CSRF를 disable?(간단, 요약) (0) | 2023.08.11 |
테스트 진행하기(Entity 기본과 약간의 Repository를 곁들인) (0) | 2023.08.05 |