在微服務中,Swagger是每一個服務 好比會員服務,訂單服務,支付服務 進行繼承、html
如何將整個微服務中的Swagger進行合成,同一臺服務器上。java
使用Zuul+Swagger實現管理整個微服務API文檔web
使用Nginx+Swagger以不一樣的項目區分跳轉到不一樣的接口文檔spring
Spring Boot支持對Swagger管理,只須要Zuul網關添加對應服務Swagger文檔便可api
一、會員服務和訂單服務都引入對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已經整合好了)app
<dependency>
<groupId>com.spring4all</groupId>
<artifactId>swagger-spring-boot-starter</artifactId>
<version>1.7.0.RELEASE</version>
</dependency>負載均衡
二者選擇其一maven
而後啓動類須要:ide
@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
也須要引入相同的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 而後訪問
獲取接口文檔