环境
spring-cloud-gateway 3.1.0
springGateway整合skywalking
skywalking 默认是不整合springGateway的,需要手动拷贝skywalking optional-plugins下的
apm-spring-cloud-gateway-N.x-plugin-8.13.0.jar
和
apm-spring-webflux-5.x-plugin-8.13.0.jar
架包拷贝到plugins目录下
gateway架包的选择根据springgateway的版本进行选择
配置请求标识
经过上一步配置的请求会存在调用链路,但是链路仅到网关,不会到后续服务
处理方法
- 服务中引入架包
pom
<dependency><groupId>org.apache.skywalking</groupId><artifactId>apm-toolkit-trace</artifactId><version>8.14.0</version></dependency><dependency><groupId>org.apache.skywalking</groupId><artifactId>apm-toolkit-webflux</artifactId><version>8.14.0</version></dependency>
- 配置链路id
package com.kittlen.gateway.filter;import org.apache.skywalking.apm.toolkit.trace.TraceContext;
import org.apache.skywalking.apm.toolkit.webflux.WebFluxSkyWalkingOperators;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;/*** @author kittlen* @version 1.0* @date 2023/9/5 13:41* skywalking 全链路配置*/
@Component
public class PutTraceIdIntoResponseHeaderFilter implements GlobalFilter {String xTraceIdKey = "x-trace-id";@Overridepublic Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {String traceId = WebFluxSkyWalkingOperators.continueTracing(exchange, TraceContext::traceId);exchange.getResponse().getHeaders().set(xTraceIdKey, traceId);return chain.filter(exchange);}
}