文章目录提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
- 前言
- 一、下载Nacos
- 二、安装数据库
- 1.MySQL数据库
- 2.MariaDB数据库
- 三、导入数据库结构
- 四、Nacos集群配置
- 1.配置JavaHome环境变量
- 2.配置端口及数据库```application.properties```
- 3.配置Cluster文件```cluster.conf```
- 4.复制nacos目录
- 5.运行nacos集群
- 总结
前言
Nacos集群部署是相当的不人性化,日志简直就是奇葩,太程序员有时候并不好,异常抛出来完全不知所以然,为了纪念这次的成功部署,特写此文进行记录。希望能够帮助更多的人。2022.6.23
一、下载Nacos
下载地址:https://github.com/alibaba/nacos/releases/
最新版本为 2.1.0。这里使用windows版本作为栗子,下载nacos-server-2.1.0.zip,下载完成后并解压。
Nacos默认只支持MySQL/MariaDB数据库,所以这里以MySQL/MariaDB为例。
1.MySQL数据库下载地址为:https://dev.mysql.com/downloads/installer/
最新版本为 8.0.29,安装过程不赘述,参考相关教程。
下载地址为:https://mariadb.org/download/?t=mariadb&p=mariadb&r=10.10.0
最新版本为10.10.0,安装过程不赘述,参考相关教程。
打开解压后nacos文件夹,找到conf/nacos-mysql.sql文件,将其导入到数据库中,在此之前,请自行创建数据库实例,后续将以nacos实例为栗子。
四、Nacos集群配置 1.配置JavaHome环境变量首先打开cmd,输入
echo %JAVA_HOME%
查看JavaHome是否存在,如果不存在,我们有两种方式。
第一种:将JAVA_HOME配置到环境变量中,自行百度。点此直达
第二种:用文本编辑器打开nacos文件夹下的bin/startup.cmd文件,找到文件开头部分最后一行rem,加入
set JAVA_HOME=C:Program FilesJavajre1.8.0_331
头部全文为:
@echo off rem Copyright 1999-2018 Alibaba Group Holding Ltd. rem Licensed under the Apache License, Version 2.0 (the "License"); rem you may not use this file except in compliance with the License. rem You may obtain a copy of the License at rem rem http://www.apache.org/licenses/LICENSE-2.0 rem rem Unless required by applicable law or agreed to in writing, software rem distributed under the License is distributed on an "AS IS" BASIS, rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. rem See the License for the specific language governing permissions and rem limitations under the License. rem **********以下为加入的环境变量******** set JAVA_HOME=C:Program FilesJavajre1.8.0_331 rem **********以上为加入的环境变量******** if not exist "%JAVA_HOME%binjava.exe" echo Please set the JAVA_HOME variable in your environment, We need java(x64)! jdk8 or later is better! & EXIT /B 1 set "JAVA=%JAVA_HOME%binjava.exe" setlocal enabledelayedexpansion2.配置端口及数据库application.properties
打开Nacos目录下的conf/application.properties文件,找到如下配置:
# 默认为8848,我这里模拟了3台集群,所以端口号为8848,8850,8852 server.port=8848
不要问我为什么是8848、8850、8852,而不是8848、8849、8850,这就是大坑之一,鬼知道为什么会这样,有兴趣的同学,可以看看nacos的源码。如果是连续的,可能抛出端口被占用的异常(Address already in use: bind),而且是netty抛出来的。
然后找到
### 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=密码 ###这里配置你的数据库连接
这里一定要放开全,否则容易导致启动失败,抛出Unable to start embedded Tomcat异常。
3.配置Cluster文件cluster.conf将conf/cluster.conf.example复制保存为cluster.conf文件,修改内容为
# Copyright 1999-2021 Alibaba Group Holding Ltd. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # #it is ip #example localhost:8848 localhost:8850 localhost:8852
根据自己的实际情况配置ip地址。
4.复制nacos目录将整个nacos目录复制出额外两份,分别修改application.properties中的server.port=8848端口。改为8850,8852。这里值得注意的是,整个文件地址中切勿包含中文以及空格。
5.运行nacos集群双击bin/startup.cmd,运行,每个nacos都要运行,linux同理,运行startup.sh即可。
在浏览器中输入http://localhost:8848/nacos/index.html即可进入nacos管理后台。默认用户名密码为:nacos/nacos。
后台:
总结
nacos集群配置到此为止,配置过程中颇有曲折,由于后台异常相当不明确,有时候漏配,错配导致启动失败,然鹅,又没有相关友好的提示。希望阿里系的中间件能够越来越好。



