欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Springcloud基于Zuul做网关路由聚合SwaggerUi的配置

程序员文章站 2022-05-29 19:23:38
...

第一步,在服务中手动创建Docket;
 

swagger配置类SwaggerConfig

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("公共APi")
                .description("测试")
                .version("1.0")
                .build();
    }
}

第二步,引入依赖
 

      <dependency>
            <groupId>com.spring4all</groupId>
            <artifactId>swagger-spring-boot-starter</artifactId>
            <version>1.8.0.RELEASE</version>
        </dependency>

第三步,在启动类上加入注解@EnableSwagger2Doc

 

@SpringCloudApplication
@EnableSwagger2Doc
public class PublicApplication {

    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder builder) {
        return builder.build();
    }

    public static void main(String[] args) {
        SpringApplication.run(PublicApplication.class, args);
    }

第三步在网关Zuul启动类上加上EnableSwagger2Doc,并配置SwaggerUi Resource

@EnableZuulProxy
@EnableFeignClients
@SpringCloudApplication
@EnableCaching
@EnableSwagger2Doc
public class TripGatewayApplication {
    public static void main(String[] args) {
        SpringApplication.run(TripGatewayApplication.class, args);
    }

    @Primary
    @Component
    class DocumentationConfig implements SwaggerResourcesProvider {


        @Override
        public List<SwaggerResource> get() {
            List<SwaggerResource> swaggerResourceList = new ArrayList<>();
            swaggerResourceList.add(createSwaggerResource("public-swagger", "http://localhost:10015/v2/api-docs", "1.0"));
            swaggerResourceList.add(createSwaggerResource("country-swagger", "http://localhost:10012/v2/api-docs", "1.0"));
            return swaggerResourceList;
        }

        private SwaggerResource createSwaggerResource(String name, String url, String version){
            SwaggerResource swaggerResource = new SwaggerResource();
            swaggerResource.setName(name);
            swaggerResource.setUrl(url);
            swaggerResource.setSwaggerVersion(version);
            return swaggerResource;
        }
    }

}

第五步,分别在上public-swagger、country-swagger 两个服务中加入跨域请求的配置;


@Configuration
public class CorsConfig {
    private CorsConfiguration buildConfig() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        return corsConfiguration;
    }

    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        // 配置所有请求
        source.registerCorsConfiguration("/**", buildConfig());
        return new CorsFilter(source);
    }

}

大功告成,看效果;说明:网关端口是10001,public-swagger端口是10015、country-swagger端口是10012

Springcloud基于Zuul做网关路由聚合SwaggerUi的配置