zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    学习笔记-记ActiveMQ学习摘录与心得(一)
    c#写个基础的Socket通讯
    c#配置文件appStrings配置节的读取、添加和修改
    做个无边框winform窗体,并美化界面
    winform模拟鼠标按键
    winform程序开机自动启动代码
    记入园第一天
    HTTP Post 测试工具 (C#源代码)
    VC下调试内存泄漏的办法
    VMware共享文件夹遇到的问题
  • 原文地址:https://www.cnblogs.com/xjknight/p/12349110.html
Copyright © 2011-2022 走看看