If you want to know more Nacos tutorials, welcome to star "On Nacos" open source project. Based on the introduction, principle, source code, and practical introduction of Nacos 2.x, it helps developers to quickly get started with Nacos.

Installation package

Install

Step 1 : You can find the corresponding version through https://github.com/alibaba/nacos/releases and download the packaged Nacos. You can use the following command to download the corresponding Nacos version. The current download is version 2.1.0, you can use the following command:

 wget https://github.com/alibaba/nacos/releases/download/2.1.0/nacos-server-2.1.0.tar.gz

Step 2 : After downloading the compressed package, you need to decompress it through the command:

 tar -xzf nacos-server-2.1.0.tar.gz

Step 3 : cd to the nacos directory, first to introduce the directory structure

 .
|____LICENSE
|____bin                                 // nacos 的启动、停止脚本
| |____startup.sh
| |____startup.cmd
| |____shutdown.sh
| |____shutdown.cmd
|____target                              // nacos 服务端 jar 包
| |____nacos-server.jar
|____NOTICE
|____conf                                // naocs 配置文件、sql脚本、集群配置等
| |____1.4.0-ipv6_support-update.sql
| |____schema.sql
| |____nacos-mysql.sql
| |____application.properties.example
| |____nacos-logback.xml
| |____cluster.conf.example
| |____application.properties

Step 4 : Start Nacos in stand-alone mode

Linux/Unix/Mac

Startup command (-m standalone means stand-alone mode startup):

sh startup.sh -m standalone

If you are using ubuntu system, or if you run the script and get an error message [[symbol not found, you can try to run as follows:

bash startup.sh -m standalone

Windows

Startup command (-m standalone means stand-alone mode startup):

startup.cmd -m standalone

The following log appears, indicating that naocs has been started. For detailed logs, see nacos/logs/start.out.

 nacos is starting with standalone
nacos is starting,you can check the /Users/lixiaoshuang/nacos-related/nacos/logs/start.out

Step 5 : Access through the browser: http://127.0.0.1:8848 /nacos Log in to the nacos console. The default account name and password are: nacos, nacos.

https://tva1.sinaimg.cn/large/e6c9d24ely1h3jghe1b57j215u0u0div.jpg

Note: MySQL is not required by default for stand-alone mode startup. If you want to use MySQL, you can modify the data source information in the configuration

Configuration management

Use the curl command to call Nacos' Open API to quickly experience Nacos's configuration management functions.

Release configuration :

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

After publishing the configuration through the curl command, you can view the configuration information in the configuration management-configuration list through the console.

https://tva1.sinaimg.cn/large/e6c9d24ely1h3jh75hwdhj22300tgtcu.jpg

Get configuration :

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

https://tva1.sinaimg.cn/large/e6c9d24ely1h3jh8xya7ej21fa0eq761.jpg

Service Registration & Discovery

Use the curl command to call Nacos' Open API to quickly experience Nacos' service discovery function.

Service registration :

 curl -X POST 'http://127.0.0.1:8848/nacos/v1/ns/instance?serviceName=nacos.naming.serviceName&ip=20.18.7.10&port=8080'

After executing the curl command, you can view the registered service information in the console service management - service list.

https://tva1.sinaimg.cn/large/e6c9d24ely1h3jhexgudnj223i0quwie.jpg

Service discovery :

 curl -X GET 'http://127.0.0.1:8848/nacos/v1/ns/instance/list?serviceName=nacos.naming.serviceName'

https://tva1.sinaimg.cn/large/e6c9d24ely1h3jhi77yujj21no0ki0y3.jpg

Using MySQL

If you want Nacos to use MySQL as the underlying storage, you first need to find nacos-mysql.sql in the nacos/conf directory. Execute the sql script to create the nacos related library table.

Then you only need to modify the application.properties file, first cd to the nacos/conf directory, use the vim command to modify the application.properties file, and find the following comment:

 #*************** Config Module Related Configurations ***************#
### If use 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?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
# db.user.0=nacos
# db.password.0=nacos

Release the comment, modify the database link of db.url.0 to the actual database address to be connected, and modify the corresponding user and password.

 #*************** Config Module Related Configurations ***************#
### If use 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?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
db.user.0=root
db.password.0=12345678

After modifying the above configuration, start nacos through the startup script in the nacos/bin directory:

 sh startup.sh -m standalone

Debug source code

Step 1 : Clone the nacos repository to the local via the git command

 git clone https://github.com/alibaba/nacos.git

Step 2 : Use idea to open nacos source code

https://tva1.sinaimg.cn/large/e6c9d24ely1h3jl16bygzj21bg0u0got.jpg

Step 3 : Use the mvn clean compile -U -Dmaven.test.skip=true command to compile the project

https://tva1.sinaimg.cn/large/e6c9d24ely1h3jlbydm93j20yi0u0dn0.jpg

Step 4 : Find the com.alibaba.nacos.Nacos startup class, add the VM parameter -Dnacos.standalone=true when the idea starts, and then start it again to debug the nacos source code.

https://tva1.sinaimg.cn/large/e6c9d24ely1h3jllyxo7sj219c0u0tbp.jpg


晓双
76 声望15 粉丝

🏅Alibaba Nacos & Apache EventMesh Committer