-
Notifications
You must be signed in to change notification settings - Fork 12
/
AddTraceHeaderGatewayFilterFactory.java
32 lines (28 loc) · 1.35 KB
/
AddTraceHeaderGatewayFilterFactory.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.weweibuy.gateway.route.filter.trace;
import com.weweibuy.framework.common.core.model.constant.CommonConstant;
import com.weweibuy.gateway.core.constant.ExchangeAttributeConstant;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
/**
* @author durenhao
* @date 2020/7/11 13:02
**/
@Component
public class AddTraceHeaderGatewayFilterFactory extends AbstractGatewayFilterFactory {
@Override
public GatewayFilter apply(Object config) {
return (exchange, chain) -> {
String traceCode = exchange.getAttribute(ExchangeAttributeConstant.TRACE_ID_ATTR);
String userCode = exchange.getAttribute(ExchangeAttributeConstant.USER_ID_ATTR);
ServerHttpRequest.Builder builder = exchange.getRequest().mutate()
.header(CommonConstant.LogTraceConstant.HTTP_TRACE_CODE_HEADER, traceCode);
if (StringUtils.isNotBlank(userCode)) {
builder.header(CommonConstant.LogTraceConstant.HTTP_USER_CODE_HEADER, userCode);
}
return chain.filter(exchange.mutate().request(builder.build()).build());
};
}
}