当前位置:网站首页>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 !
边栏推荐
- Serialization, listening, custom annotation
- VSYNC+三重缓存机制+Choreographer
- [ESP nanny level tutorial] crazy completion chapter - Case: temperature and humidity monitoring system based on Alibaba cloud, applet and Arduino
- Shell script -while loop explanation
- Shell脚本-read命令:读取从键盘输入的数据
- Shell script - array definition and getting array elements
- 软件工程师面试刷题网站、经验方法
- Shell script - definition, assignment and deletion of variables
- Leetcode daily question brushing record --540 A single element in an ordered array
- 如何做好固定资产管理?易点易动提供智能化方案
猜你喜欢

Principles of Microcomputer - internal and external structure of microprocessor

如何做好固定资产管理?易点易动提供智能化方案

Public network cluster intercom +gps visual tracking | help the logistics industry with intelligent management and scheduling

I use flask to write the website "one"
![[interview brush 101] linked list](/img/52/d159bc66c0dbc44c1282a96cf6b2fd.png)
[interview brush 101] linked list

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

【pytorch】softmax函数

3D打印Arduino 四轴飞行器

Redis——Lettuce连接redis集群

Vsync+ triple cache mechanism +choreographer
随机推荐
中小企业固定资产管理办法哪种好?
Principles of Microcomputer - Introduction
Dynamic proxy
Redis——Lettuce连接redis集群
jeecg 重启报40001
Jetson Nano 安装TensorFlow GPU及问题解决
【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于物联网的GY906红外测温门禁刷卡系统
NiO zero copy
Interrupt sharing variables with other functions and protection of critical resources
The jar package embedded with SQLite database is deployed by changing directories on the same machine, and the newly added database records are gone
Imitation of Baidu search results top navigation bar effect
Key points of NFT supervision and overseas policies
[MFC development (17)] advanced list control list control
Shell script - special variables: shell $, $*, [email protected], $$$
Understand shallow replication and deep replication through code examples
Differences among tasks, threads and processes
[ESP nanny level tutorial] crazy completion chapter - Case: gy906 infrared temperature measurement access card swiping system based on the Internet of things
Input标签的type设置为number,去掉上下箭头
Shell script case in statement
How to manage fixed assets well? Easy to point and move to provide intelligent solutions







Refresh the page again 