SpringCloud网关-gateway

一 什么是网关?为什么选择 Gateway?

网关功能如下:

  1. 身份认证和权限校验
  2. 服务路由、负载均衡
  3. 请求限流

在 Spring Cloud 中网关的实现包含两种:

  1. Gateway(推荐):是基于 Spring5 中提供的 WebFlux ,属于响应式编程的实现,具备更好的性能,因此作为比较推荐实现网关的方式。
  2. Zuul:是基于 Servlet 的实现,属于阻塞式编程

Gateway 网关

主要分为以下几个步骤:

1 创建新的module-gateway

引入SpringCloudGateway的依赖和nacos的服务发现依赖:

java 复制代码
<!--网关依赖-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<!--nacos服务发现依赖-->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> 
</dependency>

高版本错误:

a)如果使用的是 springcloud高版本(例如 2020.1.0),那么 gateway 中的 ribbon 负载均衡已经被剔除了,因此需要引入 springcloud loadbalencer 作为 gateway 的负载均衡.

java 复制代码
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-loadbalancer</artifactId>
        </dependency>

b)引入此依赖后,可能还会报 cache 的警告,引入一下两个依赖即可

java 复制代码
        <dependency>
            <groupId>com.github.ben-manes.caffeine</groupId>
            <artifactId>caffeine</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
        </dependency>

2 编写nacos地址和路由配置

java 复制代码
server:
  port: 10010 # 网关端口
spring:
  application:
    name: gateway
  cloud:
    nacos:
      server-addr: localhost:8848 # nacos 地址
    gateway:
      routes:  # 网关路由配置
        - id: user-service # 路由 id,自定义,只要唯一即可
          uri: lb://userservice # 路由的目标地址 lb 就是负载均衡,后面跟微服务名
          predicates: #路由断言
            - Path=/user/** # 这个是按照路径匹配,只要以 /user/ 开头就符合要求
        - id: order-service
          uri: lb://orderservice
          predicates:
            - Path=/order/**

路由配置解释如下:

  1. 路由id:路由的唯一标示(自定义)
  2. 路由目标(uri):路由的目标地址,http代表固定地址,lb代表根据服务名负载均衡
  3. 路由断言(predicates):路由的规则(匹配的才被 gateway 拦截下来).
  4. 路由过滤器(filters):对请求或响应做处理,例如 StripProfix 就是去掉指定数量的前缀路由,例如请求是 api/user/login,filters 处理为 StripProfix = 1(去掉一个前缀),那么处理后请求就是 /user/login.

3 效果展示

出现下述结果展示,说明路由转发成功.

相关推荐
渣瓦攻城狮18 小时前
互联网大厂Java面试:从数据库连接池到分布式缓存及微服务
java·redis·spring cloud·微服务·hikaricp·数据库连接池·分布式缓存
百锦再19 小时前
Java中的日期时间API详解:从Date、Calendar到现代时间体系
java·开发语言·spring boot·struts·spring cloud·junit·kafka
百锦再1 天前
Java IO详解:File、FileInputStream与FileOutputStream
java·开发语言·jvm·spring boot·spring cloud·kafka·maven
百锦再1 天前
Java InputStream和OutputStream实现类完全指南
java·开发语言·spring boot·python·struts·spring cloud·kafka
Dragon Wu1 天前
SpringCloud 多模块下引入独立bom模块的正确架构方案
java·spring boot·后端·spring cloud·架构·springboot
利刃大大1 天前
【SpringCloud】Gateway Filter Factories && 过滤器执行顺序 && 自定义过滤器
java·后端·网关·spring cloud·gateway
梵得儿SHI2 天前
Spring Cloud 核心组件精讲:负载均衡深度对比 Spring Cloud LoadBalancer vs Ribbon(原理 + 策略配置 + 性能优化)
java·spring cloud·微服务·负载均衡·架构原理·对比单体与微服务架构·springcloud核心组件
2401_834120872 天前
spring-cloud-kubernetes与SpringCloud Gateway
spring cloud·kubernetes·gateway
猫头虎2 天前
web开发常见问题解决方案大全:502/503 Bad Gateway/Connection reset/504 timed out/400 Bad Request/401 Unauthorized
运维·前端·nginx·http·https·gateway·openresty
Jinkxs2 天前
Gateway - 内置 Filter 使用指南:AddRequestHeader、RewritePath 等实战
gateway