Application practice of 04 Nacos service registry

Background analysis

In microservices, the first problem we need to face is how to find services (software is a service), and the second is how to communicate between different services? How to manage every service in the application better and more conveniently, and how to establish the link between various services, so that the registration center was born (for example, Taobao sellers provide services and buyers invoke services).
Zookeeper (Yahoo, APACHE), Eureka (Netfix), Nacos (Alibaba) and consult (Google) are commonly used registration centers in the market. What are their characteristics and how do we select them? We mainly consider the community activity, stability, function and performance In this microservice study, we chose Nacos, which well supports Alibaba's double 11 activities. It can not only be used as a registration center, but also as a configuration center. It has good stability and performance.

Nacos overview

Nacos (dynamic naming and configuration service) is a platform applied to service registration, discovery and configuration management. It was incubated in Alibaba and grew up in the flood peak test of double 11 in the past ten years. It has precipitated the core competitiveness of simplicity and ease of use, stability and reliability and excellent performance. Its official website address is as follows:

Building Nacos services

Preparatory work

First: make sure your computer is configured with JAVA_HOME environment variable (required when Nacos is started), for example:

Second: make sure your MySQL version is above 5.7 (above MariaDB 10.5), for example


Download and install

Step 1: to download Nacos, you can directly enter the following address in the browser:

Step 2: select the corresponding version and download it directly, as shown in the figure:

Step 3: unzip Nacos (it's better not to unzip it to the Chinese directory), and its directory structure is as follows:

Initialize configuration

Step 1: log in to mysql and the sql script. For example, we can use the built-in client of mysql, first log in to mysql on the command line, and then execute the following instructions:


source E:\app\nacos-server-1.4.2\nacos\conf\nacos-mysql.sql

After successful execution, a Nacos will be created_ Config database. When you open the database, you will see some tables, such as;

Note: when executing this file, the version of mysql should be greater than 5.7 (MariaDB is better than 10.5.11). Otherwise, the following errors will appear:

Step 2: open / conf / application Open the default configuration in properties, configure the database to be connected based on your current environment, and the user name and password to be used when connecting to the database (if there is "#" in front, it should be removed):

### If use MySQL as datasource:

### Count of DB:

### Connect URL of DB:

nacos service startup and access

Step 1: start the Nacos service.

Linux/Unix/Mac startup command (standalone stands for stand-alone mode, non cluster mode):

./ -m standalone

Windows startup command (standalone stands for stand-alone mode, non cluster mode):

startup.cmd -m standalone


1) When executing the execution order, either configure the environment variable or execute it directly in the nacos/bin directory
2) When Nacos starts, Java needs to be configured in the local environment variable_ Home (the installation directory corresponding to jdk),
3) Make sure that the database you connect to (nacos_config) exists
4) If all the configurations are correct and cannot be connected, check how many databases you have (mysql,...)

Step 2: access the Nacos service.

Open the browser and enter http://localhost:8848/nacos Address, the following landing page appears:


The default account password is nacos/nacos

Introduction to service registration and invocation (key points)

Business description

Create two project modules: service providers and service consumers (if they already exist, they do not need to be created). Both of them should be registered in the NacosServer (this server is essentially a web service, and the port is 8848 by default). Then the service provider can provide remote calling services for the service consumers (for example, the payment service is the service provider, and the order service is the service consumer), As shown in the figure:


Producer services creation and registration

Step 1: create a service provider project (the module name is SCA provider, if it already exists, there is no need to create it), inherit the parent project (01 SCA), and its POM The contents of the XML file are as follows:

discovery is like a client, sending heartbeat packets to nacos

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns=""

    <!--be based on springboot Engineering web Service dependency-->
    <!--be based on alibaba of nacos Implement service registration management,When this dependency is added, the system
        When starting, it will send a message to nacos The service sends some heartbeat packets for service registration,When we are in the project
        Adding such a dependency indicates that the project is nacos A client object of the service-->


Step 2: create and modify the configuration file application YML (or to realize service registration. The key codes are as follows:

#Service port
  port: 8081
#service name
    name: sca-provider
#Service registration address (when the service is started, the heartbeat packet is sent to the configured address, usually every 5 seconds. When the service is started, the heartbeat packet is sent to the configured address)
#  cloud:
#    nacos:
#      discovery:
#        Server addr: localhost: 8848 in the future, nacos will be deployed on a separate machine

Note: do not use underscores ("") for service names, A bar ("-") should be used, which is the rule.
Step 3: create a startup class (if it already exists, it does not need to be defined). The key codes are as follows:

package com.jt;

public class ProviderApplication {

    public static void main(String[] args) {, args);

Step 4: start the startup class, and then brush the nacos service first to check whether the service registration is successful, as shown in the figure:


Step 5: stop the SCA provider service, and then constantly refresh the nacos service list to check the health status of the service.



ctrl +n find class 

Consumer service discovery and invocation

Step 1: create a service provider object in the SCA provider project and provide external services based on this object, for example:


Keywords: Java Microservices eureka

Added by Assim on Thu, 03 Mar 2022 10:11:53 +0200