Spring Cloud Gateway系列:简介和入门
一、简介
官网:https://cloud.spring.io/spring-cloud-gateway/reference/html/
SpringCloud Gateway是SpringCloud的一个全新项目,它旨在为微服务架构提供一种简单有效的统一的API路由管理方式。SpringCloud Gateway作为SpringCloud生态系统中的网关,目标是替代Zuul,由于Zuul 1.x使用的仍然是非Reactor模式的老版本。而为了提升网关的性能,SpringCloud Gateway是基于WebFlux框架实现的,而WebFlux框架底层则使用了高性能的Reactor模式通讯框架Netty。
SpringCloud Gateway的目标提供统一的路由方式,且基于filter链的方式提供了网关基本的功能,例如:安全,监控和限流 。
二、重要概念
1、route 路由
路由是网关的最基本组成,由一个路由 id、一个目标地址 url,一组断言工厂及一组 filter组成。若断言为 true,则请求将经由 filter 被路由到目标 url。
2、predicate 断言
断言即一个条件判断,根据当前的 http 请求进行指定规则的匹配,比如说 http 请求头,请求时间等。只有当匹配上规则时,断言才为 true,此时请求才会被直接路由到目标地址(目标服务器),或先路由到某过滤器链,经过过滤器链的层层处理后,再路由到相应的目标地址(目标服务器)。
3、filter 过滤器
对请求进行处理的逻辑部分。当请求的断言为 true 时,会被路由到设置好的过滤器,以对请求或响应进行处理。例如,可以为请求添加一个请求参数,或对请求 URI 进行修改,或为响应添加 header 等。总之,就是对请求或响应进行处理。
三、工作原理
四、使用
环境:
jdk:17
idea:2023.2
spring-boot:3.0.2
1、创建父工程
引入坐标
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mcode</groupId>
<artifactId>gateway-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>gateway-demo</name>
<url>http://maven.apache.org</url>
<description>the gateway</description>
<modules>
<module>gateway-config</module>
</modules>
<properties>
<java.version>17</java.version>
<spring-boot.version>3.0.2</spring-boot.version>
<spring-cloud.version>2022.0.0</spring-cloud.version>
<spring-cloud-alibaba.version>2022.0.0.0</spring-cloud-alibaba.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2、创建gateway-config模块(配置式)
导入坐标
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.mcode</groupId>
<version>1.0-SNAPSHOT</version>
<artifactId>gateway-demo</artifactId>
</parent>
<artifactId>gateway-config</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>gateway-config</name>
<description>gateway-config</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
</dependencies>
</project>
注意:引入gateway后不能引入spring-web
Spring MVC found on classpath, which is incompatible with Spring Cloud Gateway.
Action:
Please set spring.main.web-application-type=reactive or remove spring-boot-starter-web dependency.
编写路由
Application.yml
server:
port: 8080
spring:
cloud:
gateway:
routes:
- id: baidu_route
uri: https://www.baidu.com
predicates:
- Path=/**
3、创建gateway-api模块(API式)
导入坐标
<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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.mcode</groupId>
<artifactId>gateway-demo</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>gateway-api</artifactId>
<packaging>jar</packaging>
<name>gateway-api</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
</dependencies>
</project>
修改配置文件
添加Gateway配置类
package com.mcode.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;
/**
* ClassName: GatewayConfig
* Package: com.mcode.config
* Description:
*
* @Author: robin
* @Create: 2023/10/25 - 10:26 PM
* @Version: v1.0
*/
@Configuration
public class GatewayConfig {
@Bean
public RouteLocator routeLocator(RouteLocatorBuilder builder) {
return builder.routes().route("baidu_route", predicateSpec ->
predicateSpec.path("/**").uri("https://www.baidu.com"))
.build();
}
}
Spring Cloud Gateway系列:简介和入门的更多相关文章
- Spring Cloud Gateway使用简介
Spring Cloud Gateway是类似Nginx的网关路由代理,有替代原来Spring cloud zuul之意: Spring 5 推出了自己的Spring Cloud Gateway,支持 ...
- Spring Cloud Gateway(一):认识Spring Cloud Gateway
1.Spring Cloud Gateway 简介 Spring Cloud Gateway 系列目录 Spring Cloud Gateway(一):认识Spring Cloud Gateway S ...
- Spring Cloud Gateway入门
1.什么是Spring Cloud GatewaySpring Cloud Gateway是Spring官方基于Spring 5.0,Spring Boot 2.0和Project Reactor等技 ...
- Spring Cloud Gateway简单入门,强大的微服务网关
我最新最全的文章都在南瓜慢说 www.pkslow.com,欢迎大家来喝茶! 1 简介 见名知义,Spring Cloud Gateway是用于微服务场景的网关组件,它是基于Spring WebFlu ...
- SpringCloud无废话入门05:Spring Cloud Gateway路由、filter、熔断
1.什么是路由网关 截至目前为止的例子中,我们创建了一个service,叫做:HelloService,然后我们把它部署到了两台服务器(即提供了两个provider),然后我们又使用ribbon将其做 ...
- spring cloud:服务网关 Spring Cloud GateWay 入门
Spring 官方最终还是按捺不住推出了自己的网关组件:Spring Cloud Gateway ,相比之前我们使用的 Zuul(1.x) 它有哪些优势呢?Zuul(1.x) 基于 Servlet,使 ...
- Spring Cloud Alibaba系列(四)使用gateway作为服务网关
什么是网关 在微服务架构里,服务的粒度被进一步细分,各个业务服务可以被独立的设计.开发.测试.部署和管理.这时,各个独立部署单元可以用不同的开发测试团队维护,可以使用不同的编程语言和技术平台进行设计, ...
- Spring Cloud gateway 网关服务二 断言、过滤器
微服务当前这么火爆的程度,如果不能学会一种微服务框架技术.怎么能升职加薪,增加简历的筹码?spring cloud 和 Dubbo 需要单独学习.说没有时间?没有精力?要学俩个框架?而Spring C ...
- Spring Cloud(一):入门篇
Spring Cloud 简介 Spring Cloud 是一个基于 Spring Boot 实现的微服务架构开发工具,可以快速构建分布式系统中的某些常用模式,如配置管理.服务治理.断路器.智能路由. ...
- Spring Cloud Gateway(十):网关过滤器工厂 GatewayFilterFactory
本文基于 spring cloud gateway 2.0.1 1.GatewayFilterFactory 简介 路由过滤器允许以某种方式修改传入的HTTP请求或传出的HTTP响应. 路径过滤器的范 ...
随机推荐
- Js中几种循环的使用
在JavaScript中有五种常用的循环,现在来分别介绍一下五种循环的用法. 1.while 当满足条件时进入循环,进入循环后,当条件不满足时,跳出循环.while语句的一般表达式为:while(表达 ...
- 单节点kafka部署笔记
1 背景 因为工作中需要对接kafka,准备在测试环境中自己部署一套,考虑方便决定部署一台单点. 2 部署 2.1 scala 2.1.1 java环境 openjdk即可,我使用的是openjdk1 ...
- 【go语言】1.1.1 Go 语言的历史和背景
Go 语言,也被称为 Golang,是一种静态强类型.编译型的开源编程语言.Go 语言的出现是为了解决当下的软件开发问题,特别是大规模软件系统的开发. Go 语言的设计者包括 Robert Gries ...
- zabbix system.run
Zabbix的system.run是一个主动模式的监控项,它允许用户在Zabbix服务器上执行自定义的命令或脚本,并获取执行结果作为监控数据. system.run监控项的配置包括以下几个关键参数: ...
- redis 使用 nologin 用户启动
添加不可登录的redis用户 sudo useradd -M -s /sbin/nologin redis 为redis新建目录,并设置属性 mkdir -p /data/redis &&am ...
- 【.NET6 + Vue3 + CentOS7.9 + Docker + Docker-Compose + SSL】个人博客前后端运维部署
个人博客 前端:https://lujiesheng.cn 个人博客 后端:https://api.lujiesheng.cn 个人博客 运维:https://portainer.lujiesheng ...
- FlinkSQL类型系统
类型有什么作用, 类型可以提供编译期检查, 避免到运行期才报错. 类型 首先Flink中自己定义了一套类型, 有LogicalType和DataType两个表示 LogicalType Logical ...
- cdn 引入的资源需要通过 externals 排除打包哦~
cdn 指的是通过相互连接的网络系统,使用最靠近用户的服务器将音乐.图片等资源以高效率和低成本的方式将内容传递给用户. 在 webpack 中,我们可能会将引入的第三方资源会编译成单独的文件,作为静态 ...
- [selenium]取值元素文本属性样式
前言 版本: python:3.9 selenium:4.1.5 获取元素文本 text = driver.find_element(by=By.XPATH, value=""). ...
- 关于Openssh版本升级问题及版本升级到最新版8.7p1流程(CentOS7系统)
前言: 对linux服务器做过漏洞扫描的都知道,常常服务器会被扫出一大堆关于openssh的漏洞,诸如下面这些,而其中的原因就是因为openssh版本比较低.于是就需要升级openssh的版本.下面就 ...