背景

Halo2.12版本修复索引问题:https://github.com/halo-dev/halo/pull/5121

但是该修复方式对“猫子的世界”非常不友好,“猫子的世界”实现全球各节点的数据库进行数据实时同步,由于Halo2.12版本将数据索引在程序内存中,新同步的数据并不会自动加载到内存,导致全球各节点不能展示新增的文章,这个问题让猫子非常懊恼,即时反馈到官方github也不能得到解决,官方不认为这是问题,因为Halo2并不支持多节点部署。官方也在有状态服务越做越深,为此猫子通过技术手段来解决全球文章展示的问题。

技术方案图

技术实现

将站点静态文件同步至S3

同步脚本sync.sh

#!/bin/bash

replaceFileName(){
    keyword=$1 # 要被替换的关键字
    folder=$2 # 目标文件所在的文件夹路径

    [ -d $folder ] && cd $folder
    for file in `ls`
    do
        if [ -f "$file" ]; then
            new_name=$(echo "$file" | sed "s/$keyword//g")
            mv "$file" "$new_name"
        fi
    done
}

cd /data/sync/
wget -r -p -np -k --header="accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7" --header="user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36" http://xxx.xxx.xxx.xxx:8090

cd xxx.xxx.xxx.xxx\:8090/

sed -i 's/http\:\/\/xxx.xxx.xxx.xxx:8090//g' ./index.html ./links ./photos ./quan-qiu-cn2-gia-vps disclaimer.html
sed -i 's/index.html/\//g' ./index.html ./links ./photos ./quan-qiu-cn2-gia-vps disclaimer.html

sed -i 's/http\:\/\/xxx.xxx.xxx.xxx:8090//g' ./archives/* ./authors/* ./categories/* ./page/* ./tags/*
sed -i 's/index.html//g' ./archives/* ./authors/* ./categories/* ./page/* ./tags/*

cd /data/sync/xxx.xxx.xxx.xxx\:8090/plugins/PluginCommentWidget/assets/static
wget 'http://xxx.xxx.xxx.xxx:8090/plugins/PluginCommentWidget/assets/static/style.css?version=1.8.0'

replaceFileName '?version=1.8.0' '/data/sync/xxx.xxx.xxx.xxx:8090/plugins/PluginCommentWidget/assets/static'
replaceFileName '?22t19m' '/data/sync/xxx.xxx.xxx.xxx:8090/plugins/PluginLightGallery/assets/static/fonts'
replaceFileName '?version=1.3.1' '/data/sync/xxx.xxx.xxx.xxx:8090/plugins/PluginSearchWidget/assets/static'

cd /data/sync/xxx.xxx.xxx.xxx\:8090/
mkdir -p plugins/PluginCommentWidget/assets/static/emoji/
wget 'http://xxx.xxx.xxx.xxx:8090/plugins/PluginCommentWidget/assets/static/emoji/all.json' -O plugins/PluginCommentWidget/assets/static/emoji/all.json
mkdir -p plugins/restricted-reading/assets/static/
wget 'http://xxx.xxx.xxx.xxx:8090/plugins/restricted-reading/assets/static/style.css' -O plugins/restricted-reading/assets/static/style.css

s3cmd sync ./ s3://sync/

cd ..
rm -rf xxx.xxx.xxx.xxx\:8090/

定时任务

*/5 * * * * flock -xn /data/sync/sync.lock -c "/data/sync/sync.sh"

将站点静态文件同步至边缘节点

定时任务

*/10 * * * * flock -xn /tmp/sync.lock -c "s3cmd sync s3://sync/ /data/halo/"

节点halo重启重新索引数据

定时任务

0 2 * * * docker restart halo

边缘节点nginx设置

        location / {
            add_header Content-Type "text/html; charset=UTF-8";
            index index.html
            alias /usr/local/openresty/nginx/html/;
        }

        location /apis/ {
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://xxx.xxx.xxx.xxx:8090/apis/;
            proxy_set_header Host $host;
        }

        location /actuator/globalinfo {
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://xxx.xxx.xxx.xxx:8090/actuator/globalinfo;
            proxy_set_header Host $host;
        }

问题

边缘节点存在延迟,不能实时展现新页面,同步延迟大于5分钟。