环境
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的版本进行选择
文章来源:https://www.toymoban.com/news/detail-695685.html
配置请求标识
经过上一步配置的请求会存在调用链路,但是链路仅到网关,不会到后续服务文章来源地址https://www.toymoban.com/news/detail-695685.html
处理方法
- 服务中引入架包
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";
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
String traceId = WebFluxSkyWalkingOperators.continueTracing(exchange, TraceContext::traceId);
exchange.getResponse().getHeaders().set(xTraceIdKey, traceId);
return chain.filter(exchange);
}
}
到了这里,关于skywalking springgateway 全链路的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!