亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

SpringCloud超詳細(xì)講解微服務(wù)網(wǎng)關(guān)Gateway

 更新時(shí)間:2022年07月16日 11:18:57   作者:_時(shí)光煮雨  
這篇文章主要介紹了SpringCloud Gateway微服務(wù)網(wǎng)關(guān),負(fù)載均衡,熔斷和限流,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

前言

上一篇:微服務(wù)網(wǎng)關(guān)Zuul

上文中,我們介紹了微服務(wù)網(wǎng)關(guān)Zuul,Zuul 是 Netflix 公司開源的產(chǎn)品,被稱為第一代網(wǎng)關(guān),也是 Spring Cloud 前幾個(gè)版本默認(rèn)使用的一款提供動(dòng)態(tài)路由微服務(wù)網(wǎng)關(guān)組件,但是隨著 Netflix 公司一系列的停更事件,在最新的 Spring Cloud Greenwich 版本中已經(jīng)不建議采用 Zuul 技術(shù),官方建議使用 Spring Cloud Gateway 作為默認(rèn)的網(wǎng)關(guān)技術(shù)。 Spring Cloud Gateway作為第二代網(wǎng)關(guān)技術(shù),比Zull更強(qiáng),官方會(huì)一直維護(hù)更新下去。

所以本文需要再介紹一下Gateway。

微服務(wù)網(wǎng)關(guān)GateWay介紹

Spring Cloud Gateway 是 Spring 體系內(nèi)的一個(gè)全新項(xiàng)目,該項(xiàng)目是基于 Spring 5.0,Spring Boot 2.0 和 Project Reactor 等技術(shù)開發(fā),它旨在為微服務(wù)架構(gòu)提供一種簡單有效的統(tǒng)一的 API 路由管理方式。

Spring Cloud Gateway 作為 Spring Cloud 生態(tài)系統(tǒng)中的網(wǎng)關(guān),目標(biāo)是替代 Netflix Zuul,其不僅提供統(tǒng)一的路由方式,并且基于 Filter 鏈的方式提供了網(wǎng)關(guān)基本的功能,例如:安全、監(jiān)控/指標(biāo)和限流。

GateWay特性介紹

  • 基于 Spring Framework 5,Project Reactor 和 Spring Boot 2.0
  • 動(dòng)態(tài)路由
  • Predicates 和 Filters 作用于特定路由
  • 集成 Hystrix 斷路器
  • 集成 Spring Cloud DiscoveryClient
  • 易于編寫的 Predicates 和 Filters
  • 限流
  • 路徑重寫

Gateway 中的相關(guān)術(shù)語

  • Route(路由):這是網(wǎng)關(guān)的基本構(gòu)建塊。它由一個(gè) ID,一個(gè)目標(biāo) URI,一組斷言和一組過濾器定義。如果斷言為真,則路由匹配。
  • Predicate(斷言):這是一個(gè) Java 8 的 Predicate。輸入類型是一個(gè) ServerWebExchange。我們可以使用它來匹配來自 HTTP 請求的任何內(nèi)容,例如 headers 或參數(shù)。
  • Filter(過濾器):這是org.springframework.cloud.gateway.filter.GatewayFilter的實(shí)例,我們可以使用它修改請求和響應(yīng)。

Gateway實(shí)戰(zhàn)

上文中,我們啟動(dòng)了注冊中心registry,dms服務(wù),和app服務(wù),以及zuul服務(wù),本文我們將創(chuàng)建gateway服務(wù)以替換zuul:

1、創(chuàng)建項(xiàng)目gateway

創(chuàng)建子模塊gateway ,pom.xml引入eureka-client 和gateway 的依賴

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

2、創(chuàng)建啟動(dòng)類

@EnableEurekaClient
@SpringBootApplication
public class GateWayApplication {
    public static void main(String[] args) {
        SpringApplication.run(GateWayApplication.class, args);
    }
}

啟動(dòng)類上增加@EnableEurekaClient以及@SpringBootApplication注解。

3、新增配置文件

新增配置文件application.yml

server:
  port: 8004
spring:
  application:
    name: gateway
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true
          lower-case-service-id: true
      routes:
        - id: eureka-client-app-1
          uri: lb://app
          predicates:
            - Path=/**
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8001/eureka/

spring.cloud.gateway.routes路由參數(shù)配置說明:

  • id:我們自定義的路由 ID。
  • uri:需要轉(zhuǎn)發(fā)的目標(biāo)服務(wù)地址。
  • predicates:路由條件。
  • filters:過濾規(guī)則,本示例暫時(shí)沒用。

4、編程方式實(shí)現(xiàn)路由

上面路由規(guī)則我們也可以使用編程方式來實(shí)現(xiàn),在啟動(dòng)類中增加如下代碼,是等效的:

@Configuration
public class GatewayConfig {
    @Bean
    public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
        RouteLocatorBuilder.Builder routes = builder.routes();
        routes.route("eureka-client-app-1",r -> r.path("/**")
               .uri("lb://app"))
                .build();
        return routes.build();
    }
}

5、啟動(dòng)驗(yàn)證

訪問Gateway服務(wù)的地址:http://localhost:8004/app/index,效果如下:

總結(jié)

本文介紹了如何使用 Spring Cloud Gateway。Gateway 的特性以及兩種實(shí)現(xiàn)方式:一種是通過配置文件的方式來實(shí)現(xiàn),一種是通過編碼的方式來實(shí)現(xiàn),推薦使用配置文件的方式來使用,便于后期修改維護(hù)。

到此這篇關(guān)于SpringCloud超詳細(xì)講解微服務(wù)網(wǎng)關(guān)Gateway的文章就介紹到這了,更多相關(guān)SpringCloud Gateway內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論