换成第三方底图增加重试

This commit is contained in:
龙澳
2026-03-31 16:26:15 +08:00
parent 2e8b9cd83f
commit 08d67c9ec8
3 changed files with 15 additions and 74 deletions

View File

@@ -104,9 +104,6 @@ import { ref, computed, onMounted, watch } from 'vue'
import L from 'leaflet'
import 'leaflet/dist/leaflet.css'
// 瓦片代理地址 — 部署到内网其他机器时,将 localhost 改为服务器 IP
const TILE_PROXY_URL = 'http://localhost:8080/tiles/{z}/{x}/{y}{r}.png'
// ── 常量 ────────────────────────────────────────────────────
const TOTAL_VOLS = 63
@@ -271,10 +268,23 @@ function initMap() {
attributionControl: false
})
L.tileLayer(TILE_PROXY_URL, {
maxZoom: 18
const tileLayer = L.tileLayer('https://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}{r}.png', {
maxZoom: 18,
subdomains: 'abcd'
}).addTo(map)
// 瓦片加载失败时自动重试最多3次间隔递增
const retryCounts = new Map()
tileLayer.on('tileerror', (e) => {
const key = e.tile.src
const count = retryCounts.get(key) ?? 0
if (count >= 3) return
retryCounts.set(key, count + 1)
setTimeout(() => {
e.tile.src = e.tile.src.split('?')[0] + '?t=' + Date.now()
}, 500 * (count + 1)) // 500ms / 1000ms / 1500ms
})
L.control.attribution({ prefix: false, position: 'bottomright' })
.addAttribution('大唐双龙传 - 黄易')
.addTo(map)

View File

@@ -1,15 +0,0 @@
services:
tile-proxy:
image: nginx:alpine
ports:
- "8080:80"
volumes:
- ./nginx.conf:/etc/nginx/conf.d/default.conf:ro
- ./cache:/var/cache/nginx/tiles
restart: unless-stopped
# 健康检查每30秒确认代理存活
healthcheck:
test: ["CMD", "wget", "-qO-", "http://localhost/health"]
interval: 30s
timeout: 5s
retries: 3

View File

@@ -1,54 +0,0 @@
# 磁盘缓存配置
# levels=1:2 两级目录结构,避免单目录文件过多
# keys_zone=20m 内存索引 20MB约可索引 160,000 个瓦片
# max_size=10g 磁盘缓存上限 10GB约覆盖全国 z0-z14 级别)
# inactive=30d 30天未访问的瓦片自动清理
proxy_cache_path /var/cache/nginx/tiles
levels=1:2
keys_zone=tiles_cache:20m
max_size=10g
inactive=30d
use_temp_path=off;
server {
listen 80;
# 使用 Docker 内置 DNS 解析上游域名
resolver 127.0.0.11 valid=300s;
resolver_timeout 5s;
location /tiles/ {
# 去掉 /tiles/ 前缀,补全底图样式路径后转发
rewrite ^/tiles/(.*)$ /light_all/$1 break;
proxy_pass https://a.basemaps.cartocdn.com;
proxy_ssl_server_name on;
# 缓存配置
proxy_cache tiles_cache;
proxy_cache_key "$uri";
proxy_cache_valid 200 30d;
proxy_cache_lock on; # 同一瓦片并发请求只放行一个去上游
proxy_cache_lock_timeout 5s;
# 上游故障时继续提供过期缓存,避免地图空白
proxy_cache_use_stale error timeout updating
http_500 http_502 http_503 http_504;
proxy_cache_background_update on;
# 超时设置
proxy_connect_timeout 5s;
proxy_read_timeout 15s;
# 响应头
add_header Access-Control-Allow-Origin *;
add_header X-Cache-Status $upstream_cache_status;
expires 30d;
}
# 健康检查
location /health {
return 200 "ok\n";
add_header Content-Type text/plain;
}
}