当前位置:网站首页>【On nacos】快速上手 Nacos
【On nacos】快速上手 Nacos
2022-06-27 11:31:00 【俩佑】
如果大家想要了解更多的 Nacos 教程,欢迎 star 《On Nacos》开源项目。基于 Nacos 2.x 的入门、原理、源码、实战介绍,帮助开发者快速上手 Nacos。
安装包
安装
步骤一:可以通过 https://github.com/alibaba/nacos/releases 找到对应的版本,下载打包好的 Nacos。可使用以下命令下载对应的 Nacos版本。目前下载的是2.1.0 版本,可使用以下命令:
wget https://github.com/alibaba/nacos/releases/download/2.1.0/nacos-server-2.1.0.tar.gz
步骤二:下载完压缩包以后需要通过命令进行解压:
tar -xzf nacos-server-2.1.0.tar.gz
步骤三:cd 到 nacos 目录下,先来介绍下目录结构
.
|____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
步骤四:单机模式启动 Nacos
Linux/Unix/Mac
启动命令( -m standalone 表示单机模式启动):
sh startup.sh -m standalone
如果您使用的是ubuntu系统,或者运行脚本报错提示[[符号找不到,可尝试如下运行:
bash startup.sh -m standalone
Windows
启动命令( -m standalone 表示单机模式启动):
startup.cmd -m standalone
出现以下日志说明已经启动 naocs 了,详细的日志要看 nacos/logs/start.out。
nacos is starting with standalone
nacos is starting,you can check the /Users/lixiaoshuang/nacos-related/nacos/logs/start.out
步骤五:通过浏览器访问:http://127.0.0.1:8848/nacos 登录nacos 控制台,默认的账户名和密码是:nacos、nacos。

备注:单机模式启动默认是不需要 MySQL 的,如果想要使用 MySQL 可以修改配置中的数据源信息
配置管理
通过 curl 命令调用 Nacos 的 Open API 来快速体验一下 Nacos 的配置管理功能。
发布配置:
curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=nacos.cfg.dataId&group=test&content=Hello Nacos"
通过 curl 命令发布完配置后,可以在通过控制台配置管理-配置列表中查看到配置信息。

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

服务注册&发现
通过 curl 命令调用 Nacos 的 Open API 来快速体验一下 Nacos 的服务发现功能。
服务注册:
curl -X POST 'http://127.0.0.1:8848/nacos/v1/ns/instance?serviceName=nacos.naming.serviceName&ip=20.18.7.10&port=8080'
执行完 curl 命令后 ,可以在控制台服务管理-服务列表中查看到注册的服务信息。

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

使用 MySQL
如果想要 Nacos 使用 MySQL 来做底层存储的话,首先需要找到 nacos/conf 目录下的 nacos-mysql.sql 。执行 sql 脚本创建 nacos 相关库表。
然后就只需要修改 application.properties 文件,首先 cd 到 nacos/conf 目录下,用 vim 命令修改 application.properties 文件,找到下面这段注释:
#*************** 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
将注释放开,修改 db.url.0 的数据库链接为实际要连的数据库地址,修改对应的 user、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
当修改完以上配置以后,在通过 nacos/bin 目录下的启动脚本启动 nacos 即可:
sh startup.sh -m standalone
调试源码
步骤一:通过 git 命令将 nacos 仓库 clone 到本地
git clone https://github.com/alibaba/nacos.git
步骤二:使用idea 打开 nacos 源码

步骤三:使用 mvn clean compile -U -Dmaven.test.skip=true 命令编译下项目

步骤四:找到 com.alibaba.nacos.Nacos 启动类,在 idea 启动时添加 VM 参数-Dnacos.standalone=true,然后再启动,就可以调试 nacos 源码啦。

边栏推荐
- Jerry's serial port communication serial port receiving IO needs to set digital function [chapter]
- C语言0长度数组的妙用
- 【TcaplusDB知识库】TcaplusDB系统用户组介绍
- One copy ten, CVPR oral is accused of plagiarizing a lot
- JSP custom tag
- AUTOCAD——三种修剪方式
- Unity Shader学习(二)第一个Shader
- Code for structural design of proe/creo household appliances - electric frying pan
- Leetcode 177 The nth highest salary (June 26, 2022)
- pull request
猜你喜欢
Jerry's DAC output mode setting [chapter]
Unity shader learning (II) the first shader
[tcapulusdb knowledge base] tcapulusdb doc acceptance - transaction execution introduction
[tcapulusdb knowledge base] tcapulusdb doc acceptance - table creation approval introduction
Salesforce 容器化 ISV 场景下的软件供应链安全落地实践
Youboxun attended the openharmony technology day to create a new generation of secure payment terminals
【TcaplusDB知识库】TcaplusDB单据受理-事务执行介绍
KDD 2022 | epileptic wave prediction based on hierarchical graph diffusion learning
21: Chapter 3: develop pass service: 4: further improve [send SMS, interface]; (in [send SMS, interface], call Alibaba cloud SMS service and redis service; a design idea: basecontroller;)
杰理之串口通信 串口接收IO需要设置数字功能【篇】
随机推荐
Unity shader learning (I) understanding the basic structure of unity shader
How to adjust an integer that is entered in Excel but always displays decimals?
Precautions for use of IO interface interrupt of Jerry [chapter]
Open source model library of flying propeller industry: accelerating the development and application of enterprise AI tasks
L'utilisation de C language 0 length Array
Usage of rxjs mergemap
15+ urban road element segmentation application, this segmentation model is enough!
Heap heap sort TOPK
Codeforces Round #786 (Div. 3) ABCDE
R语言使用epiDisplay包的followup.plot函数可视化多个ID(病例)监测指标的纵向随访图、使用stress.labels参数在可视化图像中为强调线添加标签信息
千万不要错过,新媒体运营15个宝藏公众号分享
After Jerry's sleep, the regular wake-up system continues to run without resetting [chapter]
The GLM function of R language is used to build a binary logistic regression model (the family parameter is binomial), and the AIC function is used to compare the AIC values of the two models (simple
等等, 怎么使用 SetMemoryLimit?
The R language uses the DOTPLOT function of epidisplay package to visualize the frequency of data points in different intervals in the form of point graph, specifies the grouping parameters with the b
Don't miss it. New media operates 15 treasure official account to share
2022ciscn central China Web
政策关注 | 加快构建数据基础制度,维护国家数据安全
C语言0长度数组的妙用
Oracle-分组统计查询