my-csc-config-file.git 5 роки тому
батько
коміт
02f6d9b124

+ 15 - 10
pom.xml

@@ -38,16 +38,21 @@
38 38
         <dependency>
39 39
             <groupId>org.springframework.cloud</groupId>
40 40
             <artifactId>spring-cloud-starter-gateway</artifactId>
41
-          <!--  <exclusions>
42
-                <exclusion>
43
-                    <groupId>org.springframework.boot</groupId>
44
-                    <artifactId>spring-boot-starter-web</artifactId>
45
-                </exclusion>
46
-                <exclusion>
47
-                    <groupId>org.springframework.boot</groupId>
48
-                    <artifactId>spring-boot-starter-webflux</artifactId>
49
-                </exclusion>
50
-            </exclusions>-->
41
+        </dependency>
42
+
43
+        <dependency>
44
+            <groupId>io.springfox</groupId>
45
+            <artifactId>springfox-swagger2</artifactId>
46
+        </dependency>
47
+        <dependency>
48
+            <groupId>io.springfox</groupId>
49
+            <artifactId>springfox-swagger-ui</artifactId>
50
+        </dependency>
51
+
52
+        <dependency>
53
+            <groupId>org.projectlombok</groupId>
54
+            <artifactId>lombok</artifactId>
55
+            <optional>true</optional>
51 56
         </dependency>
52 57
 
53 58
         <!--hystrix熔断降级-->

+ 54 - 0
src/main/java/com/unissoft/SwaggerProvider.java

@@ -0,0 +1,54 @@
1
+package com.unissoft;
2
+
3
+import lombok.AllArgsConstructor;
4
+import org.springframework.cloud.gateway.config.GatewayProperties;
5
+import org.springframework.cloud.gateway.route.RouteLocator;
6
+import org.springframework.cloud.gateway.support.NameUtils;
7
+import org.springframework.context.annotation.Primary;
8
+import org.springframework.stereotype.Component;
9
+import springfox.documentation.swagger.web.SwaggerResource;
10
+import springfox.documentation.swagger.web.SwaggerResourcesProvider;
11
+
12
+import java.util.ArrayList;
13
+import java.util.List;
14
+
15
+/**
16
+ * 文件描述
17
+ *
18
+ * @author tongxi.xia
19
+ * @date 2020年12月16日 15:43
20
+ */
21
+
22
+@Component
23
+@Primary
24
+@AllArgsConstructor
25
+public class SwaggerProvider implements SwaggerResourcesProvider {
26
+    public static final String API_URI = "/v2/api-docs";
27
+    private final RouteLocator routeLocator;
28
+    private final GatewayProperties gatewayProperties;
29
+
30
+    @Override
31
+    public List<SwaggerResource> get() {
32
+        List<SwaggerResource> resources = new ArrayList<>();
33
+        List<String> routes = new ArrayList<>();
34
+//取出gateway的route
35
+        routeLocator.getRoutes().subscribe(route -> routes.add(route.getId()));
36
+//结合配置的route-路径(Path),和route过滤,只获取有效的route节点
37
+        gatewayProperties.getRoutes().stream().filter(routeDefinition -> routes.contains(routeDefinition.getId()))
38
+                .forEach(routeDefinition -> routeDefinition.getPredicates().stream()
39
+                        .filter(predicateDefinition -> ("Path").equalsIgnoreCase(predicateDefinition.getName()))
40
+                        .forEach(predicateDefinition -> resources.add(swaggerResource(routeDefinition.getId(),
41
+                                predicateDefinition.getArgs().get(NameUtils.GENERATED_NAME_PREFIX + "0")
42
+                                        .replace("/**", API_URI)))));
43
+        return resources;
44
+    }
45
+
46
+    private SwaggerResource swaggerResource(String name, String location) {
47
+        SwaggerResource swaggerResource = new SwaggerResource();
48
+        swaggerResource.setName(name);
49
+        swaggerResource.setLocation(location);
50
+        swaggerResource.setSwaggerVersion("2.0");
51
+        return swaggerResource;
52
+    }
53
+}
54
+

+ 51 - 0
src/main/java/com/unissoft/controller/SwaggerHandler.java

@@ -0,0 +1,51 @@
1
+package com.unissoft.controller;
2
+
3
+import org.springframework.beans.factory.annotation.Autowired;
4
+import org.springframework.http.HttpStatus;
5
+import org.springframework.http.ResponseEntity;
6
+import org.springframework.web.bind.annotation.GetMapping;
7
+import org.springframework.web.bind.annotation.RequestMapping;
8
+import org.springframework.web.bind.annotation.RestController;
9
+import reactor.core.publisher.Mono;
10
+import springfox.documentation.swagger.web.*;
11
+
12
+import java.util.Optional;
13
+
14
+/**
15
+ * 文件描述
16
+ *
17
+ * @author tongxi.xia
18
+ * @date 2020年12月16日 15:51
19
+ */
20
+@RestController
21
+@RequestMapping("/swagger-resources")
22
+public class SwaggerHandler {
23
+    @Autowired(required = false)
24
+    private SecurityConfiguration securityConfiguration;
25
+    @Autowired(required = false)
26
+    private UiConfiguration uiConfiguration;
27
+    private final SwaggerResourcesProvider swaggerResources;
28
+
29
+    @Autowired
30
+    public SwaggerHandler(SwaggerResourcesProvider swaggerResources) {
31
+        this.swaggerResources = swaggerResources;
32
+    }
33
+
34
+    @GetMapping("/configuration/security")
35
+    public Mono<ResponseEntity<SecurityConfiguration>> securityConfiguration() {
36
+        return Mono.just(new ResponseEntity<>(
37
+                Optional.ofNullable(securityConfiguration).orElse(SecurityConfigurationBuilder.builder().build()), HttpStatus.OK));
38
+    }
39
+
40
+    @GetMapping("/configuration/ui")
41
+    public Mono<ResponseEntity<UiConfiguration>> uiConfiguration() {
42
+        return Mono.just(new ResponseEntity<>(
43
+                Optional.ofNullable(uiConfiguration).orElse(UiConfigurationBuilder.builder().build()), HttpStatus.OK));
44
+    }
45
+
46
+    @GetMapping("")
47
+    public Mono<ResponseEntity> swaggerResources() {
48
+        return Mono.just((new ResponseEntity<>(swaggerResources.get(), HttpStatus.OK)));
49
+    }
50
+}
51
+

+ 2 - 2
src/main/resources/application.yml

@@ -5,8 +5,8 @@ server:
5 5
 eureka:
6 6
   instance:
7 7
     hostname: 172.16.0.2 #地址
8
-#    hostname: 172.16.0.46 #地址
9
-
8
+#    hostname: localhost #地址
9
+    status-page-url: http://${eureka.instance.hostname}:${server.port}/swagger-ui.html
10 10
   client:
11 11
     service-url:
12 12
 #      向注册中心注册服务