docs/Spring全家桶/SpringCloud/SpringCloudGateway.md
ܹУһϵͳɶɣЩܲڲͬͬͬ¡£ͻˣֻߵȣҪֱЩҪ֪ǾĵַϢ IP ַ˿ںŵȡ
ֿͻֱķʽ⣺
ǿͨ API Щ⣬ʲô API ء
API һڿͻ˺֮ķǿ API дһЩҵܵȨ֤ء桢·ɵȡ
API ؾϵͳһϵͳΨһڡͻ˻Ƚ͵ API أȻ API ظıʶϢתʵ
ͼ1ַʷʽԱ
ڷڶࡢӶȽϸߡģȽϴϵͳ˵ʹ API ؾºô
API ʵַҪ 5 ֣
ڣǾͶ Spring Cloud Gateway ϸܡ
Spring Cloud Gateway Spring Cloud Ŷӻ Spring 5.0Spring Boot 2.0 Project Reactor ȼĸ API
Spring Cloud Gateway ּṩһּЧ; APIΪṩйע㣬磺ȫԣ/ָ͵ԡ```
Spring Cloud Gateway ǻ WebFlux ʵֵģ WebFlux ܵײʹ˸ܵ Reactor ģʽͨſ Netty
Spring Cloud GateWay ҪĹܾ·תڶתʱҪ漰ĸ±
| ĸ | |
|---|---|
| Route·ɣ | ģ顣һ IDһĿ URIһԣPredicateһFilterɡ |
| Predicateԣ | ·תжǿͨ Predicate ```HTTP ƥ䣬ʽ·ͷȣƥɹתӦķ |
| Filter | ǿʹغģʹĵӦٴ |
ע⣺ Route Predicate ͬʱ
Spring Cloud Gateway ԣ
Spring Cloud Gateway ͼ
ͼ2Spring Cloud Gateway
Spring Cloud Gateway ˵£
֮ܶͻ˷͵ Spring Cloud Gateway Ҫͨһƥܶλķڵ㡣ڽתдĹǰpre postǻԶӦһЩϸơ
Predicate ·ɵƥ Filter ǶӦоϸƵĹߡԪأټĿ URIͿʵһ·ˡ
Spring Cloud Gateway ͨ```Predicate ʵ Route ·ɵƥ˵Predicate ·תжֻ Predicate ŻᱻתָķϽд
ʹ Predicate Ҫע 3 㣺
ͼ3Predicate ƥ
Predicate ±ת URI Ϊ http://localhost:8001
| ʾ | ˵ | |
|---|---|---|
| Path | - Path=/dept/list/**``` | · /dept/list/** ƥʱܱת```http://localhost:8001 ϡ |
| Before | - Before=2021-10-20T11:47:34.255+08:00[Asia/Shanghai] | 2021 10 20 11 ʱ 47 34.255 ֮ǰŻᱻתhttp://localhost:8001 ϡ |
| After | - After=2021-10-20T11:47:34.255+08:00[Asia/Shanghai] | 2021 10 20 11 ʱ 47 34.255 ֮Żᱻת```http://localhost:8001 ϡ |
| Between | - Between=2021-10-20T15:18:33.226+08:00[Asia/Shanghai],2021-10-20T15:23:33.226+08:00[Asia/Shanghai] | 2021 10 20 15 ʱ 18 33.226 2021 10 20 15 ʱ 23 33.226 ֮Żᱻת```http://localhost:8001 ϡ |
| Cookie | - Cookie=name,c.biancheng.net | ЯCookie Cookie Ϊname=c.biancheng.net Żᱻת http://localhost:8001 ϡ |
| Header | - Header=X-Request-Id,\d+ | ͷЯ X-Request-Id ֵΪŻᱻת http://localhost:8001 ϡ |
| Method | - Method=GET | ֻ GET Żᱻת http://localhost:8001 ϡ |
Ǿͨһʵʾ Predicate ʹõġ
1. ڸspring-cloud-demo2 ´һΪmicro-service-cloud-gateway-9527 Spring Boot ģ飬 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>
<artifactId>spring-cloud-demo2</artifactId>
<groupId>net.biancheng.c</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>net.biancheng.c</groupId>
<artifactId>micro-service-cloud-gateway-9527</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>micro-service-cloud-gateway-9527</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--رע⣺ gateway طв spring-boot-starter-web ᱨ-->
<!-- Spring cloud gateway -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<!--Eureka ͻ-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2. ```micro-service-cloud-gateway-9527 ·/resources Ŀ¼£½һļ application.yml¡
server:
port: 9527 #˿ں
spring:
application:
name: microServiceCloudGateway
cloud:
gateway: #·
routes:
# micro-service-cloud-provider-dept-8001 ṩķ¶ͻˣֻͻ˱¶ API صĵַ 9527
- id: provider_dept_list_routh #· id,ûй̶ΨһӦ
uri: http://localhost:8001 #ƥṩ·ɵַ
predicates:
#Ƕѡȫ
- Path=/dept/list/** #ԣ·ƥ ע⣺Path P Ϊд
- Method=GET #ֻʱ GET ʱܷ
eureka:
instance:
instance-id: micro-service-cloud-gateway-9527
hostname: micro-service-cloud-gateway
client:
fetch-registry: true
register-with-eureka: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
У spring.cloud.gateway.routes ʹ predicates ԣ
- Path=/dept/list/**
- Method=GET
ֻеⲿͻˣ͵ micro-service-cloud-gateway-9527 HTTP ͬʱеĶʱŻᱻתָķУ http://localhost:80013\. ```micro-service-cloud-gateway-9527 ϣʹ @EnableEurekaClient ע Eureka ͻ˹ܣ¡
package net.biancheng.c;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class MicroServiceCloudGateway9527Application {
public static void main(String[] args) {
SpringApplication.run(MicroServiceCloudGateway9527Application.class, args);
}
}
4. Eureka עģȺmicro-service-cloud-provider-dept-8001 Լ```micro-service-cloud-gateway-9527ʹʡhttp://localhost:9527/dept/listͼ
ͼ4Spring Cloud Gateway ·ת
Ĭ£Spring Cloud Gateway ݷעģ Eureka ServerάķбԷspring.application.nameΪ·̬·ɽתӶʵֶ̬·ɹܡ
ǿļУ Route uri ַΪʽ
lb://service-name
˵£
Ǿͨһʵչʾ Spring Cloud Gateway ʵֶ̬·ɵġ
1. ```micro-service-cloud-gateway-9527 application.yml ãʹעе̬·ɽת¡
server:
port: 9527 #˿ں
spring:
application:
name: microServiceCloudGateway #עעķ
cloud:
gateway: #·
discovery:
locator:
enabled: true #ĬֵΪ trueĬϿעĶ̬·ɵĹܣ·
routes:
# micro-service-cloud-provider-dept-8001 ṩķ¶ͻˣֻͻ˱¶ API صĵַ 9527
- id: provider_dept_list_routh #· id,ûй̶ΨһӦ
uri: lb://MICROSERVICECLOUDPROVIDERDEPT #̬·ɣʹ÷ľ˿ http://eureka7001.com:9527/dept/list
predicates:
#Ƕѡȫ
- Path=/dept/list/** #ԣ·ƥ ע⣺Path P Ϊд
- Method=GET #ֻʱ GET ʱܷ
eureka:
instance:
instance-id: micro-service-cloud-gateway-9527
hostname: micro-service-cloud-gateway
client:
fetch-registry: true
register-with-eureka: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
2. Eureka עģȺṩȺmicro-service-cloud-provider-dept-8001/8002/8003Լ```micro-service-cloud-gateway-9527
3. зʡhttp://localhost:9527/dept/listͼ
ͼ5Spring Cloud ʵֶ̬·
ͨ£ڰȫĿǣṩķһУû½״̬У顢ǩУȡ
ܹУϵͳɶɣЩҪЩУʱǾͿԽЩУд Spring Cloud Gateway Filter С
Spring Cloud Gateway ṩ͵ĹԶӦоϸơ
| ˵ | |
|---|---|
| Pre | ֹת֮ǰԶغģУ顢ȨУ顢ء־ԼЭתȲ |
| Post | ֹӦԶӦغٴӦݻӦͷ־صȡ |
÷Χ֣Spring Cloud gateway Filter ԷΪ 2 ࣺ
GatewayFilter Spring Cloud Gateway ṩһӦڵһ·ϵĹԶԵ·ɻһ·ϴʹӦأʵһЩҵصĹܣ½״̬У顢ǩУ顢ȨУ顢־صȡ
GatewayFilter ļ application.ymlед Predicate ƣʽ¡
spring:
cloud:
gateway:
routes:
- id: xxxx
uri: xxxx
predicates:
- Path=xxxx
filters:
- AddRequestParameter=X-Request-Id,1024 #ƥͷһͷΪ X-Request-Id ֵΪ 1024
- PrefixPath=/dept #·ǰ /dept
Spring Cloud Gateway ˶ 31 GatewayFilter±о˼ֳõعʹʾ
| ·ɹ | ʹʾ | ||
|---|---|---|---|
| AddRequestHeader | ```شһָͷ | nameҪӵͷ keyvalueҪӵͷ value | - AddRequestHeader=my-request-header,1024 |
| AddRequestParameter | شһָ | nameҪӵ keyvalueҪӵ value | - AddRequestParameter=my-request-param,c.biancheng.net |
| AddResponseHeader | ӦӦһָӦͷ | nameҪӵӦͷ keyvalueҪӵӦͷ value | - AddResponseHeader=my-response-header,c.biancheng.net |
| PrefixPath | ش·һָǰ | ```prefixҪӵ·ǰ | - PrefixPath=/consumer |
| PreserveHostHeader | תʱֿͻ˵ Host Ϣ䣬ȻݵṩС | - PreserveHostHeader | |
| RemoveRequestHeader | ƳͷָIJ | nameҪƳͷ key | - RemoveRequestHeader=my-request-header |
| RemoveResponseHeader | ƳӦͷָIJ | nameҪƳӦͷ | - RemoveResponseHeader=my-response-header |
| RemoveRequestParameter | Ƴָ | nameҪƳ | - RemoveRequestParameter=my-request-param |
| RequestSize | ĴСʱ᷵ 413 Payload Too Large | maxSizeĴС | - name: RequestSize`````````args:```````````````maxSize: 5000000 |
ͨһʵʾ GatewayFilter ã¡
1. ```micro-service-cloud-gateway-9527 application.yml һ̬·ɣ¡
spring:
cloud:
gateway:
routes:
- id: xxxx
uri: xxxx
predicates:
- Path=xxxx
filters:
- AddRequestParameter=X-Request-Id,1024 #ƥͷһͷΪ X-Request-Id ֵΪ 1024
- PrefixPath=/dept #·ǰ /dept
2. ```micro-service-cloud-gateway-9527ʹʡhttp://eureka7001.com:9527/get/1ͼ
ͼ6·ɹʾ
GlobalFilter һе·ϵȫֹͨǿʵһЩͳһҵܣȨ֤IP Ƶȡij·ƥʱôе GlobalFilter ·õ GatewayFilter ϳһ
Spring Cloud Gateway Ϊṩ˶Ĭϵ GlobalFilterת·ɡؾصȫֹʵʵĿУͨǶԶһЩԼ GlobalFilter ȫֹҵֱʹ Spring Cloud``` Config ṩЩĬϵ GlobalFilter
Ĭϵȫֹϸݣο```Spring Cloud
Ǿͨһʵʾ£Զ GlobalFilter ȫֹ
1. net.biancheng.c.filter £½һΪMyGlobalFilter ȫֹ࣬¡
server:
port: 9527 #˿ں
spring:
application:
name: microServiceCloudGateway #עעķ
cloud:
gateway: #·
discovery:
locator:
enabled: true #ĬֵΪ trueĬϿעĶ̬·ɵĹܣ·
routes:
# micro-service-cloud-provider-dept-8001 ṩķ¶ͻˣֻͻ˱¶ API صĵַ 9527
- id: provider_dept_list_routh #· id,ûй̶ΨһӦ
uri: lb://MICROSERVICECLOUDPROVIDERDEPT #̬·ɣʹ÷ľ˿ http://eureka7001.com:9527/dept/list
predicates:
#Ƕѡȫ
- Path=/dept/list/** #ԣ·ƥ ע⣺Path P Ϊд
- Method=GET #ֻʱ GET ʱܷ
eureka:
instance:
instance-id: micro-service-cloud-gateway-9527
hostname: micro-service-cloud-gateway
client:
fetch-registry: true
register-with-eureka: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
2. ```micro-service-cloud-gateway-9527ʹʡhttp://eureka7001.com:9527/dept/listǻַᷢʱ 406 ̨¡
2021-10-21 16:25:39.451 INFO 19116 --- [ctor-http-nio-4] net.biancheng.c.filter.MyGlobalFilter : uname Ϊ null
3. ʹʡhttp://eureka7001.com:9527/dept/list?uname=123,ͼ
ͼ7Զȫع
https://lijunyi.xyz/docs/SpringCloud/SpringCloud.html#_2-2-x-%E5%88%86%E6%94%AF https://mp.weixin.qq.com/s/2jeovmj77O9Ux96v3A0NtA https://juejin.cn/post/6931922457741770760 https://github.com/D2C-Cai/herring http://c.biancheng.net/springcloud https://github.com/macrozheng/springcloud-learning