Actuator Starter

简介

Actuator Starter 提供生产就绪的功能,帮助你监控和管理 Hiboot 应用程序。它包括健康检查、应用信息和指标端点,这些对于云原生部署至关重要。

安装

在应用程序中导入 actuator starter:

import "github.com/hidevopsio/hiboot/pkg/starter/actuator"

配置

application.yml 中启用 actuator:

app:
  profiles:
    include:
    - actuator

actuator:
  enabled: true

或通过代码启用:

package main

import (
	"github.com/hidevopsio/hiboot/pkg/app"
	"github.com/hidevopsio/hiboot/pkg/app/web"
	"github.com/hidevopsio/hiboot/pkg/starter/actuator"
)

func main() {
	web.NewApplication().
		SetProperty(app.ProfilesInclude, actuator.Profile).
		Run()
}

内置端点

健康端点

/health 端点提供应用程序健康状态:

curl http://localhost:8080/health

响应:

{
    "code": 200,
    "data": {
        "status": "UP"
    },
    "message": "Success"
}

应用信息

通过健康端点获取应用信息:

{
    "code": 200,
    "data": {
        "status": "UP",
        "app": {
            "name": "myapp",
            "version": "1.0.0"
        }
    },
    "message": "Success"
}

自定义健康指示器

创建自定义健康指示器来监控特定组件:

package health

import (
	"github.com/hidevopsio/hiboot/pkg/app"
	"github.com/hidevopsio/hiboot/pkg/starter/actuator"
)

// DatabaseHealthIndicator 检查数据库连接
type DatabaseHealthIndicator struct {
	actuator.HealthIndicator
}

func init() {
	app.Register(newDatabaseHealthIndicator)
}

func newDatabaseHealthIndicator() *DatabaseHealthIndicator {
	return &DatabaseHealthIndicator{}
}

// Name 返回指示器名称
func (h *DatabaseHealthIndicator) Name() string {
	return "database"
}

// Health 执行健康检查
func (h *DatabaseHealthIndicator) Health() *actuator.Health {
	// 检查数据库连接
	if isDatabaseConnected() {
		return &actuator.Health{
			Status: actuator.StatusUp,
			Details: map[string]interface{}{
				"database": "connected",
				"latency":  "5ms",
			},
		}
	}

	return &actuator.Health{
		Status: actuator.StatusDown,
		Details: map[string]interface{}{
			"error": "数据库连接失败",
		},
	}
}

func isDatabaseConnected() bool {
	// 实现你的数据库连接检查
	return true
}

多个健康指示器

注册多个健康指示器进行全面监控:

package health

import "github.com/hidevopsio/hiboot/pkg/app"

func init() {
	app.Register(
		newDatabaseHealthIndicator,
		newCacheHealthIndicator,
		newExternalAPIHealthIndicator,
	)
}

带有多个指示器的健康响应:

{
    "code": 200,
    "data": {
        "status": "UP",
        "components": {
            "database": {
                "status": "UP",
                "details": {
                    "database": "connected",
                    "latency": "5ms"
                }
            },
            "cache": {
                "status": "UP",
                "details": {
                    "provider": "redis",
                    "connections": 10
                }
            },
            "externalAPI": {
                "status": "UP",
                "details": {
                    "endpoint": "https://api.example.com",
                    "responseTime": "120ms"
                }
            }
        }
    },
    "message": "Success"
}

健康状态值

状态 描述
UP 组件健康且正常运行
DOWN 组件不健康或不可用
UNKNOWN 无法确定组件健康状态
OUT_OF_SERVICE 组件已被有意下线

Kubernetes 集成

存活探针

配置 Kubernetes 存活探针:

livenessProbe:
  httpGet:
    path: /health
    port: 8080
  initialDelaySeconds: 30
  periodSeconds: 10
  timeoutSeconds: 5
  failureThreshold: 3

就绪探针

配置 Kubernetes 就绪探针:

readinessProbe:
  httpGet:
    path: /health
    port: 8080
  initialDelaySeconds: 5
  periodSeconds: 5
  timeoutSeconds: 3
  failureThreshold: 3

完整部署示例

apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp
spec:
  replicas: 3
  selector:
    matchLabels:
      app: myapp
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
      - name: myapp
        image: myapp:latest
        ports:
        - containerPort: 8080
        livenessProbe:
          httpGet:
            path: /health
            port: 8080
          initialDelaySeconds: 30
          periodSeconds: 10
        readinessProbe:
          httpGet:
            path: /health
            port: 8080
          initialDelaySeconds: 5
          periodSeconds: 5
        resources:
          limits:
            cpu: "500m"
            memory: "256Mi"
          requests:
            cpu: "100m"
            memory: "128Mi"

Docker 健康检查

在 Dockerfile 中配置 Docker 健康检查:

FROM golang:1.21-alpine AS builder
WORKDIR /app
COPY . .
RUN go build -o main .

FROM alpine:latest
WORKDIR /app
COPY --from=builder /app/main .
COPY config/ config/

EXPOSE 8080

HEALTHCHECK --interval=30s --timeout=5s --start-period=5s --retries=3 \
  CMD wget -qO- http://localhost:8080/health || exit 1

CMD ["./main"]

配置参考

属性 描述 默认值
actuator.enabled 启用/禁用 actuator true

下一步