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

網(wǎng)關(guān)路由Gateway(2)

這篇具有很好參考價值的文章主要介紹了網(wǎng)關(guān)路由Gateway(2)。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

網(wǎng)關(guān)登錄校驗

單體架構(gòu)時我們只需要完成一次用戶登錄、身份校驗,就可以在所有業(yè)務(wù)中獲取到用戶信息。而微服務(wù)拆分后,每個微服務(wù)都獨立部署,不再共享數(shù)據(jù)。也就意味著每個微服務(wù)都需要做登錄校驗,這顯然不可取。

思路分析

項目登錄是基于JWT來實現(xiàn)的,校驗JWT的算法復(fù)雜,而且需要用到秘鑰。如果每個微服務(wù)都去做登錄校驗,這就存在著兩大問題:

  • 每個微服務(wù)都需要知道JWT的秘鑰,不安全
  • 每個微服務(wù)重復(fù)編寫登錄校驗代碼、權(quán)限校驗代碼,麻煩

既然網(wǎng)關(guān)是所有微服務(wù)的入口,一切請求都需要先經(jīng)過網(wǎng)關(guān)。我們完全可以把登錄校驗的工作放到網(wǎng)關(guān)去做,這樣之前說的問題就解決了:

  • 只需要在網(wǎng)關(guān)和用戶服務(wù)保存秘鑰
  • 只需要在網(wǎng)關(guān)開發(fā)登錄校驗功能

此時,登錄校驗的流程如圖:
- hm.jwt-com.hmall.gateway.config.jwtproperties: defined in unknown location,Spring Cloud,# 網(wǎng)關(guān)路由gateWay,gateway,spring cloud,微服務(wù)

不過,這里存在幾個問題:

  • 網(wǎng)關(guān)路由是配置的,請求轉(zhuǎn)發(fā)是Gateway內(nèi)部代碼,我們?nèi)绾卧谵D(zhuǎn)發(fā)之前做登錄校驗?
  • 網(wǎng)關(guān)校驗JWT之后,如何將用戶信息傳遞給微服務(wù)?
  • 微服務(wù)之間也會相互調(diào)用,這種調(diào)用不經(jīng)過網(wǎng)關(guān),又該如何傳遞用戶信息?

網(wǎng)關(guān)過濾器

登錄校驗必須在請求轉(zhuǎn)發(fā)到微服務(wù)之前做,否則就失去了意義。而網(wǎng)關(guān)的請求轉(zhuǎn)發(fā)是Gateway內(nèi)部代碼實現(xiàn)的,要想在請求轉(zhuǎn)發(fā)之前做登錄校驗,就必須了解Gateway內(nèi)部工作的基本原理。
- hm.jwt-com.hmall.gateway.config.jwtproperties: defined in unknown location,Spring Cloud,# 網(wǎng)關(guān)路由gateWay,gateway,spring cloud,微服務(wù)

如圖所示:

  1. 客戶端請求進入網(wǎng)關(guān)后由HandlerMapping對請求做判斷,找到與當(dāng)前請求匹配的路由規(guī)則(Route),然后將請求交給WebHandler去處理。
  2. WebHandler則會加載當(dāng)前路由下需要執(zhí)行的過濾器鏈(Filter chain),然后按照順序逐一執(zhí)行過濾器(后面稱為Filter)。
  3. 圖中Filter被虛線分為左右兩部分,是因為Filter內(nèi)部的邏輯分為pre和post兩部分,分別會在請求路由到微服務(wù)之前和之后被執(zhí)行。
  4. 只有所有Filter的pre邏輯都依次順序執(zhí)行通過后,請求才會被路由到微服務(wù)。
  5. 微服務(wù)返回結(jié)果后,再倒序執(zhí)行Filter的post邏輯。
  6. 最終把響應(yīng)結(jié)果返回。

如圖中所示,最終請求轉(zhuǎn)發(fā)是有一個名為NettyRoutingFilter的過濾器來執(zhí)行的,而且這個過濾器是整個過濾器鏈中順序最靠后的一個。如果我們能夠定義一個過濾器,在其中實現(xiàn)登錄校驗邏輯,并且將過濾器執(zhí)行順序定義到NettyRoutingFilter之前,這就符合我們的需求了!
網(wǎng)關(guān)過濾器鏈中的過濾器有兩種,其實GatewayFilter和GlobalFilter這兩種過濾器的方法簽名完全一致:

  • GatewayFilter:路由過濾器,作用范圍比較靈活,可以是任意指定的路由Route.
  • GlobalFilter:全局過濾器,作用范圍是所有路由,不可配置。
