Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 93b919b

Browse filesBrowse files
committed
✨ gateway-自定义过滤器
1 parent 5f04e0f commit 93b919b
Copy full SHA for 93b919b

File tree

Expand file treeCollapse file tree

3 files changed

+59
-3
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+59
-3
lines changed
+45Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package cn.bunny.gateway.filter;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
5+
import org.springframework.cloud.gateway.filter.GlobalFilter;
6+
import org.springframework.core.Ordered;
7+
import org.springframework.http.server.reactive.ServerHttpRequest;
8+
import org.springframework.stereotype.Component;
9+
import org.springframework.web.server.ServerWebExchange;
10+
import reactor.core.publisher.Mono;
11+
12+
import java.net.URI;
13+
14+
@Slf4j
15+
@Component
16+
public class RTFilter implements GlobalFilter, Ordered {
17+
18+
@Override
19+
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
20+
ServerHttpRequest request = exchange.getRequest();
21+
URI uri = request.getURI();
22+
long start = System.currentTimeMillis();
23+
log.error("请求【{}】开始时间:{}", uri, start);
24+
25+
// 处理逻辑
26+
// return chain.filter(exchange)
27+
// // 因为是异步的,不能写在下main,需要处理后续逻辑写在 doFinally
28+
// .doFinally(result -> {
29+
// long end = System.currentTimeMillis();
30+
// log.error("请求【{}】结束 ,时间:{},耗时:{}", uri, end, end - start);
31+
// });
32+
return chain.filter(exchange)
33+
.doOnError(e -> log.error("请求失败", e))
34+
.doFinally(result -> {
35+
long end = System.currentTimeMillis();
36+
log.info("请求【{}】结束,状态:{},耗时:{}ms",
37+
uri, result, end - start);
38+
});
39+
}
40+
41+
@Override
42+
public int getOrder() {
43+
return 0; // 执行顺序
44+
}
45+
}

‎cloud-demo/gateway/src/main/resources/application-route.yaml

Copy file name to clipboardExpand all lines: cloud-demo/gateway/src/main/resources/application-route.yaml
+6-2Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@ spring:
66
uri: lb://service-order
77
predicates:
88
- Path=/api/order/**
9+
filters:
10+
- AddRequestHeader=X-Request-red, blue
11+
912
- id: product-route
1013
uri: lb://service-product
1114
predicates:
1215
- name: Path
1316
args:
1417
patterns: /api/product/**
1518
matchTrailingSlash: true
19+
1620
- id: bing-route
1721
uri: https://cn.bing.com/
1822
predicates:
@@ -31,5 +35,5 @@ spring:
3135
args:
3236
param: user
3337
value: bunny
34-
35-
38+
# filters:
39+
# - RedirectTo=/api/order/?(?<segment>.*), /$\{segment}

‎cloud-demo/services/service-order/src/main/java/cn/bunny/service/controller/OrderController.java

Copy file name to clipboardExpand all lines: cloud-demo/services/service-order/src/main/java/cn/bunny/service/controller/OrderController.java
+8-1Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@
66
import com.alibaba.csp.sentinel.annotation.SentinelResource;
77
import com.alibaba.csp.sentinel.slots.block.BlockException;
88
import io.swagger.v3.oas.annotations.Operation;
9+
import jakarta.servlet.http.HttpServletRequest;
910
import lombok.RequiredArgsConstructor;
11+
import lombok.extern.slf4j.Slf4j;
1012
import org.springframework.web.bind.annotation.GetMapping;
1113
import org.springframework.web.bind.annotation.RequestMapping;
1214
import org.springframework.web.bind.annotation.RestController;
1315

1416
import java.util.List;
1517

18+
@Slf4j
1619
@RestController
1720
@RequestMapping("/api/order")
1821
@RequiredArgsConstructor
@@ -46,11 +49,15 @@ public Order createBlockHandler(Long userId, Long productId, BlockException exce
4649

4750
@Operation(summary = "读取配置")
4851
@GetMapping("config")
49-
public String config() {
52+
public String config(HttpServletRequest request) {
5053
String timeout = orderProperties.getTimeout();
5154
String autoConfirm = orderProperties.getAutoConfirm();
5255
String dbUrl = orderProperties.getDbUrl();
5356

57+
// 携带的请求头内容
58+
String header = request.getHeader("X-Request-red");
59+
log.info("Received headers: {}", header);
60+
5461
return "timeout:" + timeout + "\nautoConfirm:" + autoConfirm + "\norder.db-url" + dbUrl;
5562
}
5663
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.