当前位置:网站首页>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即可!
边栏推荐
- 如何一站式高效管理固定资产?
- 毕业季,我想对你说
- How to manage fixed assets well? Easy to point and move to provide intelligent solutions
- 通过 代码实例 理解 浅复制 与 深复制
- Can diffusion models be regarded as an autoencoder?
- Nacos - 配置管理
- Installing Oracle EE
- Principles of Microcomputer - internal and external structure of microprocessor
- Understanding and implementation of AVL tree
- 【pytorch】nn.CrossEntropyLoss() 与 nn.NLLLoss()
猜你喜欢

Understanding and implementation of AVL tree

【pytorch】2.4 卷积函数 nn.conv2d

2.4 激活函数

3D printing Arduino four axis aircraft

【pytorch】nn.CrossEntropyLoss() 与 nn.NLLLoss()

中小企业固定资产管理办法哪种好?

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

Glitch free clock switching technology

What are the differences between the architecture a, R and m of arm V7, and in which fields are they applied?

Phishing identification app
随机推荐
[MFC development (16)] tree control
Set the type of the input tag to number, and remove the up and down arrows
猿人学第20题(题目会不定时更新)
嵌入式工程师面试题3-硬件
Shell script -for loop and for int loop
Input标签的type设置为number,去掉上下箭头
Shell脚本-变量的定义、赋值和删除
Principles of Microcomputer - internal and external structure of microprocessor
【电赛训练】红外光通信装置 2013年电赛真题
日常办公耗材管理解决方案
Phishing identification app
【ESP 保姆级教程 预告】疯狂Node.js服务器篇 ——案例:ESP8266 + DS18B20温度传感器 +NodeJs本地服务+ MySQL数据库
Serialization, listening, custom annotation
【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于阿里云和Arduino的化学环境系统检测,支持钉钉机器人告警
Jetson nano installs tensorflow GPU and problem solving
Shell脚本-case in 和正则表达式
It technology ebook collection
The fixed assets management system enables enterprises to dynamically master assets
Shell script case in and regular expressions
Daily practice of C language - day 80: currency change







再次刷新該頁面