Skip to content

Commit

Permalink
聚合 Swagger
Browse files Browse the repository at this point in the history
  • Loading branch information
intomylife committed Jan 26, 2020
1 parent ed639ba commit 949bbd1
Show file tree
Hide file tree
Showing 26 changed files with 168 additions and 35 deletions.
4 changes: 2 additions & 2 deletions osc-commons/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<!-- 三坐标 -->
<groupId>com.zwc</groupId>
<artifactId>osc-commons</artifactId>
<version>1.4.1</version>
<version>1.4.2</version>

<!-- 工程名称和描述 -->
<name>osc-commons</name>
Expand Down Expand Up @@ -46,7 +46,7 @@
<!-- 而这个组件的功能就是帮助我们自动生成这个 json 文件 -->
<!-- 我们会用到的另外一个组件 springfox-swagger-ui 就是将这个 json 文件解析出来 -->
<!-- 用一种更友好的方式呈现出来 -->
<springfox.version>2.7.0</springfox.version>
<springfox.version>2.9.2</springfox.version>
</properties>

<!-- 加入依赖 -->
Expand Down
4 changes: 2 additions & 2 deletions osc-service/osc-base-service/osc-base-service-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
<parent>
<groupId>com.zwc</groupId>
<artifactId>osc-base-service</artifactId>
<version>1.4.1</version>
<version>1.4.2</version>
</parent>

<!-- 三坐标 -->
<groupId>com.zwc</groupId>
<artifactId>osc-base-service-api</artifactId>
<version>1.4.1</version>
<version>1.4.2</version>

<!-- 工程名称描述 -->
<name>osc-base-service-api</name>
Expand Down
6 changes: 3 additions & 3 deletions osc-service/osc-base-service/osc-base-service-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
<parent>
<groupId>com.zwc</groupId>
<artifactId>osc-base-service</artifactId>
<version>1.4.1</version>
<version>1.4.2</version>
</parent>

<!-- 三坐标 -->
<groupId>com.zwc</groupId>
<artifactId>osc-base-service-core</artifactId>
<version>1.4.1</version>
<version>1.4.2</version>

