GateWay实现负载均衡
GateWay实现负载均衡
通过GateWay实现负载均衡的两种方式
自动负载均衡
首先看一下网关服务9999的yml配置:
- gateway.discovery.locator.enabled: true #开启自动路由功能
- routes中的uri其实最后是不需要服务名称的,这个位置其实只需要指定的localhost:9001即可
server:port: 9999
spring:application:name: cloud-gateway-servicecloud:nacos:discovery:server-addr: localhost:8848gateway:discovery:locator:enabled: true #开启自动路由功能,根据服务名称自动创建routesroutes: # 路由- id: nacos-provider #路由ID,没有固定要求,但是要保证唯一,建议配合服务名uri: http://localhost:9001/nacos-provider # 匹配提供服务的路由地址predicates: # 断言- Path=/th/** # 断言,路径相匹配进行路由
所以这个位置可以把当前的配置优化为以下情况,它是一样可以启动的,可以进行测试启动9001和网关9999,通过网关来访问"localhost:9999/th/**"
server:port: 9999
spring:application:name: cloud-gateway-servicecloud:nacos:discovery:server-addr: localhost:8848gateway:routes: # 路由- id: nacos-provider #路由ID,没有固定要求,但是要保证唯一,建议配合服务名uri: http://localhost:9001 # 匹配提供服务的路由地址predicates: # 断言- Path=/th/** # 断言,路径相匹配进行路由
GateWay还提供了和Zuul类似的自动路由规则,具体配置如下:
- discovery.locator.enabled: true #这个配置默认为false,但是如果为true,就是开启了通过serviceId转发到具体的服务实例。“localhost:9999/ServiceID/th/**”
- 在配置好这些以后,我们可以直接通过服务名称来进行访问Nacos中注册的服务和对应的接口
- 这个位置我们为了测试可以启动2个微服务9001、9002
- GateWay在开启了自动路由以后,还自带负载均衡
server:port: 9999
spring:application:name: cloud-gateway-servicecloud:nacos:discovery:server-addr: localhost:8848gateway:discovery:locator:enabled: true #是否与服务发现组件进行结合,通过serviceId转发到具体的服务实例。默认为false,设为true便开启通过服务中心的自动根据 serviceId 创建路由的功能。
9002和9001保持一致,Controller保持一致,负载均衡测试
package com.tianhao.cloudalibabanacos9002.controller;import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/th")
public class DemoController {@Value("${server.port}")private String serverPort;@GetMapping(value = "/get")public String getServerPort(){return "库存-1:"+serverPort;}@GetMapping(value = "custom")public String customTest(){return "测试网关配置类~~custom";}
}
测试结果:
- 访问地址:
- http://localhost:9999/nacos-provider/th/get
手动负载均衡
在以上的配置中,其实是有问题的,问题在于当前的服务名称暴露,并且太过于灵活,那么如果想解决的话,其实可以进行手动配置。
具体配置如下:
server:port: 9999
spring:application:name: cloud-gateway-servicecloud:nacos:discovery:server-addr: localhost:8848gateway:discovery:locator:enabled: true #开启自动路由功能(此时可以关闭)routes: # 路由- id: nacos-provider #路由ID,没有固定要求,但是要保证唯一,建议配合服务名uri: lb://nacos-provider # 匹配提供服务的路由地址predicates: # 断言- Path=/th/**
测试
我们现在开启9001/9002两个服务和9999网关服务,然后此时我们可以通过网关去访问:http://localhost:9999/th/get
GateWay实现负载均衡相关推荐
- Spring Cloud Gateway之负载均衡
本人最近在学习Spring Cloud Gateway但是发现网上的相关文章都没有介绍其如何使用负载均衡策略,那么本篇文章就给大家带来如何使用Spring Cloud Gateway负载均衡策略. ...
- gateway网关负载均衡策略
前言 之前作业是使用 nacos注册中心来搭建有一个微服务,消费者必然要用到gateway网关来操作路由,并且配置负载均衡策略. 搭建微服务-文章链接:快速搭建微服务-Nacos_一码超人的博客-C ...
- 网关 Spring Cloud Gateway 实战负载均衡(Spring Cloud Loadbalancer)
- Azure上七层负载均衡APP Gateway
Azure的SLB和ILB是最常用的4层负载均衡工具.但有些场景是7层的负载均衡,SLB和ILB就无能为力了. Azure上已经推出了APP Gateway的服务,就是7层负载均衡的负载均衡器. 如上 ...
- GateWay负载均衡
之前说过GateWay是一个zuul的路由替代品,但GateWay也可以实现负载均衡的能力,与ribbon不一样,他通过服务注册中心的服务名/接口实现负载均衡的能力 我们把之前的代码复制一下,项目结构 ...
- springboot gateway 负载均衡_05_Springbootamp;Springcloud面试题
点击上方"蓝色字体",选择"设为星标", 可以防止走失! 1 简单说一下springboot? Spring Boot是由Pivotal团队提供的全新框架,其设 ...
- Spring Cloud Alibaba gateway ribbon 自定义负载均衡规则。发散灰度发布,金丝雀测试等
上一篇介绍了,ribbon的组件.本篇要自己写一个灰度方案.其实就是一个很简单的思维扩散. 需求 前端header请求携带version字段.路由服务根据version去需要对应版本的服务集合,进行或 ...
- Spring Cloud Gateway整合Nacos实现服务路由及集群负载均衡
目录 一.序言 二.代码示例 1.父工程spring-cloud-gateway-learning 2.子工程spring-cloud-api-gateway (1) pom.xml (2) 配置文件 ...
- 阿里云负载均衡 504 Gateway Time-out
问题:当请求时间到达180秒时会报504,修改nginx配置后始终是一到180秒就报504错误 原因:最终排查发现,原来是阿里云的负载均衡默认连接请求超时时间最大为180秒,所以修改nginx配置也没 ...
最新文章
- 基于Docker容器的,Jenkins、GitLab构建持续集成CI
- Spring框架学习day_02:组件扫描 / 注解内部读解 / 组件扫描中配置作用域和生命周期 / 解耦 / 自动装配(两种方式) / 读取文件
- 搜索 | 电商行业模版驱动业务增长实践
- 美团在Redis上踩过的一些坑-2.bgrewriteaof问题
- Largest Number(leetcode 179)
- mosaic数据增强_YoloV4当中的Mosaic数据增强方法(附代码详细讲解)
- 使用图片拉伸resizableImageWithCapInsets
- 射频day5:波导为什么noTEM;相速 相波长 群速 波阻抗
- 第十五周 项目一(1)直接插入排序
- 【微信开发第三章】SpringBoot实现微信授权登录
- 思维导图告诉你怎样正确的做笔记?
- PDF如何在线翻译?PDF在线翻译的方法
- Spark数据分析之第5课
- 一些实用的技巧——移动工具
- Server was unable to process request.
- Web报表系统葡萄城报表:Web 报表
- 青少年python系列 22.turtle库绘制一个正九多边形
- 兰州市2021高考成绩怎么查询,2021年兰州高考各高中成绩及本科升学率数据排名及分析...
- 从洪水中医护人员转移病例看医疗上“云”
- 数据分析师的基本职责(合集)