当前位置:网站首页>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 !
边栏推荐
- Full mark standard for sports items in the high school entrance examination (Shenzhen, Anhui and Hubei)
- LogBack
- 如何做好固定资产管理?易点易动提供智能化方案
- 【pytorch】nn.AdaptiveMaxPool2d
- Shell script -if else statement
- 大型工厂设备管理痛点和解决方案
- [ESP nanny level tutorial preview] crazy node JS server - Case: esp8266 + DS18B20 temperature sensor +nodejs local service + MySQL database
- Shell script echo command escape character
- 如何解决固定资产管理和盘点的难题?
- R language observation log (part24) -- initialization settings
猜你喜欢
随机推荐
足球篮球体育比赛比分直播平台源码/app开发建设项目
【pytorch】transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
Understand shallow replication and deep replication through code examples
如何解决固定资产管理和盘点的难题?
Shell脚本-read命令:读取从键盘输入的数据
【pytorch】nn.AdaptiveMaxPool2d
Set the type of the input tag to number, and remove the up and down arrows
Mysql 优化
Principles of Microcomputer - Introduction
Why is the Ltd independent station a Web3.0 website!
C语言学生信息管理系统
【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于阿里云和Arduino的化学环境系统检测,支持钉钉机器人告警
固定资产管理系统让企业动态掌握资产情况
如何一站式高效管理固定资产?
Input标签的type设置为number,去掉上下箭头
【pytorch】2.4 卷积函数 nn.conv2d
【MFC开发(17)】高级列表控件List Control
LogBack
Performance improvement 2-3 times! The second generation Kunlun core server of Baidu AI Cloud was launched
Promise异步编程







Refresh the page again 








