Table of contents
Flow chart of Nacos service discovery
What is Nacos?
Nacos is a service discovery, configuration management, and service management platform open sourced by Alibaba. It is a dynamic service discovery and configuration management tool based on cloud-native architecture.
What is the use of Nacos?
It provides core functions such as service registration and discovery, configuration management, DNS, and load balancing, which can help developers build and manage microservice architectures more conveniently.
Access between microservices is discovered through the registration server
In the microservice architecture, there are usually two roles: service provider and service consumer. A service provider refers to a microservice application that provides a certain service. It can register its service instance information to the Nacos registry, so that service consumers can discover and invoke the service through the Nacos registry.
use
1. Create a Nacos database and create a table to initialize data
Run the nacos-mysql.sql configuration file in the database
2. Configure in application.properties
Modify port number ip address database name password
server.port=8848 #配置端口号
db.url.0=jdbc:mysql://127.0.0.1:3306/BroRiver_nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
db.user.0=root
db.password.0=1234
start service
Access the console Nacos address
-
The default account is nacos and the password is nacos
3. Introduce the coordinates of service discovery
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2021.1</version>
</dependency>
4. Configure the Nacos address and name the application
spring:
application:
name: movieSys
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
ip: 127.0.0.1
Parameter explanation:
name: movieSys
- (Identification) The name of the application
server-addr: 127.0.0.1:8848
- The address of the Nacos service discovery server, used to query application information from other services
ip: 127.0.0.1
- The IP address registered by the application on the Nacos service discovery server is used to allow other services to access the application through this IP address.
- If this property is not set, the application will use the default IP address (localhost's IP address) for registration.
The core of Nacos service discovery is service registration and service discovery. The application registers its own service information in the service registry, and other services obtain service information by querying the service registry, and establish connections with the service.
Flow chart of Nacos service discovery
If the application's IP address or other information changes (for example, service upgrade or offline), it will send an update request to the Nacos service discovery server to update the service registration information. At the same time, other services will periodically send heartbeat requests to the Nacos service discovery server to ensure that they can receive status updates from other services.