最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

Spring Cloud Zuul 集成Swagger

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

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

step1. 創(chuàng)建微服務(wù)swagger-service-a(Spring Boot項(xiàng)目),添加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ù)注冊(cè)與發(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 實(shí)例文檔")
        .description("swagger-service-a 實(shí)例文檔 1.0")
        .termsOfServiceUrl("https:github")
        .version("1.0")
        .build();
  }

}

step4.application.properties文件中添加配置

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

step5.添加一個(gè)微服務(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.啟動(dòng)微服務(wù),訪問 http://localhost:10010/swagger-ui.html

3.創(chuàng)建微服務(wù)swagger-service-b (參考swagger-service-a ), 啟動(dòng)微服務(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 實(shí)例文檔")
        .description("swagger-service 實(shí)例文檔 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
#注冊(cè)中心
eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/
#要生成文檔的package
swagger.base-package=com.example

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

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

相關(guān)文章

  • Java 如何實(shí)現(xiàn)時(shí)間控制

    Java 如何實(shí)現(xiàn)時(shí)間控制

    這篇文章主要向大家介紹得是Java 如何實(shí)現(xiàn)時(shí)間控制,文章珠岙舉例說明該內(nèi)容,感興趣得小伙伴可以跟小編一起學(xué)習(xí)下面文章內(nèi)容
    2021-10-10
  • 詳解Java中數(shù)組判斷元素存在幾種方式比較

    詳解Java中數(shù)組判斷元素存在幾種方式比較

    這篇文章主要介紹了Java中數(shù)組判斷元素存在幾種方式比較,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2018-07-07
  • 最新評(píng)論

    博爱县| 定边县| 樟树市| 镇巴县| 思茅市| 汾阳市| 视频| 甘谷县| 衡阳县| 丹寨县| 卢氏县| 金阳县| 会泽县| 化州市| 疏勒县| 海原县| 吉安市| 屏东市| 永春县| 华亭县| 甘南县| 普兰县| 巩留县| 青铜峡市| 瓮安县| 黑水县| 修水县| 宣化县| 岳阳县| 阿拉善左旗| 五指山市| 安福县| 沾益县| 绵竹市| 涿州市| 海南省| 华宁县| 海晏县| 团风县| 石阡县| 余姚市|