| | |
| | | package com.sinata.zuul; |
| | | |
| | | import com.spring4all.swagger.EnableSwagger2Doc; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.boot.SpringApplication; |
| | | import org.springframework.boot.autoconfigure.SpringBootApplication; |
| | | import org.springframework.boot.builder.SpringApplicationBuilder; |
| | | import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; |
| | | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; |
| | | 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 java.util.ArrayList; |
| | | import java.util.List; |
| | | |
| | | import java.util.Map; |
| | | |
| | | @EnableSwagger2Doc |
| | | @EnableZuulProxy//开启网关服务 |
| | | @EnableDiscoveryClient//开启eureka客户端的消费者 |
| | | @EnableZuulProxy |
| | | @EnableDiscoveryClient |
| | | @SpringBootApplication |
| | | public class ZuulApplication extends SpringBootServletInitializer { |
| | | |
| | |
| | | SpringApplication.run(ZuulApplication.class, args); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 配置Swagger |
| | | * 根据 Zuul 路由动态生成 Swagger 资源,避免页面拿到空列表。 |
| | | */ |
| | | @Component |
| | | @Primary |
| | | class DocumentationConfig implements SwaggerResourcesProvider { |
| | | |
| | | @Autowired |
| | | private ZuulProperties zuulProperties; |
| | | |
| | | @Override |
| | | public List<SwaggerResource> get() { |
| | | List resource=new ArrayList<>(); |
| | | //name可以随便写,location前缀要与zuul配置的path一致。zuul开了token验证,要加上token,否则不用加?token=1 |
| | | // resource.add(swaggerResource("user","/user-server/v2/api-docs","1.0")); |
| | | // resource.add(swaggerResource("driver","/driver-server/v2/api-docs","1.0")); |
| | | // resource.add(swaggerResource("dispatch","/dispatch-server/v2/api-docs","1.0")); |
| | | return resource; |
| | | List<SwaggerResource> resources = new ArrayList<>(); |
| | | for (Map.Entry<String, ZuulProperties.ZuulRoute> entry : zuulProperties.getRoutes().entrySet()) { |
| | | String path = entry.getValue().getPath(); |
| | | if (path == null || path.trim().isEmpty()) { |
| | | continue; |
| | | } |
| | | resources.add(swaggerResource(entry.getKey(), buildApiDocsLocation(path), "2.0")); |
| | | } |
| | | return resources; |
| | | } |
| | | |
| | | //name可以随便写,location前缀要与zuul配置的path一致 |
| | | private SwaggerResource swaggerResource(String name, String location, String version){ |
| | | SwaggerResource swaggerResource=new SwaggerResource(); |
| | | private String buildApiDocsLocation(String path) { |
| | | return path.replace("/**", "/v2/api-docs"); |
| | | } |
| | | |
| | | private SwaggerResource swaggerResource(String name, String location, String version) { |
| | | SwaggerResource swaggerResource = new SwaggerResource(); |
| | | swaggerResource.setName(name); |
| | | swaggerResource.setLocation(location); |
| | | swaggerResource.setSwaggerVersion(version); |
| | | return swaggerResource; |
| | | } |
| | | } |
| | | |
| | | |
| | | @Override |
| | | protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { |