ApiGatewayConfig.java 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package com.yiidata.dataops.server.config;
  2. import com.yiidata.dataops.server.modules.gateway.entity.OpsGatewayServer;
  3. import com.yiidata.dataops.server.modules.gateway.service.OpsGatewayServerService;
  4. import lombok.extern.slf4j.Slf4j;
  5. import org.apache.commons.lang.StringUtils;
  6. import org.springframework.beans.factory.InitializingBean;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.boot.autoconfigure.web.ServerProperties;
  9. import org.springframework.cloud.netflix.zuul.filters.SimpleRouteLocator;
  10. import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
  11. import org.springframework.context.annotation.Bean;
  12. import org.springframework.context.annotation.Configuration;
  13. import java.util.List;
  14. import java.util.Map;
  15. import java.util.Optional;
  16. import java.util.stream.Collectors;
  17. /**
  18. * <pre>
  19. *
  20. * Created by zhenqin.
  21. * User: zhenqin
  22. * Date: 2022/4/25
  23. * Time: 下午7:08
  24. * Vendor: yiidata.com
  25. *
  26. * </pre>
  27. *
  28. * @author zhenqin
  29. */
  30. @Slf4j
  31. @Configuration
  32. public class ApiGatewayConfig implements InitializingBean {
  33. @Autowired
  34. OpsGatewayServerService opsGatewayServerService;
  35. @Autowired
  36. ServerProperties serverProperties;
  37. @Autowired
  38. ZuulProperties zuulProperties;
  39. @Override
  40. public void afterPropertiesSet() throws Exception {
  41. final List<OpsGatewayServer> gatewayServers = opsGatewayServerService.list();
  42. final Map<String, List<OpsGatewayServer>> listMap = gatewayServers.stream()
  43. .collect(Collectors.groupingBy(OpsGatewayServer::getApiRouter));
  44. for (Map.Entry<String, List<OpsGatewayServer>> entry : listMap.entrySet()) {
  45. if(entry.getValue() == null || entry.getValue().isEmpty()) {
  46. continue;
  47. }
  48. // 代理地址
  49. String url = entry.getValue().stream()
  50. .map(OpsGatewayServer::getApiUrl)
  51. .map(StringUtils::trimToNull)
  52. .filter(StringUtils::isNotBlank)
  53. .collect(Collectors.joining(","));
  54. // 代理路由
  55. String path = Optional.ofNullable(StringUtils.trimToNull(entry.getValue().get(0).getApiPath())).orElse("/" + entry.getKey() + "/**");
  56. final ZuulProperties.ZuulRoute route = new ZuulProperties.ZuulRoute();
  57. route.setId(entry.getKey());
  58. route.setPath(path);
  59. route.setUrl(url);
  60. log.info("add router {} to path: {}", path, url);
  61. zuulProperties.getRoutes().putIfAbsent(entry.getKey(), route);
  62. }
  63. // 初始化
  64. zuulProperties.init();
  65. }
  66. /**
  67. * 支持简单的负载均衡
  68. * @return
  69. */
  70. @Bean
  71. public SimpleRouteLocator simpleRouteLocator() {
  72. return new ApiRouteLocator(this.serverProperties.getServlet().getContextPath(),
  73. this.zuulProperties);
  74. }
  75. }