当前位置:网站首页>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即可!
边栏推荐
- Principles of Microcomputer - Introduction
- 毕业季,我想对你说
- Advanced C language pointer (Part 2)
- Screenshot tips
- In the middle of the year, where should fixed asset management go?
- VSYNC+三重缓存机制+Choreographer
- Microcomputer principle - bus and its formation
- Software Engineer Interview Question brushing website and experience method
- Principles of Microcomputer - internal and external structure of microprocessor
- 中断与其他函数共享变量、临界资源的保护
猜你喜欢

Phishing identification app
![[MFC development (17)] advanced list control list control](/img/e8/24c52cb51defc6c96b43c2ef3232ff.png)
[MFC development (17)] advanced list control list control

TV size and viewing distance

Jeecg restart alarm 40001

Memory size end

猿人学第20题(题目会不定时更新)

Principles of Microcomputer - internal and external structure of microprocessor

Jetson Nano 安装TensorFlow GPU及问题解决

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

How to manage fixed assets well? Easy to point and move to provide intelligent solutions
随机推荐
Dynamic proxy
Principles of Microcomputer - Introduction
An overview of the design of royalties and service fees of mainstream NFT market platforms
Databinding source code analysis
Bimianhongfu queren()
Memory size end
How to manage fixed assets efficiently in one stop?
嵌入式工程师面试-常问问题集
记一次redis超时
Shell script - array definition and getting array elements
安装Oracle EE
Full mark standard for sports items in the high school entrance examination (Shenzhen, Anhui and Hubei)
类加载
Promise异步编程
Yidian Yidong helps enterprises to efficiently manage equipment and improve equipment utilization
如何做好固定资产管理?易点易动提供智能化方案
【ESP 保姆级教程 预告】疯狂Node.js服务器篇 ——案例:ESP8266 + MQ系列 + NodeJs本地服务 + MySql存储
Nacos - gestion de la configuration
Shell脚本-数组定义以及获取数组元素
I use flask to write the website "one"







再次刷新该页面