Spring Boot 使用Swagger2構建RESTful風格的API線上文件 & 專案總結
之前做的專案中前後端完全分離,前端為嵌在手機app中的H5,後端需求限定了語言為Java,而且所給的時間非常少。
於是能夠快速搭建配置部署的Spring Boot專案就成了不二之選,加上Swagger2能夠方便的幫助我們構建出功能強大的線上介面文件,方便測試人員對介面的測試和前端的對接。所以直接選用了Spring Boot + Swagger2的方案。
簡單介紹下專案吧
需求中需要每日解析百萬級的歷史資料和百萬級的實時資料接收+計算,因此也面臨著很大的效能壓力。
最後總體後端的技術方案和架構為
技術選型為
Spring Boot +Swagger + mybaties + Jwt + RabbitMQ + haproxy + redis(這個最後沒用上)
架構為
1.3臺介面伺服器(其中1臺負責歷史資料解析(.vm檔案),這個一般每天會有100M-200M左右的資料量,幾百萬條資料,正常是在十幾分鍾內能解析持久化完畢)。
2.1臺負責負載均衡3臺介面伺服器的haproxy伺服器。
3.3臺RabbitMQ伺服器,構建了一個映象叢集
3.3臺MQ消費者(也是Spring Boot專案)伺服器,分別部署了一個消費者專案,雖然實際測試在日均50-100W條資料時一臺消費者也能完成實時資料的解析計算入庫。考慮到節假日可能會出現的波峰還是預設了多臺。
4.1臺負責負載均衡3臺MQ伺服器的haproxy伺服器。
5.1臺redis快取伺服器,不過最後由於沒啥壓力就沒有真的投入使用,作為之後壓力增大後的預備吧。
6.2臺my sql資料庫伺服器,雖然超過百萬級的資料my sql跑起來會比不上sql server和oracle,不過上頭要用這個,我才不會說是因為免費呢。_(:з」∠*)_
雖然最後只用了一臺就成功承受住了壓力就是了(笑)
7.1臺文件伺服器,用來存放每天的歷史資料,每天會從資料提供者那邊自動獲取資料檔案(.vm)然後上面提到的1臺介面伺服器會進行解析入庫。
中間還因為奇葩要求用到了一堆東西,比如
好像有點偏題了,嘛,先來看下完成時的線上文件說明介面吧
然後開始搭建一個Spring Boot + Swagger2的例子吧
1.新建一個Spring Boot專案。
pom.xml
<!--swagger-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version >2.5.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.5.0</version>
</dependency>
整體專案目錄結構
整體搭建非常簡單易懂
Swagger2TestApplication.java
package com.my.swagger2;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
@SpringBootApplication
@Api(value = "springboot服務",description="簡單的計算服務API")
@RestController
public class Swagger2TestApplication {
public static void main(String[] args) {
SpringApplication.run(Swagger2TestApplication.class, args);
}
}
Swagger2.java
SWAGGER_SCAN_BASE_PACKAGE 就是swagger2會掃描到的目錄。
package com.my.swagger2.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class Swagger2 {
public static final String SWAGGER_SCAN_BASE_PACKAGE = "com.my.swagger2.controller";
public static final String VERSION = "1.0.0";
@Bean
public Docket CreateRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage(SWAGGER_SCAN_BASE_PACKAGE))
.paths(PathSelectors.any())
.build();
}
ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Api")
.description("my api test")
.termsOfServiceUrl("https://blog.csdn.net/yeyinglingfeng")
.contact("yeyinglingfeng")
.version(VERSION)
.build();
}
}
myTestController.java
介面
可以看到ApiImplicitParam
為介面引數
多個時以,
分隔,最後一個後面不加,
如果加上required = true
就表示該引數為必填項
package com.my.swagger2.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
@RestController
@RequestMapping(value="/api")
public class myTestController {
@ApiOperation(value="測試資料", notes="獲取字串1+字串2")
@ApiImplicitParams({
@ApiImplicitParam(name = "s1", value = "字串1", paramType = "form", dataType = "String"),
@ApiImplicitParam(name = "s2", value = "字串2", paramType = "form",required = true, dataType = "String")
})
@RequestMapping(value="/get/info", method=RequestMethod.POST)
public String getInfo(HttpServletResponse response,HttpServletRequest request) {
String s1 = request.getParameter("s1");
String s2 = request.getParameter("s2");
String result = s1+s2;
return result;
}
}
這樣就配置好了,來試下,啟動專案
訪問http://localhost:8080/swagger-ui.html#!/
可以看到添加了required = true
的樣子
測試下
結果