【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(七)Spring Cloud Gateway服务网关

简介: 【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(七)Spring Cloud Gateway服务网关

1、GateWay概述

zuul停更了

gateway之所以性能号,因为底层使用WebFlux,而webFlux底层使用netty通信(NIO)

SpringCloud Gateway使用的是Webflux中的reactor-netty响应式编程组件,底层使用了Netty通讯框架

2、GateWay的特性:

Gateway能干什么?

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

3、GateWay与zuul的区别:

4、zuul1.x的模型:

5、什么是webflux:

是一个非阻塞的web框架,类似springmvc这样的

6、GateWay三大概念:

6.1,路由:

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

就是根据某些规则,将请求发送到指定服务上

6.2,断言:

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

就是判断,如果符合条件就是xxxx,反之yyyy

6.3,过滤:

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

路由前后,过滤请求

7、GateWay的工作原理:

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

下章开始配置一个gateway网关。

8、使用GateWay:

8.1,建module

新建一个GateWay的模块项目

名字: Cloud-gateway-9527

8.2,修改pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springcloud2023</artifactId>
        <groupId>com.tigerhhzz.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>Cloud-gateway-9527</artifactId>
    <dependencies>
        <!--gateway-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
        <!--Springboot web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--Springboot actuator-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!--eureka-client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!-- 引入自己定义的api通用包,可以使用Payment支付Entity -->
        <dependency>
            <groupId>com.tigerhhzz.springcloud-tigerhhzz</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!-- 引入lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!-- 引入srpingboot test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>
8.3,写配置文件
server:
  port: 9527
spring:
  application:
    name: cloud-gateway-service
eureka:
  instance:
    hostname: cloud_gateway_service
  client:
    service-url:
      defaultZone: http://www.eureka7001.com:7001/eureka/
    register-with-eureka: true
    fetch-registry: true
8.4,主启动类
package com.tigerhhzz.springcloud;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
@Slf4j
public class GateWayMain9527 {
    public static void main(String[] args) {
        SpringApplication.run(GateWayMain9527.class,args);
        log.info("GateWayMain9527启动成功~~~~~~~~~~~~~~~~~~~");
    }
}
8.5,针对pay模块,设置路由:

我们目前不想暴露8001端口,希望在8001外面套一层9527

cloud-provider-payment8001看看controller的访问地址(两个controller 分别是如下)

