当前位置:网站首页>Simple load balancing with Nacos
Simple load balancing with Nacos
2022-07-01 09:07:00 【The soul of bridge building】
Catalog
One 、 What is? Nacos?
English full name :Dynamic Naming and Configuration Service
It is a dynamic service discovery developed by Alibaba team that is easier to help build cloud native applications 、 Configuration and service management platform
Nacos Provides a range of easy-to-use features , It can help us realize dynamic service discovery quickly 、 Service configuration and other functions .
Two 、Nacos Download and install
—> Reference documents
—> github download
—> Image download
The official recommendation is to download 2.0.3 Stable version
1. Use Windows start-up
cmd Input
startup.cmd -m standalone


2. verification nacos Start successfully or not
http://192.168.10.236:8848/nacos/index.html
account number :nacos , password :nacos
3、 ... and 、Nacos Discovery Service registration / Find out
- First, introduce dependency :
<!-- 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>
- To configure nacos The address of :
nacos.discovery.server-addr=127.0.0.1:8848
- Add... To the startup class
@EnableDiscoveryClientannotation
- see Nacos Service list status
This proves that our port is registered as a service Nacos In the !
- The service list does not show problems
In ensuring that Nacos Normal start , The above operations are also configured , When the service name and other information are not displayed , Please check
spring-boot-starter-parentVersion of :
Four 、 Easy load balancing
You need to create two registrants (9001、9002) And a consumer (8083)
1. Registrant configuration
server:
port: 9001
Spring:
application:
name: nacos-provider
cloud:
discovery:
server-addr: localhost:8848
management:
endpoint:
web:
exposure:
include: '*'
2. Registrant startup class
@EnableDiscoveryClient // Turn on the registration and discovery function of the service
@SpringBootApplication
public class Port9001Application {
public static void main(String[] args) {
SpringApplication.run(Port9001Application.class, args);
}
}
3. Registrant business layer
@RestController
public class Port9001 {
@Value("${server.port}")
private String serverPort;
@GetMapping("/getPort")
public String getServerPort(){
return " Call port " + serverPort + " success !";
}
}
4. Consumer configuration
server:
port: 8083
Spring:
application:
name: nacos-consumer
cloud:
discovery:
server-addr: localhost:8848
# The name of the micro service the consumer is going to visit
service-url:
nacos-user-service: http://nacos-provider
5. Consumer startup class
@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. Consumer business
@RestController
public class Port8083Controller {
@Autowired
private RestTemplate restTemplate;
@Value("${service-url.nacos-user-service}")
private String serverURL;
@GetMapping(value = "consumer/nacos")
public String getDiscovery(){
/* Three parameters : Fill in the name of the visit and url Address + The return value type of the calling interface */
return restTemplate.getForObject(serverURL+"/getPort",String.class);
}
}
7. Start calling the interface
call 8083 Of
/consumer/nacosRefresh the page again
This proves that the remote call is completed , At the same time, it has the effect of load balancing !
8. Solve the problem
java.net.UnknownHostException: nacos-provider
Add... To the dependency spring-cloud-loadbalancer that will do !
边栏推荐
- Nacos - gestion de la configuration
- [ESP nanny level tutorial preview] crazy node JS server - Case: esp8266 + DHT11 +nodejs local service + MySQL database
- 动态代理
- Summary of reptile knowledge points
- Understanding and implementation of AVL tree
- 大型工厂设备管理痛点和解决方案
- 如何高效拉齐团队认知
- TV size and viewing distance
- 中小企业固定资产管理办法哪种好?
- Flink interview questions
猜你喜欢

Embedded Engineer Interview Question 3 Hardware

【MFC开发(17)】高级列表控件List Control

Nacos - service discovery

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

小鸟识别APP

Principles of Microcomputer - Introduction

Principles of Microcomputer - internal and external structure of microprocessor

Jetson nano installs tensorflow GPU and problem solving

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

Imitation of Baidu search results top navigation bar effect
随机推荐
Shell script - special variables: shell $, $*, [email protected], $$$
[MFC development (17)] advanced list control list control
【pytorch】nn.AdaptiveMaxPool2d
【pytorch学习】torch.device
【ESP 保姆级教程 预告】疯狂Node.js服务器篇 ——案例:ESP8266 + DHT11 +NodeJs本地服务+ MySQL数据库
pcl_ Viewer command
Naoqi robot summary 28
Shell script - definition, assignment and deletion of variables
Key points of NFT supervision and overseas policies
Shell脚本-for循环和for int循环
序列化、监听、自定义注解
Serialization, listening, custom annotation
Shell脚本-数组定义以及获取数组元素
Bimianhongfu queren()
Shell script -for loop and for int loop
How to effectively align team cognition
3D printing Arduino four axis aircraft
【MFC开发(17)】高级列表控件List Control
Shell script -if else statement
FreeRTOS学习简易笔记







Refresh the page again 