在微服务中,Swagger是每个服务 比如会员服务,订单服务,支付服务 进行继承、

如何将整个微服务中的Swagger进行合成,同一台服务器上。

使用Zuul+Swagger实现管理整个微服务API文档

使用Nginx+Swagger以不同的项目区分跳转到不同的接口文档

Spring Boot支持对Swagger管理,只需要Zuul网关添加对应服务Swagger文档即可

服务配置

1、会员服务和订单服务都引入对swagger的maven支持

        <dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.8.0</version></dependency><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>2.8.0</version></dependency>

  

等于:(Spring Boot已经整合好了)

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

  

两者选择其一

然后启动类需要:

@EnableSwagger2Doc

接着,controller中需要:

类上注解

@Api("订单接口")

接口方法上

@ApiOperation("订单服务接口")
 @PostMapping("/getOrder")

yml加入扫包范围:

swagger:
     base-package: com.toov5.api

(每个服务都一样)

如Member:

package com.toov5.api.service.impl;import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;import com.toov5.api.entity.UserEntity;
import com.toov5.api.service.IMemberService;
import com.toov5.base.BaseApiService;
import com.toov5.base.ResponseBase;import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;@RestController
@Api("会员服务接口")
public class MemberServiceImpl extends BaseApiService implements IMemberService {@Value("${server.port}")private String serverPort; @RequestMapping("/getMember")  public UserEntity getMember(@RequestParam("name") String name) {UserEntity userEntity = new UserEntity();userEntity.setName(name);userEntity.setAge(10);return userEntity;}@RequestMapping("/getUserInfo") public ResponseBase getUserInfo() {try {Thread.sleep(1500);} catch (Exception e) {}return setResultSuccess("getUserInfo调用成功....");}@RequestMapping("/") public String Index() {return "我是member"+serverPort;} @ApiOperation(value = "获取会员信息接口") // 具体描述@ApiImplicitParam(name = "userName", value = "用户信息参数", required = true, dataType = "String") // 传入的参数 ,描述 , 必须传递true                                                                                                    // , 类型String@GetMapping("/getMemberInfo")public String getMemberInfo(String userName) {System.out.println(userName);return "userName" + userName;}}

启动:

package com.toov5.api.service.impl;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;import com.spring4all.swagger.EnableSwagger2Doc;@SpringBootApplication (scanBasePackages={"com.toov5.*"})
@EnableEurekaClient
@EnableFeignClients
@EnableSwagger2Doc //开启swagger文档)
public class AppMember {public static void main(String[] args) {SpringApplication.run(AppMember.class, args);
}
}

yml

server:port: 8005spring:application:name: app-toov5-member
ַ
eureka:client:service-url:defaultZone: http://localhost:8100/eurekaregister-with-eureka: truefetch-registry: trueswagger:base-package:  com.toov5.api.service.impl

  

网关gateway配置:

也需要引入相同的pom

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

然后启动类:

package com.toov5;import java.util.ArrayList;
import java.util.List;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 com.spring4all.swagger.EnableSwagger2Doc;import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy  //开启网关代理
@EnableSwagger2Doc  //开启swagger
public class AppGateway {public static void main(String[] args) {SpringApplication.run(AppGateway.class, args);
}// 添加文档来源
       @Component@Primaryclass DocumentationConfig implements SwaggerResourcesProvider {public List<SwaggerResource> get() {List resources = new ArrayList<Object>();//app-itmayiedu-order
               resources.add(swaggerResource("app-toov5-member", "/api-member/v2/api-docs", "2.0")); // 第一个参数可以随便写 第二个参考yml对应resources.add(swaggerResource("app-toov5-order", "/api-order/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;}}
}

yml配置:

###注册 中心
eureka:client:serviceUrl:defaultZone: http://localhost:8100/eureka/
server:  ##api网关端口号port: 81
###网关名称
spring:   ##网关服务名称application:name: service-zuul###网关名称  cloud:config:####读取后缀profile: dev####读取config-server注册地址discovery:service-id: confi### 配置网关反向代理
zuul:routes:api-member:  ##随便写的### 以 /api-member/访问转发到会员服务   通过别名找path: /api-member/**serviceId: app-toov5-member  ##别名  如果集群的话  默认整合了ribbon 实现轮训 负载均衡api-order:   ##随便写的### 以 /api-order/访问转发到订单服务path: /api-order/**serviceId: app-toov5-order   ##别名

  

与yml的对应!

启动 eureka  zuul member 然后访问

获取接口文档

转载于:https://www.cnblogs.com/toov5/p/9975389.html

Spring Cloud之Swagger集群搭建相关推荐

  1. 【SpringCloud】Spring cloud Alibaba Nacos 集群和持久化配置

    文章目录 1.概述 2. Nacos持久化配置解释 2.1 Nacos默认使用derby数据库 2.2 MySQL存储 2.3 构建Nacos集群 2.3.1 复制3分 2.3.2 修改端口 2.3. ...

  2. 疯狂Spring Cloud连载(5)Eureka集群搭建

    2019独角兽企业重金招聘Python工程师标准>>> 本文节选自<疯狂Spring Cloud微服务架构实战> 京东购买地址:https://item.jd.com/1 ...

  3. Spring Cloud(一)Eureka Server-单体及集群搭建

    一.Enreka介绍 Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务,主要用于定位运行在AWS域中的中间层服务,以达到负载均衡和中间层服务故障转移的目的.SpringCl ...

  4. spring cloud搭建_Spring Cloud Eureka 注册中心集群搭建,Greenwich 最新版!

    Spring Cloud 的注册中心可以由 Eureka.Consul.Zookeeper.ETCD 等来实现,这里推荐使用 Spring Cloud Eureka 来实现注册中心,它基于 Netfl ...

  5. spring cloud多模块项目框架搭建-Redis-Cluster集群搭建及系统集成

    第九章 Redis-Cluster集群搭建及系统集成 本系列博客旨在搭建一套能用于实际开发使用的spring cloud多模块微服务项目框架,并不是一个spring cloud的demo而已,提供系统 ...

  6. Solr集群搭建,zookeeper集群搭建,Solr分片管理,Solr集群下的DataImport,分词配置。...

    1   什么是SolrCloud SolrCloud(solr 云)是Solr提供的分布式搜索方案,当你需要大规模,容错,分布式索引和检索能力时使用 SolrCloud.当一个系统的索引数据量少的时候 ...

  7. spring boot 微服务集群 + 注册中心

    spring boot 微服务框架下载地址: https://start.spring.io/ 注册中心 Eureka Server提供服务注册服务,各个节点启动后,会在Eureka Server中进 ...

  8. Solr集群搭建,zookeeper集群搭建,Solr分片管理,Solr集群下的DataImport,分词配置。

    1   什么是SolrCloud SolrCloud(solr 云)是Solr提供的分布式搜索方案,当你需要大规模,容错,分布式索引和检索能力时使用 SolrCloud.当一个系统的索引数据量少的时候 ...

  9. Nacos配置管理-nacos集群搭建

    Nacos集群搭建 1.集群结构图 官方给出的Nacos集群图: 其中包含3个nacos节点,然后一个负载均衡器代理3个Nacos.这里负载均衡器可以使用nginx. 我们计划的集群结构: 三个nac ...

  10. SpringCloud(三)Eureka之集群搭建以及将服务注册到多个EurekaServer

    概述:为了保证EurekaServer的高可用,可以采用服务集群方式.当有一台挂掉时,其他节点仍可以继续提供服务.在EurekaServer的集群配置中,他们为分别配置对方为自己的注册中心,并将自己节 ...

最新文章

  1. 程序自我销毁(VB源代码)
  2. Java 一组温度从摄氏到华氏的转换
  3. html中after伪类原理,css :after伪类+content使用说明和方法
  4. 清华团队最新成果:可致特朗普能咏比特币,AI写古诗“更上一层楼”
  5. new php,PHP: 新特性 - Manual
  6. python数据库管理软件_MySQL管理工具MySQL Utilities — 介绍与安装(1)
  7. 当开发人员遇上非功能性需求
  8. mysql 5.6 二进制安装包_centos6上mysql5.6二进制包安装
  9. 2008考研数学辅导讲义理工类高等数学部分-蔡燧林
  10. 单片机实验报告太原理工大学_太原理工大学单片机实验报告
  11. 计算机财务模型管理实验内容,计算机财务管理实验报告详细分解.doc
  12. mysql5.0免安装版_MySql5.0免安装版配置
  13. 中国程序员鼓励师都干啥? 美媒:按摩谈心样样通
  14. 输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数。
  15. 吸入糖皮质激素行业调研报告 - 市场现状分析与发展前景预测
  16. 软件项目规模评估方法之软件源代码行法
  17. 【Coursera】深度神经网络的改进:超参数调整、正则化和优化(更新中2023/04/12)
  18. CSDN 2018博客之星活动报名开始了!
  19. 衡量子空间相似度和距离的主角 principal angles
  20. 关键词拍卖中最优保留价的研究

热门文章

  1. js 跨域访问 找了好长时间
  2. liunx apache 的安装
  3. js实现页面滚动,菜单固定
  4. 经典排序算法(三)--插入排序Insertion Sort
  5. procedure mysql_所有子节点、Procedure、MySQL
  6. IDEA中单元测试使用Scanner控制台无法输入
  7. word 产生很多temp 不显示_Word与PPT互转,怎样才能30秒内搞定?教程来了
  8. Tomcat:开启Tomcat服务CMD窗口乱码
  9. C#:获取Web.config中数据库连接地址
  10. Git:操作报错:fatal: ‘origin‘ does not appear to be a git repository解决方案