当前位置:网站首页>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即可!
边栏推荐
猜你喜欢

Ape anthropology topic 20 (the topic will be updated from time to time)

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

Nacos - 配置管理

Microcomputer principle - bus and its formation

FreeRTOS learning easy notes

3D printing Arduino four axis aircraft

nacos简易实现负载均衡

【pytorch】softmax函数

Public network cluster intercom +gps visual tracking | help the logistics industry with intelligent management and scheduling

3D打印Arduino 四轴飞行器
随机推荐
Key points of NFT supervision and overseas policies
[MFC development (16)] tree control
Databinding source code analysis
Pain points and solutions of equipment management in large factories
Shell脚本-位置参数(命令行参数)
Input标签的type设置为number,去掉上下箭头
Can diffusion models be regarded as an autoencoder?
钓鱼识别app
ARM v7的体系结构A、R、M区别,分别应用在什么领域?
嵌入式工程师面试题3-硬件
Insert mathematical formula in MD document and mathematical formula in typora
Dynamic proxy
Shell脚本-echo命令 转义符
美团2022年机试
Vsync+ triple cache mechanism +choreographer
毕业季,我想对你说
Log4j 日志框架
如何一站式高效管理固定资产?
Jetson Nano 安装TensorFlow GPU及问题解决
R language observation log (part24) -- initialization settings






再次刷新該頁面