ApiGatewayConfig.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 com.datasophon.api.configuration;
  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 org.springframework.context.annotation.DependsOn;
  31. import org.springframework.core.annotation.Order;
  32. import java.util.HashSet;
  33. import java.util.Map;
  34. import java.util.Optional;
  35. import java.util.Set;
  36. /**
  37. *
  38. * Http 代理, WebSocket 代理
  39. *
  40. * <pre>
  41. *
  42. * Created by zhenqin.
  43. * User: zhenqin
  44. * Date: 2022/4/25
  45. * Time: 下午7:08
  46. *
  47. * </pre>
  48. *
  49. * @author zhenqin
  50. */
  51. @Slf4j
  52. @Setter
  53. @Getter
  54. @Configuration
  55. public class ApiGatewayConfig implements InitializingBean {
  56. @Autowired
  57. ApiRouteProperties apiRouteProperties;
  58. @Autowired
  59. ServerProperties serverProperties;
  60. @Autowired
  61. ZuulProperties zuulProperties;
  62. @Override
  63. public void afterPropertiesSet() throws Exception {
  64. final Map<String, ApiRoute> routers = apiRouteProperties.getRouters();
  65. final Set<String> ignoreUrlSet = new HashSet<>(); // 排除的 URL
  66. for (Map.Entry<String, ApiRoute> entry : routers.entrySet()) {
  67. if(entry.getValue() == null || StringUtils.isBlank(entry.getValue().getUrl())) {
  68. continue;
  69. }
  70. // 代理地址
  71. final String url = entry.getValue().getUrl();
  72. final String api = entry.getValue().getApi();
  73. // 代理路由
  74. String path = Optional.ofNullable(StringUtils.trimToNull(api)).orElse("/" + entry.getKey() + "/**");
  75. final ZuulProperties.ZuulRoute route = new ZuulProperties.ZuulRoute();
  76. route.setId(entry.getKey());
  77. route.setPath(path);
  78. route.setUrl(url);
  79. // WebSocket 的代理需要特殊处理
  80. if ("WS".equalsIgnoreCase(entry.getValue().getType())) {
  81. // websocket 是绝对路径,不需要做正则处理
  82. route.setPath(api);
  83. route.setUrl(url);
  84. log.info("add websocket router {}{} to path: {}", serverProperties.getServlet().getContextPath(), api, url);
  85. WebSocketConfig.addWebSocketRouter(api, entry.getValue());
  86. ignoreUrlSet.add(api);
  87. } else {
  88. // 返回拼接的地址
  89. String tmpUrl = apiRouteProperties.getServletPath().endsWith("/") ?
  90. apiRouteProperties.getServletPath().substring(0, apiRouteProperties.getServletPath().length() - 1) + path
  91. :
  92. apiRouteProperties.getServletPath() + path;
  93. log.info("add router {}{} to path: {}", serverProperties.getServlet().getContextPath(), tmpUrl, url);
  94. zuulProperties.getRoutes().putIfAbsent(entry.getKey(), route);
  95. }
  96. }
  97. // 需要排除的 URL, 因为 WebSocket 连接地址 /api/live/ws 是包含在:/api/**, 因此需要排除
  98. zuulProperties.setIgnoredPatterns(Sets.newHashSet(ignoreUrlSet));
  99. // 初始化
  100. zuulProperties.init();
  101. }
  102. /**
  103. * 支持简单的负载均衡
  104. * @return
  105. */
  106. @Bean
  107. public SimpleRouteLocator simpleRouteLocator() {
  108. final ApiRouteLocator apiRouteLocator = new ApiRouteLocator(apiRouteProperties.getServletPath(), this.zuulProperties);
  109. apiRouteLocator.getIgnoredPaths();
  110. return apiRouteLocator;
  111. }
  112. }