gateway 微服务的入口-笔记

本文属于b站图灵课堂springcloud笔记系列。讲的好还不要钱,值得推荐。

为什么需要API网关?

  • 客户端多次请求不同的微服务,会增加客户端代码和配置的复杂性,维护成本比价高
  • 认证复杂,每个微服务可能存在不同的认证方式,客户端去调用,要去适配不同的认证
  • 存在跨域的请求,调用链有一定的相对复杂性(防火墙 / 浏览器不友好的协议)
  • 难以重构,随着项目的迭代,可能需要重新划分微服务

为了解决上面的问题,引入了API网关

Spring Cloud Gateway 是什么?

This project provides an API Gateway built on top of the Spring Ecosystem, including: Spring 6, Spring Boot 3 and Project Reactor. Spring Cloud Gateway aims to provide a simple, yet effective way to route to APIs and provide cross cutting concerns to them such as: security, monitoring/metrics, and resiliency.

There are two distinct flavors of Spring Cloud Gateway: Server and Proxy Exchange. Each flavor offers WebFlux and MVC compatibility.

  • The Server variant is a full-featured API gateway that can be standalone or embedded in a Spring Boot application.

  • The Proxy Exchange variant is exclusively for use in annotation based WebFlux or MVC applications and allows the use of a special ProxyExchange object as a parameter to a web handler method.

就是spring cloud 官方推出的第二代网关,是由 WebFlux + Netty + Reactor 实现的响应式的 API 网关。

特性:

  • Java 17

  • Spring Framework 6

  • Spring Boot 3

  • Dynamic routing

  • Route matching built into Spring Handler Mapping

  • Route matching on HTTP Request (Path, Method, Header, Host, etc...​)

  • Filters scoped to Matching Route

  • Filters can modify downstream HTTP Request and HTTP Response (Add/Remove Headers, Add/Remove Parameters, Rewrite Path, Set Path, Hystrix, etc...​)

  • API or configuration driven

  • Supports Spring Cloud DiscoveryClient for configuring Routes

其余详细介绍请看GitHub - spring-cloud/spring-cloud-gateway: An API Gateway built on Spring Framework and Spring Boot providing routing and more.

行业中通常把网关分为两个大类:流量网关与业务网关,流量网关主要提供全局性的、与后端业务无关的策略配置,如NG,随着应用架构模式从单体演进到现在的分布式微服务,业务网关也有了新的叫法 - 微服务网关,比如gateway.当然阿里也有开源的Higress实现了流量网关 + 微服务网关 + 安全网关高度集成的功能,这是一种发展趋势。

微服务接入gateway

新建一个模块,就是一个一个启动类

核心配置,pom引入gateway依赖

复制代码
<!-- gateway网关 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>

        <!--nacos-discovery  注册中心依赖-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

        <!-- loadbalancer 负载均衡器依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-loadbalancer</artifactId>
        </dependency>

配置文件:application.yml 如下

复制代码
server:
  port: 18888

spring:
  application:
    name: tlmall-gateway
  cloud:
    nacos:
#      discovery:
#        server-addr: tlmall-nacos-server:8848
      config:
        server-addr: tlmall-nacos-server:8848
        file-extension: yml   #指定配置文件扩展名为yml

#    gateway:
#      #设置路由:路由id、路由到微服务的uri、断言
#      routes:
#        - id: order_route  #路由ID,全局唯一,建议配置服务名
#          uri: lb://tlmall-order  #lb 整合负载均衡器loadbalancer
#          predicates:
#            - Path=/order/**   # 断言,路径相匹配的进行路由
#
#        - id: storage_route   #路由ID,全局唯一,建议配置服务名
#          uri: lb://tlmall-storage  #lb 整合负载均衡器loadbalancer
#          predicates:
#            - Path=/storage/**   # 断言,路径相匹配的进行路由
#
#        - id: account_route   #路由ID,全局唯一,建议配置服务名
#          uri: lb://tlmall-account  #lb 整合负载均衡器loadbalancer
#          predicates:
#            - Path=/account/**   # 断言,路径相匹配的进行路由

  config:
    import:
      - optional:nacos:${spring.application.name}.yml
      - nacos:nacos-discovery.yml

注释掉内容抽取到nacos, Nacos配置中心创建一个dataId为tlmall-gateway.yml的配置,导入网关的配置.

测试:

启动网关服务。

修改postman,改用网关地址下单:http://127.0.0.1:18888/order/create

修改页面地址,order.html中访问地址都替换为tmall-gateway:18888,测试下单是否成功

小结:gateway可以实现微服务中流量入口。

相关推荐
ouliten29 分钟前
vllm笔记(1):最基础的离线推理
笔记·vllm·模型推理
wtmReiner1 小时前
山东大学数值计算2026.1大三上期末考试回忆版
笔记·算法
jimmyleeee1 小时前
人工智能基础知识笔记三十二:向量数据库的查找类型和工作原理
人工智能·笔记
做cv的小昊1 小时前
【TJU】信息检索与分析课程笔记和练习(6)英文数据库检索—web of science
大数据·数据库·笔记·学习·全文检索
DencyCheng2 小时前
Nacos 的全面价值分析:从多角色视角到多架构场景的深度解析
微服务·架构
北岛寒沫2 小时前
北京大学国家发展研究院 经济学原理课程笔记(第二十五课 开放宏观基本概念)
经验分享·笔记·学习
北京理工大学软件工程2 小时前
代码随想录-C-笔记
笔记
小白探索世界欧耶!~3 小时前
用iframe实现单个系统页面在多个系统中复用
开发语言·前端·javascript·vue.js·经验分享·笔记·iframe
551只玄猫3 小时前
新编大学德语1第三版笔记 第3课Studentenleben
笔记·德语·外语·德语a1·德语笔记·自学德语·新编大学德语
老朋友此林4 小时前
React Hook原理速通笔记1(useEffect 原理、使用踩坑、渲染周期、依赖项)
javascript·笔记·react.js