本文屬于b站圖靈課堂springcloud筆記系列。講的好還不要錢,值得推薦。
為什么需要API網關?
- 客戶端多次請求不同的微服務,會增加客戶端代碼和配置的復雜性,維護成本比價高
- 認證復雜,每個微服務可能存在不同的認證方式,客戶端去調用,要去適配不同的認證
- 存在跨域的請求,調用鏈有一定的相對復雜性(防火墻 / 瀏覽器不友好的協議)
- 難以重構,隨著項目的迭代,可能需要重新劃分微服務
為了解決上面的問題,引入了API網關
Spring Cloud Gateway 是什么?
This project provides an API Gateway built on top of the Spring Ecosystem, including: Spring 6, Spring Boot 3 and Project Reactor. Spring Cloud Gateway aims to provide a simple, yet effective way to route to APIs and provide cross cutting concerns to them such as: security, monitoring/metrics, and resiliency.
There are two distinct flavors of Spring Cloud Gateway:?Server?and?Proxy Exchange. Each flavor offers WebFlux and MVC compatibility.
The Server variant is a full-featured API gateway that can be standalone or embedded in a Spring Boot application.
The Proxy Exchange variant is exclusively for use in annotation based WebFlux or MVC applications and allows the use of a special?
ProxyExchange
?object as a parameter to a web handler method.
就是spring cloud 官方推出的第二代網關,是由 WebFlux + Netty + Reactor 實現的響應式的 API 網關。
特性:
Java 17
Spring Framework 6
Spring Boot 3
Dynamic routing
Route matching built into Spring Handler Mapping
Route matching on HTTP Request (Path, Method, Header, Host, etc…?)
Filters scoped to Matching Route
Filters can modify downstream HTTP Request and HTTP Response (Add/Remove Headers, Add/Remove Parameters, Rewrite Path, Set Path, Hystrix, etc…?)
API or configuration driven
Supports Spring Cloud?
DiscoveryClient
?for configuring Routes
其余詳細介紹請看GitHub - spring-cloud/spring-cloud-gateway: An API Gateway built on Spring Framework and Spring Boot providing routing and more.
?行業中通常把網關分為兩個大類:流量網關與業務網關,流量網關主要提供全局性的、與后端業務無關的策略配置,如NG,隨著應用架構模式從單體演進到現在的分布式微服務,業務網關也有了新的叫法 - 微服務網關,比如gateway.當然阿里也有開源的Higress實現了流量網關 + 微服務網關 + 安全網關高度集成的功能,這是一種發展趨勢。
微服務接入gateway
新建一個模塊,就是一個一個啟動類
核心配置,pom引入gateway依賴
<!-- gateway網關 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId></dependency><!--nacos-discovery 注冊中心依賴--><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency><!-- loadbalancer 負載均衡器依賴--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-loadbalancer</artifactId></dependency>
配置文件:application.yml 如下
server:port: 18888spring:application:name: tlmall-gatewaycloud:nacos:
# discovery:
# server-addr: tlmall-nacos-server:8848config:server-addr: tlmall-nacos-server:8848file-extension: yml #指定配置文件擴展名為yml# gateway:
# #設置路由:路由id、路由到微服務的uri、斷言
# routes:
# - id: order_route #路由ID,全局唯一,建議配置服務名
# uri: lb://tlmall-order #lb 整合負載均衡器loadbalancer
# predicates:
# - Path=/order/** # 斷言,路徑相匹配的進行路由
#
# - id: storage_route #路由ID,全局唯一,建議配置服務名
# uri: lb://tlmall-storage #lb 整合負載均衡器loadbalancer
# predicates:
# - Path=/storage/** # 斷言,路徑相匹配的進行路由
#
# - id: account_route #路由ID,全局唯一,建議配置服務名
# uri: lb://tlmall-account #lb 整合負載均衡器loadbalancer
# predicates:
# - Path=/account/** # 斷言,路徑相匹配的進行路由config:import:- optional:nacos:${spring.application.name}.yml- nacos:nacos-discovery.yml
注釋掉內容抽取到nacos,?Nacos配置中心創建一個dataId為tlmall-gateway.yml的配置,導入網關的配置.
測試:
啟動網關服務。
修改postman,改用網關地址下單:http://127.0.0.1:18888/order/create
修改頁面地址,order.html中訪問地址都替換為tmall-gateway:18888,測試下單是否成功
小結:gateway可以實現微服務中流量入口。