SpringCloud GateWay网关-学习笔记

本文涉及的产品
传统型负载均衡 CLB,每月750个小时 15LCU
网络型负载均衡 NLB,每月750个小时 15LCU
应用型负载均衡 ALB,每月750个小时 15LCU
简介: SpringCloud GateWay网关-学习笔记

1,概述

一句话总结:SpringCloud Gateway使用的是Webflux中的reactor-netty响应式编程组件,底层使用了Netty通讯框架。

源码架构

2,gateway能做什么

反向代理
      鉴权
      流量控制
      熔断
      日志监控
      ...

3,微服务中的网关位置


非阻塞异步!非阻塞异步!非阻塞异步!


4,三大核心概念

路由(route)

路由是构建网关的基本模块,它由ID,目标URI,一系列的断言和过滤器组成,如断言为true则匹配该路由

断言(predicate)

参考的是Java8的java.util.function.Predicate

开发人员可以匹配HTTP请求中的所有内容(例如请求头或请求参数),如果请求与断言相匹配则进行路由

过滤(filter)

指的是Spring框架中GatewayFilter的实例,使用过滤器,可以在请求被路由前或者之后对请求进行修改.

总体:

5,gateway工作流程

核心逻辑 : 路由转发+执行过滤器链

6,Gateway网关路由有两种配置方式:

在配置文件yaml中配置:
server:
  port: 9527
spring:
  application:
    name: cloud-gateway-service
  cloud:
    gateway:
      routes:                                     #多个路由
        - id: payment_routh                       #路由的ID,没有固定规则但要求唯一,建议配合服务名
          #          uri: http://localhost:8001   #匹配后提供服务的路由地址
          uri: lb://cloud-payment-service         #路由的ID,没有固定规则但要求唯一,建议配合服务名
          predicates:                             #断言
            - Path=/payment/getById/**                #断言 路径相匹配的进行路由
        - id: payment_routh2
          #          uri: http://localhost:8001
          uri: lb://cloud-payment-service
          predicates:
            - Path=/payment/lb/**
      discovery:
        locator:
          enabled: true  #开启从注册中心动态生成路由的功能,用微服务名进行路由
eureka:
  instance:
    hostname: cloud_gateway_service
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/
代码中注入RouteLocator的Bean
package com.tigerhhzz.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 tigerhhzz
 * @date 2022/6/16 11:29
 */
@Configuration
public class GateWayConfig {
    /*
    * 配置了一个id为route-name的路由规则
    * 当访问地址http://localhost:9527/guonei时自动转发到地址:http://news.baidu.com/guonei
    * */
    @Bean
    public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
        RouteLocatorBuilder.Builder routes = builder.routes();
        routes.route("path_route",r->r.path("/guonei").uri("http://news.baidu.com/guonei"));
        routes.route("path_route1",r->r.path("/guoji").uri("http://news.baidu.com/guoji"));
        return routes.build();
    }
}

7,开启注册中心动态创建路由,通过服务名实现动态路由

9527服务中的yml配置

8,常用的8种gateway路由断言(Route Predicate)

curl测试请求(携带cookie的例子)

C:\Users\Administrator>curl http://localhost:9527/payment/lb
{"timestamp":"2022-06-16T12:37:14.285+0000","path":"/payment/lb","status":404,"error":"Not Found","message":null,"requestId":"ee95c33b"}
C:\Users\Administrator>curl http://localhost:9527/payment/lb --cookie "username=tigerhhzz"
8001
C:\Users\Administrator>curl http://localhost:9527/payment/lb --cookie "username=tigerhhzz"
8002
C:\Users\Administrator>curl http://localhost:9527/payment/lb --cookie "username=tigerhhzz"
8001
C:\Users\Administrator>curl http://localhost:9527/payment/lb --cookie "username=tigerhhzz"
8002
C:\Users\Administrator>curl http://localhost:9527/payment/lb --cookie "username=tigerhhzz"
8001
C:\Users\Administrator>

断言yml配置参考

总结:Predicate就是为了实现一组匹配规则, 让请求过来找到对应的Route进行处理!!

9,Filter的使用

实现两个接口 implments GlobalFilter,OrderId

filter的作用是 全局日志记录和统一网关鉴权 等等等

自定义过滤器

package com.tigerhhzz.springcloud.filter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
import java.util.Date;
/**
 * @author tigerhhzz
 * @date 2022/6/16 21:05
 */
@Component
public class MyGateWayFilter implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        System.out.println("**************come in MylogGateWayGilter:  " + new Date());
        String uname = exchange.getRequest().getQueryParams().getFirst("uname");
        if (uname == null) {
            System.out.println("********用户名为空,非法用户。");
            exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);
            return exchange.getResponse().setComplete();
        }
        return chain.filter(exchange);
    }
    @Override
    public int getOrder() {
        return 0;
    }
}

测试地址:http://localhost:9527/payment/lb?uname=z3

