思想远远比编码重要,一定要去了解这些原理,这些文化。代码啥的不是最重要的。

什么是Gateway网关?

Gateway是在Spring生态系统之上构建的API网关服务,基于Spring5,SpringBoot2和Project Reactor等技术。

Gateway旨在提供一 种简单而有效的方式来对API进行路由,以吸提供一些强大的过滤器功能,例如: 熔断、限流、重试

SpringCloud Gateway是SpringCloud的一个全新项目,基于Spring5.0,SpringBoot2.0和Project Reactor等技术开发的网关,它旨在为微服务架构提供一种简单有效的统一的API路由管理方式。

SpringCloud Gateway作为Spring Cloud生态系统中的网关,目标是替代Zuul。在SpringCloud2.0以上版本中,没有对新版本的Zuul2.0以上最新高性能版本进行集成,仍然还是使用的Zuul1.x非Reactor模式的老版本。而为了提升网关的性能,SpringCloud Gateway是基于WebFlux框架实现的,而WebFlux框架底层则使用了高性能的Reactor模式通信框架Netty

SpringCloud Gateway的目标提供统一的路由方式且基于Filter链的方式提供了网关基本的功能,例如:安全,监控/指标, 和限流。

所谓网关,就是挡在访问微服务的前面。

Gateway特性:

  • 基于SpringFramework5, Project Reactor和SpringBoot2.0进行构建
  • 动态路由:能够匹配任何请求属性
  • 可以对路由指定Predicate (断言)和Filter (过滤器)
  • 集成Hystrix的断路器功能
  • 集成SpringCloud服务发现功能
  • 易于编写的Predicate (断言)和Filter (过滤器)
  • 请求限流功能
  • 支持路径重写

为什么使用Gateway

一方面是因为Zuul1.0已经进入了维护阶段,而且Gateway是SpringCloud团队研发的。

Gateway是基于异步非阻塞模型上进行开发的,性能方面不需要担心。虽然Netflix早就发布了最新的Zuul2.x,但Spring Cloud似乎没有整合计划。而且Netflix相关组件都宣布进入维护期,截至目前,Gateway是最理想的网关选择。

Gateway与Zuul的区别

在SpringCloud Finchley正式版之前,SpringCloud推荐的网关是Netflix提供的Zuul:

1、Zuul 1.x, 是一个基于阻塞I/ O的API Gateway

2、Zuul 1.x基于Servlet2.5使用阻塞架构它不支持任何长连接(如WebSocket),Zuul的设计模式和Nginx较像,每次I/O操作都是从工作线程中选择一个执行,请求线程被阻塞到工作线程完成,但是差别是Nginx用C++实现,Zuul用Java实现,而JVM本身会有第一次加载较慢的情况,使得Zuul的性能相对较差。

3、Zuul 2.x理念更先进,想基于Netty非阻塞和支持长连接,但SpringCloud目前还没有整合。Zuul2.x的性能较Zuul1.x有较大提升。在性能方面,根据官方提供的基准测试,Gateway的RPS(每秒请求数)是Zuul的1.6倍。

4、Gateway建立在SpringFramework5、Project Reactor和SpringBoot2之上,使用非阻塞APl。

5、Gateway还支持WebSocket,并且与Spring紧密集成拥有更好的开发体验。

常见的操作与使用

新建module

name:cloud-gateway-gateway9527

依赖:注意:不需要再引入Web依赖,因为Geteway本身依赖了WebFlux

<dependencies>
    <!--gateway-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-gateway</artifactId>
    </dependency>
    <!--eureka client-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <!--引入自定义的api通用包,可使用Payment支付Entity-->
    <dependency>
        <groupId>com.xn2001.springcloud</groupId>
        <artifactId>cloud-api-commons</artifactId>
        <version>1.0-SNAPSHOT</version>
        <scope>compile</scope>
    </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>

下面轮到最重要的配置环节,(下面还会讲到另外一种通过Java来配置路由)

application.yml

server:
  port: 9527

