feign 源码解析 && RequestInterceptor

参考:Feign源码解析

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.amoros.pumpkin.scorecard.infrastructure.config;

import com.amoros.cloud.core.AgentId;
import com.amoros.pumpkin.scorecard.common.constants.BizConstants;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

/**
* feign 请求拦截器。可以添加 headers
* 2021-12-09 16:36:05
*/
@Component
@Slf4j
public class FeignRequestInterceptor implements RequestInterceptor {

@Override
public void apply(RequestTemplate requestTemplate) {
String agentUid = AgentId.getAgentUid();
if (StringUtils.isNotEmpty(agentUid)) {
log.info("FeignRequestInterceptor feign拦截器,请求 AgentId = " + agentUid);
requestTemplate.header(BizConstants.X_CHANGE_DATA_SOURCE, agentUid);
return;
}

log.warn("FeignRequestInterceptor feign拦截器,请求 AgentId 未设置");
}
}