摘要
Spring Cloud Gateway 為 SpringBoot 應用提供了API網關支持,具有強大的智能路由與過濾器功能 。
Gateway 簡介
Gateway是在Spring生態系統之上構建的API網關服務,基於Spring 5,Spring Boot 2和 Project Reactor等技術。Gateway旨在提供一種簡單而有效的方式來對API進行路由,以及提供一些強大的過濾器功能, 例如:熔斷、限流、重試等。
Spring Cloud Gateway 具有如下特性:
-
基於Spring Framework 5, Project Reactor 和 Spring Boot 2.0 進行構建;
-
動態路由:能夠匹配任何請求屬性;
-
可以對路由指定 Predicate(斷言)和 Filter(過濾器);
-
集成Hystrix的斷路器功能;
-
集成 Spring Cloud 服務發現功能;
-
-
請求限流功能;
-
支持路徑重寫。
相關概念
-
Route(路由):路由是構建網關的基本模塊,它由ID,目標URI,一系列的斷言和過濾器組成,如果斷言為true則匹配該路由;
-
Predicate(斷言):指的是Java 8 的 Function Predicate。 輸入類型是Spring框架中的ServerWebExchange。 這使開發人員可以匹配HTTP請求中的所有內容,例如請求頭或請求參數。如果請求與斷言相匹配,則進行路由;
-
Filter(過濾器):指的是Spring框架中GatewayFilter的實例,使用過濾器,可以在請求被路由前后對請求進行修改。
創建 api-gateway模塊
pom.xml文件
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.reno</groupId> <artifactId>springcloud.api.gateway</artifactId> <version>1.0</version> <name>springcloud.api.gateway</name> <description>Demo project for Spring Boot</description> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Hoxton.RELEASE</spring-cloud.version> </properties> <dependencies> <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>--> <!--使用redis對路由限速時添加--> <!--<dependency>--> <!--<groupId>org.springframework.boot</groupId>--> <!--<artifactId>spring-boot-starter-data-redis-reactive</artifactId>--> <!--</dependency>--> <!--使用Hystrix做服務降級時添加--> <!--<dependency>--> <!--<groupId>org.springframework.cloud</groupId>--> <!--<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>--> <!--</dependency>--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
yml文件
#server:
# port: 39201
#service-url:
# demo-service: http://192.16.10.208:38201
#spring:
# cloud:
# gateway:
# routes:
# - id: path_route #路由的ID
# uri: ${service-url.demo-service}/demo/{id} #匹配后路由地址
# predicates: # 斷言,路徑相匹配的進行路由
# - Path=/demo/{id}
service-url:
demo-service: http://192.16.10.208:38201
server:
port: 39201
spring:
application:
name: api-gateway
cloud:
gateway:
discovery:
locator:
enabled: true #開啟從注冊中心動態創建路由的功能
lower-case-service-id: true #使用小寫服務名,默認是大寫
routes:
- id: path_route #路由的ID
uri: ${service-url.demo-service}/demo/{id} #匹配后路由地址
predicates: # 斷言,路徑相匹配的進行路由
- Path=/demo/{id}
eureka:
instance:
hostname: 192.16.10.208
#hostname: localhost
#設置是否將自己作為客戶端注冊到注冊中心(缺省true)
#這里為不需要,查看@EnableEurekaServer注解的源碼,會發現它間接用到了@EnableDiscoveryClient
instance-id: apiGateway-${spring.cloud.client.ipaddress}-${server.port}
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://192.16.10.208:38761/eureka/
registry-fetch-interval-seconds: 5
instance-info-replication-interval-seconds: 10
logging:
level:
org.springframework.cloud.gateway: debug
本文參考自MacroZheng鏈接 著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。