(十六)SpringCloud学习笔记之微服务网关Spring Cloud Gateway

1 Gateway概述

1.1 简介

Spring Cloud Gateway 是 Spring 官方基于 Spring 5.0,Spring Boot 2.0 和 Project Reactor 等技术开发的网关,旨在为微服务架构提供一种简单而有效的统一的 API 路由管理方式,统一访问接口。SpringCloud Gateway 作为 Spring Cloud 生态系中的网关,目标是替代 Netflix ZUUL,其不仅提供统一的路由方式,并且基于 Filter 链的方式提供了网关基本的功能,例如:安全,监控/埋点,和限流等。它是基于Nttey的响应式开发模式。
在这里插入图片描述
上表为Spring Cloud Gateway与Zuul的性能对比,从结果可知,Spring Cloud Gateway的RPS是Zuul的1.6倍

1.2 核心概念

在这里插入图片描述

  1. 路由(route) 路由是网关最基础的部分,路由信息由一个ID、一个目的URL、一组断言工厂和一组Filter组成。如果断言为真,则说明请求URL和配置的路由匹配。
  2. 断言(predicates) Java8中的断言函数,Spring Cloud Gateway中的断言函数输入类型是Spring5.0框架中的ServerWebExchange。Spring Cloud Gateway中的断言函数允许开发者去定义匹配来自Http Request中的任何信息,比如请求头和参数等。
  3. 过滤器(filter) 一个标准的Spring webFilter,Spring Cloud Gateway中的Filter分为两种类型,分别是Gateway Filter和Global Filter。过滤器Filter可以对请求和响应进行处理。

2 入门案例

2.1 入门案例

(1) 创建工程导入依赖
在项目中添加新的模块 shop_gateway_server ,并导入依赖

<!-- 注意SpringCloud Gateway使用的web框架为webflux,和SpringMVC不兼容。
        引入的限流组件是hystrix。redis底层不再使用jedis,而是lettuce。-->
<dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>

(2) 配置启动类

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

(3) 编写配置文件
创建 application.yml 配置文件

server:
  port: 8080 #端口
spring:
  application:
    name: api-gateway-server #服务名称
  cloud: #配置SpringCloudGateway的路由
    gateway:
      routes:
      #配置路由 : 路由id,路由到微服务的uri,断言(判断条件)
      - id: product-service  #保持唯一
        uri: http://127.0.0.1:9001  #目标微服务请求地址
        predicates:
        - Path=/product/**  #路由条件  path : 路由匹配条件
  • id:我们自定义的路由 ID,保持唯一
  • uri:目标服务地址
  • predicates:路由条件,Predicate 接受一个输入参数,返回一个布尔值结果。该接口包含多种默认方法来将 Predicate 组合成其他复杂的逻辑(比如:与,或,非)。

上面这段配置的意思是,配置了一个 id 为 product-service的路由规则,当访问网关请求地址以product 开头时,会自动转发到地址: http://127.0.0.1:9002/ 。配置完成启动项目即可在浏览器访问进行测试,当我们访问地址 http://localhost:8080/product/1 时会展示页面展示如下:
在这里插入图片描述

2.2 路由规则

Spring Cloud Gateway 的功能很强大,前面我们只是使用了 predicates 进行了简单的条件匹配,其实Spring Cloud Gataway 帮我们内置了很多 Predicates 功能。在 Spring Cloud Gateway 中 Spring 利用Predicate 的特性实现了各种路由匹配规则,有通过 Header、请求参数等不同的条件来进行作为条件匹配到对应的路由。
在这里插入图片描述

2.3 动态路由

和zuul网关类似,在SpringCloud GateWay中也支持动态路由:即自动的从注册中心中获取服务列表并访问。
(1)添加注册中心依赖
在工程的pom文件中添加注册中心的客户端依赖(这里以Eureka为例)

<dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

(2)配置动态路由
修改 application.yml 配置文件,添加eureka注册中心的相关配置,并修改访问映射的URI为服务名称

server:
  port: 8080 #端口
spring:
  application:
    name: api-gateway-server #服务名称
  cloud: #配置SpringCloudGateway的路由
    gateway:
      routes:
      #配置路由 : 路由id,路由到微服务的uri,断言(判断条件)
      - id: product-service  #保持唯一
        #uri: http://127.0.0.1:9001  #目标微服务请求地址
        uri: lb://service-product    #lb:// 根据微服务名称从注册中心中拉取服务请求路径
        predicates:
        - Path=/product/**  #路由条件  path : 路由匹配条件
#eureka注册中心
eureka:
  client:
    service-url:
      defaultZone: http://localhost:9000/eureka/
  instance:
    prefer-ip-address: true #使用ip地址注册
  • uri : uri以 lb: //开头(lb代表从注册中心获取服务),后面接的就是你需要转发到的服务名称

2.4 重写转发路径

在SpringCloud Gateway中,路由转发是直接将匹配的路由path直接拼接到映射路径(URI)之后,那么在微服务开发中往往没有那么便利。这里就可以通过RewritePath机制来进行路径重写。
(1) 案例改造
修改 application.yml ,将匹配路径改为 /product-service/**

spring:
  application:
    name: api-gateway-server #服务名称
  cloud: #配置SpringCloudGateway的路由
    gateway:
      routes:
      #配置路由 : 路由id,路由到微服务的uri,断言(判断条件)
      - id: product-service  #保持唯一
        #uri: http://127.0.0.1:9001  #目标微服务请求地址
        uri: lb://service-product    #lb:// 根据微服务名称从注册中心中拉取服务请求路径
        predicates:
        #- Path=/product/**  #路由条件  path : 路由匹配条件
        - Path=/product-service/** #将当前请求转发到   http://127.0.0.1:9001/product/1

重新启动网关,我们在浏览器访问http://127.0.0.1:8080/product-service/product/1,会抛出404。这是由于路由转发规则默认转发到商品微服务( http://127.0.0.1:9002/product- service/product/1 )路径上,而商品微服务又没有 product-service 对应的映射配置。

(2) 添加RewritePath重写转发路径
修改 application.yml ,添加重写规则。

spring:
  application:
    name: api-gateway-server #服务名称
  cloud
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值