国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁/技術(shù)文章
文章詳情頁

Spring Cloud Zuul集成Swagger實現(xiàn)過程解析

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

Spring Cloud Zuul 集成Swagger

1.準(zhǔn)備服務(wù)注冊中心eureka-server

2.創(chuàng)建微服務(wù)swagger-service-a

step1. 創(chuàng)建微服務(wù)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 ,,將當(dāng)前應(yīng)用 添加到 服務(wù)治理體系中,開啟微服務(wù)注冊與發(fā)現(xiàn)。

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文件中添加配置

#微服務(wù)基本信息spring.application.name=swagger-service-aserver.port=10010#注冊中心eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/#要生成文檔的packageswagger.base-package=com.example

step5.添加一個微服務(wù)提供的功能

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.啟動微服務(wù),訪問 http://localhost:10010/swagger-ui.html

3.創(chuàng)建微服務(wù)swagger-service-b (參考swagger-service-a ), 啟動微服務(wù)swagger-service-b并訪問http://localhost:10020/swagger-ui.html

4.構(gòu)建API網(wǎng)關(guān)并整合Swagger

step1.創(chuàng)建API網(wǎng)關(guān)微服務(wù)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文件中添加配置

#微服務(wù)基本信息spring.application.name=swagger-api-gatewayserver.port=10030#注冊中心eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/#要生成文檔的packageswagger.base-package=com.example

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

Spring Cloud Zuul集成Swagger實現(xiàn)過程解析

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 欧美在线播放成人免费 | 国产一区二区三区免费在线视频 | 老湿菠萝蜜在线看 | 精品国产成人高清在线 | 日韩欧美国产视频 | 成人7777| 成人网18免费 | 日韩欧美高清在线 | 亚洲另类视频 | 精品视频亚洲 | 女人张开腿给男人桶爽免费 | 日韩大片高清播放器大全 | 欧美日本高清视频在线观看 | 18在线| 99精品国产高清一区二区三区香蕉 | 曰本黄页 | 成人三级视频在线观看 | 欧美日本道免费一区二区三区 | 一级做a爰 | 黄色美女网站免费看 | 香蕉国产人午夜视频在线 | 手机在线毛片免费播放 | 欧美男女网站 | 日韩aⅴ在线观看 | 91精品免费国产高清在线 | 超级碰碰碰视频视频在线视频 | 日韩在线第一区 | 国产99久9在线视频 国产99久久 | 91手机看片国产福利精品 | 欧美一级毛片免费高清的 | 九九99久久 | 亚洲毛片在线免费观看 | 在线观看久草 | 欧美成人性做爰网站免费 | 中文字幕一区二区在线视频 | 91精品国产一区二区三区左线 | 成年视频在线 | 日本一区二区三区高清在线观看 | 亚洲综合精品一区二区三区中文 | 色偷偷亚洲女人天堂观看欧 | 色精品视频 |