/**
 * 處理請求并將其傳遞給下一個過濾器
 * @param exchange 當(dāng)前請求的上下文,其中包含request、response等各種數(shù)據(jù)
 * @param chain 過濾器鏈,基于它向下傳遞請求
 * @return 根據(jù)返回值標(biāo)記當(dāng)前請求是否被完成或攔截,chain.filter(exchange)就放行了。
 */
Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain);

FilteringWebHandler在處理請求時,會將GlobalFilter裝飾為GatewayFilter,然后放到同一個過濾器鏈中,排序以后依次執(zhí)行。
Gateway內(nèi)置的GatewayFilter過濾器使用起來非常簡單,無需編碼,只要在yaml文件中簡單配置即可。而且其作用范圍也很靈活,配置在哪個Route下,就作用于哪個Route.
例如,有一個過濾器叫做AddRequestHeaderGatewayFilterFacotry,顧明思議,就是添加請求頭的過濾器,可以給請求添加一個請求頭并傳遞到下游微服務(wù)。
使用的使用只需要在application.yaml中這樣配置:

spring:
  cloud:
    gateway:
      routes:
      - id: test_route
        uri: lb://test-service
        predicates:
          -Path=/test/**
        filters:
          - AddRequestHeader=key, value # 逗號之前是請求頭的key,逗號之后是value

如果想要讓過濾器作用于所有的路由,則可以這樣配置:

spring:
  cloud:
    gateway:
      default-filters: # default-filters下的過濾器可以作用于所有路由
        - AddRequestHeader=key, value
      routes:
      - id: test_route
        uri: lb://test-service
        predicates:
          -Path=/test/**

自定義過濾器

自定義GatewayFilter

自定義GatewayFilter不是直接實現(xiàn)GatewayFilter,而是實現(xiàn)AbstractGatewayFilterFactory。最簡單的方式是這樣的:

@Component
public class PrintAnyGatewayFilterFactory extends AbstractGatewayFilterFactory<Object> {
    @Override
    public GatewayFilter apply(Object config) {
        return new GatewayFilter() {
            @Override
            public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
                // 獲取請求
                ServerHttpRequest request = exchange.getRequest();
                // 編寫過濾器邏輯
                System.out.println("過濾器執(zhí)行了");
                // 放行
                return chain.filter(exchange);
            }
        };
    }
}

注意:該類的名稱一定要以GatewayFilterFactory為后綴!
然后在yaml配置中這樣使用:

spring:
  cloud:
    gateway:
      default-filters:
            - PrintAny # 此處直接以自定義的GatewayFilterFactory類名稱前綴類聲明過濾器

另外,這種過濾器還可以支持動態(tài)配置參數(shù),不過實現(xiàn)起來比較復(fù)雜,示例:


@Component
public class PrintAnyGatewayFilterFactory // 父類泛型是內(nèi)部類的Config類型
                extends AbstractGatewayFilterFactory<PrintAnyGatewayFilterFactory.Config> {

    @Override
    public GatewayFilter apply(Config config) {
        // OrderedGatewayFilter是GatewayFilter的子類,包含兩個參數(shù):
        // - GatewayFilter:過濾器
        // - int order值:值越小,過濾器執(zhí)行優(yōu)先級越高
        return new OrderedGatewayFilter(new GatewayFilter() {
            @Override
            public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
                // 獲取config值
                String a = config.getA();
                String b = config.getB();
                String c = config.getC();
                // 編寫過濾器邏輯
                System.out.println("a = " + a);
                System.out.println("b = " + b);
                System.out.println("c = " + c);
                // 放行
                return chain.filter(exchange);
            }
        }, 100);
    }

    // 自定義配置屬性,成員變量名稱很重要,下面會用到
    @Data
    static class Config{
        private String a;
        private String b;
        private String c;
    }
    // 將變量名稱依次返回,順序很重要,將來讀取參數(shù)時需要按順序獲取
    @Override
    public List<String> shortcutFieldOrder() {
        return List.of("a", "b", "c");
    }
        // 返回當(dāng)前配置類的類型,也就是內(nèi)部的Config
    @Override
    public Class<Config> getConfigClass() {
        return Config.class;
    }

}

然后在yaml文件中使用:

spring:
  cloud:
    gateway:
      default-filters:
            - PrintAny=1,2,3 # 注意,這里多個參數(shù)以","隔開,將來會按照shortcutFieldOrder()方法返回的參數(shù)順序依次復(fù)制

自定義GlobalFilter

自定義GlobalFilter則簡單很多,直接實現(xiàn)GlobalFilter即可,而且也無法設(shè)置動態(tài)參數(shù):

@Component
public class PrintAnyGlobalFilter implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        // 編寫過濾器邏輯
        System.out.println("未登錄,無法訪問");
        // 放行
        // return chain.filter(exchange);

        // 攔截
        ServerHttpResponse response = exchange.getResponse();
        response.setRawStatusCode(401);
        return response.setComplete();
    }

    @Override
    public int getOrder() {
        // 過濾器執(zhí)行順序,值越小,優(yōu)先級越高
        return 0;
    }
}

登錄校驗

接下來,我們就利用自定義GlobalFilter來完成登錄校驗。
登錄校驗需要用到JWT,而且JWT的加密需要秘鑰和加密工具。
具體作用如下:

  • AuthProperties:配置登錄校驗需要攔截的路徑,因為不是所有的路徑都需要登錄才能訪問
  • JwtProperties:定義與JWT工具有關(guān)的屬性,比如秘鑰文件位置
  • SecurityConfig:工具的自動裝配
  • JwtTool:JWT工具,其中包含了校驗和解析token的功能
  • hmall.jks:秘鑰文件

其中AuthProperties和JwtProperties所需的屬性要在application.yaml中配置:

hm:
  jwt:
    location: classpath:hmall.jks # 秘鑰地址
    alias: hmall # 秘鑰別名
    password: hmall123 # 秘鑰文件密碼
    tokenTTL: 30m # 登錄有效期
  auth:
    excludePaths: # 無需登錄校驗的路徑
      - /search/**
      - /users/login
      - /items/**

登錄校驗過濾器

package com.hmall.gateway.filter;

import com.hmall.common.exception.UnauthorizedException;
import com.hmall.common.utils.CollUtils;
import com.hmall.gateway.config.AuthProperties;
import com.hmall.gateway.util.JwtTool;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.List;

@Component
@RequiredArgsConstructor
@EnableConfigurationProperties(AuthProperties.class)
public class AuthGlobalFilter implements GlobalFilter, Ordered {

    private final JwtTool jwtTool;

    private final AuthProperties authProperties;

    private final AntPathMatcher antPathMatcher = new AntPathMatcher();

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        // 1.獲取Request
        ServerHttpRequest request = exchange.getRequest();
        // 2.判斷是否不需要攔截
        if(isExclude(request.getPath().toString())){
            // 無需攔截,直接放行
            return chain.filter(exchange);
        }
        // 3.獲取請求頭中的token
        String token = null;
        List<String> headers = request.getHeaders().get("authorization");
        if (!CollUtils.isEmpty(headers)) {
            token = headers.get(0);
        }
        // 4.校驗并解析token
        Long userId = null;
        try {
            userId = jwtTool.parseToken(token);
        } catch (UnauthorizedException e) {
            // 如果無效,攔截
            ServerHttpResponse response = exchange.getResponse();
            response.setRawStatusCode(401);
            return response.setComplete();
        }

        // TODO 5.如果有效,傳遞用戶信息
        System.out.println("userId = " + userId);
        // 6.放行
        return chain.filter(exchange);
    }

    private boolean isExclude(String antPath) {
        for (String pathPattern : authProperties.getExcludePaths()) {
            if(antPathMatcher.match(pathPattern, antPath)){
                return true;
            }
        }
        return false;
    }

    @Override
    public int getOrder() {
        return 0;
    }
}

微服務(wù)獲取用戶

現(xiàn)在,網(wǎng)關(guān)已經(jīng)可以完成登錄校驗并獲取登錄用戶身份信息。但是當(dāng)網(wǎng)關(guān)將請求轉(zhuǎn)發(fā)到微服務(wù)時,微服務(wù)又該如何獲取用戶身份呢?
由于網(wǎng)關(guān)發(fā)送請求到微服務(wù)依然采用的是Http請求,因此我們可以將用戶信息以請求頭的方式傳遞到下游微服務(wù)。然后微服務(wù)可以從請求頭中獲取登錄用戶信息??紤]到微服務(wù)內(nèi)部可能很多地方都需要用到登錄用戶信息,因此我們可以利用SpringMVC的攔截器來實現(xiàn)登錄用戶信息獲取,并存入ThreadLocal,方便后續(xù)使用。
據(jù)圖流程圖如下:

因此,接下來我們要做的事情有:

  • 改造網(wǎng)關(guān)過濾器,在獲取用戶信息后保存到請求頭,轉(zhuǎn)發(fā)到下游微服務(wù)
  • 編寫微服務(wù)攔截器,攔截請求獲取用戶信息,保存到ThreadLocal后放行

保存用戶到請求頭

首先,我們修改登錄校驗攔截器的處理邏輯,保存用戶信息到請求頭中:
- hm.jwt-com.hmall.gateway.config.jwtproperties: defined in unknown location,Spring Cloud,# 網(wǎng)關(guān)路由gateWay,gateway,spring cloud,微服務(wù)

攔截器獲取用戶

  • 在項目中已經(jīng)有一個用于保存登錄用戶的ThreadLocal工具,其中已經(jīng)提供了保存和獲取用戶的方法
  • 接下來,我們只需要編寫攔截器,獲取用戶信息并保存到UserContext,然后放行即可。
  • 由于每個微服務(wù)都有獲取登錄用戶的需求,因此攔截器我們直接寫在common中,并寫好自動裝配。這樣微服務(wù)只需要引入common就可以直接具備攔截器功能,無需重復(fù)編寫。
package com.hmall.common.interceptor;

import cn.hutool.core.util.StrUtil;
import com.hmall.common.utils.UserContext;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class UserInfoInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 1.獲取請求頭中的用戶信息
        String userInfo = request.getHeader("user-info");
        // 2.判斷是否為空
        if (StrUtil.isNotBlank(userInfo)) {
            // 不為空,保存到ThreadLocal
                UserContext.setUser(Long.valueOf(userInfo));
        }
        // 3.放行
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        // 移除用戶
        UserContext.removeUser();
    }
}

接著在common模塊下編寫SpringMVC的配置類,配置登錄攔截器:

package com.hmall.common.config;

import com.hmall.common.interceptor.UserInfoInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MvcConfig implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new UserInfoInterceptor());
    }
}

不過,需要注意的是,這個配置類默認是不會生效的,因為它所在的包是com.hmall.common.config,與其它微服務(wù)的掃描包不一致,無法被掃描到,因此無法生效。
基于SpringBoot的自動裝配原理,我們要將其添加到resources目錄下的META-INF/spring.factories文件中:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.hmall.common.config.MyBatisConfig,\
  com.hmall.common.config.MvcConfig

OpenFeign傳遞用戶

前端發(fā)起的請求都會經(jīng)過網(wǎng)關(guān)再到微服務(wù),由于我們之前編寫的過濾器和攔截器功能,微服務(wù)可以輕松獲取登錄用戶信息。
但有些業(yè)務(wù)是比較復(fù)雜的,請求到達微服務(wù)后還需要調(diào)用其它多個微服務(wù)。比如下單業(yè)務(wù),流程如下:
- hm.jwt-com.hmall.gateway.config.jwtproperties: defined in unknown location,Spring Cloud,# 網(wǎng)關(guān)路由gateWay,gateway,spring cloud,微服務(wù)

單的過程中,需要調(diào)用商品服務(wù)扣減庫存,調(diào)用購物車服務(wù)清理用戶購物車。而清理購物車時必須知道當(dāng)前登錄的用戶身份。但是,訂單服務(wù)調(diào)用購物車時并沒有傳遞用戶信息,購物車服務(wù)無法知道當(dāng)前用戶是誰!

由于微服務(wù)獲取用戶信息是通過攔截器在請求頭中讀取,因此要想實現(xiàn)微服務(wù)之間的用戶信息傳遞,就必須在微服務(wù)發(fā)起調(diào)用時把用戶信息存入請求頭。

微服務(wù)之間調(diào)用是基于OpenFeign來實現(xiàn)的,并不是我們自己發(fā)送的請求。我們?nèi)绾尾拍茏屆恳粋€由OpenFeign發(fā)起的請求自動攜帶登錄用戶信息呢?
這里要借助Feign中提供的一個攔截器接口:feign.RequestInterceptor

public interface RequestInterceptor {

  /**
   * Called for every request. 
   * Add data using methods on the supplied {@link RequestTemplate}.
   */
  void apply(RequestTemplate template);
}

