nacos叢集部署
1、簡介
Nacos是阿裡巴巴開源的一款支援服務注冊與發現,配置管理以及微服務管理的元件。用來取代以前常用的注冊中心(zookeeper , eureka等等),以及配置中心(spring cloud config等等)。Nacos是內建了注冊中心和配置中心的功能,做到了二合一。
2、叢集架構圖
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiAzNfRHLGZkRGZkRfJ3bs92YsYTMfVmepNHLxkkaOpXWq5UMNpHW4Z0MMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2X0hXZ0xCMx81dvRWYoNHLrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZuBnLyQzMzUDOyATM3ADNwEjMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
3、基礎環境
1、JDK 1.8+;
2、MySQL 5.6.5+(生産使用建議至少主備模式,或者采用高可用資料庫);
3、3個或3個以上Nacos節點才能構成叢集。
4、部署節點
HOSTNAME | IP |
---|---|
nacos-01 | 10.0.8.101 |
nacos-02 | 10.0.8.102 |
nacos-03 | 10.0.8.103 |
mysql | 10.0.8.104 |
nginx | 10.0.8.105 |
5、部署nacos叢集
5.1、 安裝jdk1.8+(nacos三台機器均執行)
[[email protected] java]# cd /usr/java/
[[email protected] java]# ll
-rw-r--r-- 1 yukw yukw 181352138 4月 7 11:55 jdk-8u101-linux-x64.tar.gz
[[email protected] java]# tar xf jdk-8u101-linux-x64.tar.gz
[[email protected] java]# ll
drwxr-xr-x 8 10 143 4096 6月 22 2016 jdk1.8.0_101
-rw-r--r-- 1 yukw yukw 181352138 4月 7 11:55 jdk-8u101-linux-x64.tar.gz
[[email protected] java]# vim /etc/profile (最後添加如下資訊)
export JAVA_HOME=/usr/java/jdk1.8.0_101
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
[[email protected] java]# source /etc/profile
[ root @ nacos-01 10.0.8.101 ] /usr/java
# java -version
java version "1.8.0_101"
Java(TM) SE Runtime Environment (build 1.8.0_101-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.101-b13, mixed mode)
5.2、 安裝nacos叢集(nacos三台機器均執行)
$ pwd
/data/service
$ ll
-rw-rw-r-- 1 yukw yukw 74805643 4月 7 12:10 nacos-server-1.3.1.tar.gz
$ tar xf nacos-server-1.3.1.tar.gz
$ ll
drwxrwxr-x 5 yukw yukw 72 4月 7 12:11 nacos
-rw-rw-r-- 1 yukw yukw 74805643 4月 7 12:10 nacos-server-1.3.1.tar.gz
$ cd nacos/
$ ll
drwxrwxr-x 2 yukw yukw 82 4月 7 12:11 bin
drwxr-xr-x 2 yukw yukw 168 7月 10 2020 conf
-rw-r--r-- 1 yukw yukw 16583 7月 10 2020 LICENSE
-rw-r--r-- 1 yukw yukw 1305 5月 14 2020 NOTICE
drwxrwxr-x 2 yukw yukw 30 4月 7 12:11 target
[ yukw @ nacos-01 10.0.8.101 ] /data/service/nacos
$ cd conf/
修改conf/application.properties檔案,增加支援mysql資料源配置(目前隻支援mysql),添加mysql資料源的url、使用者名和密碼。進入conf目錄下(cd conf),編輯application.properties檔案
$ 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://10.0.8.104:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
db.user=nacos
db.password=nacos
修改後,按Esc,儲存并退出(:wq)
修改叢集配置檔案,将三台機器加入叢集
$ cp cluster.conf.example cluster.conf
$ ll
總用量 72
-rw-r--r-- 1 yukw yukw 6410 4月 7 13:48 application.properties
-rw-r--r-- 1 yukw yukw 6416 7月 10 2020 application.properties.example
-rw-r--r-- 1 yukw yukw 670 4月 7 13:49 cluster.conf
-rw-r--r-- 1 yukw yukw 670 7月 10 2020 cluster.conf.example
-rw-r--r-- 1 yukw yukw 25714 7月 10 2020 nacos-logback.xml
-rw-r--r-- 1 yukw yukw 10660 7月 10 2020 nacos-mysql.sql
-rw-r--r-- 1 yukw yukw 8073 7月 10 2020 schema.sql
$ vim cluster.conf (将nacos三台機器加入叢集中)
10.0.8.101:8848
10.0.8.102:8848
10.0.8.103:8848
5.3、初始化資料庫(mysql機器上執行)
這裡部落客省略掉了安裝mysql的步驟,請自行安裝mysql服務
将nacos機器上nacos/conf目錄下的nacos-mysql.sql檔案拷貝到mysql機器,然後進行資料庫導庫初始化
$ mysql -uroot -p
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8219449
Server version: 5.7.14-log MySQL Community Server (GPL)
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
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 DATABASE nacos DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
Query OK, 0 rows affected, 1 warning (0.16 sec)
mysql> grant all privileges on nacos.* to nacos@'localhost' identified by 'nacos';
Query OK, 0 rows affected, 2 warnings (0.16 sec)
mysql> grant all privileges on nacos.* to nacos@'127.0.0.1' identified by 'nacos';
Query OK, 0 rows affected, 1 warning (0.08 sec)
mysql> grant all privileges on nacos.* to nacos@'%' identified by 'nacos';
Query OK, 0 rows affected, 1 warning (0.06 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.24 sec)
mysql> commit;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
$ mysql -unacos -pnacos nacos < nacos-mysql.sql
5.4、啟動nacos服務(nacos三台機器均執行)
$ cd /data/service/nacos/bin/
$ ll
總用量 20
-rwxr-xr-x 1 yukw yukw 954 5月 14 2020 shutdown.cmd
-rwxr-xr-x 1 yukw yukw 949 7月 3 2020 shutdown.sh
-rwxr-xr-x 1 yukw yukw 3089 7月 10 2020 startup.cmd
-rwxr-xr-x 1 yukw yukw 5026 7月 10 2020 startup.sh
$ ./startup.sh
/usr/java/jdk1.8.0_101/bin/java -server -Xms2g -Xmx2g -Xmn1g -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m -XX:-OmitStackTraceInFastThrow -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/data/service/nacos/logs/java_heapdump.hprof -XX:-UseLargePages -Dnacos.member.list= -Djava.ext.dirs=/usr/java/jdk1.8.0_101/jre/lib/ext:/usr/java/jdk1.8.0_101/lib/ext -Xloggc:/data/service/nacos/logs/nacos_gc.log -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=10 -XX:GCLogFileSize=100M -Dloader.path=/data/service/nacos/plugins/health,/data/service/nacos/plugins/cmdb -Dnacos.home=/data/service/nacos -jar /data/service/nacos/target/nacos-server.jar --spring.config.location=classpath:/,classpath:/config/,file:./,file:./config/,file:/data/service/nacos/conf/ --logging.config=/data/service/nacos/conf/nacos-logback.xml --server.max-http-header-size=524288
nacos is starting with cluster
nacos is starting,you can check the /data/service/nacos/logs/start.out
到這裡,nacos叢集就已經部署完成了,接下來需要通過nginx代理來保障服務的高可用性和負載均衡
6、 配置nginx代理(nginx機器上執行)
這裡部落客省略掉了安裝nginx的步驟,請自行安裝nginx服務,修改nginx.conf配置檔案,添加如下内容
upstream nacos{
#sticky expires=8h;
server 10.0.8.101:8848;
server 10.0.8.102:8848;
server 10.0.8.103:8848;
check interval=3000 rise=2 fall=2 timeout=1000 ;
}
server{
listen 8848;
server_name localhost;
access_log logs/nacos-access.log;
error_log logs/nacos-error.log;
location / {
proxy_store off;
proxy_redirect off;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://nacos; ##微服務注冊位址
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
修改後,儲存退出,重新加載nginx
/usr/sbin/nginx -s reload
7、 通路驗證
打開浏覽器,輸入通路位址 10.0.8.105:8848/nacos
預設賬号密碼 nacos/nacos
附:
1、nacos可以配置單機模式或者叢集模式,單機模式可以不加mysql資料源配置,直接将資料儲存在記憶體中
如果需要配置為單機模式,需修改啟動檔案,預設為cluster叢集模式
$ vim /data/service/nacos/bin/startup.sh
好了,這就是nacos叢集部署的方法了,如有問題可與部落客一起交流讨論!