欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring?Cloud?Gateway?整合?knife4j?聚合接口文檔功能

 更新時間:2022年02月14日 09:59:17   作者:張維鵬  
這篇文章主要介紹了Spring?Cloud?Gateway?整合?knife4j?聚合接口文檔的相關知識,我們可以基于?Spring?Cloud?Gateway?網(wǎng)關?+?nacos?+?knife4j?對所有微服務項目的接口文檔進行聚合,從而實現(xiàn)我們想要的文檔管理功能,需要的朋友可以參考下

當系統(tǒng)中微服務數(shù)量越來越多時,如果任由這些服務散落在各處,那么最終管理每個項目的接口文檔將是一件十分麻煩的事情,單是記住所有微服務的接口文檔訪問地址就是一件苦差事了。當如果能夠將所有微服務項目的接口文檔都統(tǒng)一匯總在同一個可視化頁面,那么將大大減少我們的接口文檔管理維護工作,為此,我們可以基于 Spring Cloud Gateway 網(wǎng)關 + nacos + knife4j 對所有微服務項目的接口文檔進行聚合,從而實現(xiàn)我們想要的文檔管理功能

注:本案例需要 springboot 提前整合 nacos 作為注冊中心,springcloud 整合 nacos 注冊中心部分內容歡迎閱讀這篇文章:Nacos注冊中心的部署與用法詳細介紹

1、Spring Cloud Gateway 網(wǎng)關整合 Knife4j:

(1)開啟gateway自動路由功能:

隨著我們的系統(tǒng)架構不斷地發(fā)展,系統(tǒng)中微服務的數(shù)量肯定會越來越多,我們不可能每添加一個服務,就在網(wǎng)關配置一個新的路由規(guī)則,這樣的維護成本很大;特別在很多種情況,我們在請求路徑中會攜帶一個路由標識方便進行轉發(fā),而這個路由標識一般都是服務在注冊中心中的服務名,因此這是我們就可以開啟 spring cloud gateway 的自動路由功能,網(wǎng)關自動根據(jù)注冊中心的服務名為每個服務創(chuàng)建一個router,將以服務名開頭的請求路徑轉發(fā)到對應的服務,配置如下:

# enabled:默認為false,設置為true表明spring cloud gateway開啟服務發(fā)現(xiàn)和路由的功能,網(wǎng)關自動根據(jù)注冊中心的服務名為每個服務創(chuàng)建一個router,將以服務名開頭的請求路徑轉發(fā)到對應的服務
spring.cloud.gateway.discovery.locator.enabled = true
# lowerCaseServiceId:啟動 locator.enabled=true 自動路由時,路由的路徑默認會使用大寫ID,若想要使用小寫ID,可將lowerCaseServiceId設置為true
spring.cloud.gateway.discovery.locator.lower-case-service-id = true

        這里需要注意的是,如果我們的配置了 server.servlet.context-path 屬性,這會導致自動路由失敗的問題,因此我們需要做如下兩個修改:

# 重寫過濾鏈,解決項目設置了 server.servlet.context-path 導致 locator.enabled=true 默認路由策略404的問題
spring.cloud.gateway.discovery.locator.filters[0] = PreserveHostHeader
@Configuration
public class GatewayConfig
{
    @Value ("${server.servlet.context-path}")
    private String prefix;
 
    /**
     * 過濾 server.servlet.context-path 屬性配置的項目路徑,防止對后續(xù)路由策略產(chǎn)生影響,因為 gateway 網(wǎng)關不支持 servlet
     */
    @Bean
    @Order (-1)
    public WebFilter apiPrefixFilter()
    {
        return (exchange, chain) ->
        {
            ServerHttpRequest request = exchange.getRequest();
            String path = request.getURI().getRawPath();
            path = path.startsWith(prefix) ? path.replaceFirst(prefix, "") : path;
            ServerHttpRequest newRequest = request.mutate().path(path).build();
            return chain.filter(exchange.mutate().request(newRequest).build());
        };
    }
}

