/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.datasophon.api.configuration; import com.google.common.collect.Sets; import lombok.Getter; import lombok.Setter; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.web.ServerProperties; import org.springframework.cloud.netflix.zuul.filters.SimpleRouteLocator; import org.springframework.cloud.netflix.zuul.filters.ZuulProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.DependsOn; import org.springframework.core.annotation.Order; import java.util.HashSet; import java.util.Map; import java.util.Optional; import java.util.Set; /** * * Http 代理, WebSocket 代理 * *
 *
 * Created by zhenqin.
 * User: zhenqin
 * Date: 2022/4/25
 * Time: 下午7:08
 *
 * 
* * @author zhenqin */ @Slf4j @Setter @Getter @Configuration public class ApiGatewayConfig implements InitializingBean { @Autowired ApiRouteProperties apiRouteProperties; @Autowired ServerProperties serverProperties; @Autowired ZuulProperties zuulProperties; @Override public void afterPropertiesSet() throws Exception { final Map routers = apiRouteProperties.getRouters(); final Set ignoreUrlSet = new HashSet<>(); // 排除的 URL for (Map.Entry entry : routers.entrySet()) { if(entry.getValue() == null || StringUtils.isBlank(entry.getValue().getUrl())) { continue; } // 代理地址 final String url = entry.getValue().getUrl(); final String api = entry.getValue().getApi(); // 代理路由 String path = Optional.ofNullable(StringUtils.trimToNull(api)).orElse("/" + entry.getKey() + "/**"); final ZuulProperties.ZuulRoute route = new ZuulProperties.ZuulRoute(); route.setId(entry.getKey()); route.setPath(path); route.setUrl(url); // WebSocket 的代理需要特殊处理 if ("WS".equalsIgnoreCase(entry.getValue().getType())) { // websocket 是绝对路径,不需要做正则处理 route.setPath(api); route.setUrl(url); log.info("add websocket router {}{} to path: {}", serverProperties.getServlet().getContextPath(), api, url); WebSocketConfig.addWebSocketRouter(api, entry.getValue()); ignoreUrlSet.add(api); } else { // 返回拼接的地址 String tmpUrl = apiRouteProperties.getServletPath().endsWith("/") ? apiRouteProperties.getServletPath().substring(0, apiRouteProperties.getServletPath().length() - 1) + path : apiRouteProperties.getServletPath() + path; log.info("add router {}{} to path: {}", serverProperties.getServlet().getContextPath(), tmpUrl, url); zuulProperties.getRoutes().putIfAbsent(entry.getKey(), route); } } // 需要排除的 URL, 因为 WebSocket 连接地址 /api/live/ws 是包含在:/api/**, 因此需要排除 zuulProperties.setIgnoredPatterns(Sets.newHashSet(ignoreUrlSet)); // 初始化 zuulProperties.init(); } /** * 支持简单的负载均衡 * @return */ @Bean public SimpleRouteLocator simpleRouteLocator() { final ApiRouteLocator apiRouteLocator = new ApiRouteLocator(apiRouteProperties.getServletPath(), this.zuulProperties); apiRouteLocator.getIgnoredPaths(); return apiRouteLocator; } }