### Install dependencies and bootstrap
Source: https://github.com/a597873885/webfunny_monitor/blob/main/DES.md
Run npm install and npm run bootstrap in the project root to install dependencies and initialize the project.
```bash
npm install && npm run bootstrap
```
--------------------------------
### Complete tracingConfig.js example
Source: https://github.com/a597873885/webfunny_monitor/blob/main/OTEL_USAGE_GUIDE.md
Full configuration example for config/tracingConfig.js, including SDK settings, URL filtering, feature toggles for ClickHouse and business tracing, and performance/debug options.
```javascript
const { version } = require("../package.json")
const WebfunnyConfig = require("../webfunny.config")
module.exports = {
// ==================== SDK 配置 ====================
enabled: true,
serviceName: 'webfunny-monitor',
serviceVersion: version,
serviceInstanceId: `webfunny-${require("os").hostname()}`,
serviceNamespace: 'default',
deploymentEnvironment: 'production',
exporterEndpoint: 'http://localhost:4317', // 或 'http://localhost:4318' (HTTP)
// ==================== URL 过滤 ====================
// 这些 URL 不会被追踪
ignoreUrls: [
'/health',
'/ping',
'/favicon.ico',
'*/upLog', // 通配符匹配
/^\/static\//, // 正则匹配:静态资源
],
// 出站请求过滤(可选)
ignoreOutgoingUrls: [
/:\/\/[\d.]+:8123/, // 正则匹配:包含 8123 端口的请求
],
// ==================== 功能开关 ====================
// ClickHouse 追踪
clickhouse: {
enabled: true,
maxSqlLength: 500, // SQL 最大长度
recordRows: true, // 是否记录返回行数
},
// 业务方法追踪
business: {
enabled: true,
traceController: true, // 是否追踪 Controller 方法
traceModel: true, // 是否追踪 Model 方法
excludeMethods: [ // 排除列表:不追踪这些方法(全局生效)
'getUserTokenDetailByToken', // 高频调用的 token 验证方法
'checkToken', // 避免产生大量无用 Span
// 添加更多需要排除的方法名...
],
},
// ==================== 高级配置 ====================
performance: {
slowQueryThreshold: 1000, // 慢查询阈值(ms)
warnDuration: 500, // 警告耗时(ms)
},
debug: {
logSpanCreation: false, // 是否打印 Span 创建日志
logErrors: true, // 是否打印追踪错误
},
};
```
--------------------------------
### Run Docker container
Source: https://github.com/a597873885/webfunny_monitor/blob/main/DES_DOCKER.md
Starts the container, mapping ports 8010 and 8011 to the host. The monitor system uses both ports.
```bash
docker container run -p 8010:8010 -p 8011:8011 -it webfunny_monitor
```
--------------------------------
### Run production mode and restart
Source: https://github.com/a597873885/webfunny_monitor/blob/main/DES.md
Start the application in production mode with 'npm run prd' for the first run, and use 'npm run restart' for subsequent restarts.
```bash
npm run prd
```
```bash
npm run restart
```
--------------------------------
### Switch to ARMS tracer in app.js
Source: https://github.com/a597873885/webfunny_monitor/blob/main/OTEL_USAGE_GUIDE.md
Step 3 of ARMS setup: comment out the default gRPC tracer and use the ARMS tracer instead.
```javascript
// 注释掉默认的 gRPC 追踪器
// const { initOtel } = require('./utils/otelTracer');
// 使用阿里云 ARMS 追踪器(HTTP/Protobuf 协议)
const { initOtel } = require('./utils/otelTracerForArms');
initOtel();
```
--------------------------------
### Check and install PM2
Source: https://github.com/a597873885/webfunny_monitor/blob/main/DES.md
Verify PM2 is installed with 'pm2 -v'. If not, install it globally with 'npm install pm2 -g'.
```bash
pm2 -v
```
```bash
npm install pm2 -g
```
--------------------------------
### Trace output example for ClickHouse query
Source: https://github.com/a597873885/webfunny_monitor/blob/main/OTEL_USAGE_GUIDE.md
This is an example of the trace output generated by the OpenTelemetry integration, showing the span hierarchy for an HTTP request down to the ClickHouse query, including attributes like db.system, db.statement, db.rows_affected, and duration.
```text
HTTP Request: GET /api/getUserList
└─ Controller/MonitorUserController.getUserList
└─ Model/MonitorUserModel.findAll
└─ ClickHouse/query (SELECT * FROM users WHERE ...)
├─ db.system: clickhouse
├─ db.statement: SELECT * FROM users...
├─ db.rows_affected: 150
└─ duration: 45ms
```
--------------------------------
### ARMS headers in tracingConfig.js
Source: https://github.com/a597873885/webfunny_monitor/blob/main/OTEL_USAGE_GUIDE.md
Step 2 of ARMS setup: add optional armsHeaders to config/tracingConfig.js for authentication.
```javascript
module.exports = {
// ... 其他配置(会自动从 webfunny.config 读取)
// 如果 ARMS 需要认证,在此添加请求头
armsHeaders: {
'Authorization': 'Bearer your-token',
},
};
```
--------------------------------
### ARMS configuration in webfunny.config/index.js
Source: https://github.com/a597873885/webfunny_monitor/blob/main/OTEL_USAGE_GUIDE.md
Step 1 of ARMS setup: modify the selfMonitor object in webfunny.config/index.js to include the ARMS endpoint and a backup exporter endpoint.
```javascript
const otherConfig = {
// ... 其他配置
"selfMonitor": {
"enabled": true,
"serviceName": 'Webfunny_online',
"serviceInstanceId": 'webfunny_prod_001',
"serviceNamespace": 'webfunny',
"deploymentEnvironment": 'prod',
// 阿里云 ARMS 配置
"armsEndpoint": 'http://tracing-analysis-dc-sh.aliyuncs.com/adapt_xxx@xxx/api/otlp/traces',
"exporterEndpoint": 'localhost:4317', // 自建 APM 地址(备用)
},
}
```
--------------------------------
### 使用配置文件运行迁移 (start_cross_server_migration.sh)
Source: https://github.com/a597873885/webfunny_monitor/blob/main/servers/monitor/migration/README.md
推荐的使用方式:先配置 migration.config.js,然后直接运行脚本。
```bash
# 1. 配置 migration.config.js
# 2. 直接运行
./start_cross_server_migration.sh
```
--------------------------------
### 快速命令参考:端口检查、Docker、ClickHouse、防火墙和迁移工具
Source: https://github.com/a597873885/webfunny_monitor/blob/main/servers/monitor/migration/ClickHouse集群部署实战指南.md
附录中的bash命令集合,用于ClickHouse集群的运维和迁移。包含端口检查(netstat/telnet/nc)、Docker容器管理、ClickHouse客户端查询、副本状态查看、复制队列查看、重启复制、防火墙配置以及迁移工具脚本的调用。
```bash
# ===== 端口检查 =====
sudo netstat -tlnp | grep 9009 # 检查端口监听
telnet IP 9009 # 测试连接
nc -zv IP 9009 # 测试连接(推荐)
# ===== Docker =====
docker ps | grep clickhouse # 查看容器
docker port CONTAINER # 查看端口映射
docker logs CONTAINER # 查看日志
docker exec -it CONTAINER bash # 进入容器
docker restart CONTAINER # 重启容器
# ===== ClickHouse =====
clickhouse-client --query="..." # 执行SQL
clickhouse-client -m # 交互模式
# 查看副本状态
SELECT * FROM system.replicas WHERE database = 'xxx';
# 查看复制队列
SELECT * FROM system.replication_queue WHERE database = 'xxx';
# 重启复制
SYSTEM RESTART REPLICAS;
# ===== 防火墙 =====
sudo firewall-cmd --list-ports # 查看开放端口
sudo firewall-cmd --permanent --add-port=9009/tcp
sudo firewall-cmd --reload
# ===== 迁移工具 =====
bash preview_cross_server_migration.sh # 预览
bash start_cross_server_migration.sh # 迁移
node diagnose_replication.js # 诊断
node check_sync_progress_v2.js # 进度
```
--------------------------------
### 检查Docker端口映射并重新创建容器
Source: https://github.com/a597873885/webfunny_monitor/blob/main/servers/monitor/migration/ClickHouse集群部署实战指南.md
检查Docker端口映射,如果没有映射则需要重新创建容器。
```bash
# 检查
docker port clickhouse-server | grep 9009
# 如果没有,需要重新创建容器
docker stop clickhouse-server
docker run -d -p 9009:9009 ...
```
--------------------------------
### 引入并启动 gRPC 服务
Source: https://github.com/a597873885/webfunny_monitor/blob/main/servers/apm/grpc/README.md
在主服务文件中引入 startGrpcServer 函数并调用以启动 gRPC 服务。
```javascript
const { startGrpcServer } = require('./grpc')
startGrpcServer()
```
--------------------------------
### Build Docker image
Source: https://github.com/a597873885/webfunny_monitor/blob/main/DES_DOCKER.md
Run this command in the project root to build the Docker image with the platform flag for linux/amd64.
```bash
docker image build -t webfunny_monitor --platform linux/amd64 .
```
--------------------------------
### SQL: 查看副本配置 (system.replicas)
Source: https://github.com/a597873885/webfunny_monitor/blob/main/servers/monitor/migration/ClickHouse集群部署实战指南.md
用于检查ClickHouse集群中副本的配置和状态,包括zookeeper路径、副本名称、总副本数和活跃副本数。正常输出显示total_replicas和active_replicas均为2,表示复制正常。
```sql
-- 1. 查看副本配置
SELECT
database,
table,
zookeeper_path,
replica_name,
total_replicas,
active_replicas
FROM system.replicas
WHERE database = 'webfunny_cloud_db_cluster'
LIMIT 5;
```
--------------------------------
### Filter external HTTP requests with ignoreOutgoingUrls
Source: https://github.com/a597873885/webfunny_monitor/blob/main/OTEL_USAGE_GUIDE.md
Configure ignoreOutgoingUrls to filter external HTTP requests from tracing. Supports regular expressions and string patterns. The example filters ClickHouse (port 8123), MySQL (port 3306), and requests containing 'internal-service'.
```javascript
ignoreOutgoingUrls: [
/:\/\/[\d.]+:8123/, // 过滤 ClickHouse 请求
/:\/\/[\d.]+:3306/, // 过滤 MySQL 请求
'internal-service', // 过滤包含此字符串的请求
],
```
--------------------------------
### 通过环境变量启用调试模式
Source: https://github.com/a597873885/webfunny_monitor/blob/main/servers/monitor/migration/README.md
设置 DEBUG=1 环境变量来启用调试模式。
```bash
DEBUG=1 ./start_cross_server_migration.sh
```
--------------------------------
### Manually record exception with OpenTelemetry
Source: https://github.com/a597873885/webfunny_monitor/blob/main/OTEL_USAGE_GUIDE.md
This snippet demonstrates how to manually record an exception in the active OpenTelemetry span within a try-catch block. It uses the @opentelemetry/api package to get the active span, record the exception, set the span status to ERROR, and then rethrow the error.
```javascript
const otelApi = require('@opentelemetry/api');
try {
// 业务代码
} catch (error) {
const activeSpan = otelApi.trace.getActiveSpan();
if (activeSpan) {
activeSpan.recordException(error);
activeSpan.setStatus({
code: otelApi.SpanStatusCode.ERROR,
message: error.message
});
}
throw error;
}
```
--------------------------------
### 使用命令行参数运行迁移 (cross_server_migrate.js)
Source: https://github.com/a597873885/webfunny_monitor/blob/main/servers/monitor/migration/README.md
通过命令行参数指定源和目标服务器信息,覆盖配置文件中的值。注意密码需要替换为实际值。
```bash
node cross_server_migrate.js --batch \
--source-host=http://8.133.201.61:8123 \
--source-username=webfunny_user \
--source-password=密码 \
--source-db=webfunny_cloud_db \
--target-host=http://106.14.26.133:8123 \
--target-username=default \
--target-password=密码 \
--target-db=webfunny_cloud_db_cluster
```
--------------------------------
### Filter specific requests with ignoreUrls
Source: https://github.com/a597873885/webfunny_monitor/blob/main/OTEL_USAGE_GUIDE.md
Configure ignoreUrls in config/tracingConfig.js to filter out specific requests from tracing. Supports exact string paths, wildcard patterns, regular expressions, and predicate functions. The example shows filtering health, ping, metrics, upLog, static, and debug URLs.
```javascript
ignoreUrls: [
'/health',
'/ping',
'/metrics',
'*/upLog', // 通配符匹配所有包含 upLog 的路径
/^\/static\//, // 正则匹配:所有 /static/ 开头的路径
(url) => url.includes('debug'), // 函数匹配:包含 debug 的 URL
],
```
--------------------------------
### 在容器内添加 /etc/hosts 解决主机名解析
Source: https://github.com/a597873885/webfunny_monitor/blob/main/servers/monitor/migration/ClickHouse集群部署实战指南.md
在容器内添加 /etc/hosts 条目以解决主机名解析问题。
```bash
docker exec -it clickhouse-server bash
echo "127.0.0.1 zk1" >> /etc/hosts
echo "192.168.1.20 zk2" >> /etc/hosts
exit
docker restart clickhouse-server
```
--------------------------------
### 迁移步骤命令
Source: https://github.com/a597873885/webfunny_monitor/blob/main/servers/monitor/migration/ClickHouse集群部署实战指南.md
执行迁移的 bash 命令,包括预览(只创建表结构)、检查表结构(确认引擎为 ReplicatedMergeTree)和正式迁移。
```bash
# 1. 预览(只创建表结构,不迁移数据)
bash preview_cross_server_migration.sh
# 2. 检查表结构是否正确
# 特别确认引擎是否为 ReplicatedMergeTree
# 3. 正式迁移
bash start_cross_server_migration.sh
```
--------------------------------
### 安装依赖
Source: https://github.com/a597873885/webfunny_monitor/blob/main/servers/apm/grpc/README.md
安装 gRPC 核心库和 Protobuf 加载器。
```bash
npm install @grpc/grpc-js @grpc/proto-loader
```
--------------------------------
### 配置文件结构 (migration.config.js)
Source: https://github.com/a597873885/webfunny_monitor/blob/main/servers/monitor/migration/README.md
定义迁移工具的配置文件结构,包含源服务器、目标服务器和迁移选项。目标服务器配置仅在跨服务器迁移时需要。options 中可设置批次大小、调试模式和超时时间。
```javascript
{
source: { // 源服务器配置
host: '', // 服务器地址(含协议和端口)
username: '',
password: '',
database: '' // 源数据库名
},
target: { // 目标服务器配置
host: '', // 跨服务器迁移才需要
username: '',
password: '',
database: '' // 目标数据库名
},
options: { // 迁移选项
batchSize: 10000, // 每批行数
debug: false, // 调试模式
timeout: 300000 // 超时时间(毫秒)
}
}
```
--------------------------------
### Import otelTracerForArms (HTTP)
Source: https://github.com/a597873885/webfunny_monitor/blob/main/OTEL_USAGE_GUIDE.md
Import statement for the HTTP/Protobuf-based tracer, used for Alibaba Cloud ARMS, AWS X-Ray, and most cloud services.
```javascript
const { initOtel } = require('./utils/otelTracerForArms');
```
--------------------------------
### 跨服务器迁移命令
Source: https://github.com/a597873885/webfunny_monitor/blob/main/servers/monitor/migration/README.md
用于跨服务器迁移的预览和执行命令。
```bash
# 预览(只创建表结构,不迁移数据)
./preview_cross_server_migration.sh
# 确认无误后,开始完整迁移
./start_cross_server_migration.sh
```
--------------------------------
### Docker 部署正确配置(包含 9009 端口映射)
Source: https://github.com/a597873885/webfunny_monitor/blob/main/servers/monitor/migration/ClickHouse集群部署实战指南.md
正确的 Docker 运行命令,包含 9009 端口映射和数据卷挂载,确保集群节点间通信正常。
```bash
docker run -d \
--name clickhouse-server \
-p 8123:8123 \
-p 9000:9000 \
-p 9009:9009 \ # ← 关键!
-v /data/clickhouse:/var/lib/clickhouse \
-v /etc/clickhouse-server:/etc/clickhouse-server \
clickhouse/clickhouse-server:24.3
```
--------------------------------
### 挂载主机 /etc/hosts 到容器
Source: https://github.com/a597873885/webfunny_monitor/blob/main/servers/monitor/migration/ClickHouse集群部署实战指南.md
推荐做法:通过挂载主机的 /etc/hosts 文件到容器,避免手动配置。
```bash
docker run -v /etc/hosts:/etc/hosts:ro ...
```
--------------------------------
### Bash: Docker容器持久化配置
Source: https://github.com/a597873885/webfunny_monitor/blob/main/servers/monitor/migration/ClickHouse集群部署实战指南.md
用于启动ClickHouse Docker容器,并配置数据、配置和主机名映射的持久化,以及自动重启。
```bash
docker run -d \
--name clickhouse-server \
-p 8123:8123 \
-p 9000:9000 \
-p 9009:9009 \
-v /data/clickhouse:/var/lib/clickhouse \ # 数据持久化
-v /etc/clickhouse-server:/etc/clickhouse-server \ # 配置持久化
-v /etc/hosts:/etc/hosts:ro \ # 主机名映射
--restart=always \ # 自动重启
clickhouse/clickhouse-server:24.3
```
--------------------------------
### 验证集群复制 (clickhouse-client)
Source: https://github.com/a597873885/webfunny_monitor/blob/main/servers/monitor/migration/README.md
在不同节点上查询相同的数据,验证集群复制是否正常。
```bash
# 节点1
clickhouse-client --host=节点1IP --query="SELECT count() FROM 数据库.表名"
# 节点2
clickhouse-client --host=节点2IP --query="SELECT count() FROM 数据库.表名"
```
--------------------------------
### Verify project list and data display URLs
Source: https://github.com/a597873885/webfunny_monitor/blob/main/DES.md
Check that the server is accessible by curling the project list endpoint and visiting the overview page in a browser.
```bash
curl http://xxx.xxx.xxx.xxx:8011/server/webMonitorIdList
```
```bash
curl http://www.baidu.com:8011/server/webMonitorIdList
```
--------------------------------
### Bash: 数据一致性检查 (诊断工具和手动对比)
Source: https://github.com/a597873885/webfunny_monitor/blob/main/servers/monitor/migration/ClickHouse集群部署实战指南.md
使用诊断工具node diagnose_replication.js或手动通过clickhouse-client对比两个节点的行数,确保数据一致。
```bash
# 使用诊断工具
node diagnose_replication.js
# 或手动对比行数
clickhouse-client --host=192.168.1.10 --query="
SELECT table, count() as cnt
FROM webfunny_cloud_db_cluster.*
GROUP BY table
ORDER BY table"
clickhouse-client --host=192.168.1.20 --query="
SELECT table, count() as cnt
FROM webfunny_cloud_db_cluster.*
GROUP BY table
ORDER BY table"
```
--------------------------------
### Import otelTracer (gRPC)
Source: https://github.com/a597873885/webfunny_monitor/blob/main/OTEL_USAGE_GUIDE.md
Import statement for the default gRPC-based tracer, used for self-hosted APM, SigNoz, and Jaeger.
```javascript
const { initOtel } = require('./utils/otelTracer');
```
--------------------------------
### 启用调试模式 (migration.config.js)
Source: https://github.com/a597873885/webfunny_monitor/blob/main/servers/monitor/migration/README.md
在配置文件中设置 options.debug 为 true 以显示详细 SQL。
```javascript
options: {
debug: true // 显示详细SQL
}
```
--------------------------------
### 复制并编辑配置文件
Source: https://github.com/a597873885/webfunny_monitor/blob/main/servers/monitor/migration/README.md
使用命令行复制配置示例文件并编辑,以配置数据库连接信息。
```bash
# 1. 复制配置示例文件
cp migration.config.example.js migration.config.js
# 2. 编辑配置文件
vim migration.config.js
# 或使用其他编辑器
```
--------------------------------
### 配置文件示例 (migration.config.js)
Source: https://github.com/a597873885/webfunny_monitor/blob/main/servers/monitor/migration/README.md
配置文件包含源数据库和目标数据库的连接信息,以及迁移选项(如批量大小和调试模式)。
```javascript
module.exports = {
source: {
host: 'http://8.133.201.61:8123',
username: 'webfunny_user',
password: '你的密码',
database: 'webfunny_cloud_db'
},
target: {
host: 'http://106.14.26.133:8123',
username: 'default',
password: '你的密码',
database: 'webfunny_cloud_db_cluster'
},
options: {
batchSize: 10000,
debug: false
}
}
```
--------------------------------
### Docker 部署常见错误(缺少 9009 端口映射)
Source: https://github.com/a597873885/webfunny_monitor/blob/main/servers/monitor/migration/ClickHouse集群部署实战指南.md
展示了缺少 9009 端口映射的错误 Docker 运行命令,会导致节点间通信失败。
```bash
docker run -d \
-p 8123:8123 \
-p 9000:9000 \
clickhouse/clickhouse-server
```
--------------------------------
### Docker 容器内配置主机名映射
Source: https://github.com/a597873885/webfunny_monitor/blob/main/servers/monitor/migration/ClickHouse集群部署实战指南.md
进入容器添加 /etc/hosts 映射并重启容器,确保容器内能解析 ZooKeeper 主机名。
```bash
# 进入容器
docker exec -it clickhouse-server bash
# 添加映射
echo "127.0.0.1 zk1" >> /etc/hosts
echo "192.168.1.20 zk2" >> /etc/hosts
# 退出并重启
exit
docker restart clickhouse-server
```
--------------------------------
### 只创建表结构 (--structure-only)
Source: https://github.com/a597873885/webfunny_monitor/blob/main/servers/monitor/migration/README.md
使用 --structure-only 参数只创建表结构,不迁移数据。
```bash
node cross_server_migrate.js --batch --structure-only
```
--------------------------------
### Bash: 预览模式迁移脚本
Source: https://github.com/a597873885/webfunny_monitor/blob/main/servers/monitor/migration/ClickHouse集群部署实战指南.md
使用预览模式只创建表结构,用于迁移前测试。
```bash
# 预览模式,只创建表结构
bash preview_cross_server_migration.sh
```
--------------------------------
### 修复表引擎问题的命令序列
Source: https://github.com/a597873885/webfunny_monitor/blob/main/servers/monitor/migration/ClickHouse集群部署实战指南.md
检查配置、清空数据库并重新迁移以修复表引擎问题。
```bash
# 1. 检查配置
cat migration.config.js | grep isCluster
# 2. 确认为 true
# 3. 清空数据库
node clean_target_database.js
# 4. 重新迁移
bash start_cross_server_migration.sh
```
--------------------------------
### tracingConfig.js reads base config from webfunny.config
Source: https://github.com/a597873885/webfunny_monitor/blob/main/OTEL_USAGE_GUIDE.md
Shows how config/tracingConfig.js imports and spreads the selfMonitor configuration from webfunny.config/index.js, along with the service version from package.json.
```javascript
const { version } = require("../package.json")
const WebfunnyConfig = require("../webfunny.config")
module.exports = {
// ==================== SDK 配置 ====================
...WebfunnyConfig.otherConfig.selfMonitor, // 👈 从这里读取基础配置
serviceVersion: version,
// ...
}
```
--------------------------------
### 同服务器迁移命令
Source: https://github.com/a597873885/webfunny_monitor/blob/main/servers/monitor/migration/README.md
用于同服务器迁移的预览和执行命令。
```bash
# 预览
./preview_migration.sh
# 开始迁移
./start_migration.sh
```
--------------------------------
### Configure IP address in domain.js
Source: https://github.com/a597873885/webfunny_monitor/blob/main/DES.md
Set the server and assets domains and ports using IP addresses in webfunny_monitor/bin/domain.js. Ensure the ports match the actual configuration.
```javascript
module.exports = {
localServerDomain: 'xxx.xxx.xxx.xxx:8011', // 日志上报域名
localAssetsDomain: 'xxx.xxx.xxx.xxx:8010', // 前端页面域名
localServerPort: '8011', // 日志上报端口号
localAssetsPort: '8010', // 前端页面端口号
}
```
--------------------------------
### Bash: 数据不一致恢复流程
Source: https://github.com/a597873885/webfunny_monitor/blob/main/servers/monitor/migration/ClickHouse集群部署实战指南.md
用于处理数据不一致的情况,包括停止写入、对比数据、清空节点2并重新复制。
```bash
# 1. 停止写入
# 2. 对比数据
node diagnose_replication.js
# 3. 如果差异大,重新迁移
# 清空节点2
clickhouse-client --host=节点2 --query="DROP DATABASE IF EXISTS xxx"
# 从节点1重新复制
# ClickHouse 会自动处理
```
--------------------------------
### 只预览迁移 (--dry-run)
Source: https://github.com/a597873885/webfunny_monitor/blob/main/servers/monitor/migration/README.md
使用 --dry-run 参数只预览迁移操作,不实际执行。
```bash
node cross_server_migrate.js --batch --dry-run
```
--------------------------------
### 目录结构
Source: https://github.com/a597873885/webfunny_monitor/blob/main/servers/apm/grpc/README.md
展示 grpc 目录下的文件结构。
```text
grpc/
├── index.js # gRPC 服务入口文件
├── grpcServer.js # gRPC 服务(字符串提取版本)
├── grpcServerProtobuf.js # gRPC 服务(Protobuf 完整解析版本,推荐)
└── README.md # 本文件
```
--------------------------------
### 检查并修复 interserver_http_port 配置
Source: https://github.com/a597873885/webfunny_monitor/blob/main/servers/monitor/migration/ClickHouse集群部署实战指南.md
检查配置文件中 interserver_http_port 是否被注释,并重启容器。
```bash
# 检查配置
docker exec clickhouse-server grep interserver_http_port /etc/clickhouse-server/config.xml
# 如果被注释,取消注释或添加
# 重启容器
docker restart clickhouse-server
```
--------------------------------
### 运行诊断工具 diagnose_replication.js
Source: https://github.com/a597873885/webfunny_monitor/blob/main/servers/monitor/migration/ClickHouse集群部署实战指南.md
使用诊断工具检查复制问题。
```bash
node diagnose_replication.js
```
--------------------------------
### 检查表数量 (SQL)
Source: https://github.com/a597873885/webfunny_monitor/blob/main/servers/monitor/migration/README.md
分别查询源数据库和目标数据库的表数量,用于验证迁移结果。
```sql
-- 源数据库
SELECT count() FROM system.tables WHERE database = '源数据库名';
-- 目标数据库
SELECT count() FROM system.tables WHERE database = '目标数据库名';
```
--------------------------------
### 检查表引擎是否为MergeTree的SQL查询
Source: https://github.com/a597873885/webfunny_monitor/blob/main/servers/monitor/migration/ClickHouse集群部署实战指南.md
用于检查表引擎是否为MergeTree而不是ReplicatedMergeTree。
```sql
SHOW CREATE TABLE test;
-- 显示 ENGINE = MergeTree() 而不是 ReplicatedMergeTree
```
--------------------------------
### Configure domain name in domain.js
Source: https://github.com/a597873885/webfunny_monitor/blob/main/DES.md
Set the server and assets domains using domain names in webfunny_monitor/bin/domain.js. Ensure the ports match the actual configuration.
```javascript
module.exports = {
localServerDomain: 'www.baidu.com:8011', // 日志上报域名
localAssetsDomain: 'www.baidu.com:8010', // 前端页面域名
localServerPort: '8011', // 日志上报端口号
localAssetsPort: '8010', // 前端页面端口号
}
```
--------------------------------
### MySQL write configuration
Source: https://github.com/a597873885/webfunny_monitor/blob/main/DES.md
Configure the MySQL write connection in webfunny_monitor/bin/mysqlConfig.js. Replace the placeholder IP, port, database name, username, and password with your actual values.
```javascript
module.exports = {
write: {
ip: 'xxx.xxx.xxx.xxx', // 远程ip地址
port: '3306', // 端口号
dataBaseName: 'webfunny_db', // 数据库名
userName: 'root', // 用户名
password: '123456' // 密码
}
}
```
--------------------------------
### 检查数据行数 (SQL)
Source: https://github.com/a597873885/webfunny_monitor/blob/main/servers/monitor/migration/README.md
对比源数据库和目标数据库中每个表的行数。
```sql
-- 对比每个表的行数
SELECT count() FROM 源数据库.表名;
SELECT count() FROM 目标数据库.表名;
```
--------------------------------
### Quick diagnostic flow for tracing issues
Source: https://github.com/a597873885/webfunny_monitor/blob/main/OTEL_USAGE_GUIDE.md
A step-by-step diagnostic script to verify OpenTelemetry tracing is working. It checks service startup output, configuration files, APM server connectivity, enables debug logging, and sends a test request.
```bash
# 1. 检查服务是否启动成功(查看控制台输出)
✅ OpenTelemetry SDK 已启动
📡 服务名称: webfunny-monitor
📡 实例 ID: webfunny-hostname
📡 导出器地址: http://localhost:4317
🎯 采样器: AlwaysOn (所有请求都被采样)
🎯 [AutoTracer] 自动追踪加载器已启用
- Controller 自动包装: ✅
- Model 自动包装: ✅
# 2. 检查配置文件(webfunny.config/index.js)
selfMonitor.enabled = true ✅
selfMonitor.exporterEndpoint = "localhost:4317" ✅
# 3. 检查追踪配置(config/tracingConfig.js)
enabled = true ✅
clickhouse.enabled = true ✅
business.enabled = true ✅
# 4. 检查 APM 服务器
telnet localhost 4317 # gRPC 端口是否可访问
telnet localhost 4318 # HTTP 端口是否可访问
# 5. 启用调试日志
# 在 config/tracingConfig.js 中设置:
debug.logSpanCreation = true
debug.logErrors = true
# 6. 发起测试请求
curl http://localhost:9011/api/test
# 查看控制台输出是否有 Span 创建日志
```
--------------------------------
### SQL: 插入优化 (批量 vs 单行)
Source: https://github.com/a597873885/webfunny_monitor/blob/main/servers/monitor/migration/ClickHouse集群部署实战指南.md
展示批量插入和单行插入的对比,推荐使用批量插入以提高性能。
```sql
-- 批量插入
INSERT INTO table VALUES (...), (...), (...); -- 推荐
-- 避免单行插入
INSERT INTO table VALUES (...); -- 不推荐
```
--------------------------------
### SQL: 监控指标 (复制延迟、失败任务、只读副本)
Source: https://github.com/a597873885/webfunny_monitor/blob/main/servers/monitor/migration/ClickHouse集群部署实战指南.md
用于监控复制延迟、失败的复制任务和只读副本,帮助识别复制问题。
```sql
-- 复制延迟
SELECT
table,
absolute_delay,
queue_size
FROM system.replicas
WHERE absolute_delay > 60; -- 延迟超过60秒
-- 失败的复制任务
SELECT
table,
count() as failures
FROM system.replication_queue
WHERE num_tries > 3
GROUP BY table;
-- 只读副本(通常表示有问题)
SELECT
table,
is_readonly,
last_queue_update_exception
FROM system.replicas
WHERE is_readonly = 1;
```
--------------------------------
### 检查表引擎 (SQL)
Source: https://github.com/a597873885/webfunny_monitor/blob/main/servers/monitor/migration/README.md
查询目标数据库中引擎包含 'Replicated' 的表,以确认使用了 ReplicatedMergeTree。
```sql
-- 应该看到 ReplicatedMergeTree
SELECT name, engine
FROM system.tables
WHERE database = '目标数据库名'
AND engine LIKE '%Replicated%';
```
--------------------------------
### 测试连接和检查防火墙
Source: https://github.com/a597873885/webfunny_monitor/blob/main/servers/monitor/migration/README.md
用于排查连接超时问题,先测试连接,再检查防火墙端口。
```bash
# 测试连接
telnet 服务器IP 8123
# 检查防火墙
sudo firewall-cmd --list-ports
```
--------------------------------
### 清空并重建数据库以修复副本数问题
Source: https://github.com/a597873885/webfunny_monitor/blob/main/servers/monitor/migration/ClickHouse集群部署实战指南.md
在两个节点上删除数据库并使用ON CLUSTER重新创建,然后重新迁移。
```bash
# 在两个节点都删除数据库
clickhouse-client --query="DROP DATABASE IF EXISTS webfunny_cloud_db_cluster"
# 重新创建(使用 ON CLUSTER)
clickhouse-client --query="CREATE DATABASE webfunny_cloud_db_cluster ON CLUSTER '{cluster}'"
# 重新迁移
bash start_cross_server_migration.sh
```
--------------------------------
### 启用 Interserver 端口配置(config.xml)
Source: https://github.com/a597873885/webfunny_monitor/blob/main/servers/monitor/migration/ClickHouse集群部署实战指南.md
在 config.xml 或 config.d/ 下配置 interserver_http_port 和 interserver_http_host,指定真实 IP 以避免容器主机名解析问题。
```xml
9009
192.168.1.10
```
--------------------------------
### 宏定义配置(节点1 macros.xml)
Source: https://github.com/a597873885/webfunny_monitor/blob/main/servers/monitor/migration/ClickHouse集群部署实战指南.md
节点1的宏定义,指定集群、分片和副本标识。
```xml
log_cluster
01
replica_1
```
--------------------------------
### 在 app.js 中初始化 OpenTelemetry
Source: https://github.com/a597873885/webfunny_monitor/blob/main/OTEL_USAGE_GUIDE.md
在应用入口文件的最开始(所有 require 之前)引入并初始化追踪器,然后加载自动追踪器。注释中提供了阿里云 ARMS 的替代方案。
```javascript
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 🎯 OpenTelemetry 初始化(必须在所有 require 之前引入)
// 配置文件:config/tracingConfig.js
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
const { initOtel } = require('./utils/otelTracer');
// const { initOtel } = require('./utils/otelTracerForArms'); // 阿里云 ARMS 使用此文件
// 初始化 OpenTelemetry(配置在 config/tracingConfig.js 中)
// 如果需要覆盖配置,可以传入参数:initOtel({ serviceName: '...' })
initOtel();
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// 🎯 自动追踪加载器(必须在 Koa 等框架加载之前)
// 零侵入方案:自动包装所有 Controller 和 Model,无需修改业务代码
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
require('./utils/autoTracer');
```
--------------------------------
### 安装 OpenTelemetry 依赖
Source: https://github.com/a597873885/webfunny_monitor/blob/main/OTEL_MIGRATION.md
使用 npm 安装 OpenTelemetry 相关包,包括 API、SDK、自动插桩和多种导出器。
```bash
npm install @opentelemetry/api @opentelemetry/sdk-node @opentelemetry/auto-instrumentations-node @opentelemetry/exporter-trace-otlp-grpc @opentelemetry/exporter-trace-otlp-proto @opentelemetry/exporter-trace-otlp-http @opentelemetry/resources @opentelemetry/semantic-conventions
```
--------------------------------
### 自定义批次大小
Source: https://github.com/a597873885/webfunny_monitor/blob/main/servers/monitor/migration/README.md
使用 --batch 和 --batch-size 参数指定迁移的批次大小,例如 5000。
```bash
node cross_server_migrate.js --batch --batch-size=5000
```
--------------------------------
### 输出: 副本配置正常输出
Source: https://github.com/a597873885/webfunny_monitor/blob/main/servers/monitor/migration/ClickHouse集群部署实战指南.md
展示上述SQL查询的正常输出,显示副本配置正确,total_replicas和active_replicas均为2。
```text
database | table | total_replicas | active_replicas
----------------------------|------------|----------------|----------------
webfunny_cloud_db_cluster | test | 2 | 2
```
--------------------------------
### 添加主机名映射到 /etc/hosts
Source: https://github.com/a597873885/webfunny_monitor/blob/main/servers/monitor/migration/ClickHouse集群部署实战指南.md
在无法解析的节点上添加主机名映射,以解决DNS解析问题。
```bash
echo "192.168.1.10 d0d1c46ac5c6" | sudo tee -a /etc/hosts
```
--------------------------------
### Bash: 定期检查复制进度
Source: https://github.com/a597873885/webfunny_monitor/blob/main/servers/monitor/migration/ClickHouse集群部署实战指南.md
使用脚本定期检查复制进度。
```bash
# 定期检查
node check_sync_progress_v2.js
```
--------------------------------
### 授予权限
Source: https://github.com/a597873885/webfunny_monitor/blob/main/servers/monitor/migration/README.md
用于解决权限不足问题,授予用户所有权限。
```sql
-- 授予权限
GRANT ALL ON *.* TO 用户名;
```
--------------------------------
### 主机名映射配置(节点2 /etc/hosts)
Source: https://github.com/a597873885/webfunny_monitor/blob/main/servers/monitor/migration/ClickHouse集群部署实战指南.md
节点2的 /etc/hosts 配置,将 zk1 映射到节点1 IP,zk2 映射到本机。
```bash
192.168.1.10 zk1
127.0.0.1 zk2
```
--------------------------------
### 配置 gRPC 端口
Source: https://github.com/a597873885/webfunny_monitor/blob/main/servers/apm/grpc/README.md
在 config/AccountConfig.js 中配置 gRPC 服务端口,默认使用 conf.grpc?.port 或 9018。
```javascript
grpcPort: conf.grpc?.port || 9018
```
--------------------------------
### XML: 复制优化配置
Source: https://github.com/a597873885/webfunny_monitor/blob/main/servers/monitor/migration/ClickHouse集群部署实战指南.md
通过增加复制线程数和复制超时来优化复制性能。
```xml
16
30
```
--------------------------------
### 自建 APM gRPC 导出配置示例
Source: https://github.com/a597873885/webfunny_monitor/blob/main/OTEL_MIGRATION.md
在 webfunny.config.js 的 selfMonitor 中配置 gRPC 协议和本地端点,用于连接自建 APM。
```javascript
selfMonitor: {
enabled: true,
serviceName: 'Webfunny_online',
// 使用 gRPC 协议(默认)
exporterProtocol: 'grpc',
exporterEndpoint: 'localhost:4317',
}
```
--------------------------------
### MySQL read-write splitting configuration
Source: https://github.com/a597873885/webfunny_monitor/blob/main/DES.md
Configure read replicas in webfunny_monitor/bin/mysqlConfig.js. The 'read' array is supported in the high-performance version.
```javascript
module.exports = {
write: {
ip: 'xxx.xxx.xxx.xxx', // 远程ip地址
port: '3306', // 端口号
dataBaseName: 'webfunny_db', // 数据库名
userName: 'root', // 用户名
password: '123456' // 密码
},
// 高性能版支持此属性
read: [
{ host: "xxx.xxx.xxx.xxx", username: "root1", password: "123456" },
{ host: "xxx.xxx.xxx.xxx", username: "root2", password: "123456" }
]
}
```
--------------------------------
### 检查副本数的SQL查询
Source: https://github.com/a597873885/webfunny_monitor/blob/main/servers/monitor/migration/ClickHouse集群部署实战指南.md
用于检查副本数是否显示为1。
```sql
SELECT total_replicas FROM system.replicas LIMIT 1;
-- 显示 1
```
--------------------------------
### 主机名映射配置(节点1 /etc/hosts)
Source: https://github.com/a597873885/webfunny_monitor/blob/main/servers/monitor/migration/ClickHouse集群部署实战指南.md
节点1的 /etc/hosts 配置,将 zk1 映射到本机,zk2 映射到节点2 IP。
```bash
127.0.0.1 zk1
192.168.1.20 zk2
```
--------------------------------
### 宏定义配置(节点2 macros.xml)
Source: https://github.com/a597873885/webfunny_monitor/blob/main/servers/monitor/migration/ClickHouse集群部署实战指南.md
节点2的宏定义,副本标识必须与节点1不同。
```xml
log_cluster
01
replica_2
```
--------------------------------
### 诊断端口9009连接问题的命令
Source: https://github.com/a597873885/webfunny_monitor/blob/main/servers/monitor/migration/ClickHouse集群部署实战指南.md
用于检查端口是否监听和能否连接的诊断命令。
```bash
# 1. 检查端口是否监听(在节点1)
sudo netstat -tlnp | grep 9009
# 2. 检查能否连接(在节点2)
telnet 192.168.1.10 9009
# 或
nc -zv 192.168.1.10 9009
```
--------------------------------
### 手动检查复制队列和副本状态的SQL查询
Source: https://github.com/a597873885/webfunny_monitor/blob/main/servers/monitor/migration/ClickHouse集群部署实战指南.md
用于手动检查复制队列和副本状态的SQL查询,包括查看复制队列大小和错误,以及副本状态。
```sql
-- 查看复制队列
SELECT
table,
count() as queue_size,
max(last_exception) as last_error
FROM system.replication_queue
WHERE database = 'webfunny_cloud_db_cluster'
GROUP BY table;
-- 查看副本状态
SELECT
table,
total_replicas,
active_replicas,
queue_size,
absolute_delay
FROM system.replicas
WHERE database = 'webfunny_cloud_db_cluster';
```
--------------------------------
### 防火墙开放9009端口
Source: https://github.com/a597873885/webfunny_monitor/blob/main/servers/monitor/migration/ClickHouse集群部署实战指南.md
使用firewall-cmd开放9009端口。
```bash
sudo firewall-cmd --permanent --add-port=9009/tcp
sudo firewall-cmd --reload
```
--------------------------------
### 优化复制性能的SQL设置
Source: https://github.com/a597873885/webfunny_monitor/blob/main/servers/monitor/migration/ClickHouse集群部署实战指南.md
调整复制相关参数以优化性能。
```sql
-- 调整复制相关参数
SET max_replica_delay_for_distributed_queries = 300;
SET replication_alter_partitions_sync = 2;
```
--------------------------------
### 检查复制队列积压的SQL查询
Source: https://github.com/a597873885/webfunny_monitor/blob/main/servers/monitor/migration/ClickHouse集群部署实战指南.md
用于检查复制队列中积压的任务数量。
```sql
SELECT count() FROM system.replication_queue;
-- 显示大量任务
```
--------------------------------
### 清理目标数据库
Source: https://github.com/a597873885/webfunny_monitor/blob/main/servers/monitor/migration/README.md
运行此脚本会删除目标数据库的所有表,用于重新迁移前清理。
```bash
node clean_target_database.js
```