在微服務中,Swagger是每個服務 比如會員服務,訂單服務,支付服務 進行繼承、
如何將整個微服務中的Swagger進行合成,同一台服務器上。
使用Zuul+Swagger實現管理整個微服務API文檔
使用Nginx+Swagger以不同的項目區分跳轉到不同的接口文檔
Spring Boot支持對Swagger管理,只需要Zuul網關添加對應服務Swagger文檔即可
服務配置
1、會員服務和訂單服務都引入對swagger的maven支持
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
等於:(Spring Boot已經整合好了)
<dependency>
<groupId>com.spring4all</groupId>
<artifactId>swagger-spring-boot-starter</artifactId>
<version>1.7.0.RELEASE</version>
</dependency>
兩者選擇其一
然后啟動類需要:
@EnableSwagger2Doc
接着,controller中需要:
類上注解
@Api("訂單接口")
接口方法上
@ApiOperation("訂單服務接口")
@PostMapping("/getOrder")
yml加入掃包范圍:
swagger:
base-package: com.toov5.api
(每個服務都一樣)
如Member:

package com.toov5.api.service.impl; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import com.toov5.api.entity.UserEntity; import com.toov5.api.service.IMemberService; import com.toov5.base.BaseApiService; import com.toov5.base.ResponseBase; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiOperation; @RestController @Api("會員服務接口") public class MemberServiceImpl extends BaseApiService implements IMemberService { @Value("${server.port}") private String serverPort; @RequestMapping("/getMember") public UserEntity getMember(@RequestParam("name") String name) { UserEntity userEntity = new UserEntity(); userEntity.setName(name); userEntity.setAge(10); return userEntity; } @RequestMapping("/getUserInfo") public ResponseBase getUserInfo() { try { Thread.sleep(1500); } catch (Exception e) { } return setResultSuccess("getUserInfo調用成功...."); } @RequestMapping("/") public String Index() { return "我是member"+serverPort; } @ApiOperation(value = "獲取會員信息接口") // 具體描述 @ApiImplicitParam(name = "userName", value = "用戶信息參數", required = true, dataType = "String") // 傳入的參數 ,描述 , 必須傳遞true // , 類型String @GetMapping("/getMemberInfo") public String getMemberInfo(String userName) { System.out.println(userName); return "userName" + userName; } }
啟動:
package com.toov5.api.service.impl; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.openfeign.EnableFeignClients; import com.spring4all.swagger.EnableSwagger2Doc; @SpringBootApplication (scanBasePackages={"com.toov5.*"}) @EnableEurekaClient @EnableFeignClients @EnableSwagger2Doc //開啟swagger文檔) public class AppMember { public static void main(String[] args) { SpringApplication.run(AppMember.class, args); } }
yml
server:
port: 8005
spring:
application:
name: app-toov5-member
ַ
eureka:
client:
service-url:
defaultZone: http://localhost:8100/eureka
register-with-eureka: true
fetch-registry: true
swagger:
base-package: com.toov5.api.service.impl
網關gateway配置:
也需要引入相同的pom
<dependency>
<groupId>com.spring4all</groupId>
<artifactId>swagger-spring-boot-starter</artifactId>
<version>1.7.0.RELEASE</version>
</dependency>
然后啟動類:
package com.toov5; import java.util.ArrayList; import java.util.List; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.zuul.EnableZuulProxy; import org.springframework.context.annotation.Primary; import org.springframework.stereotype.Component; import com.spring4all.swagger.EnableSwagger2Doc; import springfox.documentation.swagger.web.SwaggerResource; import springfox.documentation.swagger.web.SwaggerResourcesProvider; @SpringBootApplication @EnableEurekaClient @EnableZuulProxy //開啟網關代理 @EnableSwagger2Doc //開啟swagger public class AppGateway { public static void main(String[] args) { SpringApplication.run(AppGateway.class, args); } // 添加文檔來源 @Component @Primary class DocumentationConfig implements SwaggerResourcesProvider { public List<SwaggerResource> get() { List resources = new ArrayList<Object>(); //app-itmayiedu-order resources.add(swaggerResource("app-toov5-member", "/api-member/v2/api-docs", "2.0")); // 第一個參數可以隨便寫 第二個參考yml對應 resources.add(swaggerResource("app-toov5-order", "/api-order/v2/api-docs", "2.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; } } }
yml配置:
###注冊 中心
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8100/eureka/
server: ##api網關端口號
port: 81
###網關名稱
spring: ##網關服務名稱
application:
name: service-zuul
###網關名稱
cloud:
config:
####讀取后綴
profile: dev
####讀取config-server注冊地址
discovery:
service-id: confi
### 配置網關反向代理
zuul:
routes:
api-member: ##隨便寫的
### 以 /api-member/訪問轉發到會員服務 通過別名找
path: /api-member/**
serviceId: app-toov5-member ##別名 如果集群的話 默認整合了ribbon 實現輪訓 負載均衡
api-order: ##隨便寫的
### 以 /api-order/訪問轉發到訂單服務
path: /api-order/**
serviceId: app-toov5-order ##別名

與yml的對應!
啟動 eureka zuul member 然后訪問


獲取接口文檔
