1、基于日期时间的断言工厂
基于日期时间的断言工厂主要是通过日期时间对请求进行断言,判断请求时间是否符合配置的时间,实现类主要有三种,分别如下:
AfterRoutePredicateFactory
:接收一个日期参数判断请求时间是否在配置时间之后;
BeforeRoutePredicateFactory
:接收一个日期参数,判断请求日期是否在指定日期之前;
BetweenRoutePredicateFactory
:接收两个日期参数,判断请求日期是否在这两个指定日期之间;
2、AfterRoutePredicateFactory
org.springframework.cloud.gateway.handler.predicate.AfterRoutePredicateFactory
,用于匹配请求时间在指定时间之后的请求,其UML关系图如下(其他实现类的UML类似):

其源码如下:
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
|
public class AfterRoutePredicateFactory
extends AbstractRoutePredicateFactory<AfterRoutePredicateFactory.Config> {
public static final String DATETIME_KEY = "datetime";
public AfterRoutePredicateFactory() {
super(Config.class);
}
@Override
public List<String> shortcutFieldOrder() {
return Collections.singletonList(DATETIME_KEY);
}
@Override
public Predicate<ServerWebExchange> apply(Config config) {
ZonedDateTime datetime = config.getDatetime();
return exchange -> {
};
}
public static class Config { @NotNull
private ZonedDateTime datetime; public ZonedDateTime getDatetime() { return datetime;
} public void setDatetime(ZonedDateTime datetime) { this.datetime = datetime;
}
}
}
|
2.1 yml配置示例
application.yml
1
2
3
4
5
6
7
8
|
spring:
cloud: gateway: routes: - id: after_route uri: http://example.org predicates: - After=2019-01-20T17:42:47.789-07:00[America/Denver]
|
3、BeforeRoutePredicateFactory
org.springframework.cloud.gateway.handler.predicate.BeforeRoutePredicateFactory
,用于匹配请求时间在指定时间之前的请求,其源码如下:
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
|
public class BeforeRoutePredicateFactory
extends AbstractRoutePredicateFactory<BeforeRoutePredicateFactory.Config> {
public static final String DATETIME_KEY = "datetime";
public BeforeRoutePredicateFactory() {
super(Config.class);
}
@Override
public List<String> shortcutFieldOrder() {
return Collections.singletonList(DATETIME_KEY);
}
@Override
public Predicate<ServerWebExchange> apply(Config config) {
ZonedDateTime datetime = config.getDatetime();
return exchange -> {
};
}
public static class Config { private ZonedDateTime datetime; public ZonedDateTime getDatetime() { return datetime;
} public void setDatetime(ZonedDateTime datetime) { this.datetime = datetime;
}
}
}
|
3.1 yml配置示例
application.yml
1
2
3
4
5
6
7
8
|
spring:
cloud: gateway: routes: - id: before_route uri: http://example.org predicates: - Before=2019-01-20T17:42:47.789-07:00[America/Denver]
|
4、BetweenRoutePredicateFactory
org.springframework.cloud.gateway.handler.predicate.BetweenRoutePredicateFactory
,用于匹配请求时间在指定时间之间的请求,其源码如下:
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
public class BetweenRoutePredicateFactory
extends AbstractRoutePredicateFactory<BetweenRoutePredicateFactory.Config> {
public static final String DATETIME1_KEY = "datetime1";
public static final String DATETIME2_KEY = "datetime2";
public BetweenRoutePredicateFactory() {
super(Config.class);
}
@Override
public List<String> shortcutFieldOrder() {
return Arrays.asList(DATETIME1_KEY, DATETIME2_KEY);
}
@Override
public Predicate<ServerWebExchange> apply(Config config) {
ZonedDateTime datetime1 = config.datetime1;
ZonedDateTime datetime2 = config.datetime2;
Assert.isTrue(datetime1.isBefore(datetime2), config.datetime1 + " must be before " + config.datetime2); return exchange -> {
};
}
@Validated
public static class Config { @NotNull
private ZonedDateTime datetime1; @NotNull
private ZonedDateTime datetime2; public ZonedDateTime getDatetime1() { return datetime1;
} public Config setDatetime1(ZonedDateTime datetime1) { this.datetime1 = datetime1; return this;
} public ZonedDateTime getDatetime2() { return datetime2;
} public Config setDatetime2(ZonedDateTime datetime2) { this.datetime2 = datetime2; return this;
}
}
}
|
4.1 yml配置示例
application.yml
1
2
3
4
5
6
7
8
|
spring:
cloud: gateway: routes: - id: between_route uri: http://example.org predicates: - Between=2019-01-20T17:42:47.789-07:00[America/Denver], 2019-01-21T17:42:47.789-07:00[America/Denver]
|
文章来源: javaedge.blog.csdn.net,作者:JavaEdge.,版权归原作者所有,如需转载,请联系作者。
原文链接:javaedge.blog.csdn.net/article/details/103484844
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
评论(0)