文章目录

引言

在上一节​​《果然新鲜电商项目(05) - 注册中心及Feign远程调用》​​主要讲解了注册中心以及使用Feign远程调用。

本文将开始讲解使用Swagger管理每个微服务的API,并使用网关来进行统一管理。

1.微服务整合Swagger

下面来讲解配置微信服务和会员服务的API Swagger管理,然后整合到网关来实现API的统一管理。

1.1.配置接口层的Maven

配置guoranxinxian-shop-service-api的maven文件,添加swagger依赖:

<!-- swagger-spring-boot -->
<dependency>
<groupId>com.spring4all</groupId>
<artifactId>swagger-spring-boot-starter</artifactId>
<version>1.7.0.RELEASE</version>
</dependency>

1.2 配置会员服务

首先要开启swagger开启配置文件,在会员服务(guoranxinxian-shop-service-member)的启动类添加注解​​@EnableSwagger2Doc​​:

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableSwagger2Doc
public class AppMember {

public static void main(String[] args) {
SpringApplication.run(AppMember.class, args);
}
}

然后在配置文件添加swagger描述(可选):

####swagger相关配置
swagger:
base-package: com.guoranxinxian
title: 果然生鲜电商项目-会员服务接口
description: 该项目“基于SpringCloud2.x构建微服务电商项目。
version: 1.1
terms-of-service-url:
contact:
name: bruce
email: xxxxxx@

最后在接口(​​guoranxinxian-shop-service-api-member​​)添加Swagger文档注解描述:

@Api(tags = "会员服务接口")
public interface MemberService {

@ApiOperation(value = "会员服务调用微信服务")
@GetMapping("/memberInvokWeixin")
public AppEntity memberInvokWeixin();
}

1.3 配置微信服务

配置微信服务与配置会员服务的流程导致一致。

首先要开启swagger开启配置文件,在微信服务(​​guoranxinxian-shop-service-weixin​​​)的启动类添加注解​​@EnableSwagger2Doc​​:

@SpringBootApplication
@EnableEurekaClient
@EnableSwagger2Doc
public class AppWeiXin {

public static void main(String[] args) {
SpringApplication.run(AppWeiXin.class, args);
}
}

然后在配置文件添加swagger描述(可选):

####swagger相关配置
swagger:
base-package: com.guoranxinxian
title: 果然生鲜电商项目-微信服务接口
description: 该项目“基于SpringCloud2.x构建微服务电商项目。
version: 1.1
terms-of-service-url:
contact:
name: bruce
email: xxxxxx@

最后在接口(​​guoranxinxian-shop-service-api-weixin​​)添加Swagger文档注解描述:

@Api(tags = "微信服务接口")
public interface AppService {

@ApiOperation(value = "微信应用服务接口")
@GetMapping("/getApp")
public AppEntity getApp();

}

2.网关统一管理API

模块定位到​​guoranxinxian-shop-basics-zuul​​。

2.1 Maven依赖

<dependencies>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>

<!-- swagger-spring-boot -->
<dependency>
<groupId>com.spring4all</groupId>
<artifactId>swagger-spring-boot-starter</artifactId>
<version>1.7.0.RELEASE</version>
</dependency>

</dependencies>

2.2 统一管理API代码

配置启动类开启Swagger,并配置每个子Swagger的相关内容,如下:

package com.guoranxinxian;

import com.spring4all.swagger.EnableSwagger2Doc;
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 springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;

import java.util.ArrayList;
import java.util.List;

@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
@EnableSwagger2Doc
public class AppGateWay {

public static void main(String[] args) {
SpringApplication.run(AppGateWay.class, args);
}

// 添加文档来源
@Component
@Primary
class DocumentationConfig implements SwaggerResourcesProvider {

@Override
public List<SwaggerResource> get() {
List resources = new ArrayList<>();
// 网关使用服务别名获取远程服务的SwaggerApi
resources.add(swaggerResource("guoranxinxian-shop-service-member", "/guoranxinxian-shop-service-member/v2/api-docs", "2.0"));
resources.add(swaggerResource("guoranxinxian-shop-service-weixin", "/guoranxinxian-shop-service-weixin/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;
}
}

}

2.3 配置

###服务启动端口号
server:
port: 80
###服务名称(服务注册到eureka名称)
spring:
application:
name: guoranxinxian-shop-basics-zuul
###服务注册到eureka地址
eureka:
client:
service-url:
defaultZone: http://localhost:8100/eureka

3. 配置

3.1 验证单个服务swagger

1.启动Eureka注册中心服务(​​AppEureka​​​),启动后浏览器输入:​​http://localhost:8100/​​​,可以看到如下:
《果然新鲜》电商项目(05)- Swagger及网关统一管理API_java
2.启动会员服务(​​​AppMember​​)

3.启动微信服务(​​AppWeixin​​)

4.验证会员服务,浏览器输入:​​http://localhost:8300/swagger-ui.html#/​​​,可以看到swagger管理的接口内容如下:
《果然新鲜》电商项目(05)- Swagger及网关统一管理API_spring_02
6.验证微信服务,浏览器输入:​​​http://localhost:8200/swagger-ui.html#/​​​,可以看到swagger管理的接口内容如下:
《果然新鲜》电商项目(05)- Swagger及网关统一管理API_微信_03

3.2 验证网关整合后的swagger

最后我们来启动网关服务(​​AppGateWay​​​),然后浏览器输入
​​​http://localhost/swagger-ui.html#/​​​,在如下界面,可以看到可以选择查看项目的API文档,比如选择“会员服务服务”的接口,如下:
《果然新鲜》电商项目(05)- Swagger及网关统一管理API_微服务_04
在比如选择“会员服务项目”的接口,如下:
《果然新鲜》电商项目(05)- Swagger及网关统一管理API_微服务_05

4.总结

《果然新鲜》电商项目(05)- Swagger及网关统一管理API_微信_06