亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時間:2020年11月09日 09:51:40   作者:dreamstar  
這篇文章主要介紹了Spring Cloud Zuul集成Swagger實現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友可以參考下

Spring Cloud Zuul 集成Swagger

1.準備服務(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 ,,將當前應(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
@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文件中添加配置

#微服務(wù)基本信息
spring.application.name=swagger-service-a
server.port=10010
#注冊中心
eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/
#要生成文檔的package
swagger.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;

@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.啟動微服務(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
@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文件中添加配置

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

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

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • java中對List分段操作的實例

    java中對List分段操作的實例

    這篇文章主要介紹了java中對List分段操作的實例的相關(guān)資料,希望通過本文大家能夠掌握list的分段實現(xiàn)方法,需要的朋友可以參考下
    2017-09-09
  • SpringBoot整合Tomcat連接池的使用

    SpringBoot整合Tomcat連接池的使用

    這篇文章主要介紹了SpringBoot整合Tomcat連接池的使用,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧
    2021-04-04
  • 如何把Java程序窗口在屏幕中間顯示

    如何把Java程序窗口在屏幕中間顯示

    大家在日常Java開發(fā)中,可能會需要把程序窗口定位在屏幕中間,那該如何操作呢,下面來一起看看。
    2016-08-08
  • SpringBoot中使用Redis對接口進行限流的實現(xiàn)

    SpringBoot中使用Redis對接口進行限流的實現(xiàn)

    本文將結(jié)合實例代碼,介紹SpringBoot中使用Redis對接口進行限流的實現(xiàn),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • 如何通過Java實現(xiàn)修改視頻分辨率

    如何通過Java實現(xiàn)修改視頻分辨率

    Java除了可以修改圖片的分辨率,還可以實現(xiàn)修改視頻的分辨率,這篇文章就將帶大家學(xué)習如果編寫這一工具類,感興趣的同學(xué)可以了解一下
    2021-12-12
  • SpringBoot優(yōu)雅地實現(xiàn)全局異常處理的方法詳解

    SpringBoot優(yōu)雅地實現(xiàn)全局異常處理的方法詳解

    這篇文章主要為大家詳細介紹了SpringBoot如何優(yōu)雅地實現(xiàn)全局異常處理,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習一下
    2022-08-08
  • JavaBean四個作用域范圍的詳解

    JavaBean四個作用域范圍的詳解

    這篇文章主要介紹了JavaBean四個作用域范圍的詳解的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下
    2017-10-10
  • spring事物傳播propagation類別含義詳解

    spring事物傳播propagation類別含義詳解

    這篇文章主要介紹了spring事物傳播propagation類別含義詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友可以參考下
    2019-12-12
  • 巧妙的利用Mongodb做地理空間查詢

    巧妙的利用Mongodb做地理空間查詢

    本篇文章將會以Mongodb為數(shù)據(jù)庫,講述如何在數(shù)據(jù)庫層級進行定位查詢。具有很好的參考價值,下面跟著小編一起來看下吧
    2017-02-02
  • java實現(xiàn)文件下載的兩種方式

    java實現(xiàn)文件下載的兩種方式

    這篇文章主要為大家詳細介紹了java實現(xiàn)文件下載的兩種方式,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-11-11

最新評論