SpringCloud-Spring Cloud Alibaba Nacos集群架构(Day11)

Nacos集群架构

在这里插入图片描述
nacos默认自带的是嵌入式数据库derby,在0.7版本之前,在单机模式时nacos使用嵌入式数据库实现数据的存储,不方便观察数据存储的基本情况。0.7版本增加了支持mysql数据源能力,具体的操作步骤:

  • 安装数据库,版本要求:5.6.5+
  • 初始化mysq数据库,数据库初始化文件: nacos-mysql.sql
  • 修改conf/application.properties文件,增加支持mysql数据源配置(目前只支持mysql),添加mysql数据源的url、用户名和密码。
############################################################
spring.datasource.platform=mysql
db.num=1
db.url.0=jdbc:mysql://127.0.0.1:3306/nacos_config?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
db.user=root
db.password=123456

修改数据库

在这里插入图片描述

修改集群地址配置

cp cluster.comf.example cluster.conf

192.168.0.135:3333
192.168.0.135:4444
192.168.0.135:5555

在这里插入图片描述

修改Nacos启动脚本

while getopts ":m:f:s:p:" opt
do
    case $opt in
        m)
            MODE=$OPTARG;;
        f)
            FUNCTION_MODE=$OPTARG;;
        s)
            SERVER=$OPTARG;;
        p)
            PORT=$OPTARG;;           
        ?)
        echo "Unknown parameter"
        exit 1;;
    esac
done

在这里插入图片描述

# start
echo "$JAVA ${JAVA_OPT}" > ${BASE_DIR}/logs/start.out 2>&1 &
nohup $JAVA -Dserver.port=${PORT} ${JAVA_OPT} nacos.nacos >> ${BASE_DIR}/logs/start.out 2>&1 &
echo "nacos is starting,you can check the ${BASE_DIR}/logs/start.out"

在这里插入图片描述

启动

 ~/Downloads/nacos/bin/ ./startup.sh -p 3333
/Library/Java/JavaVirtualMachines/jdk1.8.0_301.jdk/Contents/Home/bin/java  -server -Xms2g -Xmx2g -Xmn1g -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m -XX:-OmitStackTraceInFastThrow -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/Users/guochongling/Downloads/nacos/logs/java_heapdump.hprof -XX:-UseLargePages -Djava.ext.dirs=/Library/Java/JavaVirtualMachines/jdk1.8.0_301.jdk/Contents/Home/jre/lib/ext:/Library/Java/JavaVirtualMachines/jdk1.8.0_301.jdk/Contents/Home/lib/ext:/Users/guochongling/Downloads/nacos/plugins/cmdb:/Users/guochongling/Downloads/nacos/plugins/mysql -Xloggc:/Users/guochongling/Downloads/nacos/logs/nacos_gc.log -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=10 -XX:GCLogFileSize=100M -Dnacos.home=/Users/guochongling/Downloads/nacos -Dloader.path=/Users/guochongling/Downloads/nacos/plugins/health -jar /Users/guochongling/Downloads/nacos/target/nacos-server.jar  --spring.config.location=classpath:/,classpath:/config/,file:./,file:./config/,file:/Users/guochongling/Downloads/nacos/conf/ --logging.config=/Users/guochongling/Downloads/nacos/conf/nacos-logback.xml --server.max-http-header-size=524288
nacos is starting with cluster
nacos is starting,you can check the /Users/guochongling/Downloads/nacos/logs/start.out
 ~/Downloads/nacos/bin/ ./startup.sh -p 4444
/Library/Java/JavaVirtualMachines/jdk1.8.0_301.jdk/Contents/Home/bin/java  -server -Xms2g -Xmx2g -Xmn1g -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m -XX:-OmitStackTraceInFastThrow -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/Users/guochongling/Downloads/nacos/logs/java_heapdump.hprof -XX:-UseLargePages -Djava.ext.dirs=/Library/Java/JavaVirtualMachines/jdk1.8.0_301.jdk/Contents/Home/jre/lib/ext:/Library/Java/JavaVirtualMachines/jdk1.8.0_301.jdk/Contents/Home/lib/ext:/Users/guochongling/Downloads/nacos/plugins/cmdb:/Users/guochongling/Downloads/nacos/plugins/mysql -Xloggc:/Users/guochongling/Downloads/nacos/logs/nacos_gc.log -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=10 -XX:GCLogFileSize=100M -Dnacos.home=/Users/guochongling/Downloads/nacos -Dloader.path=/Users/guochongling/Downloads/nacos/plugins/health -jar /Users/guochongling/Downloads/nacos/target/nacos-server.jar  --spring.config.location=classpath:/,classpath:/config/,file:./,file:./config/,file:/Users/guochongling/Downloads/nacos/conf/ --logging.config=/Users/guochongling/Downloads/nacos/conf/nacos-logback.xml --server.max-http-header-size=524288
nacos is starting with cluster
nacos is starting,you can check the /Users/guochongling/Downloads/nacos/logs/start.out
 ~/Downloads/nacos/bin/ ./startup.sh -p 5555
