Commit 99ccb4ae by yuwei

2.0.0项目初始化

parent 2748b800
...@@ -12,8 +12,6 @@ spring: ...@@ -12,8 +12,6 @@ spring:
locator: locator:
enabled: true enabled: true
lower-case-service-id: true lower-case-service-id: true
default-filters:
- StripPrefix=1
routes: routes:
# 认证中心 # 认证中心
- id: datax-auth - id: datax-auth
...@@ -31,6 +29,8 @@ spring: ...@@ -31,6 +29,8 @@ spring:
predicates: predicates:
- Path=/system/** - Path=/system/**
filters: filters:
- SwaggerHeaderFilter
- StripPrefix=1
- name: Hystrix - name: Hystrix
args: args:
name: systemHystrix name: systemHystrix
...@@ -41,6 +41,8 @@ spring: ...@@ -41,6 +41,8 @@ spring:
predicates: predicates:
- Path=/email/** - Path=/email/**
filters: filters:
- SwaggerHeaderFilter
- StripPrefix=1
- name: Hystrix - name: Hystrix
args: args:
name: emailHystrix name: emailHystrix
...@@ -51,6 +53,8 @@ spring: ...@@ -51,6 +53,8 @@ spring:
predicates: predicates:
- Path=/file/** - Path=/file/**
filters: filters:
- SwaggerHeaderFilter
- StripPrefix=1
- name: Hystrix - name: Hystrix
args: args:
name: fileHystrix name: fileHystrix
...@@ -61,7 +65,8 @@ spring: ...@@ -61,7 +65,8 @@ spring:
predicates: predicates:
- Path=/data/factory/** - Path=/data/factory/**
filters: filters:
- StripPrefix=1 - SwaggerHeaderFilter
- StripPrefix=2
- name: Hystrix - name: Hystrix
args: args:
name: dataFactoryHystrix name: dataFactoryHystrix
...@@ -72,7 +77,8 @@ spring: ...@@ -72,7 +77,8 @@ spring:
predicates: predicates:
- Path=/data/market/** - Path=/data/market/**
filters: filters:
- StripPrefix=1 - SwaggerHeaderFilter
- StripPrefix=2
- name: Hystrix - name: Hystrix
args: args:
name: dataMarketHystrix name: dataMarketHystrix
...@@ -82,7 +88,8 @@ spring: ...@@ -82,7 +88,8 @@ spring:
predicates: predicates:
- Path=/data/api/** - Path=/data/api/**
filters: filters:
- StripPrefix=1 - SwaggerHeaderFilter
- StripPrefix=2
- name: Hystrix - name: Hystrix
args: args:
name: dataApiHystrix name: dataApiHystrix
......
...@@ -28,6 +28,16 @@ ...@@ -28,6 +28,16 @@
<version>${swagger2.version}</version> <version>${swagger2.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-bean-validators</artifactId>
<version>${swagger2.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger2.version}</version>
</dependency>
<dependency>
<groupId>cn.datax</groupId> <groupId>cn.datax</groupId>
<artifactId>datax-common-core</artifactId> <artifactId>datax-common-core</artifactId>
<version>2.0.0</version> <version>2.0.0</version>
......
...@@ -3,14 +3,15 @@ package cn.datax.gateway.config; ...@@ -3,14 +3,15 @@ package cn.datax.gateway.config;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import org.springframework.cloud.gateway.config.GatewayProperties; import org.springframework.cloud.gateway.config.GatewayProperties;
import org.springframework.cloud.gateway.route.RouteLocator; import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.support.NameUtils;
import org.springframework.context.annotation.Primary; import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import springfox.documentation.swagger.web.SwaggerResource; import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider; import springfox.documentation.swagger.web.SwaggerResourcesProvider;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set;
@Component @Component
@Primary @Primary
...@@ -21,11 +22,10 @@ public class SwaggerProvider implements SwaggerResourcesProvider { ...@@ -21,11 +22,10 @@ public class SwaggerProvider implements SwaggerResourcesProvider {
private final RouteLocator routeLocator; private final RouteLocator routeLocator;
private final GatewayProperties gatewayProperties; private final GatewayProperties gatewayProperties;
@Override @Override
public List<SwaggerResource> get() { public List<SwaggerResource> get() {
List<SwaggerResource> resources = new ArrayList<>(); List<SwaggerResource> resources = new ArrayList<>();
List<String> routes = new ArrayList<>(); Set<String> routes = new HashSet<>();
//取出gateway的route //取出gateway的route
routeLocator.getRoutes().subscribe(route -> routes.add(route.getId())); routeLocator.getRoutes().subscribe(route -> routes.add(route.getId()));
//结合配置的route-路径(Path),和route过滤,只获取有效的route节点 //结合配置的route-路径(Path),和route过滤,只获取有效的route节点
...@@ -33,8 +33,7 @@ public class SwaggerProvider implements SwaggerResourcesProvider { ...@@ -33,8 +33,7 @@ public class SwaggerProvider implements SwaggerResourcesProvider {
.forEach(routeDefinition -> routeDefinition.getPredicates().stream() .forEach(routeDefinition -> routeDefinition.getPredicates().stream()
.filter(predicateDefinition -> ("Path").equalsIgnoreCase(predicateDefinition.getName())) .filter(predicateDefinition -> ("Path").equalsIgnoreCase(predicateDefinition.getName()))
.forEach(predicateDefinition -> resources.add(swaggerResource(routeDefinition.getId(), .forEach(predicateDefinition -> resources.add(swaggerResource(routeDefinition.getId(),
predicateDefinition.getArgs().get(NameUtils.GENERATED_NAME_PREFIX + "0") "/" + routeDefinition.getId() + API_URI))));
.replace("/**", API_URI)))));
return resources; return resources;
} }
...@@ -42,7 +41,7 @@ public class SwaggerProvider implements SwaggerResourcesProvider { ...@@ -42,7 +41,7 @@ public class SwaggerProvider implements SwaggerResourcesProvider {
SwaggerResource swaggerResource = new SwaggerResource(); SwaggerResource swaggerResource = new SwaggerResource();
swaggerResource.setName(name); swaggerResource.setName(name);
swaggerResource.setLocation(location); swaggerResource.setLocation(location);
swaggerResource.setSwaggerVersion("1.0"); swaggerResource.setSwaggerVersion("2.0");
return swaggerResource; return swaggerResource;
} }
} }
package cn.datax.gateway.filter;
import org.apache.commons.lang3.StringUtils;
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.web.server.ServerWebExchange;
@Component
public class SwaggerHeaderFilter extends AbstractGatewayFilterFactory {
private static final String HEADER_NAME = "X-Forwarded-Prefix";
private static final String URI = "/v2/api-docs";
@Override
public GatewayFilter apply(Object config) {
return (exchange, chain) -> {
ServerHttpRequest request = exchange.getRequest();
String path = request.getURI().getPath();
if (!StringUtils.endsWithIgnoreCase(path,URI)) {
return chain.filter(exchange);
}
String basePath = path.substring(0, path.lastIndexOf(URI));
ServerHttpRequest newRequest = request.mutate().header(HEADER_NAME, basePath).build();
ServerWebExchange newExchange = exchange.mutate().request(newRequest).build();
return chain.filter(newExchange);
};
}
}
package cn.datax.gateway.handler;
import org.springframework.beans.factory.annotation.Autowired;
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.*;
import java.util.Optional;
@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<ResponseEntity<SecurityConfiguration>> securityConfiguration() {
return Mono.just(new ResponseEntity<>(
Optional.ofNullable(securityConfiguration).orElse(SecurityConfigurationBuilder.builder().build()), HttpStatus.OK));
}
@GetMapping("/configuration/ui")
public Mono<ResponseEntity<UiConfiguration>> uiConfiguration() {
return Mono.just(new ResponseEntity<>(
Optional.ofNullable(uiConfiguration).orElse(UiConfigurationBuilder.builder().build()), HttpStatus.OK));
}
@GetMapping("")
public Mono<ResponseEntity> swaggerResources() {
return Mono.just((new ResponseEntity<>(swaggerResources.get(), HttpStatus.OK)));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment