Nacos 部署
Nacos 部署说明
Nacos 快速开始
Nacos 快速开始
版本选择
当前推荐的稳定版本为2.X
Releases · alibaba/nacos · GitHuban easy-to-use dynamic service discovery, configuration and service management platform for building cloud native applications. - Releases · alibaba/nacoshttps://github.com/alibaba/nacos/releases
Nacos支持三种部署模式:
-
单机模式 - 用于测试和单机试用。
-
集群模式 - 用于生产环境,确保高可用。
-
多集群模式 - 用于多数据中心场景。
环境准备
-
安装好 JDK,需要 1.8 及其以上版本
-
建议: 2核 CPU / 4G 内存 及其以上
-
建议: 生产环境 3 个节点 及其以上
单机部署
预备环境准备
Nacos 依赖 Java 环境来运行。如果您是从代码开始构建并运行Nacos,还需要为此配置mavenWelcome to Apache Maven – Maven环境,请确保是在以下版本环境中安装使用:
1. 64 bit OS,支持 Linux/Unix/Mac/Windows,推荐选用 Linux/Unix/Mac。
2. 64 bit JDK 1.8+;下载 & 配置
3. Maven 3.2.x+;下载 & 配置
下载源码或者安装包
你可以通过源码和发行包两种方式来获取 Nacos。
从 Github 上下载源码方式
git clone https://github.com/alibaba/nacos.git
cd nacos/
mvn -Prelease-nacos -Dmaven.test.skip=true clean install -U
ls -al distribution/target/
Maven环境,请确保是在以下版本环境中安装使用:
// change the $version to your actual path
cd distribution/target/nacos-server-$version/nacos/bin
下载编译后压缩包方式
您可以从 最新稳定版本Releases · alibaba/nacos · GitHub 下载 nacos-server-$version.zip 包。
unzip nacos-server-$version.zip 或者 tar -xvf nacos-server-$version.tar.gz
cd nacos/bin
范例:
[root@ubuntu2204 ~]#apt update && apt -y install openjdk-11-jdk
[root@ubuntu2204 ~]#apt update && apt -y install openjdk-8-jdk
[root@ubuntu2204 ~]#wget https://github.com/alibaba/nacos/releases/download/2.2.3/nacos-server-2.2.3.tar.gz
[root@ubuntu2204 ~]#tar xf nacos-server-2.2.3.tar.gz -C /usr/local/
[root@ubuntu2204 ~]#ls /usr/local/nacos/
bin conf data LICENSE logs NOTICE target
[root@ubuntu2204 ~]#ls /usr/local/nacos/bin/
shutdown.cmd shutdown.sh startup.cmd startup.sh
[root@ubuntu2204 ~]#ls /usr/local/nacos/target/
nacos-server.jar
[root@ubuntu2204 ~]#ls /usr/local/nacos/data
derby-data naming protocol
[root@ubuntu2204 ~]#ls /usr/local/nacos/conf/
1.4.0-ipv6_support-update.sql application.properties cluster.conf.example mysql-schema.sql
announcement.conf application.properties.example derby-schema.sql nacos-logback.xml
#修改配置,可选
[root@nacos01 ~]#vi /usr/local/nacos/conf/application.properties
#修改默认的访问URL路径
#server.servlet.contextPath=/nacos
server.servlet.contextPath=/
启动和关闭服务器
注:Nacos的运行建议至少在2C4G 60G的机器配置下运行。
启动命令(standalone代表着单机模式运行,非集群模式):
sh startup.sh -m standalone
如果您使用的是ubuntu系统,或者运行脚本报错提示[[符号找不到,可尝试如下运行:
bash startup.sh -m standalone
Windows
启动命令(standalone代表着单机模式运行,非集群模式):
startup.cmd -m standalone
范例;
[root@provider ~]#startup.sh -m standalone
/usr/lib/jvm/java-8-openjdk-amd64/bin/java -Djava.ext.dirs=/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext:/usr/lib/jvm/java-8-openjdk-amd64/lib/ext -Xms512m -Xmx512m -Xmn256m -Dnacos.standalone=true -Dnacos.member.list= -Xloggc:/usr/local/nacos/logs/nacos_gc.log -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=10 -XX:GCLogFileSize=100M -Dloader.path=/usr/local/nacos/plugins,/usr/local/nacos/plugins/health,/usr/local/nacos/plugins/cmdb,/usr/local/nacos/plugins/selector -Dnacos.home=/usr/local/nacos -jar /usr/local/nacos/target/nacos-server.jar --spring.config.additional-location=file:/usr/local/nacos/conf/ --logging.config=/usr/local/nacos/conf/nacos-logback.xml --server.max-http-header-size=524288
nacos is starting with standalone
nacos is starting,you can check the /usr/local/nacos/logs/start.out#查看端口
[root@provider ~]#ss -antlp
LISTEN 0 4096 *:9849 *:* users:(("java",pid=34222,fd=186))
LISTEN 0 4096 *:9848 *:* users:(("java",pid=34222,fd=185))
LISTEN 0 100 *:8848 *:* users:(("java",pid=34222,fd=230))
LISTEN 0 4096 *:7848 *:* users:(("java",pid=34222,fd=188))
关闭服务
Linux/Unix/Mac
sh shutdown.sh
Windows
shutdown.cmd
服务注册&发现和配置管理
服务注册
curl -X POST 'http://127.0.0.1:8848/nacos/v1/ns/instance?serviceName=nacos.wang.serviceName&ip=1.2.3.4&port=8080'
服务发现
[root@provider ~]#curl -X GET 'http://127.0.0.1:8848/nacos/v1/ns/instance/list?serviceName=nacos.wang.serviceName'
{"name":"DEFAULT_GROUP@@nacos.wang.serviceName","groupName":"DEFAULT_GROUP","clusters":"","cacheMillis":10000,"hosts":[],"lastRefTime":1733151817374,"checksum":"","allIPs":false,"reachProtectionThreshold":false,"valid":true}
发布配置
curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=nacos.cfg.dataId&group=test&content=HelloWorld"
获取配置
curl -X GET "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=nacos.cfg.dataId&group=test"
Web 访问
http://ip:8848/nacos
单机模式支持 MySQL
在0.7版本之前,在单机模式时nacos使用嵌入式数据库实现数据的存储,不方便观察数据存储的基本情况。0.7版本增加了支持mysql数据源 能力,具体的操作步骤:
-
安装数据库,版本要求:5.6.5+
-
创建数据库nacos和用户nacos,并授权
-
执行数据库初始化脚本 mysql-schema.sql
-
修改conf/application.properties文件,增加支持mysql数据源配置(目前只支持mysql),添加mysql数据源的url、用户名和密码。
spring.datasource.platform=mysql
db.num=1
db.url.0=jdbc:mysql://127.0.0.1:3306/nacos?
characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
db.user=nacos
db.password=123456
再以单机模式启动nacos,nacos所有写嵌入式数据库的数据都写到了mysql
范例:
[root@provider ~]#apt update && apt -y install mysql-server
[root@provider ~]#mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.40-0ubuntu0.22.04.1 (Ubuntu)Copyright (c) 2000, 2024, Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> create user nacos@'127.0.0.1' identified with mysql_native_password by '123456';
Query OK, 0 rows affected (0.02 sec)mysql> create database nacos;
Query OK, 1 row affected (0.01 sec)mysql> grant all on nacos.* to nacos@'127.0.0.1';
Query OK, 0 rows affected (0.01 sec)mysql> exit[root@provider conf]#mysql -unacos -p123456 -h127.0.0.1 nacos < /usr/local/nacos/conf
/mysql-schema.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@provider conf]#mysql -unacos -p123456 -h127.0.0.1 nacos -e "show tables;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+----------------------+
| Tables_in_nacos |
+----------------------+
| config_info |
| config_info_aggr |
| config_info_beta |
| config_info_tag |
| config_tags_relation |
| group_capacity |
| his_config_info |
| permissions |
| roles |
| tenant_capacity |
| tenant_info |
| users |
+----------------------+[root@provider conf]#vim application.propertiesspring.sql.init.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=123456### Connection pool configuration: hikariCP
db.pool.config.connectionTimeout=30000
db.pool.config.validationTimeout=10000
db.pool.config.maximumPoolSize=20
db.pool.config.minimumIdle=2[root@provider conf]#shutdown.sh
[root@provider conf]#startup.sh -m standalone
[root@provider conf]#tail -f /usr/local/nacos/logs/start.out
/usr/lib/jvm/java-8-openjdk-amd64/bin/java -Djava.ext.dirs=/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext:/usr/lib/jvm/java-8-openjdk-amd64/lib/ext -Xms512m -Xmx512m -Xmn256m -Dnacos.standalone=true -Dnacos.member.list= -Xloggc:/usr/local/nacos/logs/nacos_gc.log -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=10 -XX:GCLogFileSize=100M -Dloader.path=/usr/local/nacos/plugins,/usr/local/nacos/plugins/health,/usr/local/nacos/plugins/cmdb,/usr/local/nacos/plugins/selector -Dnacos.home=/usr/local/nacos -jar /usr/local/nacos/target/nacos-server.jar --spring.config.additional-location=file:/usr/local/nacos/conf/ --logging.config=/usr/local/nacos/conf/nacos-logback.xml --server.max-http-header-size=5242882024-12-02 15:22:37,806 INFO Exposing 1 endpoint(s) beneath base path '/actuator'2024-12-02 15:22:37,938 INFO Tomcat started on port(s): 8848 (http) with context path '/nacos'2024-12-02 15:22:38,012 INFO Nacos started successfully in stand alone mode. use external storage
启用登录验证
Authorization
默认无需登录就可管理nacos,基于安全,可以启用登录验证功能
#生成toke的值,至少32位,否则无法启动
[root@provider conf]#openssl rand -base64 33
NRnDN4/TrzSGfyQlRNOO05QtIRViDH5T5adws+d+egS9[root@provider conf]#vim /usr/local/nacos/conf/application.properties#*************** Access Control Related Configurations ***************#......nacos.core.auth.system.type=nacos#nacos.core.auth.enabled=false
nacos.core.auth.enabled=true #修改此行为truenacos.core.auth.server.identity.key=chen
#指定值,否则无法启动
nacos.core.auth.server.identity.value=chen #指定值,否则无法启动
nacos.core.auth.plugin.nacos.token.secret.key=NRnDN4/TrzSGfyQlRNOO05QtIRViDH5T5adws+d+egS9 #添加前面生成的token的值[root@provider conf]#shutdown.sh
The nacosServer(101669) is running...
Send shutdown request to nacosServer(101669) OK
[root@provider conf]#startup.sh -m standalone
/usr/lib/jvm/java-8-openjdk-amd64/bin/java -Djava.ext.dirs=/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext:/usr/lib/jvm/java-8-openjdk-amd64/lib/ext -Xms512m -Xmx512m -Xmn256m -Dnacos.standalone=true -Dnacos.member.list= -Xloggc:/usr/local/nacos/logs/nacos_gc.log -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=10 -XX:GCLogFileSize=100M -Dloader.path=/usr/local/nacos/plugins,/usr/local/nacos/plugins/health,/usr/local/nacos/plugins/cmdb,/usr/local/nacos/plugins/selector -Dnacos.home=/usr/local/nacos -jar /usr/local/nacos/target/nacos-server.jar --spring.config.additional-location=file:/usr/local/nacos/conf/ --logging.config=/usr/local/nacos/conf/nacos-logback.xml --server.max-http-header-size=524288
nacos is starting with standalone
nacos is starting,you can check the /usr/local/nacos/logs/start.out
测试访问,默认用户名和密码都是nacos
http://ip:8848/nacos
密码保存在nacos库中的users表中
[root@provider conf]#mysql -unacos -p123456 -h127.0.0.1 nacos -e 'select * from users'
mysql: [Warning] Using a password on the command line interface can be insecure.
+----------+--------------------------------------------------------------+---------+
| username | password | enabled |
+----------+--------------------------------------------------------------+---------+
| nacos | $2a$10$EuWPZHzz32dJN7jexM34MOeYirDdFAZm2kuWj7VEOJhhZkDrxfvUu | 1 |
+----------+--------------------------------------------------------------+---------+