Nacos——Introduction to Nacos and installation of Nacos Server

Source: 02-Nacos Configuration Management - What is Configuration Center_哔哩哔哩_bilibili

Remember to download version 2.x of nacos, and you will be responsible for the "publishing error, please check whether the parameters are correct" error when you create a new configuration in the future! ! ! !

Table of contents

1. Introduction to Nacos

1.1 Four major functions:

1.2 Comparison

2. Configuration Center

2.1 What is configuration

  2.1.1 Features 

2.2 What is Configuration Center

3. Nacos Server installation

3.1 File download

3.2 Start the server (Windos mode)

3.3 OPEN API configuration management test

 3.3.1 curl tool installation

 3.3.2 Publish configuration

 3.3.3 Get configuration

 3.3.4 Shut down the server (windows mode)

 3.3.5 External MySQL database support


1. Introduction to Nacos

Nacos is an open source product of Ali, a comprehensive solution for service discovery, configuration management, and service governance in the microservice architecture .

1.1 Four major functions:

① Service discovery and service health check

     Nacos makes it easier for services to register and discover other services through DNS or HTTP interfaces. Nacos also provides real-time health checks of services to prevent requests from being sent to unhealthy hosts or service instances.

②Dynamic configuration management

     Dynamic configuration services allow you to centrally and dynamically manage the configuration of all services across all environments. Nacos eliminates the need to redeploy applications when updating configurations, which makes configuration changes more efficient and flexible.

③Dynamic DNS service

     Nacos provides service discovery capabilities based on the DNS protocol (Nacos not only performs service discovery based on HTTP, but can also perform service discovery through the DNS protocol), which aims to support service discovery in heterogeneous languages, and supports services registered on Nacos in the form of domain names Expose endpoints, allowing third-party applications to view and discover them easily.
④ Service and metadata management
    Nacos allows you to manage all services and metadata in the data center from the perspective of microservice platform construction, including management service description, life cycle, static dependency analysis of services, health status of services, traffic management of services, routing and security policies .
The characteristics of dynamic configuration management here illustrate the configuration management capabilities of Naocs.

1.2 Comparison

      From the perspective of the configuration center, in terms of performance, Nacos has the highest read and write performance, followed by Apollo . The scenario where Spring CloudConfig relies on Git is not suitable for open large-scale automated operation and maintenance APIs . In terms of functions , Apollo is the most complete. Nacos has most of the configuration management functions of Apollo , while SpringCloud Config does not have an operation and maintenance management interface and needs to be developed by itself. A major advantage of Nacos is that it integrates the registration center and configuration center functions, compared with deployment and operation

Apollo should be intuitive and simple, so it simplifies the complexity of the architecture and reduces the work of operation and maintenance and deployment.

2. Configuration Center

In general, the configuration center is a basic service component that manages various application configurations in a unified manner

2.1 What is configuration

       When an application starts and runs, it often needs to read some configuration information. The configuration is basically accompanied by the entire life cycle of the application, such as: database connection parameters, startup parameters, etc.

  2.1.1 Features 

Configuration is a read-only variable    independent of the program
       The configuration is read-only for the program, the program changes its behavior by reading the configuration, but the program should not change the configuration
   Configure the entire lifecycle of the accompanying application
        
      Configuration runs through the entire life cycle of the application. The application initializes by reading the configuration at startup, and adjusts the behavior according to the configuration at runtime.
      For example: the port number of the service needs to be read when starting up, and the timing policy needs to be read to execute timing tasks during the running of the system.
    Configuration can be loaded in multiple ways
      Common ones include hardcode inside the program , configuration files, environment variables, startup parameters, based on databases, etc.
    Configuration needs governance
        The same program often needs to have different configurations in different environments (development, testing, production) and different clusters (such as different data centers), so it is necessary to have a complete environment and cluster configuration management

2.2 What is Configuration Center

       In the microservice architecture, when the system is split from a single application to service nodes on a distributed system, the configuration files must also be migrated (split), so that the configuration is dispersed. Not only that, but also Contains redundancy, as shown below:
   In order to avoid redundancy, we can create a configuration center and let all services read configuration files from the configuration center.
   
   That is to say, the configuration is separated from each application, and the configuration is managed by the configuration center in a unified manner. The application itself does not need to manage the configuration by itself.

The process is shown in the figure below: the whole process is very intelligent

So how to get the latest configuration?
   For example, through some network protocols, remote network protocols read out.
Why read it through the remote network protocol?
   The configuration center is a service that is deployed independently. It may be on an independent server. Service A and Service B are both on an independent server. Then the access can only be read through the remote network protocol.
    

3. Nacos Server installation

Nacos relies on the Java environment to run. If you are building and running Nacos from code , you also need to configure the Maven environment for this.
 
