当前位置:网站首页>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即可!
边栏推荐
- 【ESP 保姆级教程 预告】疯狂Node.js服务器篇 ——案例:ESP8266 + DHT11 +NodeJs本地服务+ MySQL数据库
- 足球篮球体育比赛比分直播平台源码/app开发建设项目
- 【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于物联网的GY906红外测温门禁刷卡系统
- Ape anthropology topic 20 (the topic will be updated from time to time)
- Bimianhongfu queren()
- Shell脚本-case in语句
- 用C语言编程:用公式计算:e≈1+1/1!+1/2! …+1/n!,精度为10-6
- Naoqi robot summary 28
- Public network cluster intercom +gps visual tracking | help the logistics industry with intelligent management and scheduling
- Shell脚本-case in 和正则表达式
猜你喜欢

Bimianhongfu queren()

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

Pain points and solutions of fixed assets management of group companies

Nacos - 配置管理

Jeecg restart alarm 40001

Phishing identification app

Nacos - gestion de la configuration

How to manage fixed assets efficiently in one stop?

Football and basketball game score live broadcast platform source code /app development and construction project

Bird recognition app
随机推荐
ARM v7的体系结构A、R、M区别,分别应用在什么领域?
R语言观察日志(part24)--初始化设置
钓鱼识别app
Which method is good for the management of fixed assets of small and medium-sized enterprises?
固定资产管理系统让企业动态掌握资产情况
Jetson nano installs tensorflow GPU and problem solving
Memory size end
Summary of reptile knowledge points
如何做好固定资产管理?易点易动提供智能化方案
Redis -- lattice connects to redis cluster
记一次redis超时
Embedded Engineer Interview frequently asked questions
Is it safe to dig up money and make new shares
Shell脚本-case in 和正则表达式
The jar package embedded with SQLite database is deployed by changing directories on the same machine, and the newly added database records are gone
中小企业固定资产管理办法哪种好?
Shell script -read command: read data entered from the keyboard
How to effectively align team cognition
Screenshot tips
MySQL optimization







再次刷新该页面