zuul网关实现统一管理swagger

前提环境

搭建一个eureka项目,两个服务项目(这里我搭建的分别是app-shoptest-weixin和app-shoptest-member服务名)

 

 

实践

1.新建zuul项目

 

2.添加依赖

        
            org.springframework.cloud
            spring-cloud-starter-netflix-zuul
        
        
        
            com.spring4all
            swagger-spring-boot-starter
            1.7.0.RELEASE
        

 

3.修改配置文件application.yml

###服务启动端口号
server:
  port: 80
###服务名称(服务注册到eureka名称)  
spring:
    application:
        name: app-shoptest-zuul
###服务注册到eureka地址
eureka:
  client:
    service-url:
           defaultZone: http://localhost:8100/eureka

### 配置网关反向代理
zuul:
  routes:
    api-a:
     ### 以 /api-weixin/访问转发到微信服务
      path: /api-weixin/**
      serviceId: app-shoptest-weixin
    api-b:
        ### 以 /api-member/访问转发到会员服务
      path: /api-member/**
      serviceId: app-shoptest-member

 

4.修改springboot入口类

package com.example;

import com.spring4all.swagger.EnableSwagger2Doc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName GatWayApp
 * @Description TODO
 * @Author lhw
 * @Date 2019/10/10 14:25
 * @Version 1.0
 **/
@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
@EnableSwagger2Doc
public class GatWayApp {

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

    //添加文档来源
    @Component
    @Primary
    class DocumentationConfig implements SwaggerResourcesProvider{

        @Override
        public List get() {
            List resources = new ArrayList<>();
            // 网关使用服务别名获取远程服务的SwaggerApi
            resources.add(swaggerResource("app-shoptest-member","/app-shoptest-member/v2/api-docs","2.0"));
            resources.add(swaggerResource("app-shoptest-weixin","/app-shoptest-weixin/v2/api-docs","2.0"));
            return resources;
        }

        private SwaggerResource swaggerResource(String name, String location, String version) {
            SwaggerResource swaggerResource = new SwaggerResource();
            swaggerResource.setName(name);
            swaggerResource.setLocation(location);
            swaggerResource.setSwaggerVersion(version);
            return swaggerResource;
        }
    }
}

 

5.启动项目

打开浏览器访问http://127.0.0.1/swagger-ui.html

 

zuul网关实现统一管理swagger_第1张图片

 

zuul网关实现统一管理swagger_第2张图片

你可能感兴趣的:(springcloud)