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


猜你喜欢
- springboot对压缩请求的处理最近对接银联需求,为了节省带宽,需要对报文进行压缩处理。但是使用springboot自带的压缩设置不起作
- springBoot集成Elasticsearch 报错 Health check failed今天集成Elasticsearch 时启动报
- 代理模式:为其他对象提供一种代理以控制某个对象的访问。用在:在某些情况下,一个客户不想或者不能直接访问另一个对象,而代理对象可以在客户端和目
- monaco editor创建//创建和设置值if (!this.monacoEditor) { this.monacoEdit
- 一、基本RPC框架简介在分布式计算中,远程过程调用(Remote Procedure Call,缩写 RPC)允许运行于一台计算机的程序调用
- 我们用NuGet还原.NET Core项目会报以下错误:error NETSDK1064: 未找到版本为 1.8.2 的包 BouncyCa
- 在关注者与公众号产生消息交互后,公众号可获得关注者的OpenID(加密后的微信号,每个用户对每个公众号的OpenID是唯一的。对于不同公众号
- IoC的概念介绍控制反转(IOC)模式(又称DI:Dependency Injection)就是Inversion of Control,控
- 前言在开发中常要处理横竖屏切换,怎么处理先看生命周期申明Activity 横竖屏切换时需要回调两个函数 ,所以在此将这个两个函数暂时看成是A
- 根据狂神的视频做的,然后自己优化了一些bug,比如新生成食物的时候不会生成在蛇的身体上,再新增长身体的时候不会在左上角出现一个绿色的方块以及
- 本文实例讲述了java数据结构与算法之快速排序。分享给大家供大家参考,具体如下:交换类排序的另一个方法,即快速排序。快速排序:改变了冒泡排序
- DownloadManager三大组件介绍DownloadManager类似于下载队列,管理所有当前正在下载或者等待下载的项目;他可以维持
- 在Android开发中,录入信息是最基本的操作,使用非常广泛。但是Android对输入法弹出/收起的支持,并不是很好。对弹出,提供了forc
- Java中Collections.sort()的使用在日常开发中,很多时候都需要对一些数据进行排序的操作。然而那些数据一般都是放在一个集合中
- 目录一、什么是Spring二、什么是IOC三、快速搭建框架环境四、spring之依赖注入五、详解Spring框架的IOC之注解方式七、Spr
- 介绍装饰者模式(Decorator Pattern):动态地给一个对象增加一些额外的职责,增加对象功能来说,装饰模式比生成子类实现更为灵活。
- 1、MediaCodec调用流程首先,我们先看下MediaCodec::CreateByType函数里面做了什么:sp<MediaCo
- 详解Http请求中Content-Type讲解以及在Spring MVC中的应用引言: 在Http请求中,我们每天都在使用Content-t
- 前言:这个语句的作用是,确保该语句执行之后,关闭每一个资源,也就是说它确保了每个资源都在生命周期结束之后被关闭,因此,比如读写文
- 一、ReentrantLockpackage com.ietree.basicskill.mutilthread.lock;import j