nacos-2.3.2單機和叢集部署

杨梅冲發表於2024-06-13

一、前期準備

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

官網:https://nacos.io/

1.1 資料庫部署

# mysql 8.0 下載
官網:https://dev.mysql.com/downloads/mysql/8.0.html

cd /usr/local/
tar xf mysql-8.0.36-linux-glibc2.17-x86_64.tar.xz
ln -s mysql-8.0.36-linux-glibc2.17-x86_64 mysql

echo "export PATH=$PATH:/usr/local/mysql8/bin" >>/etc/profile
source /etc/profile

vim /usr/local/mysql/my.cnf
[mysql]
#預設字符集
default-character-set=utf8mb4
socket = /usr/local/mysql/data/mysql.sock
[client]
port       = 3306
socket = /usr/local/mysql/data/mysql.sock
 
[mysqld]
port       = 3306
server-id  = 3306
socket     = /usr/local/mysql/data/mysql.sock
# 安裝目錄
basedir    = /usr/local/mysql
# 資料存放目錄
datadir    = /usr/local/mysql/data
log-bin    = /usr/local/mysql/data/mysql-bin
innodb_data_home_dir      = /usr/local/mysql/data
innodb_log_group_home_dir = /usr/local/mysql/data
#日誌及程序資料的存放目錄
log-error =/usr/local/mysql/data/mysql-err.log
pid-file  =/usr/local/mysql/data/mysql.pid
# 服務端使用的字符集預設為8位元編碼
character-set-server=utf8mb4
lower_case_table_names=1
autocommit =1
# 建立新表時將使用的預設儲存引擎
default_storage_engine = InnoDB

mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql --initialize-insecure

各引數意義
            --defaults-file: 指定配置檔案 (放在--initialize-insecure前)
            --user: 指定使用者
            --basedir: 指定安裝目錄
            --datadir: 指定初始化資料目錄
            --initialize-insecure: 初始化不設定密碼(若無該引數,則隨機生成密碼,需在 /data/mysql8/logs/mysql.log 檢視

# 啟動mysql
/usr/local/mysql8/bin/mysqld_safe --defaults-file=/usr/local/mysql8/my.cnf &

1.2 jdk下載部署

下載地址:https://www.oracle.com/cn/java/technologies/downloads/#java11

echo "export JAVA_HOME=/usr/local/jdk-11.0.23" >> /etc/profile
export PATH=$JAVA_HOME/bin:$PATH
source /etc/profile

[root@harbor mysql]# java -version
java version "11.0.23" 2024-04-16 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.23+7-LTS-222)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.23+7-LTS-222, mixed mode)

生成jre
cd /usr/local/jdk-11.0.23
bin/jlink --module-path jmods --add-modules java.desktop --output jre

二、nacos單機安裝

1.1 nacos單機安裝

下載地址:https://github.com/alibaba/nacos/releases

cd /usr/local/
tar -xzvf nacos-server-2.3.2.tar.gz
# 資料庫設定
# 1.建立nscos使用者,如果在nacos和mysql在2臺機器上
CREATE USER 'nacos'@'%';
ALTER USER 'nacos'@'%' IDENTIFIED BY 'nacos';
GRANT ALL privileges on nacos.* TO 'nacos'@'%';

# 如果nacos和mysql在同一臺機器上
CREATE user 'nacos'@'localhost';
ALTER USER 'nacos'@'localhost' IDENTIFIED BY 'nacos';
GRANT ALL privileges on nacos.* TO 'nacos'@'localhost';

FLUSH PRIVILEGES;

CREATE DATABASE nacos;
quit;

mysql -P 3307 -u root -p"wg1q2w3e" -D nacos < /usr/local/nacos/conf/mysql-schema.sql

mysql> use nacos
Database changed
mysql> show tables;
+----------------------+
| 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                |
+----------------------+
12 rows in set (0.00 sec)

# 配置nacos
vim /usr/local/nacos/conf/application.properties

db.url.0 中的 ip: 如果是mysql與nacos在同一臺機器上,就不用更改,使用原來的127.0.0.1。
db.url.0 中的 connectTimeout、socketTimeout 如果設定時間較短可能在啟動nacos時會連線不上資料庫而啟動失敗。

1.2 啟動nacos

cd /usr/local/nacos/bin
sh startup.sh -m standalone    # -m standalone 代表單機模式啟動
或
nohup sh startup.sh -m standalone &
[root@harbor conf]# netstat -ntlp | grep java
tcp6       0      0 :::7848                 :::*                    LISTEN      2318/java           
tcp6       0      0 :::8848                 :::*                    LISTEN      2318/java           
tcp6       0      0 :::9848                 :::*                    LISTEN      2318/java           
tcp6       0      0 :::9849                 :::*                    LISTEN      2318/java

# 8848 使用者web頁面訪問
# 98489849 用於2.x版本服務端連結
# 7848 Nacos叢集通訊埠,用於Nacos叢集間進行檢舉、檢測
https://nacos.io/zh-cn/docs/upgrading/2.0.0-compatibility/

# 停止服務
cd /usr/local/nacos/bin
sh shutdown.sh

# 瀏覽器訪問:
http://192.168.10.20:8848/nacos

1.3 叢集部署

配置nacos配置檔案/usr/local/nacos/conf/cluster.conf.example

cp cluster.conf.example cluster.conf

# 新增叢集中其它機器IP地址和埠資訊
192.168.10.20:8848
192.168.10.21:8848
192.168.10.22:8848

# 再配置 application.properties 

# 啟動nacos
cd /usr/local/bin/
nohup sh ./startup.sh &      # nacos 預設的啟動方式就是以叢集模式啟動

# 如果出現問題檢視日誌,虛擬機器一般就記憶體問題
檢視啟動日誌,cat /usr/local/nacos/logs/start.out ,或檢視 nacos 日誌cat /usr/local/nacos/logs/nacos.log

實際環境實際配置

相關文章