Docker运行moleculer

在 Docker 环境中运行和监测 Moleculer 微服务

以下是完整的 Docker 化方案,包含运行、监测和可视化:

1. 项目结构

1
2
3
4
5
6
7
8
9
10
moleculer-demo/
├── services/
│ ├── math.service.js
│ ├── greeter.service.js
│ └── gateway.service.js
├── index.js
├── package.json
├── Dockerfile
├── docker-compose.yml
└── prometheus.yml

2. 修改 index.js 支持 Docker 环境

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
const { ServiceBroker } = require("moleculer");

// 使用环境变量配置
const broker = new ServiceBroker({
logger: true,
metrics: process.env.METRICS_ENABLED === "true",
transporter: process.env.TRANSPORTER || "TCP",
tracing: {
enabled: process.env.TRACING_ENABLED === "true",
exporter: process.env.TRACING_EXPORTER || "Console"
}
});

// 加载服务
broker.loadService("./services/math.service.js");
broker.loadService("./services/greeter.service.js");
broker.loadService("./services/gateway.service.js");

// 启动服务
broker.start();

3. Dockerfile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 使用 Node.js 官方镜像
FROM node:18-alpine

# 设置工作目录
WORKDIR /app

# 复制依赖文件并安装
COPY package*.json ./
RUN npm install

# 复制应用代码
COPY . .

# 暴露端口
EXPOSE 3000 3030

# 启动命令
CMD ["node", "index.js"]

4. docker-compose.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
version: '3.8'

services:
# 主应用服务
app:
build: .
environment:
- METRICS_ENABLED=true
- TRACING_ENABLED=true
- TRANSPORTER=nats://nats:4222
ports:
- "3000:3000" # API 网关
- "3030:3030" # 指标端点
depends_on:
- nats
- prometheus
networks:
- moleculer-net

# NATS 消息代理
nats:
image: nats:latest
ports:
- "4222:4222"
networks:
- moleculer-net

# Prometheus 监控
prometheus:
image: prom/prometheus:latest
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml
ports:
- "9090:9090"
networks:
- moleculer-net

# Grafana 可视化
grafana:
image: grafana/grafana:latest
environment:
- GF_SECURITY_ADMIN_PASSWORD=admin
ports:
- "3001:3000"
volumes:
- grafana-storage:/var/lib/grafana
depends_on:
- prometheus
networks:
- moleculer-net

# Jaeger 分布式追踪
jaeger:
image: jaegertracing/all-in-one:latest
ports:
- "16686:16686" # Jaeger UI
- "6831:6831/udp" # Jaeger Agent
networks:
- moleculer-net

networks:
moleculer-net:

volumes:
grafana-storage:

5. prometheus.yml 配置

1
2
3
4
5
6
7
global:
scrape_interval: 15s

scrape_configs:
- job_name: 'moleculer'
static_configs:
- targets: ['app:3030'] # 监控应用指标

6. 更新网关服务支持指标端点

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// gateway.service.js
const ApiGateway = require("moleculer-web");

// 添加指标端点
const METRICS_PORT = process.env.METRICS_PORT || 3030;

module.exports = {
name: "api",
mixins: [ApiGateway],
settings: {
port: process.env.PORT || 3000,

// 添加指标端点
metrics: {
enabled: true,
port: METRICS_PORT
},

routes: [
{
path: "/api",
aliases: {
"GET /math/add": "math.add",
"GET /math/multiply": "math.multiply",
"POST /greet": "greeter.hello",
"GET /welcome/:name": "greeter.welcome"
}
}
]
}
};

7. 构建和运行

1
2
3
4
5
# 启动所有服务
docker-compose up --build

# 查看运行状态
docker-compose ps

8. 访问服务

9. 监控系统访问

  1. Prometheus:

  2. Grafana:

  3. Jaeger 分布式追踪:

10. 关键监控指标

Moleculer 自动暴露的 Prometheus 指标:

  • moleculer_actions_active_requests: 当前活跃请求数
  • moleculer_actions_time: 请求处理时间
  • moleculer_events_total: 事件总数
  • moleculer_requests_total: 请求总数
  • moleculer_errors_total: 错误总数
  • process_cpu_usage: CPU 使用率
  • process_memory_usage: 内存使用量

11. 生产环境建议

  1. 健康检查:

    1
    2
    3
    4
    5
    6
    # docker-compose.yml
    healthcheck:
    test: ["CMD", "curl", "-f", "http://localhost:3000/health"]
    interval: 30s
    timeout: 5s
    retries: 3
  2. 日志管理:

    1
    2
    3
    4
    5
    6
    # docker-compose.yml
    logging:
    driver: "json-file"
    options:
    max-size: "10m"
    max-file: "3"
  3. 集群部署:

    1
    2
    # 扩展服务实例
    docker-compose up -d --scale app=3
  4. 使用环境变量配置:

    1
    2
    3
    4
    5
    # .env 文件
    TRANSPORTER=nats://nats:4222
    METRICS_PORT=3030
    TRACING_ENABLED=true
    TRACING_EXPORTER=Jaeger

12. 常用诊断命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 查看服务日志
docker-compose logs -f app

# 进入容器检查
docker-compose exec app sh

# 查看服务状态
curl http://localhost:3000/~node/health

# 查看服务列表
curl http://localhost:3000/~node/services

# 查看指标
curl http://localhost:3030/metrics

此方案提供了完整的 Docker 化运行环境,并集成了:

  • NATS 消息代理(用于服务间通信)
  • Prometheus(指标收集)
  • Grafana(指标可视化)
  • Jaeger(分布式追踪)

所有组件通过 Docker Compose 一键启动,适合开发和生产环境使用。