当前位置:网站首页>nacos簡易實現負載均衡
nacos簡易實現負載均衡
2022-07-01 09:07:00 【建橋之魂】
目錄
一、什麼是Nacos?
英文全稱:Dynamic Naming and Configuration Service
是由阿裏巴巴團隊開發的一個更易於幫助構建雲原生應用的動態服務發現、配置和服務管理平臺
Nacos 提供了一系列簡單易用的特性,能够幫助我們快速地實現動態服務發現、服務配置等功能。
二、Nacos下載和安裝
官方推薦的是下載 2.0.3穩定版本
1. 使用Windows啟動
cmd輸入
startup.cmd -m standalone


2. 驗證nacos是否成功啟動
http://192.168.10.236:8848/nacos/index.html
賬號:nacos , 密碼:nacos
三、Nacos Discovery服務注册/發現
- 首先引入依賴:
<!-- https://mvnrepository.com/artifact/com.alibaba.cloud/spring-cloud-starter-alibaba-nacos-discovery -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2021.1</version>
</dependency>
- 配置nacos的地址:
nacos.discovery.server-addr=127.0.0.1:8848
- 在啟動類上加上
@EnableDiscoveryClient注解
- 查看Nacos服務列錶狀態
這樣子就證明我們的端口作為服務注册到了Nacos中去了!
- 服務列錶不顯示問題
在確保Nacos正常啟動,上述操作也配置完畢,還是沒有顯示服務名稱等信息時,請檢查
spring-boot-starter-parent的版本:
四、簡易實現負載均衡
需要創建兩個注册者(9001、9002)和一個消費者(8083)
1. 注册者配置
server:
port: 9001
Spring:
application:
name: nacos-provider
cloud:
discovery:
server-addr: localhost:8848
management:
endpoint:
web:
exposure:
include: '*'
2. 注册者啟動類
@EnableDiscoveryClient //開啟服務的注册與發現功能
@SpringBootApplication
public class Port9001Application {
public static void main(String[] args) {
SpringApplication.run(Port9001Application.class, args);
}
}
3. 注册者業務層
@RestController
public class Port9001 {
@Value("${server.port}")
private String serverPort;
@GetMapping("/getPort")
public String getServerPort(){
return "調用端口 " + serverPort + " 成功!";
}
}
4. 消費者配置
server:
port: 8083
Spring:
application:
name: nacos-consumer
cloud:
discovery:
server-addr: localhost:8848
#消費者將要去訪問的微服務名稱
service-url:
nacos-user-service: http://nacos-provider
5. 消費者啟動類
@EnableDiscoveryClient
@SpringBootApplication
public class NacosConsumer8083Application {
public static void main(String[] args) {
SpringApplication.run(NacosConsumer8083Application.class, args);
}
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
6. 消費者業務層
@RestController
public class Port8083Controller {
@Autowired
private RestTemplate restTemplate;
@Value("${service-url.nacos-user-service}")
private String serverURL;
@GetMapping(value = "consumer/nacos")
public String getDiscovery(){
/* 三個參數:填寫訪問的名稱和url地址 + 調用接口的返回值類型 */
return restTemplate.getForObject(serverURL+"/getPort",String.class);
}
}
7. 開始調用接口
調用8083的
/consumer/nacos再次刷新該頁面
此處證明完成了遠程的調用,同時具有負載均衡的效果!
8. 遇到問題解决
java.net.UnknownHostException: nacos-provider
依賴中加入spring-cloud-loadbalancer即可!
边栏推荐
- In the middle of the year, where should fixed asset management go?
- The jar package embedded with SQLite database is deployed by changing directories on the same machine, and the newly added database records are gone
- NiO zero copy
- 日常办公耗材管理解决方案
- Shell script - special variables: shell $, $*, [email protected], $$$
- Log4j 日志框架
- Shell脚本-位置参数(命令行参数)
- LogBack
- LogBack
- 【ESP 保姆级教程 预告】疯狂Node.js服务器篇 ——案例:ESP8266 + MQ系列 + NodeJs本地服务 + MySql存储
猜你喜欢

FreeRTOS learning easy notes

Which method is good for the management of fixed assets of small and medium-sized enterprises?

Jeecg restart alarm 40001

It is designed with high bandwidth, which is almost processed into an open circuit?

【pytorch】2.4 卷积函数 nn.conv2d

Understanding and implementation of AVL tree

Why is the Ltd independent station a Web3.0 website!

Jetson Nano 安装TensorFlow GPU及问题解决

Performance improvement 2-3 times! The second generation Kunlun core server of Baidu AI Cloud was launched

3D打印Arduino 四轴飞行器
随机推荐
LogBack
Meituan machine test in 2022
nacos简易实现负载均衡
嵌入式工程师面试-常问问题集
Shell script -select in loop
Why is the Ltd independent station a Web3.0 website!
【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于阿里云、小程序、Arduino的温湿度监控系统
DataBinding源码分析
Shell script -if else statement
Redis source code learning (29), compressed list learning, ziplist C (II)
软件工程师面试刷题网站、经验方法
An overview of the design of royalties and service fees of mainstream NFT market platforms
NiO zero copy
Personal decoration notes
Shell脚本-case in语句
Installing Oracle EE
Win7 pyinstaller reports an error DLL load failed while importing after packaging exe_ Socket: parameter error
【pytorch】transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
日常办公耗材管理解决方案
Shell脚本-select in循环







再次刷新該頁面