• spring cloud day(6) gateway网关


    一、gateWay网关

    1.1 gateway简介

    在这里插入图片描述

    1.2 微服务架构

    在这里插入图片描述

    1.3 spring cloud gateway的特性

    在这里插入图片描述

    1.4 与zuul的区别

    在这里插入图片描述

    1.5 zuul 1.x的模型

    在这里插入图片描述

    1. 6 webflux

    在这里插入图片描述

    1.7 流程

    在这里插入图片描述
    在这里插入图片描述

    二、简单案例

    2.1 案例pro

    2.2.1 pom

        <dependencies>>
           <dependency>
               <groupId>org.springframework.cloud</groupId>
               <artifactId>spring-cloud-starter-gateway</artifactId>
               <version>2.2.3.RELEASE</version>
            </dependency>
    
            <!--eureka-client-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            </dependency>
            <!-- 引入自己定义的api通用包,可以使用Payment支付Entity -->
            <dependency>
                <groupId>com.atguigu.springcloud</groupId>
                <artifactId>cloud-api-commons</artifactId>
                <version>${project.version}</version>
            </dependency>
            <!--        <dependency>-->
            <!--            <groupId>org.springframework.boot</groupId>-->
            <!--            <artifactId>spring-boot-devtools</artifactId>-->
            <!--            <scope>runtime</scope>-->
            <!--            <optional>true</optional>-->
            <!--        </dependency>-->
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
    
    • 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

    2.2.2 yml

    server:
      port: 9527
    
    spring:
      application:
        name: cloud-gateway
      cloud:
        gateway:
          discovery:
            locator:
              enabled: true #开启从注册中心动态创建路由的功能,利用微服务名进行路由
          routes:
            - id: payment_routh #payment_route    #路由的ID,没有固定规则但要求唯一,建议配合服务名
              uri: http://localhost:8001          #匹配后提供服务的路由地址
              predicates:
                - Path=/payment/get/**         # 断言,路径相匹配的进行路由
    eureka:
      instance:
        hostname: cloud-gateway-service
      client: #服务提供者provider注册进eureka服务列表内
        service-url:
          register-with-eureka: true
          fetch-registry: true
          defaultZone: http://eureka7001.com:7001/eureka
    
    
    
    • 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

    2.2.3 config

    @Configuration
    public class configGateway {
        @Bean
        public RouteLocator customRouteLocator(configGateway routeLocatorBuilder) {
            RouteLocatorBuilder.Builder routes = routeLocatorBuilder.routes();
            routes.route("path_route_atguigu", r -> r.path("/guoji").uri("http://news.baidu.com/guonei"))
                    .build();
            return routes.build();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    2.2.5 主启动类

    @SpringBootApplication
    @EnableEurekaClient
    public class gatewayApp {
        public static void main(String[] args) {
            SpringApplication.run(gatewayApp.class, args);
            System.out.println("启动成功");
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    2.3 gateWay效果

    • 原8001端口的接口,现在9527也可以访问的到

    在这里插入图片描述

    2.4 路由配置方式

    • yaml配置
    • 代码中注入RouterLocator的bean
    package com.pack.gateway.config;
    
    
    import org.springframework.cloud.gateway.route.RouteLocator;
    import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class configGateway {
        @Bean
        public RouteLocator customRouteLocator(RouteLocatorBuilder routeLocatorBuilder) {
            RouteLocatorBuilder.Builder routes = routeLocatorBuilder.routes();
            routes.route("path_route_atguigu", r -> r.path("/guoji").uri("http://news.baidu.com/guonei"))
                    .build();
            routes.route("path_route_atguigu", r -> r.path("/guonei").uri("http://news.baidu.com/guoji"))
                    .build();
            return routes.build();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 作用
      访问http://localhost:9527/guoj会转到http://news.baidu.com/guonei,但是路由还是http://localhost:9527/guoj

    2.5 动态路由

    2.5.1 动态路由概念

    在这里插入图片描述

    2.5.2 YML配置

    • 开启动态路由,去掉具体的uri配置
    • uri: lb://CONSUMERORDER-80:配置uri,uri对应eureka中的服务名称,lb是负载均衡的意思,动态路由就是由eureka动态调用多个同名的服务提供者,(轮询…等其他方式)
      在这里插入图片描述
    server:
      port: 9527
    
    spring:
      application:
        name: cloud-gateway
      cloud:
        gateway:
          discovery:
            locator:
              enabled: true #开启从注册中心动态创建路由的功能,利用微服务名进行路由
          routes:
            - id: payment_routh #payment_route    #路由的ID,没有固定规则但要求唯一,建议配合服务名
    #          uri: http://localhost:8001          #匹配后提供服务的路由地址
              uri: lb://CONSUMERORDER-80  #匹配后提供服务的路由地址
              predicates:
                - Path=/payment/get/**         # 断言,路径相匹配的进行路由
            - id: payment_routh #payment_route    #路由的ID,没有固定规则但要求唯一,建议配合服务名
    #          uri: http://localhost:8001          #匹配后提供服务的路由地址
              #lb是负载均衡的意思
              uri: lb://CONSUMERORDER-80 #匹配后提供服务的路由地址
              predicates:
                  - Path=/getServiceInfo        # 断言,路径相匹配的进行路由
            - id: payment_routh2 #payment_route    #路由的ID,没有固定规则但要求唯一,建议配合服务名
    #          uri: http://localhost:8001          #匹配后提供服务的路由地址
              #uri: lb://cloud-payment-service #匹配后提供服务的路由地址
              predicates:
                - Path=/payment/lb/**         # 断言,路径相匹配的进行路由
                - After=2020-03-08T10:59:34.102+08:00[Asia/Shanghai]
                #            - Before=2020-03-08T10:59:34.102+08:00[Asia/Shanghai]
                #            - Between=2020-03-08T10:59:34.102+08:00[Asia/Shanghai] ,  2020-03-08T10:59:34.102+08:00[Asia/Shanghai]
                #            curl http://localhost:9527/payment/lb --cookie "username=zzyy"
                #            - Cookie=username,zzyy   #Cookie=cookieName,正则表达式
                # 请求头要有X-Request-Id属性并且值为整数的正则表达式 curl http://localhost:9527/payment/lb --cookie "username=zzyy" -H "X-Request-Id:11"
    #            - Header=X-Request-Id, \d+
    #            - Host=**.atguigu.com  # curl http://localhost:9527/payment/lb -H "Host:afae.atguigu.com"
    
    eureka:
      instance:
        hostname: cloud-gateway-service
      client: #服务提供者provider注册进eureka服务列表内
        service-url:
          register-with-eureka: true
          fetch-registry: true
          defaultZone: http://eureka7001.com:7001/eureka
    
    
    
    
    • 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

    2.6 Predicate

    2.6.1 作用

    • 可以对路由的判定加一些条件,比如路由什么时间生效之类的
    • 简单的说就是对请求添加一些先决的条件,只有通过条件才去进行转发。
      cloud:
        gateway:
          discovery:
            locator:
              enabled: true #开启从注册中心动态创建路由的功能,利用微服务名进行路由
          routes:
            - id: payment_routh #payment_route    #路由的ID,没有固定规则但要求唯一,建议配合服务名
    #          uri: http://localhost:8001          #匹配后提供服务的路由地址
              uri: lb://CONSUMERORDER-80  #匹配后提供服务的路由地址
              predicates:
                - Path=/payment/get/**         # 断言,路径相匹配的进行路由
                - After=2022-06-21T20:27:25.987+08:00[Asia/Shanghai]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 路由的时间格式可以通过ZonedDateTime获得
    public class tets {
        public static void main(String[] args) {
    
            ZonedDateTime now = ZonedDateTime.now();
            System.out.println(now);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    2.6.2 cookie相关

    • predicates中设置请求Cookie的username是zzyy
    server:
      port: 9527
    
    spring:
      application:
        name: cloud-gateway
      cloud:
        gateway:
          discovery:
            locator:
              enabled: true #开启从注册中心动态创建路由的功能,利用微服务名进行路由
          routes:
            - id: payment_routh #payment_route    #路由的ID,没有固定规则但要求唯一,建议配合服务名
    #          uri: http://localhost:8001          #匹配后提供服务的路由地址
              uri: lb://CONSUMERORDER-80  #匹配后提供服务的路由地址
              predicates:
                - Path=/payment/get/**         # 断言,路径相匹配的进行路由
                - After=2022-06-21T20:27:25.987+08:00[Asia/Shanghai]
                - Cookie=username,zzyy
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 通过cmd模拟请求

    在这里插入图片描述

    2.6.3 对请求头等其他断言

       predicates:
                - Path=/payment/get/**         # 断言,路径相匹配的进行路由
    #            - After=2022-06-21T20:27:25.987+08:00[Asia/Shanghai]
    #            - Cookie=username,zzyy
                - Header=X-Request-Id, \d+ #请求头断言
                - Host=**.atguigu.com  #Host断言
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    
    C:\Users\Administrator>curl http://localhost:9527/payment/get/Cookie -H "X-Request-Id;12"
    
    • 1
    • 2

    2.7 filter

    2.7.1 官网提供的过滤器

    • 在请求前后对请求进行修改

    2.7.2 自定义过滤器

    package com.pack.gateway.filter;
    
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.cloud.gateway.filter.GatewayFilterChain;
    import org.springframework.cloud.gateway.filter.GlobalFilter;
    import org.springframework.core.Ordered;
    import org.springframework.http.HttpStatus;
    import org.springframework.stereotype.Component;
    import org.springframework.web.server.ServerWebExchange;
    import reactor.core.publisher.Mono;
    
    import java.util.Date;
    
    @Component
    @Slf4j
    class MyLogGateWayFilter implements GlobalFilter, Ordered {
    
        @Override
        public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
            log.info("***********come in MyLogGateWayFilter:  " + new Date());
    
            // 获取请求参数中的 uname
            String uname = exchange.getRequest().getQueryParams().getFirst("uname");
    
            if (uname == null) {
                log.info("*******用户名为null,非法用户,o(╥﹏╥)o");
                exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);
                return exchange.getResponse().setComplete();
            }
    //合法放行,到新的过滤器,传到新的过滤链
            return chain.filter(exchange);
        }
    
        /**
         * 加载过滤器顺序,数字越小优先级越高
         *
         * @return
         */
        @Override
        public int getOrder() {
            return 0;
        }
    }
    
    
    • 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
    • 上述自定义全局过滤器的作用就是没有加uname时中断请求
  • 相关阅读:
    云安虚拟化应用性能监测系统—应用异常检测
    微服务面试问题小结( 微服务、分布式、MQ、网关、zookeeper、nginx)
    位深度/像素/分辨率/图像大小的计算/帧率/刷新率
    尚医通 (十) --------- axios、Element UI 与 Node.js
    WRFDA资料同化:如何轻松掌握
    计算机毕业设计(附源码)python在线学习交流平台
    “还是太年轻”,实习生花2分钟解决bug,老程序员的反应耐人寻味
    鸿蒙系统扫盲(一):鸿蒙OS和开源鸿蒙什么关系?
    RabbitMQ 之 Work Queues 工作队列
    关于空洞填充和求重心
  • 原文地址:https://blog.csdn.net/qq_42306803/article/details/125122729