@GetMapping("/payment/lb")
    public String getPaymentLB(){
        return serverPort;
    }
  @GetMapping(value = "/payment/{id}")
  public CommonResult getPaymentById(@PathVariable("id")Long id){
      Payment result = paymentService.getPaymentById(id);
      log.info("****查询结果:" + result);
      if(result != null){
          return new CommonResult(200, "查询成功,serverPort:  "+serverPort, result);
      }
      return new CommonResult(400, "没有对应id的记录", null);

**修改GateWay模块(9527)的配置文件:

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/**                #断言 路径相匹配的进行路由
        - id: payment_routh2
          uri: http://localhost:8001
          #uri: lb://cloud-payment-service
          predicates:
            - Path=/payment/lb/**
            #- After=2022-06-16T19:49:30.417+08:00[Asia/Shanghai]
            #- Cookie=username,tigerhhzz
eureka:
  instance:
    hostname: cloud_gateway_service
  client:
    service-url: //服务提供者provider注册进eureka服务中心
      defaultZone: http://www.eureka7001.com:7001/eureka/
    register-with-eureka: true
    fetch-registry: true

这里表示,

当访问localhost:9527/payment/31时,

路由到localhost:8001/payment/31

8.6,开始测试

启动Cloud-eureka-server7001

启动Cloud-provider-payment8001

启动Cloud-gateway-9527

如果启动GateWay报错
        可能是GateWay模块引入了web和监控的starter依赖,需要移除

访问:

添加网关前:http://localhost:8001/payment/1

添加网关后:http://localhost:9527/payment/1

9、GateWay的网关配置,

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

  • 在配置文件yaml中配置
  • 代码中注入RouteLocator的Bean

GateWay的网关配置,除了支持配置文件,还支持硬编码方式

9.1 使用硬编码配置GateWay:

自己写一个:通过9527网关访问到外网的百度和腾讯网站

创建配置类:

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;
@Configuration
public class GateWayConfig {
    @Bean
    public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
        RouteLocatorBuilder.Builder routes = builder.routes();
        routes.route("path_route",r->r.path("/baidunews").uri("https://news.baidu.com/"));
        return routes.build();
    }
    @Bean
    public RouteLocator customRouteLocator2(RouteLocatorBuilder builder) {
        RouteLocatorBuilder.Builder routes = builder.routes();
        routes.route("path_route1",r->r.path("/tencent").uri("https://www.tencent.com/"));
        return routes.build();
    }
}
9.2 重启服务 测试
9.3 通过服务名实现动态路由

上面的配置虽然首先了网关,但是是在配置文件中写死了要路由的地址

现在需要修改,不指定地址,而是根据微服务名字进行路由,我们可以在注册中心获取某组微服务的地址

默认情况下Gatway会根据注册中心注册的服务列表, 以注册中心上微服务名为路径创建动态路由进行转发,从而实现动态路由的功能

需要注意的是uri的协议lb,表示启用Gateway的负载均衡功能.

lb://serverName是spring cloud gatway在微服务中自动为我们创建的负载均衡uri

需要:

一个eureka7001+两个服务提供者8001/8002

9.3.1 修改GateWay模块的配置文件:
server:
  port: 9527
spring:
  application:
    name: cloud-gateway-service
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true #开启从注册中心动态创建路由的功能,利用微服务名进行路由
      routes: #多个路由
        - id: payment_routh                       #路由的ID,没有固定规则但要求唯一,建议配合服务名
          #uri: http://localhost:8001   #匹配后提供服务的路由地址
          uri: lb://cloud-provider-service         #路由的ID,没有固定规则但要求唯一,建议配合服务名
          predicates: #断言
            - Path=/payment/**                #断言 路径相匹配的进行路由
        - id: payment_routh2
          #uri: http://localhost:8001
          uri: lb://cloud-provider-service
          predicates:
            - Path=/payment/lb/**
            #- After=2022-06-16T19:49:30.417+08:00[Asia/Shanghai]
            #- Cookie=username,tigerhhzz
eureka:
  instance:
    hostname: cloud-gateway-service
  client:
    service-url: #服务提供者provider注册进eureka服务中心
      defaultZone: http://localhost:7001/eureka/
    register-with-eureka: true
    fetch-registry: true
9.3.2 然后就可以启动微服务.测试

8001/8002两个端口切换 http://localhost:9527/payment/lb

9.3.3 Pridicate断言:

我们之前在配置文件中配置了断言:

这个断言表示,如果外部访问路径是指定路径,就路由到指定微服务上

可以看到,这里有一个Path,这个是断言的一种,断言的类型:

After:
        可以指定,只有在指定时间后,才可以路由到指定微服务
• 1
• 2

这里表示,只有在2020年的2月21的15点51分37秒之后,访问才可以路由

在此之前的访问,都会报404

如何获取当前时区?**

import java.time.ZonedDateTime;
/**
 * @author tigerhhzz
 * @date 2023/4/17 17:10
 */
public class T2 {
    public static void main(String[] args) {
        ZonedDateTime now = ZonedDateTime.now();
        System.out.println(now);
        //2023-04-17T17:11:53.743+08:00[Asia/Shanghai]
    }
}
before:
        与after类似,他说在指定时间之前的才可以访问
        between:
        需要指定两个时间,在他们之间的时间才可以访问

cookie:
        只有包含某些指定cookie(key,value),的请求才可以路由

利用curl访问测试:

yml中加上:

- Cookie=username,tigerhhzz

curl测试:

Header:
        只有包含指定请求头的请求,才可以路由

测试:

host:
        只有指定主机的才可以访问,
        比如我们当前的网站的域名是www.aa.com
        那么这里就可以设置,只有用户是www.aa.com的请求,才进行路由

可以看到,如果带了域名访问,就可以,但是直接访问ip地址.就报错了

method:
        只有指定请求才可以路由,比如get请求...

path:
        只有访问指定路径,才进行路由
        比如访问,/abc才路由

Query:
        必须带有请求参数才可以访问

9.3.4 Filter过滤器:

生命周期:

在请求进入路由之前,和处理请求完成,再次到达路由之前

种类:

GateWayFilter,单一的过滤器

与断言类似,比如闲置,请求头,只有特定的请求头才放行,反之就过滤:

GlobalFilter,全局过滤器:

自定义过滤器:

实现两个接口

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;
@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;
    }
}

