1,因为整个微服务会有好多服务,比如会员服务,支付服务,订单服务,每个服务都集成了swagger

我们在访问的时候,不可能每个服务输入一个url 去访问,看起来很麻烦,所以我们需要在一个页面上集成整个微服务项目中所有的 swagger

效果图:可以选择不同的应用,出来的是不同的swagger 接口文档

2,实现思路:

zuul 网关 + swagger

客户端访问一个应用,zuul 网关转发到相应的界面,看起来是在一个服务上的效果

3,eureka :注册中心

springcloud-config:注册中心:路由转发用配置中心做的,没有写在本地。可以参考springcloud-config 动态网关路由

springcloud-api-member-impl-service:在eureka 注册的服务是:app-aiyuesheng-member

springcloud-api-order-impl-service:在eureka 注册的服务是:app-aiyuesheng-order

springcloud-swagger2:swagger 服务

springcloud-zuul :zuul 网关服务

4,

第一步:

member 服务 和 order ,zuul 需要添加maven 依赖:

  1. <dependency>
  2. <groupId>com.spring4all</groupId>
  3. <artifactId>swagger-spring-boot-starter</artifactId>
  4. <version>1.7.0.RELEASE</version>
  5. </dependency>

第二步:

member ,order 配置文件添加:

  1. ##swagger扫包
  2. swagger:
  3. base-package: com.aiyuesheng.api.impl

第三步:

swagger 的接口类的写法还是一样:

  1. package com.aiyuesheng.api.impl;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.web.bind.annotation.GetMapping;
  5. import org.springframework.web.bind.annotation.PostMapping;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.RestController;
  8.  
  9. import com.aiyuesheng.entity.Order;
  10. import com.aiyuesheng.feign.OrderServiceFeign;
  11. import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
  12.  
  13. import io.swagger.annotations.Api;
  14. import io.swagger.annotations.ApiImplicitParam;
  15. import io.swagger.annotations.ApiOperation;
  16.  
  17. @RestController
  18. @Api("会员接口文档")
  19. public class MemberServiceImpl {
  20.  
  21. @Autowired
  22. private OrderServiceFeign orderServiceFeign;
  23.  
  24. @ApiOperation("方法描述用例")
  25. @PostMapping("/swaggerIndex")
  26. public String swaggerMember() {
  27. return "swaggerIndex";
  28. }
  29.  
  30. @ApiOperation("参数描述用例")
  31. @ApiImplicitParam(name = "name", value = "用户名", required = true, dataTypeClass = String.class)
  32. @GetMapping("/getAge")
  33. public String swaggerMemberParam(String userName) {
  34. return "chris";
  35. }
  36.  
  37. @RequestMapping("/")
  38. public String index() {
  39. return "app-aiyuesheng-member";
  40. }
  41.  
  42. // @HystrixCommand 默认开启服务隔离,是以线程池方式
  43. // @HystrixCommand 默认开启服务降级,fallbackMethod 方法名就是服务的降级名称
  44. // @HystrixCommand 默认开启服务熔断机制
  45. // @Hystrix 要禁止超时时间,默认1 秒,如果没有即使响应,会走业务逻辑,但是也会走服务降级方法,所以要禁止超时时间
  46. @HystrixCommand(fallbackMethod = "orderServiceFallback")
  47. @RequestMapping("/getOrder")
  48. public Order getOrder(String orderId) {
  49. System.out.println("orderToUserInfo:" + "当前线程池名称:" + Thread.currentThread().getName());
  50. return orderServiceFeign.getOrder(orderId);
  51. }
  52.  
  53. @RequestMapping("/getOrder2")
  54. public Order getOrder2(String orderId) {
  55. System.out.println("orderToUserInfo:" + "当前线程池名称:" + Thread.currentThread().getName());
  56. return orderServiceFeign.getOrder(orderId);
  57. }
  58.  
  59. @RequestMapping("/orderServiceFallback")
  60. public String orderServiceFallback() {
  61. return "服务器繁忙,请稍后重试";
  62. }
  63.  
  64. }

order 因为之前由feign 客户端调用,所以在父类里面也要加@Api()注解:

  1. package com.aiyuesheng.api.impl;
  2.  
  3. import org.springframework.web.bind.annotation.GetMapping;
  4. import org.springframework.web.bind.annotation.PostMapping;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RestController;
  7.  
  8. import com.aiyuesheng.api.IOrderService;
  9. import com.aiyuesheng.entity.Order;
  10.  
  11. import io.swagger.annotations.Api;
  12. import io.swagger.annotations.ApiImplicitParam;
  13. import io.swagger.annotations.ApiOperation;
  14.  
  15. @RestController
  16. @Api("订单接口文档")
  17. public class OrderServiceImpl implements IOrderService {
  18.  
  19. @ApiOperation("方法描述用例")
  20. @PostMapping("/swaggerIndex")
  21. public String swaggerOrder() {
  22. return "swaggerIndex";
  23. }
  24.  
  25. @ApiOperation("参数描述用例")
  26. @ApiImplicitParam(name = "name", value = "用户名", required = true, dataTypeClass = String.class)
  27. @GetMapping("/getAge")
  28. public String swaggerOrderrParam(String userName) {
  29. return "chris";
  30. }
  31.  
  32. @RequestMapping("/")
  33. public String index() {
  34. return "app-aiyuesheng-order";
  35. }
  36.  
  37. @RequestMapping("/getOrder")
  38. public Order getOrder(String orderId) {
  39. Order order = new Order();
  40. order.setOrderId(orderId);
  41. order.setOrderName("订单名称");
  42. return order;
  43. }
  44.  
  45. }

