关于java:Zuul管理整个微服务Swagger文档

4次阅读

共计 3613 个字符,预计需要花费 10 分钟才能阅读完成。

思路

每个微服务引入各自的 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 会有抵触

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

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;
}

启动类 AppMember 退出 @EnableSwagger2Doc

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

OrderServiceImpl中引入 swagger 相干注解

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

![上传中 …]()

启动类 AppOrder 退出 @EnableSwagger2Doc

  • 网关 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>


留神:我这里还引入的 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 拦截器临时正文掉:

启动类 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;
 }
    }
}

启动

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

启动胜利后应用 nginx 做服务转发拜访 http://localhost/swagger-ui.html

切换服务名称:

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

正文完
 0