refactor(srv): 内置 -healthcheck 参数并移除独立 healthcheck 二进制
Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
1 parent
57fd444c29
commit
ba9d7d83fe
4 files changed
+72
-41
No files matched your search
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user