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