当前位置:   article > 正文

分布式中traceId链接服务间的日志

分布式中traceId链接服务间的日志

使用技术:

        网关:SpringCloudGateway

        RPC调用:Feign

一:在网关入口处设置header:key-traceId,value-UUID

  1. import com.kw.framework.common.croe.constant.CommonConstant;
  2. import com.kw.framework.gateway.utils.BuildHeaderFilter;
  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.stereotype.Component;
  8. import org.springframework.web.server.ServerWebExchange;
  9. import reactor.core.publisher.Mono;
  10. import java.util.LinkedHashMap;
  11. import java.util.UUID;
  12. @Component
  13. @Slf4j
  14. public class HeaderFilter implements GlobalFilter, Ordered {
  15. @Override
  16. public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
  17. // 封装需要向后续封装的header对象
  18. LinkedHashMap<String, String> headerMap = new LinkedHashMap<>();
  19. headerMap.put(CommonConstant.TRACE_ID, UUID.randomUUID().toString());
  20. exchange = BuildHeaderFilter.chainFilterAndSetHeaders(chain, exchange, headerMap);
  21. return chain.filter(exchange);
  22. }
  23. @Override
  24. public int getOrder() {
  25. return 0;
  26. }
  27. }

二:新建FeignRequestInterceptor实现RequestInterceptor来实现header透传

  1. import feign.RequestInterceptor;
  2. import feign.RequestTemplate;
  3. import lombok.extern.slf4j.Slf4j;
  4. import org.springframework.web.context.request.RequestContextHolder;
  5. import org.springframework.web.context.request.ServletRequestAttributes;
  6. import javax.servlet.http.HttpServletRequest;
  7. import java.util.Enumeration;
  8. import java.util.Iterator;
  9. import java.util.LinkedHashMap;
  10. import java.util.Map;
  11. /**
  12. * feign请求头传递
  13. */
  14. @Slf4j
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/602138
推荐阅读
相关标签
  

闽ICP备14008679号