/Library/Java/JavaVirtualMachines/jdk1.8.0_301.jdk/Contents/Home/bin/java  -server -Xms2g -Xmx2g -Xmn1g -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m -XX:-OmitStackTraceInFastThrow -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/Users/guochongling/Downloads/nacos/logs/java_heapdump.hprof -XX:-UseLargePages -Djava.ext.dirs=/Library/Java/JavaVirtualMachines/jdk1.8.0_301.jdk/Contents/Home/jre/lib/ext:/Library/Java/JavaVirtualMachines/jdk1.8.0_301.jdk/Contents/Home/lib/ext:/Users/guochongling/Downloads/nacos/plugins/cmdb:/Users/guochongling/Downloads/nacos/plugins/mysql -Xloggc:/Users/guochongling/Downloads/nacos/logs/nacos_gc.log -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=10 -XX:GCLogFileSize=100M -Dnacos.home=/Users/guochongling/Downloads/nacos -Dloader.path=/Users/guochongling/Downloads/nacos/plugins/health -jar /Users/guochongling/Downloads/nacos/target/nacos-server.jar  --spring.config.location=classpath:/,classpath:/config/,file:./,file:./config/,file:/Users/guochongling/Downloads/nacos/conf/ --logging.config=/Users/guochongling/Downloads/nacos/conf/nacos-logback.xml --server.max-http-header-size=524288
nacos is starting with cluster
nacos is starting,you can check the /Users/guochongling/Downloads/nacos/logs/start.out
 ~/Downloads/nacos/bin/ 

验证

 ~/Downloads/nacos/bin/ ps -ef | grep nacos|grep -v grep|wc -l
       3
 ~/Downloads/nacos/bin/ 

架构解析

在这里插入图片描述

我们目前已经将数据库和三个Nacos服务已经搞定了,我们还需要去配置Nginx服务就可以了。

Nginx配置

我使用的是Macos为了能够安装方便一点,我就使用Homebrew安装nginx。考虑到大家在使用Homebrew中间可能会遇到一些问题,我这里就重点说明一下。

Macos安装Homebrew

/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"

在这里插入图片描述

Homebrew安装Nginx遇到443问题

macOS安装Homebrew时总是报错(Failed to connect to raw.githubusercontent.com port 443: Connection refused),由于一些因素,导致GitHub的raw.githubusercontent.com域名解析被污染了。解决办法如下

  • https://www.ipaddress.com/,通过该网站查询raw.githubusercontent.com的真实IP。
  • 按照查询出来的IP修改本机的hosts文件。
    在这里插入图片描述
    在这里插入图片描述

Homebrew安装Nginx并查看信息

brew install nginx
 ~/ brew info nginx 
nginx: stable 1.21.4, HEAD
HTTP(S) server and reverse proxy, and IMAP/POP3 proxy server
https://nginx.org/
/usr/local/Cellar/nginx/1.21.4 (11 files, 2.1MB) *
  Built from source on 2021-12-17 at 09:09:15
From: https://mirrors.ustc.edu.cn/homebrew-core.git/Formula/nginx.rb
License: BSD-2-Clause
==> Dependencies
Required: [email protected] ✔, pcre ✔
==> Options
--HEAD
	Install HEAD version
==> Caveats
Docroot is: /usr/local/var/www

The default port has been set in /usr/local/etc/nginx/nginx.conf to 8080 so that
nginx can run without sudo.

nginx will load all files in /usr/local/etc/nginx/servers/.

To restart nginx after an upgrade:
  brew services restart nginx
Or, if you don't want/need a background service you can just run:
  /usr/local/opt/nginx/bin/nginx -g daemon off;
==> Analytics
install: 36,449 (30 days), 121,098 (90 days), 504,359 (365 days)
install-on-request: 36,406 (30 days), 120,869 (90 days), 503,241 (365 days)
build-error: 56 (30 days)
 ~/ 

修改默认的conf,其位置在/usr/local/etc/nginx/nginx.conf

在finder中打开对应的文件夹

 open -a finder /usr/local/etc/nginx
 #gzip  on;

 upstream cluster{
     server 127.0.0.1:3333;
     server 127.0.0.1:4444;
     server 127.0.0.1:5555;
 }

 server {
     listen       8848;
     server_name  localhost;

     #charset koi8-r;

     #access_log  logs/host.access.log  main;

     location / {
         #root   html;
         #index  index.html index.htm;
         proxy_pass http://cluster;
     }

启动

 ~/ nginx

#

如果出现端口占用
lsof -i tcp:port
kill -9 pid

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码

)">
< <上一篇
下一篇>>