spring:
  application:
    name: cloud-gateway
  cloud:
    gateway:
      routes:
        - id: payment_routh #payment_routh    #路由的ID,没有固定规则但要求唯一,简易配合服务名
          uri: http://localhost:8001         #匹配后提供服务的路由地址
          predicates:
            - Path=/payment/get/**          #断言,路径相匹配的进行路由

        - id: payment_routh2 #payment_routh   #路由的ID,没有固定规则但要求唯一,简易配合服务名
          uri: http://localhost:8001          #匹配后提供服务的路由地址
          predicates:
            - Path=/payment/lb/**             #断言,路径相匹配的进行路由
eureka:
  instance:
    hostname: cloud-gateway-service
  client:
    fetch-registry: true
    register-with-eureka: true
    service-url:
      #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址
      #      defaultZone: http://eureka7002.com:7002/eureka/
      #      单机版eureka
      defaultZone: http://eureka7001.com:7001/eureka/

什么意思呢,通过断言的路由与原来的路由进行匹配,可以很好规避原接口的暴露。

我们去启动一下就知道了

启动类:

@SpringBootApplication
@EnableEurekaClient
public class GateWayMain9527 {
    public static void main(String[] args) {
        SpringApplication.run(GateWayMain9527.class,args);
    }
}

访问:http://localhost:9527/payment/get/1http://localhost:9527/payment/lb/

可以看到8001的接口已经被9527网关调用了,是不是很像feign的服务调用

另一种配置,使用代码的形式,我们也见识一下。

package com.xn2001.springcloud.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;

/**
 * @author 乐心湖
 * @date 2020/5/26 22:27
 **/
@Configuration
public class GatewayConfig {
    @Bean
    public RouteLocator customRouteLocator(RouteLocatorBuilder routeLocatorBuilder){
        RouteLocatorBuilder.Builder routes = routeLocatorBuilder.routes();
        routes.route("payment_routh3",
                r -> r.path("/guonei")
                        .uri("http://news.baidu.com/guonei")).build();
        routes.route("payment_routh4",
                r -> r.path("/guoji")
                        .uri("http://news.baidu.com/guoji")).build();
        return routes.build();
    }
}

什么意思呢,就是通过guonei,guoji两个路由去匹配百度新闻的这两个网址

启动,访问http://localhost:9527/guoneihttp://localhost:9527/guoji,秀吧。

动态路由(重要)

如果我们同时有8001,8002,8003,怎么让他去调用多个微服务呢?

把application.yml中的uri改成lb://微服务的名称,就可以了,lb代表的是load balance(负载均衡)

例如:uri: lb://cloud-payment-service

下面是我修改后的application.yml

server:
  port: 9527

spring:
  application:
    name: cloud-gateway
  cloud:
    gateway:
      routes:
        - id: payment_routh #payment_routh    #路由的ID,没有固定规则但要求唯一,简易配合服务名
#          uri: http://localhost:8001         #匹配后提供服务的路由地址
          uri: lb://cloud-payment-service
          predicates:
            - Path=/payment/get/**          #断言,路径相匹配的进行路由

        - id: payment_routh2 #payment_routh   #路由的ID,没有固定规则但要求唯一,简易配合服务名
#          uri: http://localhost:8001          #匹配后提供服务的路由地址
          uri: lb://cloud-payment-service
          predicates:
            - Path=/payment/lb/**             #断言,路径相匹配的进行路由
eureka:
  instance:
    hostname: cloud-gateway-service
  client:
    fetch-registry: true
    register-with-eureka: true
    service-url:
      #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址
      #      defaultZone: http://eureka7002.com:7002/eureka/
      #      单机版eureka
      defaultZone: http://eureka7001.com:7001/eureka/

启动8001和8002http://localhost:9527/payment/get/2你会发现已经开启了轮询负载均衡。

Predicate

后面的有机会在写吧,东西很多。

待更新:2020.05.28


Last modification:August 18, 2020
如果觉得我的文章对你有用,请随意赞赏