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

項(xiàng)目示例 - 3.服務(wù)網(wǎng)關(guān) - 3.Gateway

這篇具有很好參考價(jià)值的文章主要介紹了項(xiàng)目示例 - 3.服務(wù)網(wǎng)關(guān) - 3.Gateway。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

項(xiàng)目示例 - 3.服務(wù)網(wǎng)關(guān) - 3.Gateway

關(guān)聯(lián)知識(shí):

  • 分布式微服務(wù) - 3.服務(wù)網(wǎng)關(guān) - 4.Gateway

內(nèi)容提要:文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-839634.html

  • 基本使用
  • 自定義斷言
  • 自定義局部、全局過(guò)濾器

基本使用

  1. 建Module:微服務(wù)起名為gateway-server
  2. 改pom:引入以下依賴
    <dependencies>
        <!--gateway-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-loadbalancer</artifactId>
        </dependency>
        <!--Nacos服務(wù)注冊(cè)-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
    </dependencies>
  1. 寫yml:在resources目錄下創(chuàng)建application.yml文件,并做以下配置
server:
  port: 80

spring:
  application:
    name: gateway-server
  cloud:
    nacos:
      discovery:
        # nacos的地址
        server-addr: localhost:8848
    gateway:
      discovery:
        locator:
          # 從服務(wù)注冊(cè)中心根據(jù)別名來(lái)進(jìn)行路由
          enabled: true
      routes:
        # 路由Id,自定義,唯一即可
        - id: nacos-provider-test
          # 成功匹配后,要轉(zhuǎn)換成的路由地址(配合服務(wù)注冊(cè)中心使用別名)
          uri: lb://nacos-provider
          # 成功匹配后,要轉(zhuǎn)換成的路由地址(不配合服務(wù)注冊(cè)中心)
          #uri: http://localhost:8001
          # 斷言
          predicates:
            # 路徑匹配的進(jìn)行路由
            - Path=/**
  1. 主啟動(dòng):在src下創(chuàng)建如下主啟動(dòng)類
package learn.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class GatewayServer {

    public static void main(String[] args) {
        SpringApplication.run(GatewayServer.class, args);
    }

}
  1. 測(cè)試:
    1. 啟動(dòng)nacos,能正常打開nacos首頁(yè)
    2. 啟動(dòng)nacos-provider微服務(wù),瀏覽器訪問(wèn)localhost:8001/nacos/provider/test 接口,能正常返回信息,且在nacos服務(wù)列表中可看到成功注冊(cè)
    3. 啟動(dòng)本微服務(wù)后,瀏覽器訪問(wèn)localhost/nacos/provider/test 接口,能正常返回信息

自定義斷言

  1. 創(chuàng)建自定義斷言類:
package learn.demo.config;

import org.springframework.cloud.gateway.handler.predicate.AbstractRoutePredicateFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;

import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

@Component
public class CustomRoutePredicateFactory extends AbstractRoutePredicateFactory<CustomRoutePredicateFactory.Config> {

    public CustomRoutePredicateFactory() {
        super(Config.class);
    }

    @Override
    public List<String> shortcutFieldOrder() {
        return Arrays.asList("key", "value");
    }

    @Override
    public Predicate<ServerWebExchange> apply(Config config) {
        return new Predicate<ServerWebExchange>() {
            @Override
            public boolean test(ServerWebExchange serverWebExchange) {
                List<String> strings = serverWebExchange.getRequest().getHeaders().get(config.key);
                if (strings==null || strings.isEmpty())
                    return false;
                return config.value.equals(strings.get(0));
            }
        };
    }

    public static class Config {
        private String key;
        private String value;

        public String getKey() {
            return key;
        }

        public void setKey(String key) {
            this.key = key;
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }
    }

}
  1. application.yml中配置斷言:
server:
  port: 80

spring:
  application:
    name: gateway-server
  cloud:
    nacos:
      discovery:
        # nacos的地址
        server-addr: localhost:8848
    gateway:
      discovery:
        locator:
          # 從服務(wù)注冊(cè)中心根據(jù)別名來(lái)進(jìn)行路由
          enabled: true
      routes:
        # 路由Id,自定義,唯一即可
        - id: nacos-provider-test
          # 成功匹配后,要轉(zhuǎn)換成的路由地址(配合服務(wù)注冊(cè)中心使用別名)
          uri: lb://nacos-provider
          # 成功匹配后,要轉(zhuǎn)換成的路由地址(不配合服務(wù)注冊(cè)中心)
          #uri: http://localhost:8001
          # 斷言
          predicates:
            # 路徑匹配的進(jìn)行路由
            - Path=/**
            - Custom=test, a
  1. 測(cè)試:
    1. 啟動(dòng)nacos,能正常打開nacos首頁(yè)
    2. 啟動(dòng)nacos-provider微服務(wù),瀏覽器訪問(wèn)localhost:8001/nacos/provider/test 接口,能正常返回信息,且在nacos服務(wù)列表中可看到成功注冊(cè)
    3. 啟動(dòng)本微服務(wù)后,API工具訪問(wèn)localhost/nacos/provider/test 接口,返回404。在header中,攜帶key為test、value為a的參數(shù)后再次訪問(wèn),可以正常返回信息。

自定義過(guò)濾器

自定義局部過(guò)濾器

  1. 創(chuàng)建自定義過(guò)濾器類:
package learn.demo.config;

import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.Arrays;
import java.util.List;

@Component
public class CustomGatewayFilterFactory extends AbstractGatewayFilterFactory<CustomGatewayFilterFactory.Config> {

    public CustomGatewayFilterFactory() {
        super(Config.class);
    }

    @Override
    public List<String> shortcutFieldOrder() {
        return Arrays.asList("key", "value");
    }

    @Override
    public GatewayFilter apply(Config config) {
        return new GatewayFilter() {
            @Override
            public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
                // pre操作
                String pre;
                List<String> strings = exchange.getRequest().getHeaders().get("pre");
                if (strings==null || strings.isEmpty())
                    pre="no args";
                else
                    pre=strings.get(0);

                return chain.filter(exchange).then(Mono.fromRunnable(() -> {
                    // 異步post操作
                    exchange.getResponse().getHeaders().set(config.key, pre+" | "+config.value);
                }));
            }
        };
    }

    public static class Config {
        private String key;
        private String value;

        public String getKey() {
            return key;
        }

        public void setKey(String key) {
            this.key = key;
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }
    }

}
  1. 添加配置:
server:
  port: 80

spring:
  application:
    name: gateway-server
  cloud:
    nacos:
      discovery:
        # nacos的地址
        server-addr: localhost:8848
    gateway:
      discovery:
        locator:
          # 從服務(wù)注冊(cè)中心根據(jù)別名來(lái)進(jìn)行路由
          enabled: true
      routes:
        # 路由Id,自定義,唯一即可
        - id: nacos-provider-test
          # 成功匹配后,要轉(zhuǎn)換成的路由地址(配合服務(wù)注冊(cè)中心使用別名)
          uri: lb://nacos-provider
          # 成功匹配后,要轉(zhuǎn)換成的路由地址(不配合服務(wù)注冊(cè)中心)
          #uri: http://localhost:8001
          # 斷言
          predicates:
            # 路徑匹配的進(jìn)行路由
            - Path=/**
            - Custom=test, a
          filters:
            - Custom=post,test
  1. 測(cè)試:
    1. 啟動(dòng)nacos,能正常打開nacos首頁(yè)
    2. 啟動(dòng)nacos-provider微服務(wù),瀏覽器訪問(wèn)localhost:8001/nacos/provider/test 接口,能正常返回信息,且在nacos服務(wù)列表中可看到成功注冊(cè)
    3. 啟動(dòng)本微服務(wù)后,API工具訪問(wèn)localhost/nacos/provider/test 接口,返回404。在header中,攜帶key為test、value為a的參數(shù)后再次訪問(wèn),可以正常返回信息,查看響應(yīng)頭中有key為post、value為no args | test的參數(shù)。

自定義全局過(guò)濾器

  1. 創(chuàng)建自定義過(guò)濾器類:
package learn.demo.config;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.List;

@Component
public class CustomGlobalFilter implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        // pre操作
        String pre;
        List<String> strings = exchange.getRequest().getHeaders().get("pre");
        if (strings==null || strings.isEmpty())
            pre="no args";
        else
            pre=strings.get(0);

        return chain.filter(exchange).then(Mono.fromRunnable(() -> {
            // 異步post操作
            exchange.getResponse().getHeaders().set("post", pre+" | test");
        }));
    }

    // 值越小,優(yōu)先級(jí)越高
    @Override
    public int getOrder() {
        return 0;
    }
}
  1. 修改配置文件:
server:
  port: 80

spring:
  application:
    name: gateway-server
  cloud:
    nacos:
      discovery:
        # nacos的地址
        server-addr: localhost:8848
    gateway:
      discovery:
        locator:
          # 從服務(wù)注冊(cè)中心根據(jù)別名來(lái)進(jìn)行路由
          enabled: true
      routes:
        # 路由Id,自定義,唯一即可
        - id: nacos-provider-test
          # 成功匹配后,要轉(zhuǎn)換成的路由地址(配合服務(wù)注冊(cè)中心使用別名)
          uri: lb://nacos-provider
          # 成功匹配后,要轉(zhuǎn)換成的路由地址(不配合服務(wù)注冊(cè)中心)
          #uri: http://localhost:8001
          # 斷言
          predicates:
            # 路徑匹配的進(jìn)行路由
            - Path=/**
            - Custom=test, a
  1. 測(cè)試:
    1. 啟動(dòng)nacos,能正常打開nacos首頁(yè)
    2. 啟動(dòng)nacos-provider微服務(wù),瀏覽器訪問(wèn)localhost:8001/nacos/provider/test 接口,能正常返回信息,且在nacos服務(wù)列表中可看到成功注冊(cè)
    3. 啟動(dòng)本微服務(wù)后,API工具訪問(wèn)localhost/nacos/provider/test 接口,返回404。在header中,攜帶key為test、value為a的參數(shù)后再次訪問(wèn),可以正常返回信息,查看響應(yīng)頭中有key為post、value為no args | test的參數(shù)。

到了這里,關(guān)于項(xiàng)目示例 - 3.服務(wù)網(wǎng)關(guān) - 3.Gateway的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

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

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

相關(guān)文章

  • 【SpringCloud技術(shù)專題】「Gateway網(wǎng)關(guān)系列」(2)微服務(wù)網(wǎng)關(guān)服務(wù)的Gateway功能配置指南分析

    【SpringCloud技術(shù)專題】「Gateway網(wǎng)關(guān)系列」(2)微服務(wù)網(wǎng)關(guān)服務(wù)的Gateway功能配置指南分析

    Spring Cloud Gateway簡(jiǎn)介 Spring Cloud Gateway是Spring Cloud體系的第二代網(wǎng)關(guān)組件,基于Spring 5.0的新特性WebFlux進(jìn)行開發(fā),底層網(wǎng)絡(luò)通信框架使用的是Netty,所以其吞吐量高、性能強(qiáng)勁,未來(lái)將會(huì)取代第一代的網(wǎng)關(guān)組件Zuul。 Spring Cloud Gateway可以通過(guò)服務(wù)發(fā)現(xiàn)組件自動(dòng)轉(zhuǎn)發(fā)請(qǐng)求,默認(rèn)集成了

    2024年02月11日
    瀏覽(53)
  • 【SpringCloud技術(shù)專題】「Gateway網(wǎng)關(guān)系列」(1)微服務(wù)網(wǎng)關(guān)服務(wù)的Gateway組件的原理介紹分析

    【SpringCloud技術(shù)專題】「Gateway網(wǎng)關(guān)系列」(1)微服務(wù)網(wǎng)關(guān)服務(wù)的Gateway組件的原理介紹分析

    為什么要有服務(wù)網(wǎng)關(guān)? 我們都知道在微服務(wù)架構(gòu)中,系統(tǒng)會(huì)被拆分為很多個(gè)微服務(wù)。那么作為客戶端要如何去調(diào)用這么多的微服務(wù)呢?難道要一個(gè)個(gè)的去調(diào)用嗎?很顯然這是不太實(shí)際的,我們需要有一個(gè)統(tǒng)一的接口與這些微服務(wù)打交道,這就是我們需要服務(wù)網(wǎng)關(guān)的原因。 我們

    2024年02月11日
    瀏覽(32)
  • 【微服務(wù)】服務(wù)網(wǎng)關(guān)----Gateway

    上篇文章講解了如何實(shí)現(xiàn)服務(wù)容錯(cuò),這篇就介紹下,服務(wù)網(wǎng)關(guān)— Gateway 。在本篇文章中,你將了解到什么是服務(wù)網(wǎng)關(guān)?什么是Gateway?如何實(shí)現(xiàn)Gateway?Gateway的核心架構(gòu)有哪些?

    2024年01月24日
    瀏覽(20)
  • 兩天學(xué)會(huì)微服務(wù)網(wǎng)關(guān)Gateway-Gateway路由規(guī)則

    兩天學(xué)會(huì)微服務(wù)網(wǎng)關(guān)Gateway-Gateway路由規(guī)則

    鋒哥原創(chuàng)的微服務(wù)網(wǎng)關(guān)Gateway視頻教程: Gateway微服務(wù)網(wǎng)關(guān)視頻教程(無(wú)廢話版)_嗶哩嗶哩_bilibili Gateway微服務(wù)網(wǎng)關(guān)視頻教程(無(wú)廢話版)共計(jì)17條視頻,包括:1_Gateway簡(jiǎn)介、2_Gateway工作原理、3_Gateway HelloWorld快速入門等,UP主更多精彩視頻,請(qǐng)關(guān)注UP賬號(hào)。 https://www.bilibili.com

    2024年03月18日
    瀏覽(48)
  • 服務(wù)網(wǎng)關(guān)Gateway

    服務(wù)網(wǎng)關(guān)Gateway

    API 網(wǎng)關(guān)出現(xiàn)的原因是微服務(wù)架構(gòu)的出現(xiàn),不同的微服務(wù)一般會(huì)有不同的網(wǎng)絡(luò)地址,而外部客戶端可能需要調(diào)用多個(gè)服務(wù)的接口才能完成一個(gè)業(yè)務(wù)需求,如果讓客戶端直接與各個(gè)微服務(wù)通信,會(huì)有以下的問(wèn)題: 破壞了服務(wù)無(wú)狀態(tài)特點(diǎn)。 為了保證對(duì)外服務(wù)的安全性,我們需要實(shí)

    2024年02月06日
    瀏覽(18)
  • SpringCloud:Gateway服務(wù)網(wǎng)關(guān)

    SpringCloud:Gateway服務(wù)網(wǎng)關(guān)

    網(wǎng)關(guān)(Gateway)是將兩個(gè)使用不同協(xié)議的網(wǎng)絡(luò)段連接在一起的設(shè)備。 網(wǎng)關(guān)的作用就是對(duì)兩個(gè)網(wǎng)絡(luò)段中的使用不同傳輸協(xié)議的數(shù)據(jù)進(jìn)行互相的翻譯轉(zhuǎn)換。 創(chuàng)建服務(wù) 導(dǎo)入依賴 編寫啟動(dòng)類 添加配置 Route Predicate Factories :: Spring Cloud Gateway 對(duì)所有路徑都生效 全局過(guò)濾器的作用也是處理

    2024年02月01日
    瀏覽(27)
  • 微服務(wù)04-Gateway網(wǎng)關(guān)

    微服務(wù)04-Gateway網(wǎng)關(guān)

    身份認(rèn)證 :用戶能不能訪問(wèn) 服務(wù)路由 :用戶訪問(wèn)到那個(gè)服務(wù)中去 負(fù)載均衡 :一個(gè)服務(wù)可能有多個(gè)實(shí)例,甚至集群,負(fù)載均衡就是你的請(qǐng)求到哪一個(gè)實(shí)例上去 · 請(qǐng)求限流功能 :對(duì)請(qǐng)求進(jìn)行流量限制,對(duì)服務(wù)進(jìn)行請(qǐng)求限制 流程 : 1.首先咱們先將服務(wù)以及網(wǎng)關(guān)注冊(cè)到Nacos注冊(cè)

    2024年02月09日
    瀏覽(22)
  • Gateway的服務(wù)網(wǎng)關(guān)

    Gateway的服務(wù)網(wǎng)關(guān)

    Gateway網(wǎng)關(guān)是我們服務(wù)的守門神,所有微服務(wù)的統(tǒng)一入口。 網(wǎng)關(guān)的 核心功能特性 : 請(qǐng)求路由 權(quán)限控制 限流 架構(gòu)如下:? ? 創(chuàng)建gateway服務(wù),引入依賴 ? 創(chuàng)建application.yml文件,內(nèi)容如下 在gateway服務(wù)的application.yml文件中,添加下面的配置 ? ? ?

    2024年02月10日
    瀏覽(16)
  • 微服務(wù)網(wǎng)關(guān) Gateway

    微服務(wù)網(wǎng)關(guān) Gateway

    不同的微服務(wù)一般會(huì)有不同的網(wǎng)絡(luò)地址,客戶端在訪問(wèn)這些微服務(wù)時(shí)必須記住幾十甚至幾百個(gè)地址,這對(duì)于客戶端方來(lái)說(shuō)太復(fù)雜也難以維護(hù)。 如果讓客戶端直接與各個(gè)微服務(wù)通訊,可能會(huì)有很多問(wèn)題: 客戶端會(huì)請(qǐng)求多個(gè)不同的服務(wù),需要維護(hù)不同的請(qǐng)求地址,增加開發(fā)難度

    2024年02月08日
    瀏覽(23)
  • 【SpringCloud】Gateway服務(wù)網(wǎng)關(guān)

    【SpringCloud】Gateway服務(wù)網(wǎng)關(guān)

    Spring Cloud Gateway 是 Spring Cloud 的一個(gè)全新項(xiàng)目,該項(xiàng)目是基于 Spring 5.0,Spring Boot 2.0 和 Project Reactor 等響應(yīng)式編程和事件流技術(shù)開發(fā)的網(wǎng)關(guān),它旨在為微服務(wù)架構(gòu)提供一種簡(jiǎn)單有效的統(tǒng)一的 API 路由管理方式。 Gateway網(wǎng)關(guān)是我們服務(wù)的守門神,所有微服務(wù)的統(tǒng)一入口。 網(wǎng)關(guān)的

    2024年02月13日
    瀏覽(24)

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

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

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

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包