SpringCloud Alibaba Nacos service registration and discovery
Article Directory
1. Nacos installation
nacous download address: https://github.com/alibaba/nacos/releases, I use the latest 1.3.1
After downloading and decompressing, enter the bin directory, the end of cmd is windows, and the end of sh is the startup and shutdown device of linux and mac
The second picture below represents a successful startup. Singleton ~ The cluster mode will be introduced later
After successful startup, enter this web page http://127.0.0.1:8848/nacos/#/login, nacos default port 8488, which can be modified in the configuration file application.properties. The default password is nacos
After entering the password, enter the following page~
2. Create a project and register the service to Nacos
1. Introduce the following dependencies
Note: spring-cloud-alibaba-dependencies dependency groupid has become com.alibaba.cloud since version 2.1.0, don't choose org anymore. And remember to add import, otherwise it will can't resolve xxxxid.
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.1.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
2. The configuration file configuration is as follows
3. After the service is started, check the background service registration list
Here you can see that our service has been successfully registered.
4. Check the'details'
The service name is registered by ourselves, the default grouping is DEFAULT_GROUP, the default service routing type is none, regular items, these two configurations are just fine.
Threshold : between 0 and 1. Healthy examples/all examples. When this ratio is less than this threshold, all instances (including healthy and unhealthy) are returned to consumers. Under normal circumstances, nacos will only return healthy instances to consumers.
In the ' cluster ' of this module, we can see the 'temporary instance' is true, this is temporary and persistent examples of instances in nacos. Temporary instance: Nacos actively detects. If no heartbeat is sent for a certain period of time, it is considered an unhealthy instance. Persistent example: no health check. Both springcloud or dubbo are temporary instances.
' Offline ': We can see that there is an'offline' operation in the remaining operations. After this offline, our service is still activated, but when the consumer pulls the information, it can't be pulled. .
Three. Nacos data persistence to MySQL
1. Create a nacos_config database, and import nacos-mysql.sql in the conf directory into this database.
2. Set the connection in application.properties as follows.
#*************** Config Module Related Configurations ***************#
### If user MySQL as datasource:
spring.datasource.platform=mysql
### Count of DB:
db.num=1
### Connect URL of DB:
db.url.0=jdbc:mysql://127.0.0.1:3306/nacos_config?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
db.user=root
db.password=123456
Four. Nacos cluster mode
1. Copy our singleton Nacos and name them nacos01, nacos02, nacos03
2. Modify the port of the configuration file application.properties
nacos01: The port is 8848, and ip 127.0.0.1 is specified
nacos02: The port is 8849, and the ip 127.0.0.1 is specified
nacos03: The port is 8850, and ip 127.0.0.1 is specified
The following figure shows the modified content of application.properties in nacos02:
3. Create cluster.conf
This creation is very simple. In the conf file directory, nacos has provided me with the template cluster.conf.example file. We copy the file, modify the file name to cluster.conf, and add our node IPs. Make a copy of cluster.conf for each nacos node.
The content of the file is edited as shown in the figure below:
4. Start
- If it is a Linux or Mac system, the configuration is all right here, execute the sh startup.sh -m cluster command to start in cluster mode. The startup effect is shown in the figure below:
- If it is windows, you need to modify the startup.cmd file of each node, and just interchange the content of if else below. Then directly click startup.cmd under each nacos node to start it.
The effect diagram after startup is as follows:
5. View node metadata
Here 8848 is the leader, 8849 is the follower, and 8850 is also the follower.
His master and slave are elected by himself, you can try it, stop 8848~ and a new leader will appear
Five. Nacos automatic configuration
nacos is equivalent to eureka+config+bus.
Nacos doesn't need github and the like, you can put the configuration file in the nacos directory.
Automatically configure the remaining content to be updated later ~