源码分析-SpringCloudGateway源码阅读-网关自定义事件

源码解析-SpringCloudGateway
01 源码分析-SpringCloudGateway源码阅读准备
02 源码分析-SpringCloudGateway源码阅读-网关监控端点
03 源码分析-SpringCloudGateway源码阅读-网关配置类
04 源码分析-SpringCloudGateway源码阅读-服务发现
05 源码分析-SpringCloudGateway源码阅读-网关自定义事件
06 源码分析-SpringCloudGateway源码阅读-过滤器包类总览
07 源码分析-SpringCloudGateway源码阅读-过滤器-全局过滤器源码(一)
08 源码分析-SpringCloudGateway源码阅读-过滤器-全局过滤器源码(二)
09 源码分析-SpringCloudGateway源码阅读-过滤器-全局过滤器源码(三)
10 源码分析-SpringCloudGateway源码阅读-过滤器-路由过滤器源码解读
11 源码分析-SpringCloudGateway源码阅读-处理器源码

一 网关自定义事件相关类

主要有六个事件类及其说明如下:

1
2
3
4
5
6
7
8
9
10
11
12
.
└── org
└── springframework
└── cloud
└── gateway
└── event
   ├── EnableBodyCachingEvent.java 缓存请求body的事件
   ├── FilterArgsEvent.java 过滤器参数事件
   ├── PredicateArgsEvent.java 路由谓词参数事件
   ├── RefreshRoutesEvent.java 刷新路由事件
   ├── RefreshRoutesResultEvent.java 路由刷新结果事件
   └── WeightDefinedEvent.java 权重定义事件

二 每个类的具体说明

EnableBodyCachingEvent

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*
* Copyright 2013-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.cloud.gateway.event;

import org.springframework.context.ApplicationEvent;

/**
* 缓存请求body的事件
*
* 当使用RetryGatewayFilterFactory路由过滤器时,会在该过滤器发送一个缓存请求body的事件
* 然后在全局过滤器AdaptCachedBodyGlobalFilter中会监听该事件,监听到之后会把该路由的请求body缓存起来,便于后面重试使用
*/
public class EnableBodyCachingEvent extends ApplicationEvent {

private final String routeId;

public EnableBodyCachingEvent(Object source, String routeId) {
super(source);
this.routeId = routeId;
}

public String getRouteId() {
return this.routeId;
}

}

FilterArgsEvent

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
* Copyright 2013-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.cloud.gateway.event;

import java.util.Map;

import org.springframework.context.ApplicationEvent;

/**
* 过滤器参数事件
* 由路由定义定位器RouteDefinitionRouteLocator中加载路由过滤器的时候发送事件(主要是限流相关的)
* 在限流抽象类AbstractRateLimiter中监听事件并处理事件(非限流相关参数不处理直接返回)
*/
public class FilterArgsEvent extends ApplicationEvent {

private final Map<String, Object> args;

private String routeId;

public FilterArgsEvent(Object source, String routeId, Map<String, Object> args) {
super(source);
this.routeId = routeId;
this.args = args;
}

public String getRouteId() {
return routeId;
}

public Map<String, Object> getArgs() {
return args;
}

}

PredicateArgsEvent

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
* Copyright 2013-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.cloud.gateway.event;

import java.util.Map;

import org.springframework.context.ApplicationEvent;

/**
* 路由谓词参数事件
*
* 由路由定义定位器RouteDefinitionRouteLocator中转换路由谓词的时候发送事件(主要是权重相关的)
* 在权重计算web过滤器WeightCalculatorWebFilter中监听事件并处理事件(非权重相关参数不处理直接返回)
*
*/
public class PredicateArgsEvent extends ApplicationEvent {

private final Map<String, Object> args;

private String routeId;

public PredicateArgsEvent(Object source, String routeId, Map<String, Object> args) {
super(source);
this.routeId = routeId;
this.args = args;
}

public String getRouteId() {
return routeId;
}

public Map<String, Object> getArgs() {
return args;
}

}

RefreshRoutesEvent

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*
* Copyright 2013-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.cloud.gateway.event;

import org.springframework.context.ApplicationEvent;

/**
* 刷新路由事件
*
* 可由网关端点AbstractGatewayControllerEndpoint刷新接口端点 触发
* 或者由RouteRefreshListener重置时触发
*
* 缓存路由定位CachingRouteLocator中订阅事件并处理刷新缓存
*
*
* @author Spencer Gibb
*/
public class RefreshRoutesEvent extends ApplicationEvent {

/**
* Create a new ApplicationEvent.
* @param source the object on which the event initially occurred (never {@code null})
*/
public RefreshRoutesEvent(Object source) {
super(source);
}

}

RefreshRoutesResultEvent

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
* Copyright 2013-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.cloud.gateway.event;

import org.springframework.context.ApplicationEvent;

/**
* 路由刷新结果事件
* 由缓存路由定位CachingRouteLocator处理刷新路由事件时触发的结果事件
*
* @author alvin
*/
public class RefreshRoutesResultEvent extends ApplicationEvent {

private Throwable throwable;

public RefreshRoutesResultEvent(Object source, Throwable throwable) {
super(source);
this.throwable = throwable;
}

public RefreshRoutesResultEvent(Object source) {
super(source);
}

public Throwable getThrowable() {
return throwable;
}

public boolean isSuccess() {
return throwable == null;
}

}

WeightDefinedEvent

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*
* Copyright 2013-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.cloud.gateway.event;

import org.springframework.cloud.gateway.support.WeightConfig;
import org.springframework.context.ApplicationEvent;

/**
* 权重定义事件
* 由权重路由谓词工厂WeightRoutePredicateFactory触发发送事件
* 由权重计算Web过滤器 WeightCalculatorWebFilter 订阅处理
*/
public class WeightDefinedEvent extends ApplicationEvent {

private final WeightConfig weightConfig;

public WeightDefinedEvent(Object source, WeightConfig weightConfig) {
super(source);
this.weightConfig = weightConfig;
}

public WeightConfig getWeightConfig() {
return weightConfig;
}

}