Mac OS下单机Docker实现Nacose集群部署 + Nginx代理 概述 最近在复习Springcloud相关的知识点,刚好看到微服务架构下服务治理相关的内容,在对比Springcloud各个服务治理组件后,对Nacos产生了极大的兴趣,为了在本地搭建一套服务治理集群,在网上参考了很多资料,发现针对Mac OS下Docker单机Nacos集群搭建的教程很零散,在经过一下午的踩坑后,终于在本地完成搭建及测试,为了方便自己后续的查阅及帮助网上的朋友少走弯路,决定记录下来。 准备工作 环境参数
Mac OS 13.6.1 (22G313) Intel版本 Nacos 2.0.3 ~ Nginx latest ~ MySQL 5.7.26 ~
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 docker pull nacos/nacos-server:2.0.3 mkdir -p ${user} /docker_resource/nacos/{nacos-01,nacos-02,nacos-03}docker run --name nacos-server -d nacos/nacos-server:2.0.3 docker cp nacos-server:/home/nacos/conf ${user} /docker_resource/nacos/nacos-01/ docker cp nacos-server:/home/nacos/logs ${user} /docker_resource/nacos/nacos-01/ docker cp nacos-server:/home/nacos/bin ${user} /docker_resource/nacos/nacos-01/ docker cp nacos-server:/home/nacos/data ${user} /docker_resource/nacos/nacos-01/ docker cp nacos-server:/home/nacos/conf ${user} /docker_resource/nacos/nacos-02/ docker cp nacos-server:/home/nacos/logs ${user} /docker_resource/nacos/nacos-02/ docker cp nacos-server:/home/nacos/bin ${user} /docker_resource/nacos/nacos-02/ docker cp nacos-server:/home/nacos/data ${user} /docker_resource/nacos/nacos-02/ docker cp nacos-server:/home/nacos/conf ${user} /docker_resource/nacos/nacos-03/ docker cp nacos-server:/home/nacos/logs ${user} /docker_resource/nacos/nacos-03/ docker cp nacos-server:/home/nacos/bin ${user} /docker_resource/nacos/nacos-03/ docker cp nacos-server:/home/nacos/data ${user} /docker_resource/nacos/nacos-03/ docker rm -f nacos-server:2.0.3 == nacos-01/conf/application.properties == spring.datasource.platform=mysql server.port=8848 spring.datasource.platform=mysql db.num=1 db.url.0=jdbc:mysql://192.168.1.4:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true &useUnicode=true &useSSL=false &serverTimezone=Asia/Shanghai db.user=root db.password=root == nacos-02/conf/application.properties == spring.datasource.platform=mysql server.port=8858 spring.datasource.platform=mysql db.num=1 db.url.0=jdbc:mysql://192.168.1.4:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true &useUnicode=true &useSSL=false &serverTimezone=Asia/Shanghai db.user=root db.password=root == nacos-03/conf/application.properties == spring.datasource.platform=mysql server.port=8868 spring.datasource.platform=mysql db.num=1 db.url.0=jdbc:mysql://192.168.1.4:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true &useUnicode=true &useSSL=false &serverTimezone=Asia/Shanghai db.user=root db.password=root == nacos-01 | nacos-02 | nacos-03 /cluster.conf 三个文件内容一致 == == 192.168.1.4 为 ifconfig 命令执行后得到的宿主机IP,需替换为自己的本地IP == 192.168.1.4:8848 192.168.1.4:8858 192.168.1.4:8868 == nacos-01 | nacos-01 | nacos-01 /docker-startup.sh 三个文件内容一致 == == 替换 print_servers()方法实现 function print_servers () { if [[ ! -d "${PLUGINS_DIR} " ]]; then for server in `cat ../conf/cluster.conf`; do echo "$server " >>"$CLUSTER_CONF " done else bash $PLUGINS_DIR /plugin.sh sleep 30 fi } == 调整 JVM 参数 根据宿主机可用内存设置 == JAVA_OPT="${JAVA_OPT} -server -Xms512m -Xmx512m -Xmn512m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m"
Nacos持久化MySQL配置
1 2 3 4 5 6 7 1、下载 Nacos 数据库脚本 https://github.com/alibaba/nacos/releases/download/2.0.3/nacos-server-2.0.3.zip 解压后复制 conf/nacos-mysql.sql 内容至 Navicat中执行脚本。 GRANT ALL PRIVILEGES ON *.* TO 'root' @'192.168.1.4' IDENTIFIED BY 'root' WITH GRANT OPTION;
Nacos集群部署 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 ============= nacos-01 ================ docker run \ --name nacos-01 -d \ --privileged=true \ --restart=always \ -p 8848:8848 \ -p 9848:9848 \ -p 9849:9849 \ -e MODE=cluster \ -e PREFER_HOST_MODE=hostname \ -e NACOS_SERVER_IP=192.168.1.4 \ -v ${user} /docker_resource/nacos/nacos-01/logs:/home/nacos/logs \ -v ${user} /docker_resource/nacos/nacos-01/data:/home/nacos/data \ -v ${user} /docker_resource/nacos/nacos-01/conf:/home/nacos/conf \ -v ${user} /docker_resource/nacos/nacos-01/bin:/home/nacos/bin \ nacos/nacos-server:2.0.3 ============= nacos-02 ================ docker run \ --name nacos-02 -d \ --privileged=true \ --restart=always \ -p 8858:8858 \ -p 9858:9858 \ -p 9859:9859 \ -e MODE=cluster \ -e PREFER_HOST_MODE=hostname \ -e NACOS_SERVER_IP=192.168.1.4 \ -v ${user} /docker_resource/nacos/nacos-02/logs:/home/nacos/logs \ -v ${user} /docker_resource/nacos/nacos-02/data:/home/nacos/data \ -v ${user} /docker_resource/nacos/nacos-02/conf:/home/nacos/conf \ -v ${user} /docker_resource/nacos/nacos-02/bin:/home/nacos/bin \ nacos/nacos-server:2.0.3 ============= nacos-03 ================ docker run \ --name nacos-03 -d \ --privileged=true \ --restart=always \ -p 8868:8868 \ -p 9868:9868 \ -p 9869:9869 \ -e MODE=cluster \ -e PREFER_HOST_MODE=hostname \ -e NACOS_SERVER_IP=192.168.1.4 \ -v ${user} /docker_resource/nacos/nacos-03/logs:/home/nacos/logs \ -v ${user} /docker_resource/nacos/nacos-03/data:/home/nacos/data \ -v ${user} /docker_resource/nacos/nacos-03/conf:/home/nacos/conf \ -v ${user} /docker_resource/nacos/nacos-03/bin:/home/nacos/bin \ nacos/nacos-server:2.0.3
Nginx配置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 docker pull nginx mkdir -p ${user} /docker_resource/nginx/{conf,html,log }docker run -d --name nginx nginx docker cp nginx:/etc/nginx/conf.d ${user} /docker_resource/nginx/conf docker cp nginx:/etc/nginx/nginx.conf ${user} /docker_resource/nginx/conf/nginx.conf docker cp nginx:/usr/share/nginx/html/index.html ${user} /docker_resource/nginx/html/index.html docker rm -f nginx upstream nacos{ server 192.168.1.4:8848; server 192.168.1.4:8858; server 192.168.1.4:8868; } server { listen 8899; server_name _; location / { proxy_pass http://nacos; } } docker run --name nginx -v ${user} /docker_resource/nginx/conf/nginx.conf:/etc/nginx/nginx.conf:ro -p 8899:8899 -d nginx
测试 http://192.168.1.4:8899/nacos/#/login
## 总结该 Nacos 集群采用挂载启动集群方式部署,至少需要部署3个节点,如果本地测试需要使用域名访问,内网可以使用绑定host方式,外网需要内网穿透工具做映射。