当前位置:网站首页>GateWay implements load balancing
GateWay implements load balancing
2022-07-31 21:11:00 【m0_67394006】
GateWay实现负载均衡
通过GateWayThere are two ways to achieve load balancing
自动负载均衡
First look at the gateway service9999的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/** # 断言,路径相匹配进行路由
So this position can optimize the current configuration to the following situation,It can be started just as well,A test start is possible9001和网关9999,access through the gateway"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 "Test the gateway configuration class~~custom";
}
}
测试结果:
手动负载均衡
在以上的配置中,其实是有问题的,The problem is that the current service name is exposed,And too flexible,So if you want to solve it,In fact, manual configuration is possible.
具体配置如下:
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开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担.添加下方名片,即可获取全套学习资料哦
边栏推荐
- Talking about the algorithm security of network security
- MATLAB program design and application 2.4 Common internal functions of MATLAB
- 【PIMF】OpenHarmony 啃论文俱乐部—盘点开源鸿蒙三方库【3】
- How can we improve the real yourself, become an excellent architect?
- 老牌音乐播放器 WinAmp 发布 5.9 RC1 版:迁移到 VS 2019 完全重建,兼容 Win11
- Write a database document management tool based on WPF repeating the wheel (1)
- 架构实战营模块 8 作业
- Douyin fetches video list based on keywords API
- MySQL---Create and manage databases and data tables
- 返回一个零长度的数组或者空的集合,不要返回null
猜你喜欢
SiC MOSFET的短路特性及保护
PCB stackup design
Socket Review and I/0 Model
AI 自动写代码插件 Copilot(副驾驶员)
Redis Overview: Talk to the interviewer all night long about Redis caching, persistence, elimination mechanism, sentinel, and the underlying principles of clusters!...
财务盈利、偿债能力指标
请问我的这段sql中sql语法哪里出了错
ThreadLocal
利用反射实现一个管理对象信息的简单框架
Student management system on the first day: complete login PyQt5 + MySQL5.8 exit the operation logic
随机推荐
Three. Introduction to js
Apache EventMesh distributed event-driven multi-runtime
顺序表的实现
【核心概念】图像分类和目标检测中的正负样本划分以及架构理解
Douyin fetches video list based on keywords API
leetcode:6135. 图中的最长环【内向基环树 + 最长环板子 + 时间戳】
matplotlib ax bar color Set the color, transparency, label legend of the ax bar
[Code Hoof Set Novice Village 600 Questions] Merge two numbers without passing a character array
Memblaze发布首款基于长存颗粒的企业级SSD,背后有何新价值?
[Open class preview]: Research and application of super-resolution technology in the field of video image quality enhancement
【Yugong Series】July 2022 Go Teaching Course 025-Recursive Function
Implementing a Simple Framework for Managing Object Information Using Reflection
使用 Flutter 和 Firebase 制作!计数器应用程序
Poker Game in C# -- Introduction and Code Implementation of Blackjack Rules
MATLAB program design and application 2.4 Common internal functions of MATLAB
统计UTF-8字符串中的字符函数
Financial profitability and solvency indicators
Thymeleaf是什么?该如何使用。
A few permanent free network transmission, convenient and simple (Intranet through tutorials)
<artifactId>ojdbc8</artifactId>「建议收藏」