6.【Spring Cloud Alibaba】API网关-SpringCloudGateway
SpringCloud Gateway是什么?优缺点分析
springCloud Gateway优点
springCloud Gateway缺点
编写SpringCloundGateway
pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
application.yml
server:
port: 8040
spring:
application:
name: gateway
cloud:
nacos:
discovery:
server-addr: localhost:8848
gateway:
discovery:
locator:
# 让gateway通过服务发现组件找到其他的微服务
enabled: true
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: always
logging:
level:
org.springframework.cloud.gateway: trace
核心概念
路由配置示例
架构剖析
路由谓词工厂详解
路由谓词工厂的作用是:符合Predicate的条件,就使用该路由的配置,否则就不管。
自定义路由谓词工厂
TimeBetweenRoutePredicateFactory
@Component
public class TimeBetweenRoutePredicateFactory
extends AbstractRoutePredicateFactory<TimeBeweenConfig> {
public TimeBetweenRoutePredicateFactory() {
super(TimeBeweenConfig.class);
}
@Override
public Predicate<ServerWebExchange> apply(TimeBeweenConfig config) {
LocalTime start = config.getStart();
LocalTime end = config.getEnd();
return exchange -> {
LocalTime now = LocalTime.now();
return now.isAfter(start) && now.isBefore(end);
};
}
@Override
public List<String> shortcutFieldOrder() {
return Arrays.asList("start", "end");
}
public static void main(String[] args) {
DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT);
System.out.println(formatter.format(LocalTime.now()));
}
}
TimeBeweenConfig
@Data
public class TimeBeweenConfig {
private LocalTime start;
private LocalTime end;
}
application.yml
内置过滤器工厂详解
示例
自定义过滤器工厂
过滤器生命周期
自定义过滤器工厂方式01
自定义过滤器工厂方式02
自定义过滤器工厂核心API
编写代码 PreLogGatewayFilterFactory
@Slf4j
@Component
public class PreLogGatewayFilterFactory
extends AbstractNameValueGatewayFilterFactory {
@Override
public GatewayFilter apply(NameValueConfig config) {
return ((exchange, chain) -> {
log.info("请求进来了...{},{}", config.getName(), config.getValue());
ServerHttpRequest modifiedRequest = exchange.getRequest()
.mutate()
.build();
ServerWebExchange modifiedExchange = exchange.mutate()
.request(modifiedRequest)
.build();
return chain.filter(modifiedExchange);
});
}
}
全局过滤器
示例代码
@Bean
@Order(-1)
public GlobalFilter a() {
return (exchange, chain) -> {
log.info("first pre filter");
return chain.filter(exchange).then(Mono.fromRunnable(() -> {
log.info("third post filter");
}));
};
}
@Bean
@Order(0)
public GlobalFilter b() {
return (exchange, chain) -> {
log.info("second pre filter");
return chain.filter(exchange).then(Mono.fromRunnable(() -> {
log.info("second post filter");
}));
};
}
@Bean
@Order(1)
public GlobalFilter c() {
return (exchange, chain) -> {
log.info("third pre filter");
return chain.filter(exchange).then(Mono.fromRunnable(() -> {
log.info("first post filter");
}));
};
}
监控Spring Cloud Gateway
排错,调试技巧总结
第一式:Actuator监控端点
借助Actuator的监控端点,可分析全局过滤器、过滤器工厂、路由详情。详见:Spring Cloud Gateway监控
第二式:日志
加日志,按需将如下包的日志级别设置成 debug 或 trace ,总有一款对你有用。
org.springframework.cloud.gateway
org.springframework.http.server.reactive
org.springframework.web.reactive
org.springframework.boot.autoconfigure.web
reactor.netty
redisratelimiter
配置示例:
logging:
level:
org.springframework.cloud.gateway: trace
第三式:Wiretap【从Greenwich SR3及更高版本才会支持】
Reactor Netty HttpClient 以及 HttpServer 可启用 Wiretap 。将reactor.netty 包设置成 debug 或 trace ,然后设置如下属性:
spring.cloud.gateway.httpserver.wiretap=true
spring.cloud.gateway.httpclient.wiretap=true
分别开启HttpServer及HttpClient的Wiretap。
然后,就可以分析日志啦。
过滤器的执行顺序
SpringCloudGateway限流
本章总结
欢迎关注微信或公众号!!!
PS:如果你觉得文章对你有所帮助,别忘了推荐或者分享,因为有你的支持,才是我续写下篇的动力和源泉!
出处: https://www.cnblogs.com/xjknight/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。