在使用spring cloud时 需要跟踪同一请求的路径,所以需要把请求头沿着请求传递下去,
由于使用的时feign,在此记录下找到的两个方法。
方法1、直接加RequestHeader下面加粗部分,请求时设置
@FeignClient(value = "org-service")
public interface OrgFeign {
@RequestMapping(method= RequestMethod.GET,value = "/v1/org/{orgId}",consumes = "application/json")
MyOrg getOrg(@PathVariable String orgId,
@RequestHeader String reqId,
@RequestHeader String reqToken,
@RequestHeader String userId);
}
方法2、使用配置文件FeignConfig传递参数
@FeignClient(value = "org-service",configuration = FeignConfig.class)
public interface OrgFeign {
@RequestMapping(method= RequestMethod.GET,value = "/v1/org/{orgId}",consumes = "application/json")
MyOrg getOrg(@PathVariable String orgId);
}
其中FeignConfig如下:
@Configuration
public class FeignConfig implements RequestInterceptor {
@Override
public void apply(RequestTemplate requestTemplate) {
requestTemplate.header(ReqContext.REQ_ID, ReqContextHolder.getContext().getReqId());
requestTemplate.header(ReqContext.REQ_TOKEN, ReqContextHolder.getContext().getReqToken());
requestTemplate.header(ReqContext.USER_ID, ReqContextHolder.getContext().getUserId());
}
}