zuul網(wǎng)關(guān)源碼地址: https://github.com/liangxifeng833/springcloud/tree/master/configCenter/zuul_gateway
會員服務(wù)源碼地址: https://github.com/liangxifeng833/springcloud/tree/master/parent/api-member-service-impl
訂單服務(wù)源碼地址:https://github.com/liangxifeng833/springcloud/tree/master/parent/api-order-service-impl
注意: 以上服務(wù)依賴eureka和springCloud分布式配置中心服務(wù)端,啟動(dòng)以上服務(wù)之前必須先啟動(dòng)eureka
和configServer
eureka服務(wù)源碼地址: https://github.com/liangxifeng833/springcloud/tree/master/eureka/springcloud-eureka-service
configServer源碼地址: https://github.com/liangxifeng833/springcloud/tree/master/configCenter/config2.0_server
具體會員/訂單的服務(wù)項(xiàng)目上pom中引入依賴有兩種方式
- 第一: 引入swagger相關(guān)的兩個(gè)依賴, 注意: 需要自己配置
SwaggerConfig
<!--swagger2 rest api -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.2.2</version>
</dependency
- 第二: 引入springBoot整個(gè)Swagger依賴,
swagger2-spring-boot-starter
,這里只有一個(gè)依賴, springBoot已經(jīng)整合好配置了, 所以不需要自己單獨(dú)配置:SwaggerConfig
<!-- spring-boot swagger依賴 -->
<dependency>
<groupId>com.spring4all</groupId>
<artifactId>swagger-spring-boot-starter</artifactId>
<version>1.7.0.RELEASE</version>
</dependency>
修改會員以及訂單服務(wù)的 application.yml
添加掃包配置:
- 修改application.yml,新增:
swagger:
base-package: api.member.service.impl
項(xiàng)目啟動(dòng)文件新增如下注解
//開啟swagger生成api文檔的注解
@EnableSwagger2Doc
在訂單服務(wù)controller中寫接口注解(注意:必須在controller中才會生效)
- 修改會員服務(wù)的
MemberServiceImpl.java
package api.member.servcie.impl;
import api.common.base.BaseApiService;
import api.common.base.ResponseBase;
import api.member.entity.User;
import api.member.service.IMemberServcie;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
/**
* 會員接口實(shí)現(xiàn),注意:實(shí)現(xiàn)的是api.member.servcie項(xiàng)目中的api接口
*/
@RestController
//swagger接口文檔相關(guān)注解
@Api(tags = "會員服務(wù)接口")
public class MemberServiceImpl extends BaseApiService implements IMemberServcie {
@Value("${server.port}")
private Integer serverPort;
@ApiOperation("會員服務(wù)默認(rèn)方法")
@GetMapping("/")
public String index() {
return "我是會員服務(wù),port="+serverPort;
}
@Override
//swagger接口文檔相關(guān)注解--start
@ApiOperation("查詢會員接口")
@ApiImplicitParam(name="name",value = "會員名稱",defaultValue = "用戶信息參數(shù)",required = true)
//swagger接口文檔相關(guān)注解--end
@GetMapping("/getMember")
public User getMember(@RequestParam("name") String name) {
User user = new User();
user.setName(name+serverPort);
user.setAge(20);
return user;
}
@GetMapping("/getUserInfo")
@Override
public ResponseBase getUserInfo() {
try {
//會員服務(wù)接口產(chǎn)生1.5秒的延遲
Thread.sleep(1500);
} catch (InterruptedException e) {
e.printStackTrace();
}
return setResultSuccess("調(diào)用會員服務(wù)接口成功...");
}
@PostMapping("/postDemo")
public ResponseBase postDemo(@RequestBody Map<String,Object> params)
{
return setResultSuccess("調(diào)用成功");
}
- 啟動(dòng)會員服務(wù),然后請求: http://127.0.0.1:8102/swagger-ui.html
訂單服務(wù)的配置與會員服務(wù)完全一樣
配置統(tǒng)一入口Zuul網(wǎng)關(guān)項(xiàng)目
- pom中加入的依賴
<!-- spring-boot swagger依賴 -->
<dependency>
<groupId>com.spring4all</groupId>
<artifactId>swagger-spring-boot-starter</artifactId>
<version>1.7.0.RELEASE</version>
</dependency>
- 項(xiàng)目啟動(dòng)文件
ZuulGatewayApplication.java
開啟swagger注解:
//開啟swagger文檔
@EnableSwagger2Doc
- 新建
DocumentationConfig.java
類,內(nèi)容:
package zuul_gateway;
import org.springframework.cloud.netflix.zuul.filters.RouteLocator;
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;
/**
* zuul整合swagger所用類
* 添加文檔來源嗎
*/
@Component
@Primary
public class DocumentationConfig implements SwaggerResourcesProvider {
private final RouteLocator routeLocator;
public DocumentationConfig(RouteLocator routeLocator) {
this.routeLocator = routeLocator;
}
/**
* 配置所有的微服務(wù)
*
* @return
*/
/* @Override
public List<SwaggerResource> get() {
List<SwaggerResource> resources = new ArrayList<>();
List<Route> routes = routeLocator.getRoutes();
routes.forEach(route -> {
resources.add(swaggerResource(route.getId(), route.getFullPath().replace("**", "v2/api-docs"), "1.0"));
});
return resources;
}*/
/**
* 配置特定的微服務(wù)
* @return
*/
@Override
public List<SwaggerResource> get() {
List resources = new ArrayList<>();
//配置會員服務(wù)和訂單服務(wù)文檔路徑, 注意: api-member和api-order必須與application路由中對應(yīng)的會員和訂單服務(wù)名稱一致
resources.add(swaggerResource("會員服務(wù)", "/api-member/v2/api-docs", "1.0"));
resources.add(swaggerResource("訂單服務(wù)", "/api-order/v2/api-docs", "1.0"));
return resources;
}
private SwaggerResource swaggerResource(String name, String location, String version) {
SwaggerResource swaggerResource = new SwaggerResource();
swaggerResource.setName(name);
swaggerResource.setLocation(location);
swaggerResource.setSwaggerVersion(version);
return swaggerResource;
}
}
- 啟動(dòng)網(wǎng)關(guān)服務(wù)后,訪問: http://127.0.0.1:8302/swagger-ui.html, 可以選擇
Select a spec
選擇不同服務(wù)的文檔, 也就實(shí)現(xiàn)了文檔的統(tǒng)一入口管理.