天天看點

Springboot 整合 SpringCloud元件-Gateway 網關服務 (四)

這篇我将介紹的是網關服務,那麼從标題已經知道我們整合的元件時gateway;

也許有人說,為啥不用zuul,這個元件也可以用于做網關。 至于這兩元件的性能比較與差別,我們來看一個表格簡單了解下:

Gateway與Zuul

gateway zuul
基本介紹 Spring Cloud Gateway是Spring官方基于Spring 5.0,Spring Boot 2.0和Project Reactor等技術開發的網關,Spring Cloud Gateway旨在為微服務架構提供一種簡單而有效的統一的API路由管理方式。Spring Cloud Gateway作為Spring Cloud生态系中的網關,目标是替代Netflix ZUUL,其不僅提供統一的路由方式,并且基于Filter鍊的方式提供了網關基本的功能,例如:安全,監控/埋點,和限流等。 Zuul1 是基于 Servlet 架構建構,如圖所示,采用的是阻塞和多線程方式,即一個線程處理一次連接配接請求,這種方式在内部延遲嚴重、裝置故障較多情況下會引起存活的連接配接增多和線程增加的情況發生。
性能 WebFlux 子產品的名稱是 spring-webflux,名稱中的 Flux 來源于 Reactor 中的類 Flux。Spring webflux 有一個全新的非堵塞的函數式 Reactive Web 架構,可以用來建構異步的、非堵塞的、事件驅動的服務,在伸縮性方面表現非常好。使用非阻塞API。 Websockets得到支援,并且由于它與Spring緊密內建,是以将會是一個更好的 開發 體驗。  本文的Zuul,指的是Zuul 1.x,是一個基于阻塞io的API Gateway。Zuul已經釋出了Zuul 2.x,基于Netty,也是非阻塞的,支援長連接配接,但Spring Cloud暫時還沒有整合計劃。
源碼維護組織

​spring-cloud-Gateway​

​​是spring旗下​

​spring-cloud​

​​的一個子項目。還有一種說法是因為​

​zuul2​

​​連續跳票和​

​zuul1​

​​的性能表現不是很理想,是以催生了spring孵化​

​Gateway​

​項目。

​zuul​

​​則是​

​netflix​

​​公司的項目,隻是spring将​

​zuul​

​內建在spring-cloud中使用而已。關鍵目前spring不打算內建zuul2.x。
版本 springboot2.0 springboot1.x

接下來我們開始整合gateway,實作該元件的基礎使用:

建立一個springboot項目,起名 gateway:

Springboot 整合 SpringCloud元件-Gateway 網關服務 (四)

(同樣,我們這裡選用的springcloud版本是:Finchley.RELEASE) 

pom.xml裡核心的依賴包為:

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

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>      

實作基礎功能使用,我們可以直接在application.yml進行配置:

server:
  port: 8081

spring:
  application:
    name: test-gateway-service
  cloud:
      gateway:
        discovery:
          locator:
            enabled: false
#開啟小寫驗證,預設feign根據服務名查找都是用的全大寫
            lowerCaseServiceId: true
        routes:
        - id: client-test
          uri: lb://CLIENT-TEST
          predicates:
            - Path=/testclient/**
          filters:
            - StripPrefix=1
        - id: service-feign
          uri: lb://FEIGN
          predicates:
            - Path=/service-feign/**
          filters:
            - StripPrefix=1
eureka:
  instance:
    preferIpAddress: true
    instance-id: ${spring.cloud.client.ip-address}:${server.port}
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/      

啟動類也同樣加上 @EnableEurekaClient。

可以看到我們簡單配置了2個路由:

路由id為client-test的,

隻要是通路路徑帶有 /testclient的, 都會根據從Eureka注冊中心擷取的服務資訊去尋找儲服務名為CLIENT-TEST的服務執行個體,進行接口轉發調用。

路由id為service-feign的,

同理,隻要是通路路徑帶有 /service-feign的, 都會根據從Eureka注冊中心擷取的服務資訊去尋找儲服務名為FEIGN的服務執行個體,進行接口轉發調用。

那麼我們先把網關gateway服務跑起來,逐一調用接口看看:

Springboot 整合 SpringCloud元件-Gateway 網關服務 (四)

 通路:​​http://localhost:8081/testclient/haveatry?name=JCccc​​

Springboot 整合 SpringCloud元件-Gateway 網關服務 (四)

繼續閱讀