zuul网关实现统一管理swagger

前提环境

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

 

 

实践

1.新建zuul项目

 

2.添加依赖

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
        </dependency>
        <!-- swagger-spring-boot -->
        <dependency>
            <groupId>com.spring4all</groupId>
            <artifactId>swagger-spring-boot-starter</artifactId>
            <version>1.7.0.RELEASE</version>
        </dependency>

 

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<SwaggerResource> 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

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值