Zuul支援
參考 Sentinel 網關限流(同步并阻塞)
若想跟 Sentinel Starter 配合使用,需要加上
spring-cloud-alibaba-sentinel-gateway
依賴,同時需要添加
spring-cloud-starter-netflix-zuul
依賴來讓
spring-cloud-alibaba-sentinel-gateway
子產品裡的 Zuul 自動化配置類生效:
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
什麼是 Spring Cloud Gateway
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 鍊的方式提供了網關基本的功能,例如:安全,監控/埋點,和限流等。
Spring Cloud Gateway 功能特征
- 基于 Spring Framework 5,Project Reactor 和 Spring Boot 2.0
- 動态路由
- Predicates 和 Filters 作用于特定路由
- 內建 Hystrix 斷路器
- 內建 Spring Cloud DiscoveryClient
- 易于編寫的 Predicates 和 Filters
- 限流
- 路徑重寫
Spring Cloud Gateway 工程流程
用戶端向 Spring Cloud Gateway 送出請求。然後在 Gateway Handler Mapping 中找到與請求相比對的路由,将其發送到 Gateway Web Handler。Handler 再通過指定的過濾器鍊來将請求發送到我們實際的服務執行業務邏輯,然後傳回。
過濾器之間用虛線分開是因為過濾器可能會在發送代理請求之前(
pre
)或之後(
post
)執行業務邏輯。
POM
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.wj</groupId>
<artifactId>hello-spring-cloud-alibaba-dependencies</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../hello-spring-cloud-alibaba-dependencies/pom.xml</relativePath>
</parent>
<artifactId>hello-spring-cloud-gateway</artifactId>
<packaging>jar</packaging>
<name>hello-spring-cloud-gateway</name>
<dependencies>
<!-- Spring Boot Begin -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Spring Boot End -->
<!-- Spring Cloud Begin -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!-- Spring Cloud End -->
<!-- Commons Begin -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<!-- Commons Begin -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.wj.hello.gateway.GatewayApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
主要增加了
org.springframework.cloud:spring-cloud-starter-gateway
依賴。
特别注意
- Spring Cloud Gateway 不使用 Web 作為伺服器,而是 使用 WebFlux 作為伺服器,Gateway 項目已經依賴了 starter-webflux,是以這裡 千萬不要依賴 starter-web
- 由于過濾器等功能依然需要 Servlet 支援,故這裡還需要依賴 javax.servlet:javax.servlet-api
Application
package com.wj.hello.spring.cloud.gateway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class,args);
}
}
application.yml
spring:
application:
# 應用名稱
name: spring-gateway
cloud:
# 使用 Naoos 作為服務注冊發現
nacos:
discovery:
server-addr: 192.168.198.131:8848
# 使用 Sentinel 作為熔斷器
sentinel:
transport:
port: 8720
dashboard: 192.168.198.131:8858
# 路由網關配置
gateway:
# 設定與服務注冊發現元件結合,這樣可以采用服務名的路由政策
discovery:
locator:
enabled: true
# 配置路由規則
routes:
# 采用自定義路由 ID(有固定用法,不同的 id 有不同的功能,詳見:https://cloud.spring.io/spring-cloud-gateway/2.0.x/single/spring-cloud-gateway.html#gateway-route-filters)
- id: NACOS-CONSUMER
# 采用 LoadBalanceClient 方式請求,以 lb:// 開頭,後面的是注冊在 Nacos 上的服務名
uri: lb://nacos-consumer
# Predicate 翻譯過來是“謂詞”的意思,必須,主要作用是比對使用者的請求,有很多種用法
predicates:
# Method 方法謂詞,這裡是比對 GET 和 POST 請求
- Method=GET,POST
- id: NACOS-CONSUMER-FEIGN
uri: lb://nacos-consumer-feign
predicates:
- Method=GET,POST
server:
port: 9000
# 目前無效
feign:
sentinel:
enabled: true
# 目前無效
management:
endpoints:
web:
exposure:
include: "*"
# 配置日志級别,方别調試
logging:
level:
org.springframework.cloud.gateway: debug
注意:請仔細閱讀注釋
測試通路
依次運作 Nacos 服務、NacosProviderApplication、NacosConsumerApplication、NacosConsumerFeignApplication、GatewayApplication
打開浏覽器通路:
http://localhost:9000/nacos-consumer/echo/app/name
浏覽器顯示
Hello Nacos Discovery nacos-consumer i am from port 8081
打開浏覽器通路:
http://localhost:9000/nacos-consumer-feign/echo/hi
浏覽器顯示
Hello Nacos Discovery Hi Feign i am from port 8081
注意:請求方式是 http://路由網關IP:路由網關Port/服務名/
至此說明 Spring Cloud Gateway 的路由功能配置成功
結構導圖
GateWay去Nacos根據服務名找到對應的IP,然後根據位址去通路
作者:直沖車
連結:https://www.jianshu.com/p/208db28bb78c
來源:簡書
著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。