Java
[gradle] Spring Cloud Gateway 설정 및 예제
스타박씨
2023. 5. 4. 23:35
1. 호출할 API
8100 port로 기동하는 API 프로젝트를 만든다.
application.yml
server:
port: 8100
controller
@RestController
public class RedisController {
@GetMapping("hello")
public String getHello() {
return "hello!";
}
}
2. API GATEWAY
8080 port로 기동하는 gateway 프로젝트를 만든다.
build.gradle
plugins {
id 'java'
id 'org.springframework.boot' version '2.7.5'
id 'io.spring.dependency-management' version '1.1.0'
}
group = 'com.skt'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:2021.0.5"
}
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.cloud:spring-cloud-starter-gateway'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
tasks.named('test') {
useJUnitPlatform()
}
application.yml
server:
port: 8080
spring:
cloud:
gateway:
routes:
- id: myAPI
uri: http://localhost:8100
predicates:
- Path=/api/**
filters:
- RewritePath=/api/(?<path>.*), /$\{path}
main:
web-application-type: reactive
url에 /api 문자열이 있으면 /api 를 제거하고 http://localhost:8100/hello 로 호출한다.
3. 테스트
API 프로젝트와 GATEWAY프로젝트를 모두 기동하고 호출한다.