|
@@ -36,25 +36,13 @@ import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandl
|
|
*/
|
|
*/
|
|
@Slf4j
|
|
@Slf4j
|
|
public class FeignInvokeInterceptor implements RequestInterceptor {
|
|
public class FeignInvokeInterceptor implements RequestInterceptor {
|
|
- private final List<FeignProperties.Mapping> forwards;
|
|
|
|
|
|
+ private final List<FeignProperties.Forward> forwards;
|
|
|
|
|
|
public FeignInvokeInterceptor(@NonNull ApplicationContext applicationContext) {
|
|
public FeignInvokeInterceptor(@NonNull ApplicationContext applicationContext) {
|
|
- this.forwards = this.mappings(applicationContext);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 获取请求转发映射表
|
|
|
|
- *
|
|
|
|
- * @param applicationContext 应用上下文
|
|
|
|
- * @return 请求转发映射列表
|
|
|
|
- */
|
|
|
|
- private List<FeignProperties.Mapping> mappings(ApplicationContext applicationContext) {
|
|
|
|
- FeignProperties.Forward forward = applicationContext.getBean(FeignProperties.class).getForward();
|
|
|
|
- if (!forward.isEnabled()) {
|
|
|
|
- return Collections.emptyList();
|
|
|
|
- }
|
|
|
|
- List<FeignProperties.Mapping> mappings = ObjectUtils.ifNull(
|
|
|
|
- forward.getMappings(), Lists::newArrayList, Lists::newArrayList
|
|
|
|
|
|
+ // 加载请求转发配置
|
|
|
|
+ FeignProperties properties = applicationContext.getBean(FeignProperties.class);
|
|
|
|
+ List<FeignProperties.Forward> forwards = ObjectUtils.ifNull(
|
|
|
|
+ properties.getForwards(), Lists::newArrayList, Lists::newArrayList
|
|
);
|
|
);
|
|
List<Resource> resources = ApplicationContextHolder.getApplicationResources();
|
|
List<Resource> resources = ApplicationContextHolder.getApplicationResources();
|
|
RequestMappingHandlerMapping handlers = applicationContext.getBean(RequestMappingHandlerMapping.class);
|
|
RequestMappingHandlerMapping handlers = applicationContext.getBean(RequestMappingHandlerMapping.class);
|
|
@@ -65,17 +53,18 @@ public class FeignInvokeInterceptor implements RequestInterceptor {
|
|
for (Resource resource : resources) {
|
|
for (Resource resource : resources) {
|
|
String name = ApplicationContextHolder.getApplicationName(resource);
|
|
String name = ApplicationContextHolder.getApplicationName(resource);
|
|
if (StringUtils.isEmpty(name) || ObjectUtils.equals(name, server)
|
|
if (StringUtils.isEmpty(name) || ObjectUtils.equals(name, server)
|
|
- || mappings.stream().anyMatch(mapping -> Objects.equals(mapping.getSource(), name))) {
|
|
|
|
|
|
+ || forwards.stream().anyMatch(forward -> ObjectUtils.equals(forward.getSource(), "*")
|
|
|
|
+ || Objects.equals(forward.getSource(), name))) {
|
|
continue;
|
|
continue;
|
|
}
|
|
}
|
|
- FeignProperties.Mapping mapping = new FeignProperties.Mapping();
|
|
|
|
- mapping.setSource(name);
|
|
|
|
- mapping.setTarget(HostUtils.DEFAULT_LOCAL_NAME + ":" + port);
|
|
|
|
- mapping.setPrefixed(prefixes.contains(name));
|
|
|
|
- mappings.add(mapping);
|
|
|
|
|
|
+ FeignProperties.Forward forward = new FeignProperties.Forward();
|
|
|
|
+ forward.setSource(name);
|
|
|
|
+ forward.setTarget(HostUtils.DEFAULT_LOCAL_NAME + ":" + port);
|
|
|
|
+ forward.setPrefixed(prefixes.contains(name));
|
|
|
|
+ forwards.add(forward);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- return mappings.isEmpty() ? Collections.emptyList() : Lists.newArrayList(mappings);
|
|
|
|
|
|
+ this.forwards = forwards.isEmpty() ? Collections.emptyList() : Lists.newArrayList(forwards);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -113,17 +102,18 @@ public class FeignInvokeInterceptor implements RequestInterceptor {
|
|
// 请求转发处理
|
|
// 请求转发处理
|
|
if (ObjectUtils.notEmpty(this.forwards)) {
|
|
if (ObjectUtils.notEmpty(this.forwards)) {
|
|
Target<?> target = template.feignTarget();
|
|
Target<?> target = template.feignTarget();
|
|
- for (FeignProperties.Mapping mapping : this.forwards) {
|
|
|
|
- if (ObjectUtils.equals(mapping.getSource(), target.name())
|
|
|
|
- || ObjectUtils.equals(mapping.getSource(), "*")) {
|
|
|
|
- if (mapping.isPrefixed()) {
|
|
|
|
|
|
+ for (FeignProperties.Forward forward : this.forwards) {
|
|
|
|
+ if (ObjectUtils.equals(forward.getSource(), target.name())
|
|
|
|
+ || ObjectUtils.equals(forward.getSource(), "*")) {
|
|
|
|
+ if (forward.isPrefixed()) {
|
|
template.uri(HttpUtils.uri(target.name(), template.path()));
|
|
template.uri(HttpUtils.uri(target.name(), template.path()));
|
|
}
|
|
}
|
|
- if (!Objects.equals(mapping.getTarget(), target.name())) {
|
|
|
|
|
|
+ if (StringUtils.notEmpty(forward.getTarget())
|
|
|
|
+ && !Objects.equals(forward.getTarget(), target.name())) {
|
|
URI uri = URI.create(target.url());
|
|
URI uri = URI.create(target.url());
|
|
- String url = uri.getScheme() + "://" + mapping.getTarget();
|
|
|
|
|
|
+ String url = uri.getScheme() + "://" + forward.getTarget();
|
|
template.target(url);
|
|
template.target(url);
|
|
- template.feignTarget(new Target.HardCodedTarget<>(target.type(), mapping.getTarget(), url));
|
|
|
|
|
|
+ template.feignTarget(new Target.HardCodedTarget<>(target.type(), forward.getTarget(), url));
|
|
}
|
|
}
|
|
break;
|
|
break;
|
|
}
|
|
}
|