Spring 프로젝트 생성 시 Lombok과 Gateway를 추가해 준다.
자바 8버전 기준!
application.yml에서 기본 설정가능
server:
port: 8000 # local 서버
spring:
application:
name: cloudApiGateway-service
cloud:
gateway:
routes:
- id: first-service
uri: http://localhost:8000/ #endpoint uri
predicates: # by-pass
- Path=/employee/**
filters: # context path별로 필터 적용 가능
- AddRequestHeader=first-request, 1-request-header-test
- AddResponseHeader=first-response, 1-response-header-test
- CustomFilter
- id: second-service
uri: http://localhost:8000/
predicates:
- Path=/consumer/**
filters:
- AddRequestHeader=second-request, 2-request-header-test
- AddResponseHeader=second-response, 2-response-header-test
- CustomFilter
# rewrite Path (path 지우고 by-pass)
- id: rewritepath_route
uri: http://localhost:8000/
predicates:
- Path=/maps/**
filters:
- RewritePath=/maps/(?<segment>.*), /$\{segment}
# rewrite Path (plus >> minus로 치환)
- id: third-service
uri: http://localhost:8000/
predicates:
- Path=/test/**
filters:
- RewritePath=/plus, /minus
테스트를 위해 local로 보내고 Controller를 따로 만들어 받아 보았다.
firstController.java (path TEST)
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/employee")
@Slf4j
public class FirstServiceController {
@GetMapping("/message")
public String test() {
return "Hello You are in First Service";
}
@GetMapping("/header")
public String message(@RequestHeader("Host") String header){
log.info("header > "+ header);
return "ok-first";
}
@GetMapping("/check")
public String check(){
return "hi check first";
}
}
secondController.java (path TEST)
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/consumer")
@Slf4j
public class SecondServiceController {
@GetMapping("/message")
public String test() {
return "Hello You are in Second Service";
}
@GetMapping("/header")
public String message(@RequestHeader("Host") String header){
log.info("header > "+ header);
return "ok-second";
}
@GetMapping("/check")
public String check(){
return "hi check second";
}
}
/message
/header
thirdController.java (re-write TEST)
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.security.Principal;
@RestController
public class Controller {
@GetMapping("/direct/test")
public String indexing() {
return "success";
}
@GetMapping("/test/minus")
public String minus() {
return "come here";
}
}
/maps/direct/test (/maps 제거 후 /direct/test로 이동)
/plus (/minus로 치환)
반응형
'Develop' 카테고리의 다른 글
네이티브 스프링 인 액션 (0) | 2024.04.08 |
---|---|
Spring Cloud Gateway - Keycloak 연동 (0) | 2022.04.20 |
Chart.js 연습 (0) | 2021.03.13 |
아나콘다 3 설치 및 환경설정 (Mac) (0) | 2021.03.06 |
Day1 Vue.js (0) | 2020.12.31 |