Recommend nacos 2.x jdk 1.8+ Maven 3.2.x+ mysql 5.6.5+ 

3.1 File download

Releases · alibaba/nacos (github.com)

Download the following file and unzip it, be careful not to have Chinese in the path
       The file ending in ".gz" is the Linux version, we need to download the one ending in zip

 

The following is the file directory after decompression

3.2 Start the server (Windos mode)

   The default port of Nacos is 8848, so we need to ensure that this port is not occupied by other processes.

 Start command:

cmd startup.cmd
   
    Or double-click the startup.cmd file directly

Either way, the following page will appear after running successfully, and then copy the red marked place below, this place may be different for everyone

 

If the cmd running window flashes back after we double-click, you can refer to the article of the following person, which is really great

(34 messages) NACOS startup flashback_ Qianye's blog before sunrise-CSDN blog_nacos startup flashback

After entering the address and pressing Enter, the following page will appear

    Default username: nacos Default password: nacos   

    Enter the password to enter the main interface

3.3 OPEN API configuration management test

3.3.1 curl tool installation

After successfully starting nacos , you can verify whether the nacos service is running normally through the httpapi provided by nacos .
Below we use the curl tool to test the openapi of nacos :
curl is a commonly used command line tool in development and can be used for HTTP protocol testing.
Download the windows version of curl : curl-7.66.0_2-win64-mingw , download address: https://curl.haxx.se/windows/
You can also extract data from the Baidu network disk shared above

After the download is complete, enter the bin directory of curl-7.66.0_2-win64-mingw , and perform the following test. Through the test, you can judge whether nacos is working normally

 3.3.2 Publish configuration

   Look carefully, this is a POST request

curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=nacos.cfg.dataId&group=test&content=HelloWorld"

Run the above command in the cmd window, if successful, the word "true" will appear

After true appears at this time, we open the Nacos page again, configuration list -> query -> the following configuration will appear, this is our configuration information

Click on the right for details

The content of the configuration will appear on the following page. The content of the configuration corresponds to the command we just ran.

    Corresponds to the content after "content="

3.3.3 Get configuration

 Our computer is a client, we want to get data from nacos

 After successfully publishing the configuration to nacos , you can obtain the configuration information from nacos through the client and execute the following command:
   
       GET request can be obtained
curl -X GET "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=nacos.cfg.dataId&group=test"

When we released the configuration, we specified a dataId, group, content

So when we get the configuration, we need to specify the dataId and group, and then we can get the content of the content, as shown in the figure below

3.3.4 Shut down the server (windows mode)

cmd shutdown.cmd

Or a more direct way is to close the window directly

Or double-click shutdown.cmd to run the file

3.3.5 Change to external MySQL database support

As shown in the figure below, there will be some data in nacos. In stand-alone mode, nacos uses an embedded database to store data by default.

      MySQL and SQL Server These databases belong to the database server

      SQLite , Berkeley DB, etc. are embedded databases

The biggest difference between       embedded databases and database servers is that they operate in different address spaces .

      Typically, the database server runs a daemon independently, while the embedded database runs in the same process as the application.

If you want to use external mysql to store nacos data , you need to perform the following steps:
1. Install the database , version requirements: 5.6.5+ , below mysql8
2. Initialize the mysql database, create a new database nacos_config , database initialization file: $ { nacoshome}/conf/nacos-mysql.sql
    Import the following file into the database

 

The following is the table required by nacos

3. Modify the ${nacoshome}/conf/application.properties file , add support for mysql data source configuration (currently only mysql is supported), and add the url , user name and password of the mysql data source.

 Copy the following statement into the above file

 spring.datasource.platform=mysql
 
db.num=1
db.url.0=jdbc:mysql://127.0.0.1:3306/nacos_config?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true 
db.user=nacos_devtest
db.password=youdontknow

 If the above statement does not work, we can use the following one, which has one more time zone serverTimezone=UTC than the above configuration. In addition, I also changed the database name to nacos. Both this file and the database in MySQL have been changed, because When I used the above, it kept giving me an error "com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown database 'nacos_config'", but after I modified it, I found that there was no such problem. I don't know the reason.

spring.datasource.platform=mysql

db.num=1
db.url.0=jdbc:mysql://127.0.0.1:3306/nacos?characterEncoding=utf8&connectTimeout=1000&serverTimezone=UTC&socketTimeout=3000&autoReconnect=true
db.user=root
db.password=root
127.0.0.1:3306 Because it is on my local machine, it is 127.0.0.1, and the port number of MySQL is 3306

  nacos_config indicates the database name (I later changed it to nacos)

   db.user , db.password represent the user name and password of the database  

restart nacos

 Re-enter the webpage and find that there is no data, because there is an empty table in our database, it shows that there is no data, this is normal

Guess you like

Origin blog.csdn.net/weixin_51351637/article/details/129011544