当前位置:网站首页>nacos简易实现负载均衡
nacos简易实现负载均衡
2022-07-01 09:04: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?
- Shell脚本-数组定义以及获取数组元素
- Shell脚本-for循环和for int循环
- Which method is good for the management of fixed assets of small and medium-sized enterprises?
- 易点易动助力企业设备高效管理,提升设备利用率
- 【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于阿里云和Arduino的化学环境系统检测,支持钉钉机器人告警
- Understand shallow replication and deep replication through code examples
- Shell script echo command escape character
- [MFC development (17)] advanced list control list control
- Embedded Engineer Interview frequently asked questions
猜你喜欢

Glitch free clock switching technology

如何做好固定资产管理?易点易动提供智能化方案

Nacos - 配置管理

Reproduced Xray - cve-2017-7921 (unauthorized access by Hikvision)

Pain points and solutions of fixed assets management of group companies

Principle and application of single chip microcomputer timer, serial communication and interrupt system
![[MFC development (17)] advanced list control list control](/img/e8/24c52cb51defc6c96b43c2ef3232ff.png)
[MFC development (17)] advanced list control list control

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

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

3D打印Arduino 四轴飞行器
随机推荐
What are the differences between the architecture a, R and m of arm V7, and in which fields are they applied?
Daily practice of C language - day 80: currency change
Key points of NFT supervision and overseas policies
Insert mathematical formula in MD document and mathematical formula in typora
Understand shallow replication and deep replication through code examples
Shell脚本-变量的定义、赋值和删除
Redis——Lettuce连接redis集群
The jar package embedded with SQLite database is deployed by changing directories on the same machine, and the newly added database records are gone
Is it safe to dig up money and make new shares
pcl_ Viewer command
Nacos - gestion de la configuration
Differences among tasks, threads and processes
Screenshot tips
Ranking list of domestic databases in February, 2022: oceanbase regained the "three consecutive increases", and gaussdb is expected to achieve the largest increase this month
Installing Oracle EE
Which method is good for the management of fixed assets of small and medium-sized enterprises?
Shell脚本-case in语句
Set the type of the input tag to number, and remove the up and down arrows
【ESP 保姆级教程 预告】疯狂Node.js服务器篇 ——案例:ESP8266 + MQ系列 + NodeJs本地服务 + MySql存储
Bimianhongfu queren()







再次刷新该页面