SpringCloud集成Swagger

原理

利用zuul获取其他服务的API信息进行集中管理

服务

  1. eureka 注册中心 http://localhost:7001/eureka
  2. zuul API网关 http://localhost/api/
  3. provider服务提供者htpp://localhost:8080/provider/

配置

eureka配置
zuul配置
provider为一个普通的Springcloud服务,提供api接口

zuul中添加swagger

架包

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

yml文件

可以参考zuul配置

server:
  port: 80

spring:
  application:
    name: zuul
eureka:
  client:
    register-with-eureka: true
    fetchRegistry: true
    service-url:
      defaultZone: http://localhost:7001/eureka
  instance:
    instance-id: zuul80
    prefer-ip-address: true
zuul:
  prefix: /api   # 访问网关路径的前缀(在映射路径的前面,一般用于区别开发的版本)
  routes:
    zuul-service01:       # 随意写的区分不同映射服务器的名称(只是区分不同的映射服务器)
      path: /myZullProvider/**    # 自定义映射服务器路径的名称(相当于key,外部访问这个地址会映射到下面的service-id这个value值。然后从eureka服务列表找到对应服务名称,进而负载均衡的请求一个服务器)
      #      url: http://127.0.0.1:80  # 这是写的固定映射url,可代替service-id。但是不能实现服务器的负载均衡和高可用,因为总是访问同一服务器
      service-id: provider     # eureka注册中心中要映射的服务名称,因为是双层map结构,所以可以实现负载均衡和高可用

启动项

import com.spring4all.swagger.EnableSwagger2Doc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

/**
 * @author kittlen
 * @version 1.0
 * @date 2021/4/26 0026 10:11
 * @email kittlen@qq.com
 */

@SpringBootApplication
@EnableZuulProxy
@EnableSwagger2Doc
public class ZuulApplication {
    public static void main(String[] args) {
        SpringApplication.run(ZuulApplication.class,args);
    }
}

swagger配置

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;

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

/**
 * @author kittlen
 * @version 1.0
 * @date 2021/4/28 0028 13:55
 * @email kittlen@qq.com
 */
@Configuration
@Primary
public class SwaggerConfig implements SwaggerResourcesProvider {
    @Override
    public List<SwaggerResource> get() {
        List resources = new ArrayList<>();
        /**
         * 服务提供者8080 ---> 这个就是个模块的名字,可以随便起
         * /api/myZullProvider/provider/v2/api-docs  /api为网关前缀,/myZullProvider为zuul网关中配置的路由映射前缀,/provider/v2/api-docs为具体服务提供者的api信息查询接口
         * 添加多个的话 在yml 网关中添加相应的路由并添加对应的swaggerResource信息到resources中
         */
        resources.add(swaggerResource("服务提供者8080", "/api/myZullProvider/provider/v2/api-docs", "1.0"));

        return resources;
    }

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

provider中添加swagger

架包

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

yml文件

server:
  servlet:
    context-path: /provider
  port: 8080

spring:
  application:
    name: provider

eureka:
  client:
    register-with-eureka: true #是否向注册中心注册自己 默认为true
    fetch-registry: true #是否去注册中心获取其他服务的地址 默认为true
    service-url:
#      defaultZone: http://localhost:7001/eureka,http://localhost:7002/eureka #eureka集群地址
      defaultZone: http://localhost:7001/eureka #eureka单机
  instance:
    instance-id: provider8080 #服务实例在eureka界面增加显示版本信息
    prefer-ip-address: true #是否以ip地址主车道eureka 默认为false

swagger:
  base-package: com.kittlen.cloud.controller #swagger扫描的api位置

启动项

import com.spring4all.swagger.EnableSwagger2Doc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

/**
 * @author kittlen
 * @version 1.0
 * @date 2021/2/22 10:08
 */
@SpringBootApplication
@EnableEurekaClient
@EnableSwagger2Doc
public class ProviderApplication {

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

添加api描述

import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

/**
 * @author kittlen
 * @version 1.0
 * @date 2021/2/22 10:15
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Test {

    //example默认为"",如果转换为数字,会有转换异常
    @ApiModelProperty(value = "主键",example = "0")
    private int testId;

    @ApiModelProperty("数据")
    private String data;
}
import com.kittlen.cloud.entities.Test;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * @author kittlen
 * @version 1.0
 * @date 2021/2/22 10:10
 */
@RestController
@RequestMapping("/test")
@Api(value = "test",description = "测试")
public class testController {

    @ApiOperation("获取所有测试数据")
    @GetMapping("/getTestAll")
    public List getTestAll() {
        List<String> list=new ArrayList<>();
        for(int i=0;i<10;i++){
            list.add(Integer.toString(i));
        }
        return list;
    }
    @PostMapping("/insertTestData")
    public String insertTestData(Test test) {
        return test.toString();
    }
}

在这里插入图片描述
在这里插入图片描述
其余服务参考相同的方式添加

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值