思路

每个微服务引入各自的swagger文档,zuul做统一整合。

模块

  • 父工程 springcloud-parent 引入 swagger-spring-boot-starter 依赖:
<!--swagger-spring-boot-->
<dependency>
 <groupId>com.spring4all</groupId>
 <artifactId>swagger-spring-boot-starter</artifactId>
 <version>1.7.0.RELEASE</version>
</dependency>

注意:这里版本必须为1.7,使用1.9会有冲突
image.png

  • 会员模块 springcloud-api-member-service-impl 扫包
swagger:
  base-package: com.baba.api.service.impl

image.png

MemberServiceImpl 中引入 swagger 相关注解,正常来说这部分应该写入Controller中,并且最好用PostMapping或者GetMapping注解,不然每个接口会出现多个。这里只是演示:

@ApiOperation("获取会员相关信息")
@ApiImplicitParam(name = "name",value = "用户信息参数",required = true,dataType = "String")
@RequestMapping("/getMember")
public UserEntity getMember(@RequestParam("name") String name) {
    UserEntity userEntity = new UserEntity();
 userEntity.setName(name);
 userEntity.setPassword("123456");
 return userEntity;
}

image.png

启动类 AppMember 加入 @EnableSwagger2Doc
image.png

  • 订单模块 springcloud-api-order-service-impl 扫包
swagger:
  base-package: com.baba.api

image.png

OrderServiceImpl中引入 swagger 相关注解

// 订单服务接口
@Override
@ApiOperation("获取订单相关信息")
@RequestMapping("/getOrderInfo")
public String getOrderInfo() {
    System.out.println("getOrderInfo-->线程池名称:" + Thread.currentThread().getName());
 return "订单服务接口调用成功!";
}

![上传中...]()
image.png

启动类 AppOrder 加入 @EnableSwagger2Doc
image.png

  • 网关 gateway 中同样引入 swagger-spring-boot-starter依赖
<!--swagger-spring-boot-->
<dependency>
 <groupId>com.spring4all</groupId>
 <artifactId>swagger-spring-boot-starter</artifactId>
 <version>1.7.0.RELEASE</version>
</dependency>

image.png
注意:我这里还引入的 jaxb-api 依赖解决如下报错,这个和jdk版本有关系,我这里还需要引入以下依赖

Error creating bean with name 'xmlModelPlugin': Lookup method resolution failed;
<dependency>
 <groupId>javax.xml.bind</groupId>
 <artifactId>jaxb-api</artifactId>
 <version>2.3.0</version>
</dependency>

TokenFilter 拦截器暂时注释掉:
image.png

启动类 AppGateway 中 添加文档来源 并注入 @EnableSwagger2Doc 注解

package com.baba.wlb;
import com.spring4all.swagger.EnableSwagger2Doc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
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;
/**
 * @Author wulongbo
 * @Date 2021/1/28 11:52
 * @Version 1.0
 */@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
@EnableSwagger2Doc
public class AppGateway {
    //@EnableZuulProxy 开启网关代理
 public static void main(String[] args) {
        SpringApplication.run(AppGateway.class, args);
 }
    // zuul配置能够使用config实现实时更新
 @RefreshScope
 @ConfigurationProperties("zuul")
    public ZuulProperties zuulProperties(){
        return new ZuulProperties();
 }
    // 添加文档来源
 @Component
 @Primary class DocumentationConfig implements SwaggerResourcesProvider{
        @Override
 public List<SwaggerResource> get() {
            List resource = new ArrayList();
 resource.add(swaggerResource("app-member","/api-member/v2/api-docs","2.0"));
 resource.add(swaggerResource("app-order","/api-order/v2/api-docs","2.0"));
 return resource;
 }
        private SwaggerResource swaggerResource(String name,String location,String version){
            SwaggerResource swaggerResource=new SwaggerResource();
 swaggerResource.setName(name);
 swaggerResource.setLocation(location);
 swaggerResource.setSwaggerVersion(version);
 return swaggerResource;
 }
    }
}

image.png

启动

依次启动,Eureka server(集群[8100,9100]),config server,member服务,order服务,gateway(集群[81,82]),以及nginx(80)。
image.png

启动成功后使用nginx做服务转发访问 http://localhost/swagger-ui.html
image.png
切换服务名称:
image.png

至此 Zuul网关就管理到了整个微服务Swagger文档


isWulongbo
228 声望26 粉丝

在人生的头三十年,你培养习惯,后三十年,习惯铸就你