ApiGatewayConfig.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package cn.exlive.exbooter.configure;
  18. import com.google.common.collect.Sets;
  19. import lombok.Getter;
  20. import lombok.Setter;
  21. import lombok.extern.slf4j.Slf4j;
  22. import org.apache.commons.lang.StringUtils;
  23. import org.springframework.beans.factory.InitializingBean;
  24. import org.springframework.beans.factory.annotation.Autowired;
  25. import org.springframework.boot.autoconfigure.web.ServerProperties;
  26. import org.springframework.cloud.netflix.zuul.filters.SimpleRouteLocator;
  27. import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
  28. import org.springframework.context.annotation.Bean;
  29. import org.springframework.context.annotation.Configuration;
  30. import java.util.HashSet;
  31. import java.util.Map;
  32. import java.util.Optional;
  33. import java.util.Set;
  34. /**
  35. *
  36. * Http 代理, WebSocket 代理
  37. *
  38. * <pre>
  39. *
  40. * Created by zhenqin.
  41. * User: zhenqin
  42. * Date: 2022/4/25
  43. * Time: 下午7:08
  44. *
  45. * </pre>
  46. *
  47. * @author zhenqin
  48. */
  49. @Slf4j
  50. @Setter
  51. @Getter
  52. @Configuration
  53. public class ApiGatewayConfig implements InitializingBean {
  54. @Autowired
  55. ApiRouteProperties apiRouteProperties;
  56. @Autowired
  57. ServerProperties serverProperties;
  58. @Autowired
  59. ZuulProperties zuulProperties;
  60. @Override
  61. public void afterPropertiesSet() throws Exception {
  62. final Map<String, ApiRoute> routers = apiRouteProperties.getRouters();
  63. final Set<String> ignoreUrlSet = new HashSet<>(); // 排除的 URL
  64. for (Map.Entry<String, ApiRoute> entry : routers.entrySet()) {
  65. if (entry.getValue() == null || StringUtils.isBlank(entry.getValue().getUrl())) {
  66. continue;
  67. }
  68. // 代理地址
  69. final String url = entry.getValue().getUrl();
  70. final String api = entry.getValue().getApi();
  71. // 代理路由
  72. String path = Optional.ofNullable(StringUtils.trimToNull(api)).orElse("/" + entry.getKey() + "/**");
  73. final ZuulProperties.ZuulRoute route = new ZuulProperties.ZuulRoute();
  74. route.setId(entry.getKey());
  75. route.setPath(path);
  76. route.setUrl(url);
  77. // WebSocket 的代理需要特殊处理
  78. if ("WS".equalsIgnoreCase(entry.getValue().getType())) {
  79. // websocket 是绝对路径,不需要做正则处理
  80. route.setPath(api);
  81. route.setUrl(url);
  82. log.info("add websocket router {}{} to path: {}", serverProperties.getServlet().getContextPath(), api, url);
  83. ignoreUrlSet.add(api);
  84. } else {
  85. // 返回拼接的地址
  86. String tmpUrl = apiRouteProperties.getServletPath().endsWith("/") ?
  87. apiRouteProperties.getServletPath().substring(0, apiRouteProperties.getServletPath().length() - 1) + path
  88. :
  89. apiRouteProperties.getServletPath() + path;
  90. log.info("add router {}{} to path: {}", serverProperties.getServlet().getContextPath(), tmpUrl, url);
  91. zuulProperties.getRoutes().putIfAbsent(entry.getKey(), route);
  92. }
  93. }
  94. // 需要排除的 URL, 因为 WebSocket 连接地址 /api/live/ws 是包含在:/api/**, 因此需要排除
  95. zuulProperties.setIgnoredPatterns(Sets.newHashSet(ignoreUrlSet));
  96. // 初始化
  97. zuulProperties.init();
  98. }
  99. /**
  100. * 支持简单的负载均衡
  101. * @return
  102. */
  103. @Bean
  104. public SimpleRouteLocator simpleRouteLocator() {
  105. final ApiRouteLocator apiRouteLocator = new ApiRouteLocator(apiRouteProperties.getServletPath(), this.zuulProperties);
  106. apiRouteLocator.getIgnoredPaths();
  107. return apiRouteLocator;
  108. }
  109. }