博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
用zuul将微服务的多个swagger api文档聚合成一个文档
阅读量:6927 次
发布时间:2019-06-27

本文共 3174 字,大约阅读时间需要 10 分钟。

hot3.png

先看下效果图:

ed9e08d20b50e5c751d141038c4561e9721.jpg

e7bb9ae0350e09cc039e84b511072abf41f.jpg

myc-order:代表订单服务

myc-user:代表用户服务

myc-car:代表车服务

...等等

下面我简单说下集成步骤和关键点。

1.在每个服务的pom中添加以下依赖。

io.springfox
springfox-swagger2

注意:仅仅需要添加这个就行。zuul负责ui

2.将这个放到你的config启动,注意这里我用了一个占位符获取当前文档的名称,避免写死后续可能添加其他模块。

@ConditionalOnClass(value = {Swagger.class})@Configuration@EnableSwagger2public class SwaggerConfig {    @Value("${spring.application.name}")    private String applicationName;    @Bean    public Docket createRestApi() {        return new Docket(DocumentationType.SWAGGER_2)                .apiInfo(apiInfo())                .select()                .apis(RequestHandlerSelectors.basePackage("com.miaoyouche"))                .paths(PathSelectors.any())                .build()                .globalOperationParameters(parameters());    }    private ApiInfo apiInfo() {        return new ApiInfoBuilder()                .title(applicationName+"接口文档")                .description(applicationName+"接口文档")                .contact(new Contact("miaoyouche", "http://www.miaoyouche.com", "mail.xxx@miaoyouche.com"))                .version("1.0")                .build();    }    private List
parameters() { ParameterBuilder parameterBuilder = new ParameterBuilder(); List
parameters = new ArrayList<>(); parameterBuilder.name("Authorization") .description("Authorization") .modelRef(new ModelRef("string")) .parameterType("header") .required(false).build(); parameters.add(parameterBuilder.build()); return parameters; }}

接着是zuul的配置

3.在zuul的config中添加如下配置,注意这里有个apiNames是所有的分组服务名,避免写死,直接从配置文件读取

@Component@Primarypublic class DocumentationConfig implements SwaggerResourcesProvider {    @Value("${rest.api.names}")    private String[] apiNames;    @Override    public List
get() { List resources = new ArrayList<>(); if (apiNames != null) { Arrays.stream(apiNames).forEach(s -> resources.add(swaggerResource(s, "/openapi/" + s + "/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; }}

4.zuul的pom文件中添加以下依赖:

io.springfox
springfox-swagger2
com.github.xiaoymin
swagger-bootstrap-ui

5.zuul的代理配置:

zuul:  routes:    myc-user:      path: /openapi/myc-user/**      serviceId: myc-user    myc-car:      path: /openapi/myc-car/**      serviceId: myc-car    myc-auth:      path: /openapi/myc-auth/**      serviceId: myc-auth    myc-order:      path: /openapi/myc-order/**      serviceId: myc-order  stripPrefix: true  sensitiveHeaders:

下面就是启动项目进行访问了:

http://localhost:port/doc.html

转载于:https://my.oschina.net/kkrgwbj/blog/1925969

你可能感兴趣的文章
Form表单验证
查看>>
传统OA厂商举步维艰
查看>>
【入门篇】Android学习笔记——常用布局
查看>>
我的友情链接
查看>>
Java应用服务器WildFly
查看>>
Hello 51CTO Blog
查看>>
Python 多重继承
查看>>
实战:搭建一个Internet域名解析环境
查看>>
用Ji框架进行HTML/XML解析的过程
查看>>
J2EE开发一点总结
查看>>
交换机基本配置
查看>>
北京户口的知识
查看>>
linux常见故障排除
查看>>
mysql中关于用户自定义的变量
查看>>
SCCM 2016 为客户端分发管理组件Configuration Manager(二)
查看>>
DVWA+SQLmap+Mysql注入实战
查看>>
我的友情链接
查看>>
微博人人开放平台授权
查看>>
LVM 详解
查看>>
linux下的smart服务
查看>>