feat:[REQ-3282] 引入feign依赖
This commit is contained in:
parent
01ebd01ab0
commit
a444c726b5
@ -13,11 +13,29 @@
|
||||
|
||||
|
||||
<dependencies>
|
||||
|
||||
<!-- 三方依赖 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-openfeign</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 二方SDK -->
|
||||
<dependency>
|
||||
<groupId>cn.axzo.apollo</groupId>
|
||||
<artifactId>apollo-api</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>cn.axzo.elise</groupId>
|
||||
<artifactId>elise-cdzj-api</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>cn.axzo.elise</groupId>
|
||||
<artifactId>elise-new-api</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>cn.axzo.orgmanax</groupId>
|
||||
<artifactId>orgmanax-server</artifactId>
|
||||
|
||||
@ -0,0 +1,168 @@
|
||||
package cn.axzo.orgmanax.integration.config;
|
||||
|
||||
import cn.axzo.foundation.exception.BusinessException;
|
||||
import cn.axzo.orgmanax.common.config.BizResultCode;
|
||||
import cn.azxo.framework.common.constatns.Constants;
|
||||
import cn.hutool.core.util.ReflectUtil;
|
||||
import com.google.common.collect.Lists;
|
||||
import feign.RequestInterceptor;
|
||||
import feign.RequestTemplate;
|
||||
import feign.Target;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.collections.CollectionUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.slf4j.MDC;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.env.Environment;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* pudge FeignConfig
|
||||
* <p>为什么配置这个:通过替换原feign接口url地址,解决开发者在本地开发环境启动本服务调试时feign接口无法调用的问题
|
||||
*
|
||||
* @author xiajiafu
|
||||
* @date 2022/08/08
|
||||
**/
|
||||
@Slf4j
|
||||
@Configuration
|
||||
public class FeignConfig implements RequestInterceptor {
|
||||
|
||||
@Autowired
|
||||
private Environment environment;
|
||||
|
||||
private static final String FEIGN_URL_REGEX = "http://([A-Za-z-|_]+)(:[1-9]\\d{1,4}[/]?$)?";
|
||||
private static final String LOCAL_HOST = "localhost";
|
||||
private static final String DEV_HOST = "http://dev-app.axzo.cn/";
|
||||
private static final String TEST_HOST = "http://test-api.axzo.cn/";
|
||||
private static final String PRE_HOST = "http://pre-api.axzo.cn/";
|
||||
private static final List<String> HOSTS = Lists
|
||||
.newArrayList(LOCAL_HOST, DEV_HOST, TEST_HOST, PRE_HOST);
|
||||
|
||||
private static String POD_NAMESPACE;
|
||||
|
||||
static {
|
||||
Map<String, String> env = System.getenv();
|
||||
if (env != null) {
|
||||
POD_NAMESPACE = env.get("MY_POD_NAMESPACE");
|
||||
}
|
||||
log.info("init FeignConfig, POD_NAMESPACE value is {}", POD_NAMESPACE);
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
@Override
|
||||
public void apply(RequestTemplate requestTemplate) {
|
||||
// POD_NAMESPACE为空 说明服务没有运行在rancher中则进行url替换;POD_NAMESPACE不为空时服务间通过Istio通信则不需要替换url
|
||||
if (POD_NAMESPACE == null) {
|
||||
Target.HardCodedTarget<?> target = (Target.HardCodedTarget<?>) requestTemplate.feignTarget();
|
||||
Field field = ReflectUtil.getField(target.getClass(), "url");
|
||||
field.setAccessible(true);
|
||||
String url = (String) field.get(target);
|
||||
// 如需要调用开发者本地启动的xxx服务,传入到toLocalServiceNames
|
||||
List<String> toLocalServiceNames = Lists.newArrayList("");
|
||||
String convertUrl = convertUrl(url, toLocalServiceNames);
|
||||
field.set(target, convertService(convertUrl));
|
||||
target.apply(requestTemplate);
|
||||
}
|
||||
requestTemplate.header(Constants.CTX_LOG_ID_MDC, MDC.get(Constants.CTX_LOG_ID_MDC));
|
||||
}
|
||||
|
||||
/**
|
||||
* 某些服务URL需要特殊处理
|
||||
*/
|
||||
private String convertService(String url) {
|
||||
if (url.contains("data-collection")) {
|
||||
return url.replaceAll("data-collection", "dataCollection");
|
||||
} else if (url.contains("iot-hub")) {
|
||||
return url.replaceAll("iot-hub", "iotHub");
|
||||
}
|
||||
return url;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将feign api中的url替换为其他环境
|
||||
*/
|
||||
private String convertUrl(String url, List<String> toLocalServiceNames) {
|
||||
// 已经替换过的 不再进行替换了
|
||||
if (HOSTS.stream().anyMatch(url::contains)) {
|
||||
return url;
|
||||
}
|
||||
String convertUrl = toLocalHost(url, toLocalServiceNames);
|
||||
if (!convertUrl.equals(url)) {
|
||||
return convertUrl;
|
||||
}
|
||||
// 环境为空或为生产环境,不替换
|
||||
String profile = environment.getProperty("spring.profiles.active");
|
||||
if (profile == null) {
|
||||
return url;
|
||||
}
|
||||
|
||||
// 调用profile对应环境的服务
|
||||
switch (profile.toLowerCase()) {
|
||||
case "local":
|
||||
return toLocalHost(url);
|
||||
case "dev":
|
||||
return toRemoteHost(url, DEV_HOST);
|
||||
case "test":
|
||||
return toRemoteHost(url, TEST_HOST);
|
||||
case "pre":
|
||||
return toRemoteHost(url, PRE_HOST);
|
||||
default:
|
||||
throw new BusinessException(BizResultCode.RPC_ERROR.getErrorCode(),"不支持该环境的feign api调用!", null);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 调用本地启动的服务
|
||||
*
|
||||
* @param url 原url
|
||||
* @param toLocalServiceNames 需要切换到本地的服务名
|
||||
*/
|
||||
private static String toLocalHost(String url, List<String> toLocalServiceNames) {
|
||||
if (CollectionUtils.isEmpty(toLocalServiceNames)) {
|
||||
return url;
|
||||
}
|
||||
if (toLocalServiceNames.stream().anyMatch(o -> StringUtils.isNotBlank(o) && url.contains(o))) {
|
||||
return toLocalHost(url);
|
||||
}
|
||||
return url;
|
||||
}
|
||||
|
||||
/**
|
||||
* 调用本地启动的服务,如:http://apollo:11000 -> http://localhost:11000
|
||||
*/
|
||||
private static String toLocalHost(String url) {
|
||||
String serviceName = getServiceNameFromUrl(url);
|
||||
// 替换服务名为localhost
|
||||
return serviceName == null ? url : url.replaceAll(serviceName, LOCAL_HOST);
|
||||
}
|
||||
|
||||
/**
|
||||
* 调用远程指定环境的服务,如:http://apollo:11000 -> http://dev-app.axzo.cn/apollo
|
||||
*/
|
||||
private static String toRemoteHost(String url, String host) {
|
||||
String serviceName = getServiceNameFromUrl(url);
|
||||
// 拼接远端host到url
|
||||
return serviceName == null ? url : host + serviceName;
|
||||
}
|
||||
|
||||
/**
|
||||
* 提取url中的服务名
|
||||
*/
|
||||
private static String getServiceNameFromUrl(String url) {
|
||||
Pattern pattern = Pattern.compile(FEIGN_URL_REGEX);
|
||||
Matcher matcher = pattern.matcher(url);
|
||||
String serviceName = null;
|
||||
if (matcher.matches()) {
|
||||
serviceName = matcher.group(1);
|
||||
}
|
||||
return serviceName;
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,4 +1,4 @@
|
||||
package cn.axzo.orgmanax.integration.apollo;
|
||||
package cn.axzo.orgmanax.integration.sdk.apollo;
|
||||
|
||||
import cn.axzo.apollo.api.ApolloGroupTaskOrderApi;
|
||||
import cn.axzo.apollo.api.res.CheckWorkerIsFinishGroupRes;
|
||||
Loading…
Reference in New Issue
Block a user