当前位置:网站首页>GateWay实现负载均衡
GateWay实现负载均衡
2022-07-30 20:14:00 【iijik55】
GateWay实现负载均衡
通过GateWay实现负载均衡的两种方式
自动负载均衡
首先看一下网关服务9999的yml配置:
gateway.discovery.locator.enabled: true #开启自动路由功能
routes中的uri其实最后是不需要服务名称的,这个位置其实只需要指定的localhost:9001即可
server:
port: 9999
spring:
application:
name: cloud-gateway-service
cloud:
nacos:
discovery:
server-addr: localhost:8848
gateway:
discovery:
locator:
enabled: true #开启自动路由功能,根据服务名称自动创建routes
routes: # 路由
- 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-service
cloud:
nacos:
discovery:
server-addr: localhost:8848
gateway:
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-service
cloud:
nacos:
discovery:
server-addr: localhost:8848
gateway:
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";
}
}
测试结果:


手动负载均衡
在以上的配置中,其实是有问题的,问题在于当前的服务名称暴露,并且太过于灵活,那么如果想解决的话,其实可以进行手动配置。
具体配置如下:
server:
port: 9999
spring:
application:
name: cloud-gateway-service
cloud:
nacos:
discovery:
server-addr: localhost:8848
gateway:
discovery:
locator:
enabled: true #开启自动路由功能(此时可以关闭)
routes: # 路由
- id: nacos-provider #路由ID,没有固定要求,但是要保证唯一,建议配合服务名
uri: lb://nacos-provider # 匹配提供服务的路由地址
predicates: # 断言
- Path=/th/**
测试
我们现在开启9001/9002两个服务和9999网关服务,然后此时我们可以通过网关去访问:http://localhost:9999/th/get


先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦
边栏推荐
- Frog jumping steps (recursive and non-recursive) ------- Xiaolele walks the steps
- idea plugins搜索不到插件
- Database Tuning - Database Tuning
- WPS表格怎么自动1234排下去?wps表格怎么自动生成序号?
- 推荐系统-排序层-模型(一):Embedding + MLP(多层感知机)模型【Deep Crossing模型:经典的Embedding+MLP模型结构】
- 明解C语言第五章习题
- 树形结构:二叉树的递归非递归遍历、BST
- How to copy table structure and table data in MySQL
- Typora设置标题自动标号
- 推荐系统-排序层:排序层架构【用户、物品特征处理步骤】
猜你喜欢

MySql密码

Weak Banks to data conversion ability?Matt software help solve bank dilemma

These services can't ali interview?Then don't go to, the basic notification, etc

HMS Core Discovery第16期回顾|与虎墩一起,玩转AI新“声”态

6.3有定型性 第七章

基于人脸的常见表情识别(1)——深度学习基础知识

对int变量赋值的操作是原子的吗?

Mysql——字符串函数

How to install and use PostgreSQL 14.4

MySQL Functions (Classic Collection)
随机推荐
基于Apache Doris的湖仓分析
PHP低代码开发平台 V5.0.7新版发布
Running the evict task with compensationTime
明解C语言第五章习题
英文字母间隔突然增大(全角与半角转换)
Linux download and install mysql5.7 version tutorial the most complete and detailed explanation
Face-based Common Expression Recognition (2) - Data Acquisition and Arrangement
Interviewer Ali: Describe to me the phenomenon of cache breakdown, and talk about your solution?
How to build FTP server under win2003
ERROR 1045 (28000) Access denied for user ‘root‘@‘localhost‘解决方法
并发与并行的区别
【请教】SQL语句按列1去重来计算列2之和?
【考研词汇训练营】Day18 —— amount,max,consider,account,actual,eliminate,letter,significant,embarrass,collapse
Maxwell 一款简单易上手的实时抓取Mysql数据的软件
linux下mysql8安装
啊?现在初级测试招聘都要求会自动化了?
ELK log analysis system
M3SDA: Moment matching for multi-source domain adaptation
SQLyog注释 添加 撤销 快捷键
jOOQ是如何设计事务API(详细指南)