refactor(srv): 内置 -healthcheck 参数并移除独立 healthcheck 二进制

Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
adminandClaude Code committed 2026-11-12 17:15:32 +08:00
1 parent 57fd444c29
commit ba9d7d83fe
4 files changed
+72 -41

No files matched your search

+1 -3
View File
@@ -11,15 +11,13 @@ RUN --mount=type=ssh,required=true \
--mount=type=secret,id=git_known_hosts,target=/root/.ssh/known_hosts,required=true \
go mod download
COPY . .
RUN CGO_ENABLED=0 go build -mod=readonly -trimpath -o /out/srv ./cmd/srv \
&& CGO_ENABLED=0 go build -mod=readonly -trimpath -o /out/grpc-health ./cmd/healthcheck
RUN CGO_ENABLED=0 go build -mod=readonly -trimpath -o /out/srv ./cmd/srv
FROM harbor.vitatech.com.cn:19443/vitavideo/base-alpine:3.23
RUN apk add --no-cache ca-certificates tzdata
WORKDIR /app
RUN mkdir -p logs && chown 65532:65532 logs
COPY --from=builder /out/srv ./srv
COPY --from=builder /out/grpc-health ./grpc-health
USER 65532:65532
EXPOSE 50112
ENTRYPOINT ["./srv"]
-38
View File
@@ -1,38 +0,0 @@
// Command healthcheck checks the existing gRPC Health service for container readiness.
package main
import (
"context"
"fmt"
"os"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/health/grpc_health_v1"
)
func main() {
if err := check(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func check() error {
conn, err := grpc.NewClient("127.0.0.1:50112", grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
return err
}
defer conn.Close()
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
result, err := grpc_health_v1.NewHealthClient(conn).Check(ctx, &grpc_health_v1.HealthCheckRequest{})
if err != nil {
return err
}
if result.Status != grpc_health_v1.HealthCheckResponse_SERVING {
return fmt.Errorf("gRPC service is %s", result.Status)
}
return nil
}
+50
View File
@@ -29,6 +29,7 @@ import (
"github.com/nats-io/nats.go"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/health"
"google.golang.org/grpc/health/grpc_health_v1"
"google.golang.org/grpc/reflection"
@@ -206,12 +207,61 @@ func run() error {
}
}
func healthAddress(configFile string) (string, error) {
var cfg config.Config
if err := configs.Load(configFile, &cfg); err != nil {
return "", err
}
host, port, err := net.SplitHostPort(cfg.Server.Addr)
if err != nil {
return "", err
}
switch host {
case "", "0.0.0.0":
host = "127.0.0.1"
case "::":
host = "::1"
}
return net.JoinHostPort(host, port), nil
}
func checkHealth(configFile string) error {
address, err := healthAddress(configFile)
if err != nil {
return err
}
conn, err := grpc.NewClient(address, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
return err
}
defer conn.Close()
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
result, err := grpc_health_v1.NewHealthClient(conn).Check(ctx, &grpc_health_v1.HealthCheckRequest{})
if err != nil {
return err
}
if result.Status != grpc_health_v1.HealthCheckResponse_SERVING {
return fmt.Errorf("gRPC service is %s", result.Status)
}
return nil
}
func main() {
healthcheckOnly := flag.Bool("healthcheck", false, "按服务配置检查健康状态并退出")
flag.StringVar(&logPath, "logPath", "./logs", "日志目录")
flag.StringVar(&configPath, "config", "", "配置文件地址,默认 CONFIG_FILE 或 ./configs/config.yaml")
flag.StringVar(&host, "host", "127.0.0.1", "本机 IP,未配置 registry.advertise_addr 时使用")
flag.Parse()
if *healthcheckOnly {
if err := checkHealth(configPath); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
return
}
writer, err := rotatelogs.New(filepath.Join(logPath, "micro_layout_srv_%Y%m%d.log"), rotatelogs.WithMaxAge(72*time.Hour))
if err != nil {
log.Fatal(err)
+21
View File
@@ -55,3 +55,24 @@ func TestRunMonitorStopsOnCancellation(t *testing.T) {
t.Fatal("monitor did not stop after cancellation")
}
}
func TestHealthAddressUsesConfiguredPort(t *testing.T) {
file := filepath.Join(t.TempDir(), "rpc.yaml")
if err := os.WriteFile(file, []byte("server:\n addr: \":50002\"\n"), 0600); err != nil {
t.Fatal(err)
}
t.Setenv("CONFIG_FILE", file)
if value, exists := os.LookupEnv("APP_SERVER_ADDR"); exists {
t.Setenv("APP_SERVER_ADDR", value)
if err := os.Unsetenv("APP_SERVER_ADDR"); err != nil {
t.Fatal(err)
}
}
address, err := healthAddress("")
if err != nil {
t.Fatal(err)
}
if address != "127.0.0.1:50002" {
t.Fatalf("probe address = %q, want configured address 127.0.0.1:50002", address)
}
}