SpringCloud 之初识 GateWay

1,620 阅读4分钟

这是我参与「掘金日新计划 · 8 月更文挑战」的第7天,点击查看活动详情

springGatway.png

前言

什么是 GateWay 网关,在微服务中为了方便统一管理且对外暴露服务端地址,起到了内部和外部的隔离保证了后台服务的安全性。可以根据不同的规则讲不通的请求进行路由到指定的服务器。作用很强大,且统一。 例如:网关鉴权丶跨域处理丶流量现流丶流量统计等等

今天我们的主角是 GateWay

Spring Cloud GateWay是Spring Cloud的⼀个全新项⽬,⽬标是取代Netflix Zuul,基于Spring5.0+SpringBoot2.0+WebFlux(基于⾼性能的Reactor模式响应式通信框架Netty,异步⾮阻塞模型)等技术开发,性能⾼于Zuul,官⽅测试,GateWay是Zuul的1.6倍。

那么我们可以GateWay 做什么呢

  1. 路由转发 Route
  2. 处理前后端跨域问题
  3. 防止sql注入
  4. 防止web共计(xxl)
  5. 统一日志记录
  6. 网关鉴权(没用到,我的项目中鉴权抽离了一个单独的公共模块)
  7. 流量现流

GateWay是工作原理

image.png 客户端向 Spring Cloud Gateway 发出请求。 如果网关处理程序映射确定请求与路由匹配,则将其发送到网关 Web 处理程序。 此处理程序通过特定于请求的过滤器链运行请求。 过滤器用虚线划分的原因是过滤器可以在发送代理请求之前和之后运行逻辑。 执行所有“预”过滤器逻辑。 然后发出代理请求。

使用 GateWay

当然 网关最主要的功能还是路由的转发(今天我们就先看看路由是如何进行转发的)
这是我自己的微服务项目 一共是4个服务

2cf747c85fd32918fe00d3e6c141694.jpg 首先引入依赖(这里就不引入其他的依赖了nacos的依赖等)

         <dependency>

            <groupId>org.springframework.cloud</groupId>

            <artifactId>spring-cloud-starter-gateway</artifactId>
            <!-- 这里的版本跟着微服务的自己的版本来就可以了 -->
        </dependency>

这个网关暂时很简单就是路由的转发和配置了一下跨域

跨域:

/**

 * @author Mr_xk

 * @version 1.0

 * @date 2021/6/20 15:01

 */

@Configuration
public class BugvipConfig {
    @Bean
    public CorsWebFilter corsWebFilter(){

        UrlBasedCorsConfigurationSource sourse = new UrlBasedCorsConfigurationSource();

        CorsConfiguration corsConfiguration = new CorsConfiguration();

        //设置跨域请求头

        corsConfiguration.addAllowedHeader("*");

        //设置方法

        corsConfiguration.addAllowedMethod("*");

        //那个来源

        corsConfiguration.addAllowedOrigin("*");

        //是否允许cookie

        corsConfiguration.setAllowCredentials(true);

        sourse.registerCorsConfiguration("/**",corsConfiguration);

        return  new CorsWebFilter(sourse);

    }

}

下面我们看看路由的转发规则

spring:

  application:

    name: bugvip-gatway

  cloud:

    nacos:

      discovery:

        server-addr: 192.168.1.12:8848

    gateway:

      discovery:

        locator:

          enabled: true   #设置为true 请求路径前可以添加微服务名称

          lower-case-service-id: true # 允许为小写

      routes:

        - id: bugvip-boss

          uri: lb://bugvip-boss

          predicates: 
          - Path=/api/boss/**  ## 断言规则

          filters: - RewritePath=/api/(?<segment>.*),/${segment} ## 将api过滤掉

        - id: bugvip-admin

          uri: lb://bugvip-admin

          predicates: 
          - Path=/api/admin/**

          filters: - RewritePath=/api/(?<segment>.*),/${segment}

        - id: bugvip-oss-image

          uri: lb://bugvip-oss-image

          predicates: 
          - Path=/api/third/**

          filters: - RewritePath=/api/(?<segment>.*),/${segment}

其实我这路由断言很简单 api/后面紧跟的就是区分服务的,再到filter将api过滤掉。也就是 Path 路由断言 Factory

route

路由(Route)是GateWay中最基本的组件之一,表示一个具体的路由信息载体,主要由下面几个部分组成:

  1. id:路由唯一标识,区别于其他的route
  2. url: 路由指向的目的地URL,客户端请求最终被转发到的微服务
  3. order: 用于多个Route之间的排序,数值越小越靠前,匹配优先级越高
  4. predicate:断言的作用是进行条件判断,只有断言为true,才执行路由
  5. filter: 过滤器用于修改请求和响应信息


并且官方文档中提供了很多断言规则的
例如:

After 路由断言

指定是时间之后的请求都会被断言到(当然 也有before 和 Between)

spring:
  cloud:
    gateway:
      routes:
      - id: after_route
        uri: https://example.org
        predicates:
        - After=2017-01-20T17:42:47.789-07:00[America/Denver]

Cookie 路由断言 Factory

Cookie 路由断言 Factory有两个参数,cookie名称和正则表达式。请求包含次cookie名称且正则表达式为真的将会被匹配。

spring:
  cloud:
    gateway:
      routes:
      - id: cookie_route
        uri: https://example.org
        predicates:
        - Cookie=chocolate, ch.p

Query 路由断言 Factory

参数 路由断言 Factory 有2个参数: 必选项 param(key) 和可选项 regexp(value).

spring:
  cloud:
    gateway:
      routes:
      - id: query_route
        uri: https://example.org
        predicates:
        - Query=green

这个断言为有参数green是进行路由

spring:
  cloud:
    gateway:
      routes:
      - id: query_route
        uri: https://example.org
        predicates:
        - Query=red, gree.

这个是参数有red 而且 gree.与他正则为true时就可以进行 路由
Ok 官方为我提供了不少的断言规则

image.png 大家感兴趣的 话可以去研究研究 官网地址:docs.spring.io/spring-clou…

实践是检验真理的唯一准则,感兴趣的可以去试试呀!明天见咯