当前位置:网站首页>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即可!
边栏推荐
- Is it safe to dig up money and make new shares
- 【pytorch】2.4 卷积函数 nn.conv2d
- Personal decoration notes
- pcl_viewer命令
- 【pytorch】softmax函数
- Win7 pyinstaller reports an error DLL load failed while importing after packaging exe_ Socket: parameter error
- 3D printing Arduino four axis aircraft
- Phishing identification app
- It technology ebook collection
- Shell script -if else statement
猜你喜欢

Personal decoration notes

小鸟识别APP

Nacos - 服务发现

Insert mathematical formula in MD document and mathematical formula in typora

Microcomputer principle - bus and its formation

Bird recognition app

Nacos - service discovery

ARM v7的体系结构A、R、M区别,分别应用在什么领域?

足球篮球体育比赛比分直播平台源码/app开发建设项目

2.2 【pytorch】torchvision.transforms
随机推荐
Embedded Engineer Interview frequently asked questions
Imitation of Baidu search results top navigation bar effect
Shell脚本-位置参数(命令行参数)
Shell script -select in loop
Redis源码学习(29),压缩列表学习,ziplist.c(二)
How to solve the problem of fixed assets management and inventory?
C language student information management system
pcl_ Viewer command
大型工厂设备管理痛点和解决方案
Microcomputer principle - bus and its formation
美团2022年机试
如何一站式高效管理固定资产?
Shell脚本-if else语句
【ESP 保姆级教程 预告】疯狂Node.js服务器篇 ——案例:ESP8266 + DHT11 +NodeJs本地服务+ MySQL数据库
Bimianhongfu queren()
Installing Oracle EE
2.2 【pytorch】torchvision.transforms
Serialization, listening, custom annotation
Shell script - special variables: shell $, $*, [email protected], $$$
中考体育项目满分标准(深圳、安徽、湖北)







再次刷新該頁面