我們只需要實現(xiàn)這個接口,然后實現(xiàn)apply方法,利用RequestTemplate類來添加請求頭,將用戶信息保存到請求頭中。這樣以來,每次OpenFeign發(fā)起請求的時候都會調(diào)用該方法,傳遞用戶信息。

由于FeignClient全部都是在api模塊,因此我們在api模塊的com.hmall.api.config.DefaultFeignConfig中編寫這個攔截器:
com.hmall.api.config.DefaultFeignConfig中添加一個Bean文章來源地址http://www.zghlxwxcb.cn/news/detail-838170.html

@Bean
public RequestInterceptor userInfoRequestInterceptor(){
    return new RequestInterceptor() {
        @Override
        public void apply(RequestTemplate template) {
            // 獲取登錄用戶
            Long userId = UserContext.getUser();
            if(userId == null) {
                // 如果為空則直接跳過
                return;
            }
            // 如果不為空則放入請求頭中,傳遞給下游微服務(wù)
            template.header("user-info", userId.toString());
        }
    };
}

到了這里,關(guān)于網(wǎng)關(guān)路由Gateway(2)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

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

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • SpringCloud之 Gateway路由網(wǎng)關(guān)

    SpringCloud之 Gateway路由網(wǎng)關(guān)

    提示:以下是本篇文章正文內(nèi)容,SpringCloud 系列學(xué)習(xí)將會持續(xù)更新 官網(wǎng)地址:https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/ 說到路由,想必各位一定最先想到的就是家里的路由器了,那么我們家里的路由器充當(dāng)?shù)氖且粋€什么角色呢? 我們知道,如果我們需要連接互

    2024年02月06日
    瀏覽(25)
  • spring cloud gateway網(wǎng)關(guān)(一)之網(wǎng)關(guān)路由

    spring cloud gateway網(wǎng)關(guān)(一)之網(wǎng)關(guān)路由

    1、gateway相關(guān)介紹 在微服務(wù)架構(gòu)中,系統(tǒng)往往由多個微服務(wù)組成,而這些服務(wù)可能部署在不同機房、不同地區(qū)、不同域名下。這種情況下,客戶端(例如瀏覽器、手機、軟件工具等)想要直接請求這些服務(wù),就需要知道它們具體的地址信息,例如 IP 地址、端口號等。這種客戶

    2024年02月08日
    瀏覽(25)
  • 【Spring Cloud Alibaba】8.路由網(wǎng)關(guān)(Gateway)

    【Spring Cloud Alibaba】8.路由網(wǎng)關(guān)(Gateway)

    接下來對服務(wù)消費者添加路由網(wǎng)關(guān)來實現(xiàn)統(tǒng)一訪問接口,本操作先要完成之前的步驟,詳情請參照【Spring Cloud Alibaba】Spring Cloud Alibaba 搭建教程 Spring Cloud Gateway 是 Spring 官方基于 Spring 5.0 , Spring Boot 2.0 和 Project Reactor 等技術(shù)開發(fā)的網(wǎng)關(guān),該項目提供了一個庫,用于在 Spring W

    2023年04月24日
    瀏覽(22)
  • 如何使用 Gateway 搭建網(wǎng)關(guān)服務(wù)及實現(xiàn)動態(tài)路由?

    如何使用 Gateway 搭建網(wǎng)關(guān)服務(wù)及實現(xiàn)動態(tài)路由?

    網(wǎng)關(guān)作為微服務(wù)中非常重要的一部分,是必須要掌握的;本文記錄一下我是如何使用Gateway搭建網(wǎng)關(guān)服務(wù)及實現(xiàn)動態(tài)路由的,幫助大家學(xué)習(xí)如何快速搭建一個網(wǎng)關(guān)服務(wù),了解路由相關(guān)配置,鑒權(quán)的流程及業(yè)務(wù)處理,有興趣的一定看到最后,非常適合沒接觸過網(wǎng)關(guān)服務(wù)的同學(xué)當(dāng)作

    2024年02月09日
    瀏覽(22)
  • 谷粒商城p46-配置網(wǎng)關(guān)路由與路徑重寫 Gateway配置網(wǎng)關(guān)路由和路徑重寫

    http://t.csdn.cn/Vdti6 http://t.csdn.cn/Vdti6 http://t.csdn.cn/pjLyz http://t.csdn.cn/pjLyz 前端頁面的請求會發(fā)送到上述前綴地址 url:與上面的拼接 idea-gateway的yml文件 網(wǎng)關(guān)路由與路徑重寫

    2024年02月09日
    瀏覽(16)
  • springboot和flask整合nacos,使用openfeign實現(xiàn)服務(wù)調(diào)用,使用gateway實現(xiàn)網(wǎng)關(guān)的搭建(附帶jwt續(xù)約的實現(xiàn))

    springboot和flask整合nacos,使用openfeign實現(xiàn)服務(wù)調(diào)用,使用gateway實現(xiàn)網(wǎng)關(guān)的搭建(附帶jwt續(xù)約的實現(xiàn))

    插件 版本 jdk 21 springboot 3.0.11 springcloud 2022.0.4 springcloudalibaba 2022.0.0.0 nacos 2.2.3(穩(wěn)定版) python 3.8 先創(chuàng)建目錄,分別創(chuàng)建config,logs,data目錄,單獨創(chuàng)建一個容器 ?將配置文件拷貝出來(主要是application.properties和logback.xml) 修改mysql的信息(修改文件application.properties) 再次運行

    2024年02月07日
    瀏覽(28)
  • 【深入解析spring cloud gateway】02 網(wǎng)關(guān)路由斷言

    【深入解析spring cloud gateway】02 網(wǎng)關(guān)路由斷言

    斷言是路由配置的一部分,當(dāng)斷言條件滿足,即執(zhí)行Filter的邏輯,如下例所示 當(dāng)請求路徑滿足條件/red/,即添加頭信息:X-Request-Red,value為Blue-{segment},segment是路徑里面帶的信息。 gateWay的主要功能之一是轉(zhuǎn)發(fā)請求,轉(zhuǎn)發(fā)規(guī)則的定義主要包含三個部分 Route(路由) 路由是網(wǎng)關(guān)

    2024年02月09日
    瀏覽(28)
  • Gateway網(wǎng)關(guān)路由以及predicates用法(項目中使用場景)

    Gateway網(wǎng)關(guān)路由以及predicates用法(項目中使用場景)

    1.Gateway+nacos整合微服務(wù) 服務(wù)注冊在nacos上,通過Gateway路由網(wǎng)關(guān)配置統(tǒng)一路由訪問 這里主要通過yml方式說明: route: ? ? config: ? ? #type:database nacos yml ? ? data-type: yml ? ? group: DEFAULT_GROUP ? ? data-id: jeecg-gateway-router 配置路由: ? 通過斷言里Path地址訪問到對應(yīng)的system-service服務(wù)

    2024年02月12日
    瀏覽(23)
  • 第八章 : Spring cloud 網(wǎng)關(guān)中心 Gateway (動態(tài)路由)

    第八章 : Spring cloud 網(wǎng)關(guān)中心 Gateway (動態(tài)路由) 前言 本章知識點:重點介紹動態(tài)網(wǎng)關(guān)路由的背景、動態(tài)路由與靜態(tài)路由的概念,以及如何基于Nacos實現(xiàn)動態(tài)網(wǎng)關(guān)路由 的實戰(zhàn)案例。 背景 前面章節(jié)介紹了Spring Cloud Gateway提供的配置路由規(guī)則的兩種方法,但都是在Spring Cloud Ga

    2024年01月19日
    瀏覽(37)
  • 第七章 : Spring cloud 網(wǎng)關(guān)中心 Gateway (靜態(tài)路由)

    第七章 : Spring cloud 網(wǎng)關(guān)中心 Gateway (靜態(tài)路由) 前言 本章知識點:本章將會介紹什么是Spring Cloud Gateway、為什么會出現(xiàn)Spring Cloud Gateway,以及Spring Cloud Gateway的工作原理和實戰(zhàn)用法,以及Spring Cloud Gateway 路由概念以及基于nacos注冊中心Spring Cloud Gateway 靜態(tài)路由的實戰(zhàn)。 什么

    2024年02月02日
    瀏覽(38)

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

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

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

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

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包