istio链路追踪代码入侵

java 代码

实现原理: 需要添加拦截器 用于拦截x-request-id 发送之后应用以及proxy 上报mixer 实现链路追踪

  • feign拦截器

开启拦截选项 在application

import org.springframework.cloud.netflix.feign.EnableFeignClients;
@EnableFeignClients   // 添加该注解
public class TradeApplication {
		public static void main(String[] args) {
        new SpringApplicationBuilder(TradeApplication.class)
                .web(true).run(args);
    }
}

Feign请求拦截器(设置请求头,传递X-request-id)位于java.项目名.filter

package cn.ghostcloud.filter;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

 // Feign请求拦截器(设置请求头,传递登录信息)
 
@Configuration
public class FeignBasicAuthRequestInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate requestTemplate) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        Enumeration<String> headerNames = request.getHeaderNames();
        if (headerNames != null) {
            requestTemplate.header("x-request-id",request.getHeader("x-request-id"));
        }
    }
}
  • 每一个请求传出Service前加上 位于
@FeignClient(value = "Service名字" , url = "整体Service需要传递URL参数" , configuration =Feign.class) 

示例

package cn.ghostcloud.service;

import cn.ghostcloud.filter.FeignBasicAuthRequestInterceptor;
import org.springframework.cloud.netflix.feign.FeignClient;

@FeignClient(value = "scoreService", url = "${domain.score.baseurl}/score" ,configuration = FeignBasicAuthRequestInterceptor.class)
public interface ScoreService {
		@RequestMapping(value = "/save", method = RequestMethod.POST)
    Map<String, Object> save(Score score);
   @RequestMapping("/count")
    long count(@RequestBody Score score);
}

Tracefilter 添加X-request-id 到 Response 作为查询依据(可选)

package cn.ghostcloud.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 拦截请求在请求头中添加trace_id
 */
@WebFilter(filterName = "traceFilter", urlPatterns = "/*", asyncSupported = true)
public class TraceFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) { }
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
        throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest; 
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        response.setHeader("x-request-id", request.getHeader("x-request-id"));
        filterChain.doFilter(request, response);
    }
    @Override
    public void destroy() { }
}

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×