update - Client API Feign 请求增加 header

This commit is contained in:
wangli 2024-06-28 17:43:08 +08:00
parent fbd32f59c6
commit 7e0e266fe5
2 changed files with 11 additions and 6 deletions

View File

@ -5,6 +5,7 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -199,8 +200,8 @@ public class WorkflowEngineClientAutoConfiguration {
} }
@Bean @Bean
public RequestInterceptor workflowRequestInterceptor(String serviceVersion) { public RequestInterceptor workflowRequestInterceptor(String serviceVersion, Environment environment) {
return new WorkflowRequestInterceptor(serviceVersion); return new WorkflowRequestInterceptor(serviceVersion, environment);
} }
} }

View File

@ -3,6 +3,7 @@ package cn.axzo.workflow.client.config;
import feign.RequestInterceptor; import feign.RequestInterceptor;
import feign.RequestTemplate; import feign.RequestTemplate;
import feign.Target; import feign.Target;
import org.springframework.core.env.Environment;
/** /**
* 通用的 Feign 请求拦截器 * 通用的 Feign 请求拦截器
@ -12,14 +13,16 @@ import feign.Target;
*/ */
public class WorkflowRequestInterceptor implements RequestInterceptor { public class WorkflowRequestInterceptor implements RequestInterceptor {
private final String serviceVersion; private final String serviceVersion;
private final Environment environment;
public WorkflowRequestInterceptor(String serviceVersion) { public WorkflowRequestInterceptor(String serviceVersion, Environment environment) {
this.serviceVersion = serviceVersion; this.serviceVersion = serviceVersion;
this.environment = environment;
} }
public static final String HEADER_HTTP_CLIENT = "http-client"; public static final String HEADER_HTTP_CLIENT = "Http-Client";
public static final String HEADER_HTTP_CLIENT_VALUE = "workflowEngine-feign"; public static final String HEADER_HTTP_CLIENT_VALUE = "WorkflowEngine-Feign";
public static final String HEADER_API_VERSION = "service-version"; public static final String HEADER_API_VERSION = "Service-Version";
public static final String HEADER_SERVER_NAME = "X-SERVER-NAME"; public static final String HEADER_SERVER_NAME = "X-SERVER-NAME";
@ -31,6 +34,7 @@ public class WorkflowRequestInterceptor implements RequestInterceptor {
|| apiClassPath.contains("cn.axzo.workflow.client.feign.manage")) { || apiClassPath.contains("cn.axzo.workflow.client.feign.manage")) {
requestTemplate.header(HEADER_HTTP_CLIENT, HEADER_HTTP_CLIENT_VALUE); requestTemplate.header(HEADER_HTTP_CLIENT, HEADER_HTTP_CLIENT_VALUE);
requestTemplate.header(HEADER_API_VERSION, serviceVersion); requestTemplate.header(HEADER_API_VERSION, serviceVersion);
requestTemplate.header(HEADER_SERVER_NAME, environment.getProperty("spring.application.name"));
} }
} }
} }