《springcloud学习》二十三 swgger+zuul实现分布式微服务接口文档api

要求:

     有一定的swagger基础,java基础,springboot基础,springcloud集群。

1.背景

           单节点swgger2地址

          https://blog.csdn.net/qq_16855077/article/details/103123407

          随着时代的发现,我们的项目也从以前的,单节点项目(所有功能都向一个项目中堆,维护性差),最近几年,微服务使用的人群越越来越广,一个简单的电影系统,我们也可以按模块进行切换,例如,分为订单模块,电影模块,支付模块,会员模块等等。

而文档维护起来的成本也越来越高,有时候,我们一个系统,就可以拆分成上100个服务,这是,我们的文档如何维护了?前面的博客都是单节点的swagger,假设,我们有100个服务,我们搭建100个swagger,那就得有100个网站,这我们开发人员每天都得说,我太难勒。针对这种情况,我们只能通过swagger集群的方式来解决。

2.代码

           2.1 会员服务

                          2.1.1 pom.xml

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

这是springboot集成的一个start类。不然,我们这里就需要引入两个依赖。

                   2.1.2  application.yml

####服务器端口号
server:
  port: 8001
###服务别名---服务器注册到注册中心的名称
spring:
  application:
    name: fqyd-member
eureka:
  client:
    ###需要将我的服务注册到eureka上
    registerWithEureka: true
    ###检索服务
    fetchRegistry: true
    ###当前服务注册到eureka服务地址
    serviceUrl:
      defaultZone: http://localhost:8100/eureka/
####扫描swagger注解
swagger:
  base-package: com.fqyd

           1. fqyd-member----------------服务名,后续会用到。

          2.项目开发过程中使用到了euraka,这里就不过多阐述,不了解的可以查看(一般项目调用都是通过ip+端口等调用,这里使用euraka的目的是,通过服务名和ip进行绑定,映射到eurake server上,我们通过服务名,实际上就是访问的ip)

          https://blog.csdn.net/qq_16855077/article/details/90752257

         3.swagger的一些配置,swagger+springboot启动器,会帮我们默认设置,因本博客是测试,所以这里也只设置勒包扫描区间。

          

                  2.1.3 会员控制类 

package com.fqyd.api.service.impl;

import com.fqyd.api.entity.User;
import com.fqyd.api.service.IMemberService;
import com.fqyd.util.BaseApiService;
import com.fqyd.util.BaseDataResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Description:
 * Author: wude
 * Date:  2019/7/25 15:36
 * Modified By:
 */
@RestController
@Api("会员服务接口")
public class MemberServiceImpl extends BaseApiService implements IMemberService {
    @Value("${server.port}")
    private Integer serverPort;

    @ApiOperation("swagger")
    @ApiImplicitParam(name="name",value="用户名",dataType="String", paramType = "query")
    @PostMapping("/hello")
    public String hello(String name){
        System.out.println("hello swagger"+name);
        return "hello swagger "+name;
    };

    @RequestMapping("getMember")
    @Override
    public User getMember(@RequestParam("name") String name) {
        User user = new User();
        user.setName("通过端口:"+serverPort+","+name);
        user.setAge(18);
        return user;
    }

    @RequestMapping("getUserInfo")
    @Override
    public BaseDataResult getUserInfo() {
        try {
            //延迟1.5s
            Thread.sleep(1500);
        } catch (Exception e) {

        }
        return setResultSuccess("订单服务调用会员接口成功...");
    }
}

                2.1.4 启动类

package com.fqyd;

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.openfeign.EnableFeignClients;

/**
 * Description:
 * Author: wude
 * Date:  2019/7/25 16:49
 * Modified By:
 */
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableSwagger2Doc
public class MemberServiceImplApplication {
    public static void main(String[] args) {
        SpringApplication.run(MemberServiceImplApplication.class,args);
    }
}

@EnableSwagger2Doc  开启swagger文档

                2.2  订单服务

                        实现的方式跟会员服务类似,增加订单服务,只是为了延迟分布式swagger效果。

                 2.3    网关

                         2.3.1  pom.xml

   <!--网关-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
        </dependency>
        <!--eureka整合客户端-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

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

                                          2.3.2 application.yml

####服务器端口号
server:
  port: 82
###服务别名---服务器注册到注册中心的名称
spring:
  application:
    name: zuul-service
eureka:
  client:
    ###需要将我的服务注册到eureka上
    registerWithEureka: true
    ###检索服务
    fetchRegistry: true
    ###当前服务注册到eureka服务地址
    serviceUrl:
      defaultZone: http://localhost:8100/eureka/
##设置feign超时时间
ribbon:
  ##指的是创建连接所用的时间,适用于网络状况正常的情况下,两端所用的时间,默认是1s
  ReadTimeout: 3000
  ##指的是创建连接后从服务器读取可用资源所用的时间
  ConnectTimeout: 3000
##开启Hystrix断路器
feign:
  hystrix:
    enabled: true

zuul:
  routes:
    member:
      ##类似于转发
      path: /member/**
      serviceId: fqyd-member
    order:
      path: /order/**
      serviceId: fqyd-order

网关详情:  https://blog.csdn.net/qq_16855077/article/details/102960033

            2.3.3 启动类

package com.fqyd;
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.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

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

/**
 * Description:
 * Author: wude
 * Date:  2019/7/25 16:49
 * Modified By:
 */
@SpringBootApplication
@EnableZuulProxy
@EnableEurekaClient
@EnableSwagger2
public class ZuulServiceImplApplication {
    public static void main(String[] args) {
        SpringApplication.run(ZuulServiceImplApplication.class,args);
    }

    @Component
    @Primary
    class DocumentationConfig implements SwaggerResourcesProvider {

        @Override
        public List<SwaggerResource> get() {
            List resources = new ArrayList();
            resources.add(swaggerResource("会员服务Api","/member/v2/api-docs","2.0"));
            resources.add(swaggerResource("订单服务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;
        }
    }
}

member和order不是随便定义的,而是根据yml里面zuul的路由决定的。

 

注意:zuul中需要使用@EnableSwagger2,而不能使用@EnableSwagger2Doc,不然会报一个错误。

https://blog.csdn.net/qq_16855077/article/details/103146969

3.测试

 

这两个服务显示的界面,都是通过一个网址打开的,就这样大功告成勒!

如果你热衷技术,喜欢交流,欢迎加入我们! 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值