implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.cloud:spring-cloud-starter-gateway'
management.endpoint.gateway.enabled=true
management.endpoints.web.exposure.include=gateway
존재하는 라우팅 확인
GET : /actuator/gateway/routes
라우터 추가 - JSON Body
POST : /actuator/gateway/routes/{id}

리프래쉬 : 추가 or 제거 후 꼭 해줘야함 !
POST : /actuator/gateway/refresh


package com.example.scg.component;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
@Component
public class G1Filter implements GlobalFilter, Ordered {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
System.out.println("pre global filter order -1");
return chain.filter(exchange)
.then(Mono.fromRunnable(() -> {
System.out.println("post global filter order -1");
}));
}
@Override
public int getOrder() {
return -1; // 필터 실행 순서 ( 낮을수록 먼저 실행 됨 )
}
}
package com.example.scg.component;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;
@Component
public class L1Filter extends AbstractGatewayFilterFactory<L1Filter.Config> {
public L1Filter() {
super(Config.class);
}
@Override
public GatewayFilter apply(Config config) {
return (exchange, chain) -> {
if (config.isPre()) {
System.out.println("pre local filter 1");
}
return chain.filter(exchange)
.then(Mono.fromRunnable(() -> {
if (config.isPost()) {
System.out.println("post local filter 1");
}
}));
};
}
@NoArgsConstructor
@AllArgsConstructor
@Data
public static class Config {
private boolean pre;
private boolean post;
}
}
spring.cloud.gateway.routes[0].filters[0].name=L1Filter
spring.cloud.gateway.routes[0].filters[0].args.pre=true
spring.cloud.gateway.routes[0].filters[0].args.post=true