国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

【Spring Cloud Gateway】⑥SpringBoot3.x集成SpringDoc指南

這篇具有很好參考價值的文章主要介紹了【Spring Cloud Gateway】⑥SpringBoot3.x集成SpringDoc指南。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

背景

Spring Cloud Gateway使用Netty作為嵌入式服務器,并基于響應式Spring WebFlux。做為微服務網(wǎng)關,多個微服務把API掛在Gateway上,如果查看某個APISwagger還要去各個子微服務中去查看,就很不方便,如果能在Gateway上直接查看各個微服務的API文檔,會方便很多,本文以截至目前最新的版本為示例,講解如何在Spring Cloud Gateway中集成SpringDoc。

SpringBoot 3.x需要SpringDoc 2.x

本地開發(fā)環(huán)境介紹

開發(fā)依賴 版本
Spring Boot 3.0.6
Spring Cloud 2022.0.2
Spring Cloud Alibaba 2022.0.0.0-RC2
Spring Doc 2.1.0
JDK 20

pom.xml主要依賴

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-gateway</artifactId>
    </dependency>
    <!--引?webflux-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
    </dependency>

    <!-- webflux+SpringDoc -->
    <dependency>
        <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-starter-common</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-starter-webflux-ui</artifactId>
    </dependency>
</dependencies>

application.yml

