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: openssl@1.1 ?, 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
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/384193.html
標籤:其他
