ForwardAutoConfiguration class.
@@ -27,16 +31,24 @@ * @author zzl_i * @version 1.0.0 */ + +@Configuration @EnableConfigurationProperties(GatewayProperties.class) -@ComponentScan(basePackages = "com.alipay.sofa.koupleless.ext.web") public class ForwardAutoConfiguration { @Autowired private GatewayProperties gatewayProperties; + @PostConstruct + public void init() { + if (ArkUtils.isMasterBiz()) { + ArkClient.getEventAdminService().register(new CompositeBizForwardsHandler()); + } + } + /** *forwards.
* - * @return a {@link com.alipay.sofa.koupleless.ext.web.gateway.Forwards} object + * @return a {@link Forwards} object */ @Bean public Forwards forwards() { diff --git a/koupleless-ext/koupleless-ext-web-gateway/src/main/java/com/alipay/sofa/koupleless/ext/web/gateway/ForwardItem.java b/koupleless-ext/koupleless-ext-autoconfigure/src/main/java/com/alipay/sofa/koupleless/ext/autoconfigure/web/gateway/ForwardItem.java similarity index 96% rename from koupleless-ext/koupleless-ext-web-gateway/src/main/java/com/alipay/sofa/koupleless/ext/web/gateway/ForwardItem.java rename to koupleless-ext/koupleless-ext-autoconfigure/src/main/java/com/alipay/sofa/koupleless/ext/autoconfigure/web/gateway/ForwardItem.java index 9266e9fa7..fd3525df2 100644 --- a/koupleless-ext/koupleless-ext-web-gateway/src/main/java/com/alipay/sofa/koupleless/ext/web/gateway/ForwardItem.java +++ b/koupleless-ext/koupleless-ext-autoconfigure/src/main/java/com/alipay/sofa/koupleless/ext/autoconfigure/web/gateway/ForwardItem.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alipay.sofa.koupleless.ext.web.gateway; +package com.alipay.sofa.koupleless.ext.autoconfigure.web.gateway; import lombok.Getter; diff --git a/koupleless-ext/koupleless-ext-web-gateway/src/main/java/com/alipay/sofa/koupleless/ext/web/gateway/ForwardItemComparator.java b/koupleless-ext/koupleless-ext-autoconfigure/src/main/java/com/alipay/sofa/koupleless/ext/autoconfigure/web/gateway/ForwardItemComparator.java similarity index 97% rename from koupleless-ext/koupleless-ext-web-gateway/src/main/java/com/alipay/sofa/koupleless/ext/web/gateway/ForwardItemComparator.java rename to koupleless-ext/koupleless-ext-autoconfigure/src/main/java/com/alipay/sofa/koupleless/ext/autoconfigure/web/gateway/ForwardItemComparator.java index dae3e76eb..d5b51ac4e 100644 --- a/koupleless-ext/koupleless-ext-web-gateway/src/main/java/com/alipay/sofa/koupleless/ext/web/gateway/ForwardItemComparator.java +++ b/koupleless-ext/koupleless-ext-autoconfigure/src/main/java/com/alipay/sofa/koupleless/ext/autoconfigure/web/gateway/ForwardItemComparator.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alipay.sofa.koupleless.ext.web.gateway; +package com.alipay.sofa.koupleless.ext.autoconfigure.web.gateway; import lombok.AccessLevel; import lombok.Getter; diff --git a/koupleless-ext/koupleless-ext-web-gateway/src/main/java/com/alipay/sofa/koupleless/ext/web/gateway/ForwardItems.java b/koupleless-ext/koupleless-ext-autoconfigure/src/main/java/com/alipay/sofa/koupleless/ext/autoconfigure/web/gateway/ForwardItems.java similarity index 93% rename from koupleless-ext/koupleless-ext-web-gateway/src/main/java/com/alipay/sofa/koupleless/ext/web/gateway/ForwardItems.java rename to koupleless-ext/koupleless-ext-autoconfigure/src/main/java/com/alipay/sofa/koupleless/ext/autoconfigure/web/gateway/ForwardItems.java index 45279bd64..b8d27e395 100644 --- a/koupleless-ext/koupleless-ext-web-gateway/src/main/java/com/alipay/sofa/koupleless/ext/web/gateway/ForwardItems.java +++ b/koupleless-ext/koupleless-ext-autoconfigure/src/main/java/com/alipay/sofa/koupleless/ext/autoconfigure/web/gateway/ForwardItems.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alipay.sofa.koupleless.ext.web.gateway; +package com.alipay.sofa.koupleless.ext.autoconfigure.web.gateway; import lombok.AccessLevel; import lombok.NoArgsConstructor; @@ -41,8 +41,8 @@ public class ForwardItems { /** *init.
* - * @param bean a {@link com.alipay.sofa.koupleless.ext.web.gateway.Forwards} object - * @param gatewayProperties a {@link com.alipay.sofa.koupleless.ext.web.gateway.GatewayProperties} object + * @param bean a {@link Forwards} object + * @param gatewayProperties a {@link GatewayProperties} object */ public static void init(Forwards bean, GatewayProperties gatewayProperties) { //load conf diff --git a/koupleless-ext/koupleless-ext-web-gateway/src/main/java/com/alipay/sofa/koupleless/ext/web/gateway/ForwardPath.java b/koupleless-ext/koupleless-ext-autoconfigure/src/main/java/com/alipay/sofa/koupleless/ext/autoconfigure/web/gateway/ForwardPath.java similarity index 93% rename from koupleless-ext/koupleless-ext-web-gateway/src/main/java/com/alipay/sofa/koupleless/ext/web/gateway/ForwardPath.java rename to koupleless-ext/koupleless-ext-autoconfigure/src/main/java/com/alipay/sofa/koupleless/ext/autoconfigure/web/gateway/ForwardPath.java index c7202ec0b..c0ef70ec2 100644 --- a/koupleless-ext/koupleless-ext-web-gateway/src/main/java/com/alipay/sofa/koupleless/ext/web/gateway/ForwardPath.java +++ b/koupleless-ext/koupleless-ext-autoconfigure/src/main/java/com/alipay/sofa/koupleless/ext/autoconfigure/web/gateway/ForwardPath.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alipay.sofa.koupleless.ext.web.gateway; +package com.alipay.sofa.koupleless.ext.autoconfigure.web.gateway; import lombok.Data; diff --git a/koupleless-ext/koupleless-ext-web-gateway/src/main/java/com/alipay/sofa/koupleless/ext/web/gateway/Forwards.java b/koupleless-ext/koupleless-ext-autoconfigure/src/main/java/com/alipay/sofa/koupleless/ext/autoconfigure/web/gateway/Forwards.java similarity index 95% rename from koupleless-ext/koupleless-ext-web-gateway/src/main/java/com/alipay/sofa/koupleless/ext/web/gateway/Forwards.java rename to koupleless-ext/koupleless-ext-autoconfigure/src/main/java/com/alipay/sofa/koupleless/ext/autoconfigure/web/gateway/Forwards.java index 96d4bcec5..7b2b1f473 100644 --- a/koupleless-ext/koupleless-ext-web-gateway/src/main/java/com/alipay/sofa/koupleless/ext/web/gateway/Forwards.java +++ b/koupleless-ext/koupleless-ext-autoconfigure/src/main/java/com/alipay/sofa/koupleless/ext/autoconfigure/web/gateway/Forwards.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.alipay.sofa.koupleless.ext.web.gateway; +package com.alipay.sofa.koupleless.ext.autoconfigure.web.gateway; import org.springframework.util.StringUtils; @@ -55,7 +55,7 @@ public void setItems(ListtomcatCrossContextCustomizer.
diff --git a/koupleless-ext/koupleless-ext-web-gateway/src/main/java/com/alipay/sofa/koupleless/ext/web/gateway/ForwardController.java b/koupleless-ext/koupleless-ext-web-gateway/src/main/java/com/alipay/sofa/koupleless/ext/web/gateway/ForwardController.java index f25105623..412183799 100644 --- a/koupleless-ext/koupleless-ext-web-gateway/src/main/java/com/alipay/sofa/koupleless/ext/web/gateway/ForwardController.java +++ b/koupleless-ext/koupleless-ext-web-gateway/src/main/java/com/alipay/sofa/koupleless/ext/web/gateway/ForwardController.java @@ -16,6 +16,13 @@ */ package com.alipay.sofa.koupleless.ext.web.gateway; +import com.alipay.sofa.ark.api.ArkClient; +import com.alipay.sofa.ark.spi.model.Biz; +import com.alipay.sofa.koupleless.common.log.KouplelessLogger; +import com.alipay.sofa.koupleless.common.log.KouplelessLoggerFactory; +import com.alipay.sofa.koupleless.ext.autoconfigure.web.gateway.CompositeBizForwardsHandler; +import com.alipay.sofa.koupleless.ext.autoconfigure.web.gateway.ForwardItem; +import com.alipay.sofa.koupleless.ext.autoconfigure.web.gateway.Forwards; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Controller; @@ -30,6 +37,7 @@ import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.net.URI; +import java.util.Map; /** *ForwardController class.
@@ -40,11 +48,17 @@ @Controller @RequestMapping public class ForwardController { + private static final KouplelessLogger LOGGER = KouplelessLoggerFactory + .getLogger("web-gateway"); + @Autowired - private Forwards forwards; + private Forwards baseForwards; + + private Mapredirect.
@@ -73,7 +87,7 @@ public void redirect(HttpServletRequest request, if (!StringUtils.hasLength(sourcePath)) { sourcePath = Forwards.ROOT_PATH; } - ForwardItem forwardItem = forwards.getForwardItem(host, sourcePath); + ForwardItem forwardItem = getForwardItem(host, sourcePath); if (forwardItem == null) { throw new ResponseStatusException(HttpStatus.NOT_FOUND); } @@ -84,6 +98,10 @@ public void redirect(HttpServletRequest request, if (targetPath.startsWith(DOUBLE_SEPARATORS)) { targetPath = targetPath.substring(1); } + + LOGGER.info("uri with host {}, sourcePath {} will forward to {}", host, sourcePath, + contextPath + targetPath); + ServletContext currentContext = request.getServletContext(); ServletContext nextContext = currentContext.getContext(contextPath + targetPath); if (currentContext == nextContext) { @@ -92,4 +110,34 @@ public void redirect(HttpServletRequest request, RequestDispatcher dispatcher = nextContext.getRequestDispatcher(targetPath); dispatcher.forward(request, response); } + + /** + * Matching Rule: Preferentially apply the forward configuration of the biz, and if not available, match the forward configuration of base. + * @param host the host of uri + * @param sourcePath the path of uri + * @return com.alipay.sofa.koupleless.ext.autoconfigure.web.gateway.ForwardItem + */ + private ForwardItem getForwardItem(String host, String sourcePath) { + ForwardItem item; + + // match biz forward first + for (Map.Entry