<!-- 工程名称描述 -->
<name>osc-base-service-core</name>
Expand All @@ -40,7 +40,7 @@
<dependency>
<groupId>com.zwc</groupId>
<artifactId>osc-base-service-api</artifactId>
<version>1.4.1</version>
<version>1.4.2</version>
</dependency>
<!-- 提供者消费者 -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class AUseBFeignController {

/*
* @ClassName FeignController
* @Desc TODO 通过 log-service 服务名调用 b() 方法
* @Desc TODO 通过 log-service-core 服务名调用 b() 方法
* @Date 2019/5/20 23:13
* @Version 1.0
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
* @Date 2019/5/20 23:21
* @Version 1.0
*/
@FeignClient(value = "log-service" , fallback = FeignApiFallBack.class)
@FeignClient(value = "log-service-core" , fallback = FeignApiFallBack.class)
public interface FeignApi {

/*
* @ClassName FeignApi
* @Desc TODO 通过 log-service 服务名调用 b() 方法
* @Desc TODO 通过 log-service-core 服务名调用 b() 方法
* @Date 2019/5/20 23:21
* @Version 1.0
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ spring:
## 多个环境的公用配置
application:
# 应用名称
name: base-service
name: base-service-core

# 配置 Flyway
flyway:
Expand Down
4 changes: 2 additions & 2 deletions osc-service/osc-base-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
<parent>
<groupId>com.zwc</groupId>
<artifactId>osc-service</artifactId>
<version>1.4.1</version>
<version>1.4.2</version>
</parent>

<!-- 三坐标 -->
<groupId>com.zwc</groupId>
<artifactId>osc-base-service</artifactId>
<version>1.4.1</version>
<version>1.4.2</version>

<!-- 工程名称描述 -->
<name>osc-base-service</name>
Expand Down
21 changes: 19 additions & 2 deletions osc-service/osc-gateway-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
<parent>
<groupId>com.zwc</groupId>
<artifactId>osc-service</artifactId>
<version>1.4.1</version>
<version>1.4.2</version>
</parent>

<!-- 三坐标 -->
<groupId>com.zwc</groupId>
<artifactId>osc-gateway-service</artifactId>
<version>1.4.1</version>
<version>1.4.2</version>

<!-- 工程名称描述 -->
<name>osc-gateway-service</name>
Expand All @@ -28,6 +28,12 @@
<fastjson.version>1.2.47</fastjson.version>
<!-- docker 镜像名称 -->
<docker.image.name>osc-gateway</docker.image.name>
<!-- swagger -->
<!-- pringfox-swagger2 依然是依赖 OSA 规范文档,也就是一个描述 API 的 json 文件 -->
<!-- 而这个组件的功能就是帮助我们自动生成这个 json 文件 -->
<!-- 我们会用到的另外一个组件 springfox-swagger-ui 就是将这个 json 文件解析出来 -->
<!-- 用一种更友好的方式呈现出来 -->
<springfox.version>2.9.2</springfox.version>
</properties>

<!-- 加入依赖 -->
Expand Down Expand Up @@ -63,6 +69,17 @@
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
<!-- swagger 依赖 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${springfox.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${springfox.version}</version>
</dependency>
</dependencies>

<!-- 插件依赖 -->
Expand Down
Binary file modified osc-service/osc-gateway-service/src/main/.DS_Store
Binary file not shown.
Binary file modified osc-service/osc-gateway-service/src/main/java/.DS_Store
Binary file not shown.
Binary file modified osc-service/osc-gateway-service/src/main/java/com/.DS_Store
Binary file not shown.
Binary file modified osc-service/osc-gateway-service/src/main/java/com/zwc/.DS_Store
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.zwc.gateway.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.stereotype.Component;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;

import java.util.*;

/**
* @ClassName MySwaggerResourceProvider
* @Desc TODO 聚合 Swagger
* @Date 2020/1/26 5:36 PM
* @Version 1.0
*/
@Component
public class MySwaggerResourceProvider implements SwaggerResourcesProvider {

/**
* 特定服务名称后缀
*/
public static final String SERVICE_SUFFIX = "core";

/**
* 固定地址
*/
public static final String FIXED_URL = "/v2/api-docs";

/**
* 网关路由
*/
private final RouteLocator routeLocator;

@Autowired(required = false)
public MySwaggerResourceProvider(RouteLocator routeLocator) {
this.routeLocator = routeLocator;
}

@Override
public List<SwaggerResource> get() {
// 返回结果
List<SwaggerResource> resources = new ArrayList<>();
// 所有路由
List<String> routeHosts = new ArrayList<>();
// 过滤并添加路由(只加载指定服务:如处理业务逻辑的服务)
routeLocator.getRoutes().filter(route -> route.getUri().getHost() != null)
.filter(route -> route.getUri().getHost().indexOf(SERVICE_SUFFIX.toUpperCase()) != -1)
.subscribe(route -> routeHosts.add(route.getUri().getHost()));

// 去重已添加的地址
Set<String> dealed = new HashSet<>();
// 遍历所有路由
routeHosts.forEach(instance -> {
// 拼接地址,格式为:/ + 路由 + 固定地址
String url = "/" + instance + FIXED_URL;
// 去重
if (!dealed.contains(url)) {
dealed.add(url);
SwaggerResource swaggerResource = new SwaggerResource();
swaggerResource.setUrl(url);
swaggerResource.setName(instance);
resources.add(swaggerResource);
}
});
// 返回
return resources;
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.zwc.gateway.hystrix;
package com.zwc.gateway.controller.hystrix;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.zwc.gateway.controller.swagger;

import com.zwc.gateway.config.MySwaggerResourceProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.swagger.web.*;

import java.util.List;

/**
* @ClassName SwaggerResourceController
* @Desc TODO 重写核心接口
* @Date 2020/1/26 6:38 PM
* @Version 1.0
*/
@RestController
@RequestMapping("/swagger-resources")
public class SwaggerResourceController {

private MySwaggerResourceProvider swaggerResourceProvider;

@Autowired
public SwaggerResourceController(MySwaggerResourceProvider swaggerResourceProvider) {
this.swaggerResourceProvider = swaggerResourceProvider;
}

@RequestMapping(value = "/configuration/security")
public ResponseEntity<SecurityConfiguration> securityConfiguration() {
return new ResponseEntity<>(SecurityConfigurationBuilder.builder().build(), HttpStatus.OK);
}

@RequestMapping(value = "/configuration/ui")
public ResponseEntity<UiConfiguration> uiConfiguration() {
return new ResponseEntity<>(UiConfigurationBuilder.builder().build(), HttpStatus.OK);
}

@RequestMapping
public ResponseEntity<List<SwaggerResource>> swaggerResources() {
return new ResponseEntity<>(swaggerResourceProvider.get(), HttpStatus.OK);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ spring:
# 路由标识(id:标识,具有唯一性) 综合
- id: route_base
# 目标服务地址(uri:地址,请求转发后的地址)
uri: lb://base-service
uri: lb://BASE-SERVICE-CORE
# 路由条件(predicates:断言,匹配 HTTP 请求内容)
predicates:
## 转发地址格式为 uri,/baseService 部分会被下面的过滤器给截取掉
Expand Down
4 changes: 2 additions & 2 deletions osc-service/osc-log-service/osc-log-service-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
<parent>
<groupId>com.zwc</groupId>
<artifactId>osc-log-service</artifactId>
<version>1.4.1</version>
<version>1.4.2</version>
</parent>

<!-- 三坐标 -->
<groupId>com.zwc</groupId>
<artifactId>osc-log-service-api</artifactId>
<version>1.4.1</version>
<version>1.4.2</version>

<!-- 工程名称描述 -->
<name>osc-log-service-api</name>
Expand Down
8 changes: 4 additions & 4 deletions osc-service/osc-log-service/osc-log-service-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
<parent>
<groupId>com.zwc</groupId>
<artifactId>osc-log-service</artifactId>
<version>1.4.1</version>
<version>1.4.2</version>
</parent>

<!-- 三坐标 -->
<groupId>com.zwc</groupId>
<artifactId>osc-log-service-core</artifactId>
<version>1.4.1</version>
<version>1.4.2</version>

<!-- 工程名称描述 -->
<name>osc-log-service-core</name>
Expand All @@ -40,12 +40,12 @@
<dependency>
<groupId>com.zwc</groupId>
<artifactId>osc-log-service-api</artifactId>
<version>1.4.1</version>
<version>1.4.2</version>
</dependency>
<dependency>
<groupId>com.zwc</groupId>
<artifactId>osc-base-service-api</artifactId>
<version>1.4.1</version>
<version>1.4.2</version>
</dependency>
<!-- 提供者消费者 -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ spring:
## 多个环境的公用配置
application:
# 应用名称
name: log-service
name: log-service-core

# 配置 Flyway
flyway:
Expand Down
4 changes: 2 additions & 2 deletions osc-service/osc-log-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
<parent>
<groupId>com.zwc</groupId>
<artifactId>osc-service</artifactId>
<version>1.4.1</version>
<version>1.4.2</version>
</parent>

<!-- 三坐标 -->
<groupId>com.zwc</groupId>
<artifactId>osc-log-service</artifactId>
<version>1.4.1</version>
<version>1.4.2</version>

<!-- 工程名称描述 -->
<name>osc-log-service</name>
Expand Down
4 changes: 2 additions & 2 deletions osc-service/osc-registry-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
<parent>
<groupId>com.zwc</groupId>
<artifactId>osc-service</artifactId>
<version>1.4.1</version>
<version>1.4.2</version>
</parent>

<!-- 三坐标 -->
<groupId>com.zwc</groupId>
<artifactId>osc-registry-service</artifactId>
<version>1.4.1</version>
<version>1.4.2</version>

<!-- 工程名称描述 -->
<name>osc-registry-service</name>
Expand Down
4 changes: 2 additions & 2 deletions osc-service/osc-sleuth-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
<parent>
<groupId>com.zwc</groupId>
<artifactId>osc-service</artifactId>
<version>1.4.1</version>
<version>1.4.2</version>
</parent>

<!-- 三坐标 -->
<groupId>com.zwc</groupId>
<artifactId>osc-sleuth-service</artifactId>
<version>1.4.1</version>
<version>1.4.2</version>

<!-- 工程名称描述 -->
<name>osc-sleuth-service</name>
Expand Down
Loading

0 comments on commit 949bbd1

Please sign in to comment.