nacos集群部署
1、简介
Nacos是阿里巴巴开源的一款支持服务注册与发现,配置管理以及微服务管理的组件。用来取代以前常用的注册中心(zookeeper , eureka等等),以及配置中心(spring cloud config等等)。Nacos是集成了注册中心和配置中心的功能,做到了二合一。
2、集群架构图
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三台机器均执行)
[root@nacos-01 java]# cd /usr/java/
[root@nacos-01 java]# ll
-rw-r--r-- 1 yukw yukw 181352138 4月 7 11:55 jdk-8u101-linux-x64.tar.gz
[root@nacos-01 java]# tar xf jdk-8u101-linux-x64.tar.gz
[root@nacos-01 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
[root@nacos-01 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
[root@nacos-01 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集群部署的方法了,如有问题可与博主一起交流讨论!