调用远程服务时,服务提供方要求在header中传递权限验证信息或者为方便定位问题,在header中透传一个traceId实现调用链路的跟踪。利用Feign Client,可以非常方便地统一设置
原理
Feign可以通过实现接口feign.RequestInterceptor
,完成对feign.RequestTemplate
的修改,比如添加header
自定义FeignInterceptor
- 增加权限校验信息
- 增加requestId,方便服务方完成幂等处理
import com.google.common.base.Strings;
import com.tenmao.tenmao.starter.constants.TenmaoConstant;
import com.tenmao.tenmao.starter.mvc.config.AuthProperties;
import com.tenmao.tenmao.starter.util.Md5Util;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.UUID;
/**
* @author tenmao
* @since 2019/9/21
*/
@Slf4j
@Component
@ConditionalOnClass(RequestInterceptor.class)
@SuppressWarnings("unused")
public class FeignInterceptor implements RequestInterceptor {
@Resource
private AuthProperties authProperties;
@Override
public void apply(RequestTemplate requestTemplate) {
String traceId = MDC.get(TenmaoConstant.TRACE_KEY);
log.info("set traceId: url[{}], traceId[{}]", requestTemplate.url(), traceId);
requestTemplate.header(TenmaoConstant.TRACE_KEY, traceId);
if (!Strings.isNullOrEmpty(authProperties.getAppId()) && !Strings.isNullOrEmpty(authProperties.getAppKey())) {
long timestamp = System.currentTimeMillis();
String md5 = Md5Util.calcMD5(authProperties.getAppId() + authProperties.getAppKey() + timestamp);
//对服务调用进行签名,当前签名方法比较简单,以后可以支持更加复杂的签名计算(比如读取参数内容,组合后再进行签名计算)
requestTemplate.header("appId", authProperties.getAppId());
requestTemplate.header("timestamp", Long.toString(timestamp));
requestTemplate.header("sign", md5);
//一些接口的调用需要实现幂等,比如消息发送,如果使用requestId就可以方便服务方实现幂等
requestTemplate.header("requestId", UUID.randomUUID().toString().replaceAll("-", ""));
}
}
}
权限配置信息
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
/**
* @author tenmao
* @since 2019/10/9
*/
@Data
@Slf4j
@Component
@ConfigurationProperties(prefix = "common.auth")
public class AuthProperties {
/**
* 应用ID.
* 由服务方统一分配,区分各个调用方.
*/
private String appId;
/**
* 应用的KEY.
* 由服务方统一分配,不可泄露给第三方.
*/
private String appKey;
@PostConstruct
private void init() {
log.info("AuthProperties Info: {}", this);
}
}
配置文件(application.yml)
common:
auth:
app-id: hello
app-key: world