SpringCloud GateWay网关-学习笔记

简介: 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

相关实践学习
部署高可用架构
本场景主要介绍如何使用云服务器ECS、负载均衡SLB、云数据库RDS和数据传输服务产品来部署多可用区高可用架构。
负载均衡入门与产品使用指南
负载均衡(Server Load Balancer)是对多台云服务器进行流量分发的负载均衡服务,可以通过流量分发扩展应用系统对外的服务能力,通过消除单点故障提升应用系统的可用性。 本课程主要介绍负载均衡的相关技术以及阿里云负载均衡产品的使用方法。
目录
相关文章
|
18天前
|
负载均衡 Nacos 数据安全/隐私保护
SpringCloud GateWay 使用
SpringCloud GateWay 使用
22 0
|
1月前
|
SpringCloudAlibaba Java 网络架构
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(二)Rest微服务工程搭建
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(二)Rest微服务工程搭建
53 0
|
1月前
|
SpringCloudAlibaba Java 网络架构
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(七)Spring Cloud Gateway服务网关
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(七)Spring Cloud Gateway服务网关
101 0
|
2天前
|
监控 Java API
第七章 Spring Cloud 之 GateWay
第七章 Spring Cloud 之 GateWay
10 0
|
19天前
|
JSON 安全 关系型数据库
SpringCloud Gateway 实现自定义全局过滤器 + JWT权限验证
SpringCloud Gateway 实现自定义全局过滤器 + JWT权限验证
|
1月前
|
SpringCloudAlibaba 负载均衡 Java
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(目录大纲)
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(目录大纲)
66 1
|
1月前
|
Java Nacos Sentinel
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(九)Nacos+Sentinel+Seata
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(九)Nacos+Sentinel+Seata
205 0
|
1月前
|
消息中间件 SpringCloudAlibaba Java
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(八)Config服务配置+bus消息总线+stream消息驱动+Sleuth链路追踪
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(八)Config服务配置+bus消息总线+stream消息驱动+Sleuth链路追踪
785 0
|
1月前
|
SpringCloudAlibaba Java 测试技术
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(六)Hystrix(豪猪哥)的使用
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(六)Hystrix(豪猪哥)的使用
39 1
|
1月前
|
SpringCloudAlibaba 负载均衡 Java
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(五)OpenFeign的使用
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(五)OpenFeign的使用
42 0