当前位置: 首页 > news >正文

Spring Cloud Gateway 网关

微服务网关 Spring Cloud Gateway

https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/#gateway-request-predicates-factories

Spring Cloud 在版本 2020.0.0 开始,去除了 Zuul 网关的使用,改用 Spring Cloud Gateway 作为网关。

Spring Cloud Gateway 基于 Spring WebFlux 框架实现,相对于 Zuul 来说,性能更高。

Spring Cloud Gateway 适用于许多不同的使用场景,包括但不限于:

  • 微服务架构:在微服务架构中,API 网关是连接多个微服务的关键组件,它提供了统一的入口点,并可以处理跨服务的事务。
  • 安全性要求高:当项目对安全性有高要求时,API 网关可以集中管理认证和授权,确保敏感数据受到保护。
  • 负载均衡与高可用:需要负载均衡和高可用性的情况下,API 网关可以自动分发流量并处理服务的故障。
  • 监控和日志:当需要监控和记录请求和响应时,API 网关提供了方便的工具来进行监控和故障排除。

本文讲述如何在 Spring Cloud 中使用 Nacos 作为注册中心,通过 Spring Cloud Gateway 实现 API 路由的功能。

启动 Nacos

由于需要使用 Nacos 作为注册中心,网关和微服务都注册到 Nacos服务上,因此,需要先启动 Nacos服务。

见上一篇:

启动 Gateway

添加POM

在 Spring Cloud 项目 GoboyCloud 基础上创建一个 Spring Boot 子项目,添加pom.xml 依赖:

<?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><groupId>com.cloud.goboy</groupId><artifactId>goboycloud</artifactId><version>0.0.1-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>goboy-gateway</artifactId><version>0.0.1-SNAPSHOT</version><name>nacos-gateway</name><description>Spring Cloud Gateway</description><packaging>jar</packaging><dependencies><!-- 引入nacos 注册中心--><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency><!--API网关Gateway--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><finalName>${project.name}</finalName></configuration></plugin></plugins></build></project>
  • spring-cloud-starter-alibaba-nacos-discovery:使用 Nacos 作为注册中心,需要连接上 Nacos。
  • spring-cloud-starter-gateway:使用 Spring Cloud Gateway 作为网关。

添加YML

server:port: 9090spring:application:name: goboy-gatewaycloud:nacos:discovery:server-addr: 127.0.0.1:9001gateway:routes:- id: nacos-provideruri: lb://nacos-providerpredicates:- Path=/provider/**filters:- StripPrefix=1

网关端口设置为 9090。由于需要连接 Nacos 注册中心,需要提供服务名称 goboy-gateway,以及配置 Nacos 注册中心地址 127.0.0.1:9001。

接下来是网关的重要配置 spring.cloud.gateway.routes:

  • **id:**这是路由规则的开始,指定了这个路由规则的唯一标识符(id)。在这里,路由的id是 “nacos-provider”。
  • **uri:**请求应该转发到的目标 URI,lb: 表示负载均衡,将请求转发到名为 “nacos-provider” 的服务。
  • **predicates:**路由条件,这是一个断言(predicate)的列表,用于匹配请求的条件。- Path=/provider/**:这个断言指定了请求的路径必须以 “/provider/” 开头,且可以有任意后缀。只有满足这个条件的请求才会被应用这个路由规则。
  • **filters:**这是一个路由过滤器(filter)的列表,用于对请求进行一些处理或转换。- StripPrefix=1:这个过滤器指定了要去掉请求路径的前缀。在这里,它去掉了一个路径段,因此如果请求是 “/provider/example”,则经过这个过滤器后,会变成 “/example”。

添加启动类

package com.cloud.goboy.gateway;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;@EnableDiscoveryClient
@SpringBootApplication
public class GoboyGatewayApplication {public static void main(String[] args) {SpringApplication.run(GoboyGatewayApplication.class, args);}
}

启动服务

复用文章《Spring Cloud 使用 Nacos 注册中心》服务提供者 nacos-provider 作为路由转发的微服务。

启动的实例如下图所示:

请在此添加图片描述

测试

访问http://localhost:9000/provider/provider/hello

会将请求路由至 nacos-provider 的微服务,且请求接口地址为 /provider/hello,浏览器输出:

hello

请在此添加图片描述

http://www.lryc.cn/news/486299.html

相关文章:

  • LabVIEW中的UDP与TCP比较
  • 半导体器件与物理篇3 P-N结
  • 深入剖析String类的底层实现原理
  • #其它:面试题
  • 计算机视觉中的双边滤波:经典案例与Python代码解析
  • 【AI日记】24.11.17 看 GraphRAG 论文,了解月之暗面
  • Front Panel Window Bounds 与 Front Panel Window Bounds 的区别与应用
  • 比较TCP/IP和OSI/RM的区别
  • 【Java项目】基于SpringBoot的【招聘信息管理系统】
  • 【论文笔记】LLaMA-VID: An Image is Worth 2 Tokens in Large Language Models
  • 使用Web Storage API实现客户端数据持久化
  • 基于STM32F103的秒表设计-液晶显示
  • ReentrantLock的具体实现细节是什么
  • 【JavaScript】this 指向
  • DB Type
  • python-返回函数
  • python语言基础-5 进阶语法-5.2 装饰器-5.2.1 闭包
  • 用vscode编写verilog时,如何有信号定义提示、信号定义跳转(go to definition)、模块跳转(跨文件跳转)这些功能
  • MQTT+Springboot整合
  • ERROR TypeError: AutoImport is not a function
  • 软考教材重点内容 信息安全工程师 第 3 章 密码学基本理论
  • 微信小程序 https://thirdwx.qlogo.cn 不在以下 downloadFile 合法域名列表中
  • Linux性能优化之火焰图的起源
  • 《Markdown语法入门》
  • Controller Baseband commands速览
  • Redisson 3.39.0 发布
  • 高阶C语言补充:柔性数组
  • S32K324信息安全-使用IC5000/IC5700进行debug口解锁
  • 简单实现QT对象的[json]序列化与反序列化
  • Unity肢体控制(关节控制)