From b3836120e225365950c466cccaaa7ecb573db5b8 Mon Sep 17 00:00:00 2001 From: lwl0608 Date: Wed, 15 May 2024 00:34:23 +0800 Subject: [PATCH] consul --- library/httpserver/httpserver.go | 38 +++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/library/httpserver/httpserver.go b/library/httpserver/httpserver.go index 81805769..9d4bfff6 100644 --- a/library/httpserver/httpserver.go +++ b/library/httpserver/httpserver.go @@ -14,6 +14,11 @@ import ( "service/library/logger" ) +const ( + ServerStatusFDRun = "RUN" // 服务运行中 + ServerStatusFDStop = "STOP" // 服务停止 +) + func StartHttpServer(srv *http.Server, cfg *configcenter.DefaultConfig, ip string, port int) { quit := make(chan os.Signal) signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM) @@ -29,9 +34,12 @@ func StartHttpServer(srv *http.Server, cfg *configcenter.DefaultConfig, ip strin // 注册服务 go func() { - if err := registerSD(ip, port); err != nil { + err := registerSD(ip, port) + if err != nil { quit <- syscall.SIGTERM + return } + setServerStatusFD(ServerStatusFDRun) }() <-quit @@ -53,6 +61,7 @@ func StartHttpServer(srv *http.Server, cfg *configcenter.DefaultConfig, ip strin } logger.Info("Server exited") + setServerStatusFD(ServerStatusFDStop) } func registerSD(ip string, port int) error { @@ -82,3 +91,30 @@ func registerSD(ip string, port int) error { } } } + +func setServerStatusFD(status string) { + os.Setenv("SERVER_STATUS_FD", status) + fmt.Println(os.Getenv("SERVER_STATUS_FD")) + return + + serverStatusFdPath := "/Users/erwin/SERVER_STATUS_FD" + file, err := os.OpenFile(serverStatusFdPath, os.O_RDWR|os.O_CREATE, 0666) + if err != nil { + logger.Info("OpenFile fail, p: %v, err: %v", serverStatusFdPath, err) + return + } + defer func() { + _ = file.Close() + }() + err = file.Truncate(0) + if err != nil { + logger.Info("file Truncate fail, p: %v, err: %v", serverStatusFdPath, err) + return + } + _, err = file.WriteString(status) + if err != nil { + logger.Info("file WriteString fail, p: %v, v: %v, err: %v", serverStatusFdPath, status, err) + return + } + logger.Info("SetServerStatusFD success: %v", status) +}