相关实践学习
小试牛刀,一键部署电商商城
SAE 仅需一键,极速部署一个微服务电商商城,体验 Serverless 带给您的全托管体验,一起来部署吧!
负载均衡入门与产品使用指南
负载均衡(Server Load Balancer)是对多台云服务器进行流量分发的负载均衡服务,可以通过流量分发扩展应用系统对外的服务能力,通过消除单点故障提升应用系统的可用性。 本课程主要介绍负载均衡的相关技术以及阿里云负载均衡产品的使用方法。
目录
相关文章
|
5月前
|
前端开发 Java Nacos
🛡️Spring Boot 3 整合 Spring Cloud Gateway 工程实践
本文介绍了如何使用Spring Cloud Alibaba 2023.0.0.0技术栈构建微服务网关,以应对微服务架构中流量治理与安全管控的复杂性。通过一个包含鉴权服务、文件服务和主服务的项目,详细讲解了网关的整合与功能开发。首先,通过统一路由配置,将所有请求集中到网关进行管理;其次,实现了限流防刷功能,防止恶意刷接口;最后,添加了登录鉴权机制,确保用户身份验证。整个过程结合Nacos注册中心,确保服务注册与配置管理的高效性。通过这些实践,帮助开发者更好地理解和应用微服务网关。
498 0
🛡️Spring Boot 3 整合 Spring Cloud Gateway 工程实践
|
7月前
|
JSON Java API
利用Spring Cloud Gateway Predicate优化微服务路由策略
Spring Cloud Gateway 的路由配置中,`predicates`​(断言)用于定义哪些请求应该匹配特定的路由规则。 断言是Gateway在进行路由时,根据具体的请求信息如请求路径、请求方法、请求参数等进行匹配的规则。当一个请求的信息符合断言设置的条件时,Gateway就会将该请求路由到对应的服务上。
434 69
利用Spring Cloud Gateway Predicate优化微服务路由策略
|
7月前
|
JavaScript Java Kotlin
深入 Spring Cloud Gateway 过滤器
Spring Cloud Gateway 是新一代微服务网关框架,支持多种过滤器实现。本文详解了 `GlobalFilter`、`GatewayFilter` 和 `AbstractGatewayFilterFactory` 三种过滤器的实现方式及其应用场景,帮助开发者高效利用这些工具进行网关开发。
805 1
|
8月前
|
负载均衡 Java API
项目中用的网关Gateway及SpringCloud
Spring Cloud Gateway 是一个功能强大、灵活易用的API网关解决方案。通过配置路由、过滤器、熔断器和限流等功能,可以有效地管理和保护微服务。本文详细介绍了Spring Cloud Gateway的基本概念、配置方法和实际应用,希望能帮助开发者更好地理解和使用这一工具。通过合理使用Spring Cloud Gateway,可以显著提升微服务架构的健壮性和可维护性。
303 0
|
8月前
|
负载均衡 Java 应用服务中间件
Gateway服务网关
Gateway服务网关
236 1
Gateway服务网关
|
9月前
|
XML Java 数据格式
如何使用 Spring Cloud 实现网关
如何使用 Spring Cloud 实现网关
165 3
|
4月前
|
负载均衡 Dubbo Java
Spring Cloud Alibaba与Spring Cloud区别和联系?
Spring Cloud Alibaba与Spring Cloud区别和联系?
|
5月前
|
人工智能 SpringCloudAlibaba 自然语言处理
SpringCloud Alibaba AI整合DeepSeek落地AI项目实战
在现代软件开发领域,微服务架构因其灵活性、可扩展性和模块化特性而受到广泛欢迎。微服务架构通过将大型应用程序拆分为多个小型、独立的服务,每个服务运行在其独立的进程中,服务与服务间通过轻量级通信机制(通常是HTTP API)进行通信。这种架构模式有助于提升系统的可维护性、可扩展性和开发效率。
1528 1
|
6月前
|
人工智能 安全 Java
AI 时代:从 Spring Cloud Alibaba 到 Spring AI Alibaba
本次分享由阿里云智能集团云原生微服务技术负责人李艳林主讲,主题为“AI时代:从Spring Cloud Alibaba到Spring AI Alibaba”。内容涵盖应用架构演进、AI agent框架发展趋势及Spring AI Alibaba的重磅发布。分享介绍了AI原生架构与传统架构的融合,强调了API优先、事件驱动和AI运维的重要性。同时,详细解析了Spring AI Alibaba的三层抽象设计,包括模型支持、工作流智能体编排及生产可用性构建能力,确保安全合规、高效部署与可观测性。最后,结合实际案例展示了如何利用私域数据优化AI应用,提升业务价值。
574 4
|
6月前
|
人工智能 自然语言处理 Java
Spring Cloud Alibaba AI 入门与实践
本文将介绍 Spring Cloud Alibaba AI 的基本概念、主要特性和功能,并演示如何完成一个在线聊天和在线画图的 AI 应用。
1303 7