Swagger Gateway MicroService Aggregation(Swagger 网关微服务聚合)
问题描述
我正在使用 SpringBoot 开发一个微服务应用程序.有一个面向公众的网关微服务,它将请求重定向到特定的微服务(在不同的主机上运行).
I am developing a microservice application using SpringBoot. There is Gateway Microservice which is public facing, it redirects requests to particular microservice (which are running on different hosts).
现在,我有多个微服务,每个微服务都使用 Swagger 公开了它们的 API.我们希望为公共客户汇总所有这些 API Swagger 文档.
Now, I've multiple microservices, each microservice has exposed their APIs using Swagger. We would like to aggregate all these API Swagger docs for public clients.
我们合并的临时解决方案是,只是为网关服务中的每个微服务复制了 Swagger 注释类.正确的方法是什么?
Temporary solution we've incorporated is, just copied the Swagger Annotated classes for each microservice in Gateway Service. What is the right way to do it?
推荐答案
我使用了 Zuul,解决了我的问题这就是我的应用程序的部署方式
I used Zuul and that solved my problem This is how my app would be deployed
我在我的 pom.xml
<dependencies>
....
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
</dependency>
</dependencies>
我的主课是这样的
@EnableZuulProxy
@SpringBootApplication
@EnableSwagger2
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
UiConfiguration uiConfig() {
return new UiConfiguration("validatorUrl", "list", "alpha", "schema",
UiConfiguration.Constants.DEFAULT_SUBMIT_METHODS, false, true, 60000L);
}
}
我为 swagger 文档创建了聚合器
I created the aggregator for swagger document
@Component
@Primary
@EnableAutoConfiguration
public class SwaggerAggregatorController implements SwaggerResourcesProvider {
@Override
public List<SwaggerResource> get() {
List<SwaggerResource> resources= new ArrayList<>();
SwaggerResource swaggerResource = new SwaggerResource();
swaggerResource.setName("cust-service");
swaggerResource.setLocation("/cust/v2/api-docs");
swaggerResource.setSwaggerVersion("2.0");
resources.add(swaggerResource);
return resources;
}
}
我可以在这个领域添加更多的微服务.(可以改进为从配置文件中读取)
I can add more microservices in this field. (Can be improved to be read from config file)
我的 application.properties
如下所示
...
server.port=8001
zuul.routes.cust.path=/cust/**
zuul.routes.cust.url=http://1.1.1.2:8002/cust-service/
...
这篇关于Swagger 网关微服务聚合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Swagger 网关微服务聚合


- 转换 ldap 日期 2022-01-01
- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
- 如何指定 CORS 的响应标头? 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
- 未找到/usr/local/lib 中的库 2022-01-01
- 获取数字的最后一位 2022-01-01
- Eclipse 的最佳 XML 编辑器 2022-01-01
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01