spring:
  application:
    name: demo-gateway
  cloud:
    gateway:
      api-prefix: /
      #路由配置
      routes:
        - id: wen3-framework-apidoc-springdoc-demo
          uri: http://localhost:7001
          predicates:
            - Path=/demo7001/**
          filters:
            - RewritePath=/demo7001/(?<path>.*), /$\{path}
        - id: openapi
          uri: http://localhost:${server.port}
          predicates:
            - Path=/v3/api-docs/**
          filters:
            - RewritePath=/v3/api-docs/(?<path>.*), /$\{path}/api-docs
  
######## swagger configuration ###############
springdoc:
  swagger-ui:
    urls:
      - name: demo7001
        url: /v3/api-docs/demo7001
  • demo7001是另一個SpringDoc演示服務,可以通過/api-docs獲取swagger JSON數(shù)據(jù)即可
  • 如果有多個服務,就需要添加多個routes
  • 原理是通過調用微服務本身文檔接口獲取JSON數(shù)據(jù),然后在網(wǎng)關的swagger-ui頁面上展示

效果預覽

瀏覽器訪問http://localhost:8081/swagger-ui.html
【Spring Cloud Gateway】⑥SpringBoot3.x集成SpringDoc指南,Spring Boot,SpringBoot3,SpringDoc,Gateway,Swagger,SpringBoot

動態(tài)生成swagger文檔分組

package com.wen3.demo.gateway.springdoc;

import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.properties.AbstractSwaggerUiConfigProperties;
import org.springdoc.core.properties.SwaggerUiConfigProperties;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.HashSet;

@Component
@Slf4j
public class SwaggerUiConfigPropertiesPostProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if(SwaggerUiConfigProperties.class.isAssignableFrom(bean.getClass())) {
            SwaggerUiConfigProperties obj = (SwaggerUiConfigProperties) bean;
            if(CollectionUtils.isEmpty(obj.getUrls())) {
                obj.setUrls(new HashSet<>());
            }
            obj.getUrls().add(new AbstractSwaggerUiConfigProperties.SwaggerUrl("demo7001", "/v3/api-docs/demo7001", "demo7001xx"));
        }
        return BeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName);
    }
}
  • 可以根據(jù)需要改造這個功能,可以在從routes配置中解析各個微服務,然后添加每個微服務的swagger文檔
  • 也可以從數(shù)據(jù)庫或其它地方獲取需要開啟的swagger文檔

效果預覽

【Spring Cloud Gateway】⑥SpringBoot3.x集成SpringDoc指南,Spring Boot,SpringBoot3,SpringDoc,Gateway,Swagger,SpringBoot文章來源地址http://www.zghlxwxcb.cn/news/detail-630362.html

在線文檔

  • 官網(wǎng): https://springdoc.org/#getting-started

到了這里,關于【Spring Cloud Gateway】⑥SpringBoot3.x集成SpringDoc指南的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。如若轉載,請注明出處: 如若內容造成侵權/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領支付寶紅包贊助服務器費用

相關文章

  • Spring Cloud Gateway集成Nacos實現(xiàn)負載均衡

    Spring Cloud Gateway集成Nacos實現(xiàn)負載均衡

    ??Nacas可以用于實現(xiàn)Spring Cloud Gateway中網(wǎng)關動態(tài)路由功能,也可以基于Nacos來實現(xiàn)對后端服務的負載均衡,前者利用Nacos配置中心功能,后者利用Nacos服務注冊功能。 接下來我們來看下Gateway集成Nacos實現(xiàn)負載均衡的架構圖 一. 環(huán)境準備 1. 版本環(huán)境 Jdk: java.version1.8/java.version Spr

    2024年02月10日
    瀏覽(98)
  • Spring Cloud Gateway集成sentinel進行網(wǎng)關限流

    本文使用版本如下:

    2024年02月09日
    瀏覽(21)
  • Spring Cloud Gateway集成Nacos作為注冊中心和配置中心

    本篇文章將介紹Spring Cloud Alibaba體系下Spring Cloud Gateway的搭建,服務注冊中心和分布式配置中心使用Nacos,后續(xù)將會持續(xù)更新,介紹集成Sentinel,如何做日志鏈路追蹤,如何做全鏈路灰度發(fā)布設計,以及Spring Cloud Gateway的擴展等。 ? Spring Boot,Spring Cloud,Discovery,Config等基礎依

    2024年02月11日
    瀏覽(510)
  • Springboot3.0整合swagger,廢棄Springfox改用Springdoc

    Springboot3.0整合swagger,廢棄Springfox改用Springdoc

    Automated JSON API documentation for API\\\'s built with Spring 官網(wǎng)地址:springfox.io springdoc-openapi java library helps to automate the generation of API documentation using spring boot projects. 官網(wǎng)地址:https://springdoc.org/v2/ 注意 :使用的是V2版本,這個版本支持springboot3.0 之前springboot3.0之前我用的都是Springfox來集

    2023年04月09日
    瀏覽(24)
  • 【SpringBoot3】Spring Boot 3.0 集成 Redis 緩存

    Redis緩存是一個開源的使用ANSIC語言編寫、支持網(wǎng)絡、可基于內存亦可持久化的日志型、Key-Value數(shù)據(jù)庫,并提供多種語言的API。它主要用于作為數(shù)據(jù)庫、緩存和消息中間件,以快速讀寫和豐富的數(shù)據(jù)結構支持而著稱。 在應用程序和數(shù)據(jù)庫之間,Redis緩存作為一個中間層起著關鍵

    2024年02月21日
    瀏覽(90)
  • Spring Cloud Gateway集成Actuator的安全漏洞和解決方案

    Spring Cloud Gateway是一個基于Spring Boot2.0和Spring WebFlux的API網(wǎng)關,它可以將請求轉發(fā)到多個微服務并對請求進行路由、過濾和修改。Spring Cloud Gateway集成Actuator后可以提供更多的監(jiān)控和管理功能,但是也可能導致安全漏洞。 最近線上環(huán)境出現(xiàn)一起安全事件,就是由于Spring Cloud Gat

    2024年02月09日
    瀏覽(17)
  • Spring Cloud Gateway集成Sentinel 1.8.6及Sentinel Dashboard

    Spring Cloud Gateway集成Sentinel 1.8.6及Sentinel Dashboard

    一、安裝sentinel 1.下載地址:sentinel v1.8.6 2.啟動sentinel dashboard,執(zhí)行以下命令: java -Dcsp.sentinel.log.dir=D:xxxsentinellogs -Dserver.port=9217 -Dcsp.sentinel.dashboard.server=localhost:9217 -Dcsp.sentinel.heartbeat.client.ip=localhost -Dproject.name=sentinel-dashboard -Dsentinel.dashboard.auth.username=sentinel -Dsentinel.dashboar

    2024年02月11日
    瀏覽(16)
  • 從零開始的Spring Cloud Gateway指南:構建強大微服務架構

    從零開始的Spring Cloud Gateway指南:構建強大微服務架構

    微服務架構的興起已經(jīng)改變了軟件開發(fā)的面貌,使得開發(fā)者能夠更靈活地構建、部署和維護應用程序。而在這個微服務的時代,強大而靈活的網(wǎng)關是確保微服務之間通信順暢的關鍵之一。在本文中,我們將深入研究Spring Cloud Gateway,一款開源的、基于Spring Framework的微服務網(wǎng)關

    2024年02月02日
    瀏覽(93)
  • Spring Cloud Gateway集成Swagger實現(xiàn)微服務接口文檔統(tǒng)一管理及登錄訪問

    Spring Cloud Gateway集成Swagger實現(xiàn)微服務接口文檔統(tǒng)一管理及登錄訪問

    本文將介紹如何在 Spring Cloud 微服務中使用 Swagger 網(wǎng)關來統(tǒng)一管理所有微服務的接口文檔,并通過 Spring Security 實現(xiàn)登錄后才能訪問 Swagger 文檔,以確保接口數(shù)據(jù)的安全訪問。 在開始之前,需要假設你已經(jīng)完成了 Spring Cloud Gateway 的相關配置,并且已經(jīng)了解了基本的網(wǎng)關配置知

    2024年02月05日
    瀏覽(38)
  • springboot整合spring cloud gateway搭建網(wǎng)關服務

    springboot整合spring cloud gateway搭建網(wǎng)關服務

    spring cloud netflix zuul、spring cloud gateway是最常見的微服務網(wǎng)關,通過網(wǎng)關,我們可以在請求到達后端指定服務之前/后端服務處理完業(yè)務響應數(shù)據(jù)之后對響應進行對請求/響應進行處理。 比如常見的參數(shù)校驗、接口鑒權等等,在后端服務的攔截器和過濾器能做的事在網(wǎng)關都可以做

    2024年02月07日
    瀏覽(26)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領取紅包,優(yōu)惠每天領

二維碼1

領取紅包

二維碼2

領紅包