至此,網(wǎng)關將自動根據(jù)注冊中心的服務名為每個服務創(chuàng)建一個router,將以服務名開頭的請求路徑轉發(fā)到對應的服務。

(2)pom.xml 文件引入 knife4j 依賴:

<dependency>
			<groupId>com.github.xiaoymin</groupId>
			<artifactId>knife4j-spring-boot-starter</artifactId>
			<version>2.0.4</version>
		</dependency>
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger-ui</artifactId>
		</dependency>
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger2</artifactId>
		</dependency>

(3)配置 SwaggerHeaderFilter:

在集成 Spring Cloud Gateway 網(wǎng)關的時候,會出現(xiàn)沒有 basePath 的情況,例如定義的 /user、/order 等微服務前綴,因此我們需要在 Gateway 網(wǎng)關添加一個 Filter 過濾器

import org.apache.commons.lang3.StringUtils;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.server.ServerWebExchange;
 
@Configuration
public class SwaggerHeaderFilter extends AbstractGatewayFilterFactory
{
    private static final String HEADER_NAME = "X-Forwarded-Prefix";
    private static final String URI = "/v2/api-docs";
    @Override
    public GatewayFilter apply(Object config)
    {
        return (exchange, chain) -> {
            ServerHttpRequest request = exchange.getRequest();
            String path = request.getURI().getPath();
            if(StringUtils.endsWithIgnoreCase(path, URI))
            {
                String basePath = path.substring(0, path.lastIndexOf(URI));
                ServerHttpRequest newRequest = request.mutate().header(HEADER_NAME, basePath).build();
                ServerWebExchange newExchange = exchange.mutate().request(newRequest).build();
                return chain.filter(newExchange);
            }
            else
                return chain.filter(exchange);
        };
    }
}

(4)重寫 swagger-resources:

在使用 SpringBoot 等單體架構集成 swagger 時,我們是基于包路徑進行業(yè)務分組,然后在前端進行不同模塊的展示,而在微服務架構下,一個服務就類似于原來我們寫的一個業(yè)務組。springfox-swagger 提供的分組接口是 swagger-resource,返回的是分組接口名稱、地址等信息,而在Spring Cloud微服務架構下,我們需要重寫該接口,改由通過網(wǎng)關的注冊中心動態(tài)發(fā)現(xiàn)所有的微服務文檔,代碼如下:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;
 
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
 * 使用Spring Boot單體架構集成swagger時,是通過包路徑進行業(yè)務分組,然后在前端進行不同模塊的展示,而在微服務架構下,單個服務類似于原來業(yè)務組;
 * springfox-swagger提供的分組接口是swagger-resource,返回的是分組接口名稱、地址等信息;
 * 在Spring Cloud微服務架構下,需要swagger-resource重寫接口,由網(wǎng)關的注冊中心動態(tài)發(fā)現(xiàn)所有的微服務文檔
 */
@Primary
@Configuration
public class SwaggerResourceConfig implements SwaggerResourcesProvider
{
    @Autowired
    private RouteLocator routeLocator;
    
