Spring Cloud Zuul集成Swagger实现过程解析
作者:dreamstar 发布时间:2021-05-26 12:36:09
Spring Cloud Zuul 集成Swagger
1.准备服务注册中心eureka-server
2.创建微服务swagger-service-a
step1. 创建微服务swagger-service-a(Spring Boot项目),添加eureka-client起步依赖,web起步依赖 和swagger依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
step2.在配置类添加注解@EnableDiscoveryClient ,,将当前应用 添加到 服务治理体系中,开启微服务注册与发现。
step3.配置swagger
package com.example.swaggerservicea;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any()).build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("swagger-service-a 实例文档")
.description("swagger-service-a 实例文档 1.0")
.termsOfServiceUrl("https:github")
.version("1.0")
.build();
}
}
step4.application.properties文件中添加配置
#微服务基本信息
spring.application.name=swagger-service-a
server.port=10010
#注册中心
eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/
#要生成文档的package
swagger.base-package=com.example
step5.添加一个微服务提供的功能
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class AaaController {
@Autowired
DiscoveryClient discoveryClient;
@GetMapping("/service-a")
public String dc() {
String services = "service-a Services: " + discoveryClient.getServices();
System.out.println(services);
return services;
}
}
step6.启动微服务,访问 http://localhost:10010/swagger-ui.html
3.创建微服务swagger-service-b (参考swagger-service-a ), 启动微服务swagger-service-b并访问http://localhost:10020/swagger-ui.html
4.构建API网关并整合Swagger
step1.创建API网关微服务swagger-api-gateway,添加eureka-client起步依赖,zuul起步依赖 和 swagger依赖 :spring-cloud-starter-netflix-eureka-client,spring-cloud-starter-netflix-zuul
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
step2.在配置类添加注解@SpringCloudApplication ,@EnableZuulProxy
step3.配置swagger
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any()).build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("swagger-service 实例文档")
.description("swagger-service 实例文档 1.0")
.termsOfServiceUrl("https:github")
.version("1.0")
.build();
}
}
step4.配置swagger
package com.example.swaggerapigateway;
import java.util.ArrayList;
import java.util.List;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;
@Component
@Primary
class DocumentationConfig implements SwaggerResourcesProvider {
@Override
public List<SwaggerResource> get() {
List resources = new ArrayList<>();
resources.add(swaggerResource("service-a", "/swagger-service-a/v2/api-docs", "2.0"));
resources.add(swaggerResource("service-b", "/swagger-service-b/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;
}
}
step5.application.properties文件中添加配置
#微服务基本信息
spring.application.name=swagger-api-gateway
server.port=10030
#注册中心
eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/
#要生成文档的package
swagger.base-package=com.example
step6.启动微服务,访问http://localhost:10030/swagger-ui.html
来源:https://www.cnblogs.com/dreamstar99/p/13857267.html
猜你喜欢
- 什么是自旋锁说道自旋锁就要从多线程下的锁机制说起,由于在多处理器系统环境中有些资源因为其有限性,有时需要互斥访问(mutual exclus
- JavaWeb登录界面登录失败在同一页面进行提示方法使用JSP 通过提交表单方式 判断账号密码是否正确 不正确则调用req.setAttri
- 第一节 接口慨述接口(interface)用来定义一种程序的协定。实现接口的类或者结构要与接口的定义严格一致。有了这个协定,就可以抛开编程语
- 项目地址:https://github.com/JeasonWong/SlackLoadingView老规矩,先上效果。图好大。。说下第一眼
- 1、比较器①比较器的引入a.首先,当我们单一地比较某一种数据类型的数组时,可以直接用Arrays.sort()进行实现b.而当我们同时含有多
- 之前我们学习了如何使用Jpa访问关系型数据库。通过Jpa大大简化了我们对数据库的开发工作。但是,之前的例子中我们只提到了最简单的CRUD(增
- 在java中可有两种方式实现多线程,一种是继承Thread类,一种是实现Runnable接口;Thread类是在java.lang包中定义的
- 之前介绍了SpringBoot集成Jpa的简单使用,接下来介绍一下使用Jpa连接数据库对数据进行排序、分页、条件查询和过滤操作。首先创建Sp
- 一、简介Spring Cache是一个框架,实现了基于注解的缓存功能,只需要简单地加一个注解,就能实现缓存功能。Spring Cache提供
- springboot 启动找不到主类利用eclipse的maven插件,清理了了一下springboot的项目,结果再启动就找报找不到主类的
- 如下XML文件:(算是一个属性值比较多的xml文件。。。读取该Xml算是我在公司实际的一个任务)<?xml version="
- Q1: Object类型包含哪些方法?A1: Object类型共包含6个方法,Equals, GetHashCode, ToString,
- 1.介绍有时候我们在Linux中运行Java程序时,需要调用一些Shell命令和脚本。而Runtime.getRuntime().exec(
- 前言 dubbo是一款非常优秀的服务治理型RPC框架,dubbo的优秀在于,庞大的架构体
- 最近有一个java实验,要求用java使用数据库,于是本人新手小白,在idea上卡了好半天希望看到这个博客的人能解决问题,跳过一些坑首先,我
- 项目完整代码链接:代码链接跨服务上传文件示意图一、创建项目springboot:2.2.6JDK:1.8由于资源有限,就用不同端口表示不同服
- 故障:收到服务器报警,内存使用率超过80%1.查看使用dstat和top查看内存使用最高的应用使用dstat查到内存占用最高的是java应用
- 对不起大家,昨天文章里的告别说早了,这个系列还不能就这么结束。我们前面的文章中讲解过RabbitMQ的用法,所谓MQ就是一种发布订阅模式的消
- Spring-Data-Redis项目(简称SDR)对Redis的Key-Value数据存储操作提供了更高层次的抽象,类似于Spring F
- 什么是sam 转换Single Abstract Method 实际上这是java8中提出的概念,你就把他理解为是 一个方法的接口 的就可以