亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

Spring Cloud Zuul集成Swagger實現過程解析

瀏覽:15日期:2023-08-03 14:24:10

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@EnableSwagger2public 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-aserver.port=10010#注冊中心eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/#要生成文檔的packageswagger.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;@RestControllerpublic 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@EnableSwagger2public 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@Primaryclass 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-gatewayserver.port=10030#注冊中心eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/#要生成文檔的packageswagger.base-package=com.example

step6.啟動微服務,訪問http://localhost:10030/swagger-ui.html

Spring Cloud Zuul集成Swagger實現過程解析

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
主站蜘蛛池模板: 一区二区三区四区国产 | 黄色小视频免费在线观看 | 视频一区二区免费 | 亚洲+国产+图片 | 99热这里都是国产精品 | 啪啪网站在线观看 | 一a级毛片| 青青草国产免费一区二区 | 国产在线不卡视频 | 欧美毛片a级毛片免费观 | 国产污视频在线播放 | 国产欧美精品一区二区三区-老狼 | 国产福利在线视频 | 国产免费爽爽视频免费可以看 | 金发美女与黑人巨大交 | 国产乱辈通伦影片在线播放 | 国产美女无遮挡免费视频网站 | 特级黄色视频毛片 | 亚洲国产精品久久久久久网站 | 国产精品自产拍在线观看 | 青青国产成人久久激情91麻豆 | 国产黄a三级三级三级 | 视频在线国产 | 国语一级毛片私人影院 | 欧美日韩一区二区三区高清不卡 | 国产a级毛片 | 国语对白清晰好大好白 | 青青青青手机在线视频观看国产 | 91久久国产露脸精品免费 | 国产最新精品 | 最新精品在线视频 | 免费在线黄色片 | 快射视频欧美 | 妞干网这里只有精品 | 欧美国产一区二区 | 黑人欧美一级毛片 | 日本a在线观看 | 一区二区三区免费视频 www | 全免费午夜真人毛片视频 | 国产xxxx色视频在线观看14 | 国产精品污 |