父类:

  1. @Api("订单接口文档")
  2. public interface IOrderService {
  3.  
  4. @RequestMapping("/getOrder")
  5. public Order getOrder(String orderId);
  6. }

第四步:

启动类加上注解@EnableSwagger2Doc

  1. @SpringBootApplication
  2. @EnableEurekaClient
  3. @EnableFeignClients
  4. @EnableSwagger2Doc //生成swagger文档
  5. public class OrderApp {
  6. public static void main(String[] args) {
  7. SpringApplication.run(OrderApp.class, args);
  8. }
  9. }

第五步:配置zuul:

  1. package com.aiyuesheng;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import org.springframework.boot.SpringApplication;
  7. import org.springframework.boot.autoconfigure.SpringBootApplication;
  8. import org.springframework.boot.context.properties.ConfigurationProperties;
  9. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  10. import org.springframework.cloud.context.config.annotation.RefreshScope;
  11. import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
  12. import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
  13. import org.springframework.context.annotation.Bean;
  14. import org.springframework.context.annotation.Primary;
  15. import org.springframework.stereotype.Component;
  16.  
  17. import com.spring4all.swagger.EnableSwagger2Doc;
  18.  
  19. import springfox.documentation.swagger.web.SwaggerResource;
  20. import springfox.documentation.swagger.web.SwaggerResourcesProvider;
  21.  
  22. @EnableZuulProxy
  23. @SpringBootApplication
  24. @EnableDiscoveryClient
  25. @EnableSwagger2Doc
  26. public class ZuulApplication {
  27. public static void main(String[] args) {
  28. SpringApplication.run(ZuulApplication.class, args);
  29. }
  30. // @Bean
  31. // public TokenFilter accessFilter() {
  32. // return new TokenFilter();
  33. // }
  34.  
  35. @RefreshScope
  36. @ConfigurationProperties("zuul")
  37. public ZuulProperties zuulProperties() {
  38. return new ZuulProperties();
  39. }
  40. }
  41.  
  42. @Component
  43. @Primary
  44. class DocumentationConfig implements SwaggerResourcesProvider {
  45. @Override
  46. public List<SwaggerResource> get() {
  47. List resources = new ArrayList<>();
  48. resources.add(swaggerResource("app-aiyuesheng-member", "/api-member/v2/api-docs", "2.0"));
  49. resources.add(swaggerResource("app-aiyuesheng-order", "/api-order/v2/api-docs", "2.0"));
  50. return resources;
  51. }
  52.  
  53. private SwaggerResource swaggerResource(String name, String location, String version) {
  54. SwaggerResource swaggerResource = new SwaggerResource();
  55. swaggerResource.setName(name);
  56. swaggerResource.setLocation(location);
  57. swaggerResource.setSwaggerVersion(version);
  58. return swaggerResource;
  59. }
  60. }

zuul 的配置文件,回顾下,

  1. spring:
  2. application:
  3. ####注册中心应用名称
  4. name: service-zuul
  5. cloud:
  6. config:
  7. ####读取后缀
  8. profile: dev
  9. ####读取config-server注册地址
  10. discovery:
  11. service-id: config-server
  12. enabled: true
  13. #####eureka服务注册地址
  14. eureka:
  15. client:
  16. service-url:
  17. defaultZone: http://localhost:8100/eureka
  18. server:
  19. port: 80
  20.  
  21. #配置手动实时刷新
  22. #managementendpoints.web.exposure.include=*
  23. management:
  24. endpoints:
  25. web:
  26. exposure:
  27. include: "*"

