Spring Boot 整合 Spring Cloud 和 Spring Cloud Alibaba (三)【网关 Gateway】

参考资料:https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/#glossary

网关介绍

  1. 网关作为流量的入口,常用功能包括路由转发、权限校验、限流控制等。而 Spring Cloud Gateway 作为 Spring Cloud 官方退出的第二代网关框架,取代了 Zuul 网关。
  2. 网关提供 API 全托管服务,丰富的 API 管理功能,辅助企业管理大规模的 API,以降低管理成本和安全风险,包括协议适配,协议转发、安全策略、防刷、流量、监控日志等功能。
  3. Spring Cloud Gateway 旨在提供一种简单而有效的方式来对 API 进行路由, 并为它们提供切面。例如:安全性、监控、指标和弹性等。
Spring Cloud Gateway特点
  • 基于 Spring5,支持响应式编程和 Spring Boot 2.0
  • 支持使用任何请求属性进行路由匹配
  • 特定于路由的断言和过滤器
  • 集成 Hystrix 进行断路保护
  • 集成服务发现功能
  • 易于编写 Predicates 和 Filter
  • 支持个请求速率限制
  • 支持路径重写
为什么使用 API 网关?

API 网关出现的原因是微服务架构的出现,不同的微服务一般会有不同的网络地址,而外部客户端可能需要调用多个服务的接口才能完成一个业务需求,如果让客户端直接与各个微服务通信,会有以下问题

  • 客户端会多次请求不同的微服务,增加了客户端的复杂性
  • 存在跨域请求,在一定场景下处理相对复杂
  • 认证复杂,每个服务都需要独立仍正
  • 难以重构,随着项目的迭代,可能需要重写划分微服务。例如可能将多个服务合成一个或者将一个拆分成多个。如果客户端直接与微服务通信,那么重构将会很难实施。
  • 某些微服务可能使用了防火墙、浏览器不友好的协议,直接访问会有一定困难

以上问题可以借助 API 网关解决。
API 网关是介于客户端和服务器端之间的中间层,所有的外部请求都会先经过 API 网关这一层。所以,API 的实现更多考虑业务逻辑,而完全,性能、监控可以交由 API 网关来做。这样既提供业务灵活性又不缺安全性。

API网关的优点:

  • 易于监控:可以在网关收集监控数据并将其推送到外部系统进行分析。
  • 易于认证:可以在网关上进行认证,然后将请求转发到后端的微服务,而无须再每个微服务中进行认证。
  • 减少了客户端与各个微服务之间的交互次数。

核心概念

  • 路由:网关最基础的部分。路由信息有一个 ID、一个目 URL、一组断言和一组 Filter 组成。如果断言路由为真,则说明请求的 URL 和 配置匹配
  • 断言:Java 8中的断言函数。Spring Cloud Gateway 中的断言函数输入类型是 Spring 5.0 框架中的 ServerWebExchange。Spring Cloud Gateway 中的断言函数允许开发者去定义匹配来自于 http request 中的任何信息,比如请求头和参数等。
  • 过滤器:一个标准的 Spring WebFilter。 Spring Cloud Gateway 中的 Filter 分为两种类型 Filter,分别是 Gateway Filter 和 Global Filter。Filter 将会对请求和响应进行修改处理。
    工作原理:
    在这里插入图片描述

如何使用

  1. 创建一个工程:mall-gateway

  2. 引入 spring-cloud-starter-gateway 和 spring-cloud-starter-alibaba-nacos-discovery (网关也需要注册到 nacos) 场景启动器

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-gateway</artifactId>
    </dependency>
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>
    
  3. 开启服务的注册发现功能

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

    配置 nacos 注册中心的地址

    server.port=13000
    spring.application.name=mall-gateway
    spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
    

    bootstrap.yml 中配置 nacos 配置中心的地址

    spring:
      cloud:
        nacos:
          config:
            server-addr: 127.0.0.1:8848
            namespace: 9ad23218-1c45-47e8-bca8-14338089c408
    
  4. 网关是一个 Netty 应用,而不是一个 Tomcat 应用

    2021-07-19 02:31:56.308  INFO 16920 --- [           main] com.alibaba.nacos.client.naming          : initializer namespace from System Property :null
    2021-07-19 02:31:56.819  INFO 16920 --- [           main] o.s.b.web.embedded.netty.NettyWebServer  : Netty started on port 13000
    2021-07-19 02:31:56.862  INFO 16920 --- [           main] com.alibaba.nacos.client.naming          : [BEAT] adding beat: BeatInfo{port=13000, ip='192.168.94.135', weight=1.0, serviceName='DEFAULT_GROUP@@mall-gateway', cluster='DEFAULT', metadata={preserved.register.source=SPRING_CLOUD}, scheduled=false, period=5000, stopped=false} to beat map.
    2021-07-19 02:31:56.865  INFO 16920 --- [           main] com.alibaba.nacos.client.naming          : [REGISTER-SERVICE] public registering service DEFAULT_GROUP@@mall-gateway with instance: Instance{instanceId='null', ip='192.168.94.135', port=13000, weight=1.0, healthy=true, enabled=true, ephemeral=true, clusterName='DEFAULT', serviceName='null', metadata={preserved.register.source=SPRING_CLOUD}}
    2021-07-19 02:31:56.876  INFO 16920 --- [           main] c.a.c.n.registry.NacosServiceRegistry    : nacos registry, DEFAULT_GROUP mall-gateway 192.168.94.135:13000 register finished
    2021-07-19 02:31:56.913  INFO 16920 --- [           main] c.f.mall.gateway.MallGatewayApplication  : Started MallGatewayApplication in 6.787 seconds (JVM running for 8.748)
    
  5. 在 application.yml 添加测试配置,测试网关

    spring:
      cloud:
        gateway:
          routes: 
            - id: baidu_route
              uri: https://www.baidu.com
              predicates:
                - Query=url,baidu # 满足断言规则,则会去baidu
            - id: qq_route
              uri: https://www.qq.com
              predicates:
                - Query=url,qq # 满足断言规则,则会去qq
    

    测试成功的截图:
    在这里插入图片描述

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值