skywalking springgateway 全链路

环境

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的版本进行选择
skywalking springgateway 全链路_第1张图片

配置请求标识

经过上一步配置的请求会存在调用链路,但是链路仅到网关,不会到后续服务

处理方法

  1. 服务中引入架包
    pom
        
            org.apache.skywalking
            apm-toolkit-trace
            8.14.0
        

        
            org.apache.skywalking
            apm-toolkit-webflux
            8.14.0
        
  1. 配置链路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);
    }
}

你可能感兴趣的:(SpringCloud,skywalking,spring,gateway)