配置中心上的路由转发,回顾下:

  1. ### 配置网关反向代理
  2. zuul:
  3. routes:
  4. api-member:
  5. ### 以 /api-member/访问转发到会员服务
  6. path: /api-member/**
  7. serviceId: app-aiyuesheng-member
  8. api-order:
  9. ### 以 /api-order/访问转发到订单服务
  10. path: /api-order/**
  11. serviceId: app-aiyuesheng-order

配置成功:访问http://127.0.0.1/swagger-ui.html#/

因为网关是80,然后再转发到对应的服务地址的

swagger2 接口文档,整个微服务接口文档的更多相关文章

  1. Spring Cloud微服务接口这么多怎么调试

    导读 我们知道在微服务架构下,软件系统会被拆分成很多个独立运行的服务,而这些服务间需要交互通信,就需要定义各种各样的服务接口.具体来说,在基于Spring Cloud的微服务模式中,各个微服务会基于S ...

  2. 基于华为云CSE微服务接口兼容常见问题

    微服务接口兼容常见问题 在进行微服务持续迭代开发的过程中,由于新特性在不停的加入,一些过时的特性在不停的修改,接口兼容问题面临巨大的挑战,特别是在运行环境多版本共存(灰度发布)的情况下.本章节主要描述 ...

  3. Spring Cloud微服务系列文,服务调用框架Feign

    之前博文的案例中,我们是通过RestTemplate来调用服务,而Feign框架则在此基础上做了一层封装,比如,可以通过注解等方式来绑定参数,或者以声明的方式来指定请求返回类型是JSON.    这种 ...

  4. 微服务接口设计(RESTful规范)

    微服务的接口设计(RESTful规范) 基本知识 URI:在RESTful架构中,每个URI代表一种资源 URI规范: 不用大写 用中杠-,不用下划线_ 路径中不能有动词,只能有名词 名词表示资源集合 ...

  5. feig中调用其他微服务接口无反应

    1.调用微服务时get请求接口中不能使用@RequestBody注解,不然接口调用无反应.post接口中可以使用@RequestBody注解

  6. Spring Cloud微服务系列文,Hystrix与Eureka的整合

    和Ribbon等组件一样,在项目中,Hystrix一般不会单独出现,而是会和Eureka等组件配套出现.在Hystrix和Eureka整合后的框架里,一般会用到Hystrix的断路器以及合并请求等特性 ...

  7. SpringCloud微服务之跨服务调用后端接口

    SpringCloud微服务系列博客: SpringCloud微服务之快速搭建EurekaServer:https://blog.csdn.net/egg1996911/article/details ...

  8. .netcore 3.1高性能微服务架构:封装调用外部服务的接口方法--HttpClient客户端思路分析

    众所周知,微服务架构是由一众微服务组成,项目中调用其他微服务接口更是常见的操作.为了便于调用外部接口,我们的常用思路一般都是封装一个外部接口的客户端,使用时候直接调用相应的方法.webservice或 ...

  9. 微服务&#183;API文档

    阅文时长 | 3.92分钟 字数统计 | 2754.05字符 主要内容 | 1.什么是API文档 2.API文档的使用 3.声明与参考资料 『微服务·API文档』 编写人 | SCscHero 编写时 ...

随机推荐

  1. 用ABAP 生成二维码 QR Code

    除了使用我的这篇blogStep by step to create QRCode in ABAP Webdynpro提到的使用ABAP webdynpro生成二维码之外,也可以通过使用二维码在线生成 ...

  2. spring——AOP原理及源码(四)

    前情回顾: 上文我们一路分析了从容器创建开始直到我们的AOP注解导入的核心组件AnnotationAwareAspectJAutoProxyCreator执行postProcessBeforeInst ...

  3. 如何将zTree选中节点传递给后台

    获取zTree选中节点 <body> <script type="text/javascript"> var setting = { view: { dbl ...

  4. JDk下载和环境变量Path的配置

    JDK下载与安装 下载地址 打开该网址会显示如下图,点击DOWMLOAD即可: 出现该页面时,点击接受: 选择对应的安装包下载即可(本人用的是Windows64位): 注:如果您无法确定您的windo ...

  5. 029.核心组件-Controller Manager

    一 Controller Manager原理 1.1 Controller Manager概述 一般来说,智能系统和自动系统通常会通过一个"控制系统"来不断修正系统的工作状态.在K ...

  6. 如何安装vue-devtool调试工具

    1.从git上下载工具压缩包,github下载地址:https://github.com/vuejs/vue-devtools: 2.打开cmd,切换到下载的文件目录下:npm install---- ...

  7. Struts UI标签的使用

    先来看一下日期控件 html5标签中其实已经有日期的类型,用<input type="date">便可调用. struts里面也自带了日期控件,其使用步骤为: 1. 导 ...

  8. (转)USB的VID和PID,以及分类(Class,SubClass,Protocol)

    USB的VID和PID,以及分类(Class,SubClass,Protocol) 原文地址:http://blog.csdn.net/gaojinshan/article/details/78783 ...

  9. 英伟达GPU虚拟化---申请英伟达测试License

    此文基于全新的License 2.0系统,针对vGPU License的试用申请以及软件下载和License管理进行了详细的说明,方便今后我们申请测试License,快速验证GPU的功能. 试用步骤: ...

  10. 普通人学习rust——从零到放弃 变量、不可变量、常量

    普通人学习rust--从零到放弃 变量.不可变量.常量 环境 本文章内容基于如下环境,如若出入请参考当前环境. rustc 1.42.0 (b8cedc004 2020-03-09) cargo 1. ...