Swagger Gateway MicroService Aggregation

2019-02-19 08:49发布

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?

1条回答
Anthone
2楼-- · 2019-02-19 09:33

I used Zuul and that solved my problem This is how my app would be deployed app deployment

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/
...
查看更多
登录 后发表回答