简体   繁体   中英

Swagger Gateway MicroService Aggregation

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).

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.

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?

I used Zuul and that solved my problem This is how my app would be deployed 应用部署

I added this in my 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>

My main class looks like this

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

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)

My application.properties looks like following

...
server.port=8001

zuul.routes.cust.path=/cust/**
zuul.routes.cust.url=http://1.1.1.2:8002/cust-service/
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM