diff --git a/README.md b/README.md index d535210b..e54ef03c 100644 --- a/README.md +++ b/README.md @@ -106,7 +106,6 @@ central-platform -- 父项目,公共依赖 │ ├─zlt-config -- 配置中心 │ ├─zlt-doc -- 项目文档 │ ├─zlt-gateway -- api网关一级工程 -│ │ ├─spring-cloud-gateway -- 开发中.. │ │ ├─zuul-gateway -- netflix-zuul[9900] │ ├─zlt-job -- 分布式任务调度一级工程 │ │ ├─job-admin -- 任务管理器[8081] diff --git a/zlt-gateway/pom.xml b/zlt-gateway/pom.xml index 2a079618..3c61cfb1 100644 --- a/zlt-gateway/pom.xml +++ b/zlt-gateway/pom.xml @@ -10,6 +10,5 @@ pom zuul-gateway - spring-cloud-gateway \ No newline at end of file diff --git a/zlt-gateway/spring-cloud-gateway/pom.xml b/zlt-gateway/spring-cloud-gateway/pom.xml deleted file mode 100644 index c0aeb847..00000000 --- a/zlt-gateway/spring-cloud-gateway/pom.xml +++ /dev/null @@ -1,70 +0,0 @@ - - 4.0.0 - - com.zlt - zlt-gateway - 1.4.0 - - sc-gateway - spring cloud gateway网关 - - - - com.zlt - zlt-config - - - - com.zlt - zlt-redis-spring-boot-starter - - - - org.springframework.cloud - spring-cloud-starter-alibaba-nacos-discovery - - - org.springframework.security - spring-security-core - - - org.springframework.boot - spring-boot-starter-webflux - - - org.springframework.cloud - spring-cloud-starter-gateway - - - org.springframework.boot - spring-boot-starter-actuator - - - - io.springfox - springfox-swagger2 - - - io.springfox - springfox-swagger-ui - - - - - - - org.springframework.boot - spring-boot-maven-plugin - - - - repackage - - - - - - ${project.artifactId} - - \ No newline at end of file diff --git a/zlt-gateway/spring-cloud-gateway/src/main/java/com/central/gateway/ApiGateWayApp.java b/zlt-gateway/spring-cloud-gateway/src/main/java/com/central/gateway/ApiGateWayApp.java deleted file mode 100644 index 56c04ce6..00000000 --- a/zlt-gateway/spring-cloud-gateway/src/main/java/com/central/gateway/ApiGateWayApp.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.central.gateway; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.cloud.client.discovery.EnableDiscoveryClient; - -/** - * @author zlt - */ -@EnableDiscoveryClient -@SpringBootApplication -public class ApiGateWayApp { - public static void main(String[] args) { - SpringApplication.run(ApiGateWayApp.class, args); - } -} \ No newline at end of file diff --git a/zlt-gateway/spring-cloud-gateway/src/main/java/com/central/gateway/client/config/CorsConfig.java b/zlt-gateway/spring-cloud-gateway/src/main/java/com/central/gateway/client/config/CorsConfig.java deleted file mode 100644 index 57d2c8d2..00000000 --- a/zlt-gateway/spring-cloud-gateway/src/main/java/com/central/gateway/client/config/CorsConfig.java +++ /dev/null @@ -1,65 +0,0 @@ -package com.central.gateway.client.config; - -import org.springframework.cloud.client.discovery.DiscoveryClient; -import org.springframework.cloud.gateway.discovery.DiscoveryClientRouteDefinitionLocator; -import org.springframework.cloud.gateway.discovery.DiscoveryLocatorProperties; -import org.springframework.cloud.gateway.route.RouteDefinitionLocator; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpMethod; -import org.springframework.http.HttpStatus; -import org.springframework.http.codec.ServerCodecConfigurer; -import org.springframework.http.codec.support.DefaultServerCodecConfigurer; -import org.springframework.http.server.reactive.ServerHttpRequest; -import org.springframework.http.server.reactive.ServerHttpResponse; -import org.springframework.web.cors.reactive.CorsUtils; -import org.springframework.web.server.ServerWebExchange; -import org.springframework.web.server.WebFilter; -import org.springframework.web.server.WebFilterChain; - -import reactor.core.publisher.Mono; - -@Configuration -public class CorsConfig { - private static final String ALL = "*"; - private static final String MAX_AGE = "18000L"; - - @Bean - public RouteDefinitionLocator discoveryClientRouteDefinitionLocator(DiscoveryClient discoveryClient, - DiscoveryLocatorProperties properties) { - return new DiscoveryClientRouteDefinitionLocator(discoveryClient, properties); - } - - @Bean - public ServerCodecConfigurer serverCodecConfigurer() { - return new DefaultServerCodecConfigurer(); - } - - @Bean - public WebFilter corsFilter() { - return (ServerWebExchange ctx, WebFilterChain chain) -> { - ServerHttpRequest request = ctx.getRequest(); - if (!CorsUtils.isCorsRequest(request)) { - return chain.filter(ctx); - } - HttpHeaders requestHeaders = request.getHeaders(); - ServerHttpResponse response = ctx.getResponse(); - HttpMethod requestMethod = requestHeaders.getAccessControlRequestMethod(); - HttpHeaders headers = response.getHeaders(); - headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, requestHeaders.getOrigin()); - headers.addAll(HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS, requestHeaders.getAccessControlRequestHeaders()); - if (requestMethod != null) { - headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS, requestMethod.name()); - } - headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS, "true"); - headers.add(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, ALL); - headers.add(HttpHeaders.ACCESS_CONTROL_MAX_AGE, MAX_AGE); - if (request.getMethod() == HttpMethod.OPTIONS) { - response.setStatusCode(HttpStatus.OK); - return Mono.empty(); - } - return chain.filter(ctx); - }; - } -} diff --git a/zlt-gateway/spring-cloud-gateway/src/main/java/com/central/gateway/swagger/config/GatewaySwaggerProvider.java b/zlt-gateway/spring-cloud-gateway/src/main/java/com/central/gateway/swagger/config/GatewaySwaggerProvider.java deleted file mode 100644 index 0abf3ad3..00000000 --- a/zlt-gateway/spring-cloud-gateway/src/main/java/com/central/gateway/swagger/config/GatewaySwaggerProvider.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.central.gateway.swagger.config; - - -import org.springframework.cloud.gateway.config.GatewayProperties; -import org.springframework.cloud.gateway.route.RouteLocator; -import org.springframework.cloud.gateway.support.NameUtils; -import org.springframework.context.annotation.Primary; -import org.springframework.stereotype.Component; -import springfox.documentation.swagger.web.SwaggerResource; -import springfox.documentation.swagger.web.SwaggerResourcesProvider; - -import java.util.ArrayList; -import java.util.List; - -/** - * @Primary注解的实例优先于其他实例被注入 - */ -@Component -@Primary -public class GatewaySwaggerProvider implements SwaggerResourcesProvider { - public static final String API_URI = "/v2/api-docs"; - private final RouteLocator routeLocator; - private final GatewayProperties gatewayProperties; - - public GatewaySwaggerProvider(RouteLocator routeLocator, GatewayProperties gatewayProperties) { - this.routeLocator = routeLocator; - this.gatewayProperties = gatewayProperties; - } - - @Override - public List get() { - List resources = new ArrayList<>(); - List routes = new ArrayList<>(); - //取出Spring Cloud Gateway中的route - routeLocator.getRoutes().subscribe(route -> routes.add(route.getId())); - //结合application.yml中的路由配置,只获取有效的route节点 - gatewayProperties.getRoutes().stream().filter(routeDefinition -> routes.contains(routeDefinition.getId())) - .forEach(routeDefinition -> routeDefinition.getPredicates().stream() - .filter(predicateDefinition -> ("Path").equalsIgnoreCase(predicateDefinition.getName())) - .forEach(predicateDefinition -> resources.add(swaggerResource(routeDefinition.getId(), - predicateDefinition.getArgs().get(NameUtils.GENERATED_NAME_PREFIX + "0") - .replace("/**", API_URI))))); - return resources; - } - - private SwaggerResource swaggerResource(String name, String location) { - SwaggerResource swaggerResource = new SwaggerResource(); - swaggerResource.setName(name); - swaggerResource.setLocation(location); - swaggerResource.setSwaggerVersion("2.0"); - return swaggerResource; - } -} \ No newline at end of file diff --git a/zlt-gateway/spring-cloud-gateway/src/main/java/com/central/gateway/swagger/filter/GwSwaggerHeaderFilter.java b/zlt-gateway/spring-cloud-gateway/src/main/java/com/central/gateway/swagger/filter/GwSwaggerHeaderFilter.java deleted file mode 100644 index 38ef9afe..00000000 --- a/zlt-gateway/spring-cloud-gateway/src/main/java/com/central/gateway/swagger/filter/GwSwaggerHeaderFilter.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.central.gateway.swagger.filter; - - -import org.springframework.cloud.gateway.filter.GatewayFilter; -import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory; -import org.springframework.http.server.reactive.ServerHttpRequest; -import org.springframework.stereotype.Component; -import org.springframework.util.StringUtils; -import org.springframework.web.server.ServerWebExchange; - -import com.central.gateway.swagger.config.GatewaySwaggerProvider; - -@Component -public class GwSwaggerHeaderFilter extends AbstractGatewayFilterFactory { - private static final String HEADER_NAME = "X-Forwarded-Prefix"; - - @Override - public GatewayFilter apply(Object config) { - return (exchange, chain) -> { - ServerHttpRequest request = exchange.getRequest(); - String path = request.getURI().getPath(); - if (!StringUtils.endsWithIgnoreCase(path, GatewaySwaggerProvider.API_URI)) { - return chain.filter(exchange); - } - String basePath = path.substring(0, path.lastIndexOf(GatewaySwaggerProvider.API_URI)); - ServerHttpRequest newRequest = request.mutate().header(HEADER_NAME, basePath).build(); - ServerWebExchange newExchange = exchange.mutate().request(newRequest).build(); - return chain.filter(newExchange); - }; - } -} \ No newline at end of file diff --git a/zlt-gateway/spring-cloud-gateway/src/main/java/com/central/gateway/swagger/handler/SwaggerHandler.java b/zlt-gateway/spring-cloud-gateway/src/main/java/com/central/gateway/swagger/handler/SwaggerHandler.java deleted file mode 100644 index beecdd3d..00000000 --- a/zlt-gateway/spring-cloud-gateway/src/main/java/com/central/gateway/swagger/handler/SwaggerHandler.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.central.gateway.swagger.handler; - -import org.springframework.beans.factory.annotation.Autowired; -import java.util.Optional; - -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -import reactor.core.publisher.Mono; -import springfox.documentation.swagger.web.SecurityConfiguration; -import springfox.documentation.swagger.web.SecurityConfigurationBuilder; -import springfox.documentation.swagger.web.SwaggerResourcesProvider; -import springfox.documentation.swagger.web.UiConfiguration; -import springfox.documentation.swagger.web.UiConfigurationBuilder; - -@RestController -@RequestMapping("/swagger-resources") -public class SwaggerHandler { - @Autowired(required = false) - private SecurityConfiguration securityConfiguration; - @Autowired(required = false) - private UiConfiguration uiConfiguration; - private final SwaggerResourcesProvider swaggerResources; - - @Autowired - public SwaggerHandler(SwaggerResourcesProvider swaggerResources) { - this.swaggerResources = swaggerResources; - } - - - @GetMapping("/configuration/security") - public Mono> securityConfiguration() { - return Mono.just(new ResponseEntity<>( - Optional.ofNullable(securityConfiguration).orElse(SecurityConfigurationBuilder.builder().build()), HttpStatus.OK)); - } - - @GetMapping("/configuration/ui") - public Mono> uiConfiguration() { - return Mono.just(new ResponseEntity<>( - Optional.ofNullable(uiConfiguration).orElse(UiConfigurationBuilder.builder().build()), HttpStatus.OK)); - } - - @GetMapping("") - public Mono swaggerResources() { - return Mono.just((new ResponseEntity<>(swaggerResources.get(), HttpStatus.OK))); - } -} \ No newline at end of file diff --git a/zlt-gateway/spring-cloud-gateway/src/main/resources/application.yml b/zlt-gateway/spring-cloud-gateway/src/main/resources/application.yml deleted file mode 100644 index b4c6eca6..00000000 --- a/zlt-gateway/spring-cloud-gateway/src/main/resources/application.yml +++ /dev/null @@ -1,32 +0,0 @@ -spring: - cloud: - gateway: - discovery: - locator: - lowerCaseServiceId: true - enabled: true - routes: - # ===================================== - - id: api-user - uri: lb://user-center - order: 8001 - predicates: - - Path=/api-user/** - filters: - - GwSwaggerHeaderFilter - - StripPrefix=1 - - id: api-uaa - uri: lb://uaa-server - order: 8002 - predicates: - - Path=/api-uaa/** - filters: - - GwSwaggerHeaderFilter - - StripPrefix=1 - - id: api-file - uri: lb://file-center - order: 8003 - predicates: - - Path=/api-file/** - filters: - - StripPrefix=1 \ No newline at end of file diff --git a/zlt-gateway/spring-cloud-gateway/src/main/resources/bootstrap.yml b/zlt-gateway/spring-cloud-gateway/src/main/resources/bootstrap.yml deleted file mode 100644 index 8575a93a..00000000 --- a/zlt-gateway/spring-cloud-gateway/src/main/resources/bootstrap.yml +++ /dev/null @@ -1,19 +0,0 @@ -server: - port: 9900 - -zlt: - nacos: - server-addr: 192.168.28.130:8848 - -spring: - application: - name: sc-gateway - cloud: - nacos: - config: - server-addr: ${zlt.nacos.server-addr} - file-extension: yml - shared-dataids: common.yml - refreshable-dataids: common.yml - discovery: - server-addr: ${zlt.nacos.server-addr} \ No newline at end of file