6.【Spring Cloud Alibaba】API网关-SpringCloudGateway

SpringCloud Gateway是什么?优缺点分析

image

springCloud Gateway优点

image

springCloud Gateway缺点

image

编写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

image

核心概念

image

路由配置示例

image

架构剖析

image

路由谓词工厂详解

路由谓词工厂的作用是:符合Predicate的条件,就使用该路由的配置,否则就不管

路由谓词工厂详解

image

自定义路由谓词工厂

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

image

内置过滤器工厂详解

过滤器工厂详解

示例

image

自定义过滤器工厂

过滤器生命周期

image

自定义过滤器工厂方式01

image

image

自定义过滤器工厂方式02

image

image

自定义过滤器工厂核心API

image

编写代码 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);
        });
    }
}

全局过滤器

Spring Cloud Gateway-全局过滤器

示例代码
@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

Spring Cloud Gateway监控

image

排错,调试技巧总结

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。
    
    然后,就可以分析日志啦。

过滤器的执行顺序

image

image

image

image

image

SpringCloudGateway限流

Spring Cloud Gateway限流详解

本章总结

image

posted @ 2020-02-26 11:38  程序猿Knight  阅读(2123)  评论(0编辑  收藏  举报