    // 網(wǎng)關應用名稱
    @Value ("${spring.application.name}")
    private String applicationName;
    //接口地址
    private static final String API_URI = "/v2/api-docs";
    @Override
    public List<SwaggerResource> get() {
        //接口資源列表
        List<SwaggerResource> resources = new ArrayList<>();
        //服務名稱列表
        List<String> routeHosts = new ArrayList<>();
        // 獲取所有可用的應用名稱
        routeLocator.getRoutes()
                .filter(route -> route.getUri().getHost() != null)
                .filter(route -> !applicationName.equals(route.getUri().getHost()))
                .subscribe(route -> routeHosts.add(route.getUri().getHost()));
        // 去重,多負載服務只添加一次
        Set<String> existsServer = new HashSet<>();
        routeHosts.forEach(host -> {
            // 拼接url
            String url = "/" + host + API_URI;
            //不存在則添加
            if (!existsServer.contains(url)) {
                existsServer.add(url);
                SwaggerResource swaggerResource = new SwaggerResource();
                swaggerResource.setUrl(url);
                swaggerResource.setName(host);
                resources.add(swaggerResource);
            }
        });
        return resources;
    }
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;
import springfox.documentation.swagger.web.*;
 
import java.util.Optional;
/**
 * 獲取api接口信息
 */
@RestController
@RequestMapping ("/swagger-resources")
public class SwaggerHandler
{
    @Autowired(required = false)
    private SecurityConfiguration securityConfiguration;
    private UiConfiguration uiConfiguration;
    private final SwaggerResourcesProvider swaggerResources;
    @Autowired
    public SwaggerHandler(SwaggerResourcesProvider swaggerResources) {
        this.swaggerResources = swaggerResources;
    }
    @GetMapping("/configuration/security")
    public Mono<ResponseEntity<SecurityConfiguration>> securityConfiguration()
    {
        return Mono.just(new ResponseEntity<>(Optional.ofNullable(securityConfiguration).orElse(SecurityConfigurationBuilder.builder().build()), HttpStatus.OK));
    @GetMapping ("/configuration/ui")
    public Mono<ResponseEntity<UiConfiguration>> uiConfiguration()
        return Mono.just(new ResponseEntity<>(Optional.ofNullable(uiConfiguration).orElse(UiConfigurationBuilder.builder().build()), HttpStatus.OK));
    @GetMapping("")
    public Mono<ResponseEntity> swaggerResources()
        return Mono.just((new ResponseEntity<>(swaggerResources.get(), HttpStatus.OK)));
}

2、微服務架構中其他項目接入knife4j:

<!-- knife4j文檔,微服務架構 -->
<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>knife4j-micro-spring-boot-starter</artifactId>
    <version>2.0.4</version>
</dependency>
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.async.DeferredResult;
import springfox.documentation.builders.*;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.paths.RelativePathProvider;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
 
import javax.servlet.ServletContext;
/**
 * @description: swagger配置文件
 **/
@Configuration
@EnableSwagger2
@EnableKnife4j
public class Swagger2Config
{
    @Value("${spring.profiles.active}")
    private String env;
    @Value("${spring.application.name}")
    private String serviceName;
    @Value("${gateway.service.name}")
    private String serviceNameForGateway;
    @Bean
    public Docket createDocket(ServletContext servletContext)
    {
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .genericModelSubstitutes(DeferredResult.class)
                .forCodeGeneration(true)
                .pathMapping("/")
                .select()
                .build()
                .apiInfo(new ApiInfoBuilder()
                        .title(serviceName + "接口文檔")
                        .version("1.0")
                        .contact(new Contact("xxx","",""))
                        .license("XXX有限公司")
                        .build())
                // 如果為生產(chǎn)環(huán)境,則不創(chuàng)建swagger
                .enable(!"real".equals(env));
        // 在knife4j前端頁面的地址路徑中添加gateway網(wǎng)關的項目名,解決在調試接口、發(fā)送請求出現(xiàn)404的問題
        docket.pathProvider(new RelativePathProvider(servletContext)
        {
            @Override
            public String getApplicationBasePath()
            {
                return "/" + serviceNameForGateway + super.getApplicationBasePath();
            }
        });
        return docket;
    }
}

3、最終效果:

文章的最后,再介紹 knife4j 官方提供的另一種接口文檔聚合的方式:Aggregation微服務聚合組件,官方地址:https://doc.xiaominfo.com/knife4j/resources/,感興趣的讀者可以去官方看下如何使用

到此這篇關于Spring Cloud Gateway 整合 knife4j 聚合接口文檔的文章就介紹到這了,更多相關Spring Cloud Gateway 整合 knife4j內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論