調(diào)用遠程服務(wù)時,服務(wù)提供方要求在header中傳遞權(quán)限驗證信息或者為方便定位問題恬叹,在header中透傳一個traceId實現(xiàn)調(diào)用鏈路的跟蹤候生。利用Feign Client,可以非常方便地統(tǒng)一設(shè)置
原理
Feign可以通過實現(xiàn)接口feign.RequestInterceptor
绽昼,完成對feign.RequestTemplate
的修改唯鸭,比如添加header
自定義FeignInterceptor
- 增加權(quán)限校驗信息
- 增加requestId,方便服務(wù)方完成冪等處理
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);
//對服務(wù)調(diào)用進行簽名硅确,當(dāng)前簽名方法比較簡單目溉,以后可以支持更加復(fù)雜的簽名計算(比如讀取參數(shù)內(nèi)容,組合后再進行簽名計算)
requestTemplate.header("appId", authProperties.getAppId());
requestTemplate.header("timestamp", Long.toString(timestamp));
requestTemplate.header("sign", md5);
//一些接口的調(diào)用需要實現(xiàn)冪等菱农,比如消息發(fā)送缭付,如果使用requestId就可以方便服務(wù)方實現(xiàn)冪等
requestTemplate.header("requestId", UUID.randomUUID().toString().replaceAll("-", ""));
}
}
}
權(quán)限配置信息
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 {
/**
* 應(yīng)用ID.
* 由服務(wù)方統(tǒng)一分配,區(qū)分各個調(diào)用方.
*/
private String appId;
/**
* 應(yīng)用的KEY.
* 由服務(wù)方統(tǒng)一分配循未,不可泄露給第三方.
*/
private String appKey;
@PostConstruct
private void init() {
log.info("AuthProperties Info: {}", this);
}
}
配置文件(application.yml)
common:
auth:
app-id: hello
app-key: world