Gateway网关
前提要在注册中心把网关和服务都进行注册
通俗来说,网关就是指在客户端和服务端的一面墙,这面墙有请求转发,负载均衡,权限控制,跨域,熔断降级,限流保护等功能。
客户端发送请求,请求先通过网关,网关根据特定的转发规则转发到特定的服务端。
SpringCloud中GateWay的几个重要概念:
(1):路由。路由是网关最基础的部分,路由信息有一个ID、一个目的URL、一组断言和一组Filter组成。如果断言路由为真,则说明请求的URL和配置匹配。路由简单来说就是对应菜单或者服务,不同的路由对应不同的服务。
(2):断言。Java8中的断言函数。Spring Cloud Gateway中的断言函数输入类型是Spring5.0框架中的ServerWebExchange。Spring Cloud Gateway中的断言函数允许开发者去定义匹配来自于http request中的任何信息,比如请求头和参数等。断言简单来说就是匹配规则。
(3):过滤器。一个标准的Spring webFilter。Spring cloud gateway中的filter分为两种类型的Filter,分别是Gateway Filter和Global Filter。过滤器Filter将会对请求和响应进行修改处理。
所需依赖:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | <dependencies> <dependency> <groupId>com.atguigu</groupId> <artifactId>common_utils</artifactId> <version> 0.0 . 1 -SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <!--gson--> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> </dependency> <!--服务调用--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> </dependencies> |
配置文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | # 服务端口 server.port= 8222 # 服务名 spring.application.name=service-gateway # nacos服务地址 spring.cloud.nacos.discovery.server-addr= 127.0 . 0.1 : 8848 #使用服务发现路由 spring.cloud.gateway.discovery.locator.enabled= true #服务路由名小写 #spring.cloud.gateway.discovery.locator.lower- case -service-id= true #设置路由id spring.cloud.gateway.routes[ 0 ].id=service-acl #设置路由的uri nacos中的服务名字 spring.cloud.gateway.routes[ 0 ].uri=lb: //service-acl controller中的接口路径 #设置路由断言,代理servicerId为auth-service的/auth/路径 spring.cloud.gateway.routes[ 0 ].predicates= Path=/*/acl/** #配置service-edu服务 spring.cloud.gateway.routes[ 1 ].id=service-edu spring.cloud.gateway.routes[ 1 ].uri=lb: //service-edu spring.cloud.gateway.routes[ 1 ].predicates= Path=/eduservice/** #配置service-ucenter服务 spring.cloud.gateway.routes[ 2 ].id=service-ucenter spring.cloud.gateway.routes[ 2 ].uri=lb: //service-ucenter spring.cloud.gateway.routes[ 2 ].predicates= Path=/ucenterservice/** |
在网关的启动类上加@EnableDiscoveryClient,且配置的服务必须都在nacos中注册过
Gateway会帮助我们做到负载均衡的效果,前提模块被部署到集群环境,当客户端向Gateway网关发送请求时,网关会根据匹配规则从nacos中查找相应的服务相应的接口,找到后,它会把多次请求平均分配给多个服务器,实现负载均衡。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义