✨ gateway-自定义过滤器
This commit is contained in:
parent
5f04e0f16d
commit
93b919be6a
|
@ -0,0 +1,45 @@
|
|||
package cn.bunny.gateway.filter;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
||||
import org.springframework.cloud.gateway.filter.GlobalFilter;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
public class RTFilter implements GlobalFilter, Ordered {
|
||||
|
||||
@Override
|
||||
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
|
||||
ServerHttpRequest request = exchange.getRequest();
|
||||
URI uri = request.getURI();
|
||||
long start = System.currentTimeMillis();
|
||||
log.error("请求【{}】开始时间:{}", uri, start);
|
||||
|
||||
// 处理逻辑
|
||||
// return chain.filter(exchange)
|
||||
// // 因为是异步的,不能写在下main,需要处理后续逻辑写在 doFinally
|
||||
// .doFinally(result -> {
|
||||
// long end = System.currentTimeMillis();
|
||||
// log.error("请求【{}】结束 ,时间:{},耗时:{}", uri, end, end - start);
|
||||
// });
|
||||
return chain.filter(exchange)
|
||||
.doOnError(e -> log.error("请求失败", e))
|
||||
.doFinally(result -> {
|
||||
long end = System.currentTimeMillis();
|
||||
log.info("请求【{}】结束,状态:{},耗时:{}ms",
|
||||
uri, result, end - start);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOrder() {
|
||||
return 0; // 执行顺序
|
||||
}
|
||||
}
|
|
@ -6,6 +6,9 @@ spring:
|
|||
uri: lb://service-order
|
||||
predicates:
|
||||
- Path=/api/order/**
|
||||
filters:
|
||||
- AddRequestHeader=X-Request-red, blue
|
||||
|
||||
- id: product-route
|
||||
uri: lb://service-product
|
||||
predicates:
|
||||
|
@ -13,6 +16,7 @@ spring:
|
|||
args:
|
||||
patterns: /api/product/**
|
||||
matchTrailingSlash: true
|
||||
|
||||
- id: bing-route
|
||||
uri: https://cn.bing.com/
|
||||
predicates:
|
||||
|
@ -31,5 +35,5 @@ spring:
|
|||
args:
|
||||
param: user
|
||||
value: bunny
|
||||
|
||||
|
||||
# filters:
|
||||
# - RedirectTo=/api/order/?(?<segment>.*), /$\{segment}
|
||||
|
|
|
@ -6,13 +6,16 @@ import cn.bunny.service.service.OrderService;
|
|||
import com.alibaba.csp.sentinel.annotation.SentinelResource;
|
||||
import com.alibaba.csp.sentinel.slots.block.BlockException;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/api/order")
|
||||
@RequiredArgsConstructor
|
||||
|
@ -46,11 +49,15 @@ public class OrderController {
|
|||
|
||||
@Operation(summary = "读取配置")
|
||||
@GetMapping("config")
|
||||
public String config() {
|
||||
public String config(HttpServletRequest request) {
|
||||
String timeout = orderProperties.getTimeout();
|
||||
String autoConfirm = orderProperties.getAutoConfirm();
|
||||
String dbUrl = orderProperties.getDbUrl();
|
||||
|
||||
// 携带的请求头内容
|
||||
String header = request.getHeader("X-Request-red");
|
||||
log.info("Received headers: {}", header);
|
||||
|
||||
return "timeout:" + timeout + "\nautoConfirm:" + autoConfirm + "\norder.db-url" + dbUrl;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue