当前位置:网站首页>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即可!
边栏推荐
- Vsync+ triple cache mechanism +choreographer
- 中断与其他函数共享变量、临界资源的保护
- 日常办公耗材管理解决方案
- Nacos - 配置管理
- Advanced level of C language pointer (Part 1)
- Embedded Engineer Interview frequently asked questions
- 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
- Shell script -for loop and for int loop
- Installing Oracle EE
- An overview of the design of royalties and service fees of mainstream NFT market platforms
猜你喜欢

NiO zero copy

如何解决固定资产管理和盘点的难题?

集团公司固定资产管理的痛点和解决方案

Centos7 shell script one click installation of JDK, Mongo, Kafka, FTP, PostgreSQL, PostGIS, pgrouting

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

3D打印Arduino 四轴飞行器

MySQL optimization

Redis——Lettuce连接redis集群

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

Advanced C language pointer (Part 2)
随机推荐
Insert mathematical formula in MD document and mathematical formula in typora
嵌入式工程师面试题3-硬件
Advanced level of C language pointer (Part 1)
The meaning of yolov5 training visualization index
Is it safe to dig up money and make new shares
JCL and slf4j
DataBinding源码分析
Bird recognition app
FreeRTOS learning easy notes
Interrupt sharing variables with other functions and protection of critical resources
Centos7 shell script one click installation of JDK, Mongo, Kafka, FTP, PostgreSQL, PostGIS, pgrouting
任务、线程、进程 区别
Principles of Microcomputer - Introduction
MySQL optimization
[interview brush 101] linked list
【pytorch】nn.AdaptiveMaxPool2d
ARM v7的体系结构A、R、M区别,分别应用在什么领域?
Key points of NFT supervision and overseas policies
Redis——Lettuce连接redis集群
It technology ebook collection







再次刷新该页面