然后启动服务,即可,因为过滤器通过@COmponet已经加入到容器了

相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
目录
相关文章
|
5月前
|
负载均衡 监控 Java
Spring Cloud Gateway 全解析:路由配置、断言规则与过滤器实战指南
本文详细介绍了 Spring Cloud Gateway 的核心功能与实践配置。首先讲解了网关模块的创建流程,包括依赖引入(gateway、nacos 服务发现、负载均衡)、端口与服务发现配置,以及路由规则的设置(需注意路径前缀重复与优先级 order)。接着深入解析路由断言,涵盖 After、Before、Path 等 12 种内置断言的参数、作用及配置示例,并说明了自定义断言的实现方法。随后重点阐述过滤器机制,区分路由过滤器(如 AddRequestHeader、RewritePath、RequestRateLimiter 等)与全局过滤器的作用范围与配置方式,提
Spring Cloud Gateway 全解析:路由配置、断言规则与过滤器实战指南
|
6月前
|
监控 Java API
Spring Boot 3.2 结合 Spring Cloud 微服务架构实操指南 现代分布式应用系统构建实战教程
Spring Boot 3.2 + Spring Cloud 2023.0 微服务架构实践摘要 本文基于Spring Boot 3.2.5和Spring Cloud 2023.0.1最新稳定版本,演示现代微服务架构的构建过程。主要内容包括: 技术栈选择:采用Spring Cloud Netflix Eureka 4.1.0作为服务注册中心,Resilience4j 2.1.0替代Hystrix实现熔断机制,配合OpenFeign和Gateway等组件。 核心实操步骤: 搭建Eureka注册中心服务 构建商品
1065 3
|
4月前
|
缓存 JSON NoSQL
别再手写过滤器!SpringCloud Gateway 内置30 个,少写 80% 重复代码
小富分享Spring Cloud Gateway内置30+过滤器,涵盖请求、响应、路径、安全等场景,无需重复造轮子。通过配置实现Header处理、限流、重试、熔断等功能,提升网关开发效率,避免代码冗余。
524 2
|
4月前
|
负载均衡 Java API
《深入理解Spring》Spring Cloud 构建分布式系统的微服务全家桶
Spring Cloud为微服务架构提供一站式解决方案,涵盖服务注册、配置管理、负载均衡、熔断限流等核心功能,助力开发者构建高可用、易扩展的分布式系统,并持续向云原生演进。
|
7月前
|
前端开发 Java API
Spring Cloud Gateway Server Web MVC报错“Unsupported transfer encoding: chunked”解决
本文解析了Spring Cloud Gateway中出现“Unsupported transfer encoding: chunked”错误的原因,指出该问题源于Feign依赖的HTTP客户端与服务端的`chunked`传输编码不兼容,并提供了具体的解决方案。通过规范Feign客户端接口的返回类型,可有效避免该异常,提升系统兼容性与稳定性。
510 0
|
设计模式 Java API
微服务架构演变与架构设计深度解析
【11月更文挑战第14天】在当今的IT行业中,微服务架构已经成为构建大型、复杂系统的重要范式。本文将从微服务架构的背景、业务场景、功能点、底层原理、实战、设计模式等多个方面进行深度解析,并结合京东电商的案例,探讨微服务架构在实际应用中的实施与效果。
755 6
|
设计模式 Java API
微服务架构演变与架构设计深度解析
【11月更文挑战第14天】在当今的IT行业中,微服务架构已经成为构建大型、复杂系统的重要范式。本文将从微服务架构的背景、业务场景、功能点、底层原理、实战、设计模式等多个方面进行深度解析,并结合京东电商的案例,探讨微服务架构在实际应用中的实施与效果。
370 1
|
安全 应用服务中间件 API
微服务分布式系统架构之zookeeper与dubbo-2
微服务分布式系统架构之zookeeper与dubbo-2
|
负载均衡 Java 应用服务中间件
微服务分布式系统架构之zookeeper与dubbor-1
微服务分布式系统架构之zookeeper与dubbor-1

热门文章

最新文章