如果服务没有 HTTP 服务器,则写入健康检查端点

发布于 2025-01-13 02:45:57 字数 117 浏览 0 评论 0原文

我想为 2 个不同的服务编写运行状况检查端点,但问题是它们没有 HTTP 服务器。

如果我可以编写健康检查端点,我该如何继续。或者是否必须有一个 HTTP 服务器才能使用 Golang 来处理健康检查端点。

I want to write health check endpoints for 2 different services, but the problem is they have no HTTP server.

if I can write health check endpoints how can I proceed. or is it mandatory to have an HTTP server to work on health check endpoints with Golang.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

妖妓 2025-01-20 02:45:57

HTTP 服务器不是必需的。

您可以ping通您的业务服务器的IP地址。例如,我使用 ping 存储库:

package main

import (
    "fmt"
    "time"

    "github.com/go-ping/ping"
)

func main() {
    t := time.NewTicker(5 * time.Second)
    for {
        select {
        case <-t.C:
            err := checkService("google", "216.239.38.120")
            if err != nil {
                fmt.Println("notif to email, error:", err.Error())
                time.Sleep(1 * time.Hour) // to not spam email
            }
        }
    }
}

func checkService(name string, ip string) error {
    p, err := ping.NewPinger(ip)
    if err != nil {
        return err
    }
    p.Count = 3
    p.Timeout = 5 * time.Second
    err = p.Run()
    if err != nil {
        return err
    }
    stats := p.Statistics()
    if stats.PacketLoss == 100 {
        return fmt.Errorf("service %s down", name)
    }
    fmt.Printf("stats: %#v\n", stats)
    return nil
}

It's not mandatory to have an HTTP server.

You can ping the IP address of your service server. For example I use ping repo:

package main

import (
    "fmt"
    "time"

    "github.com/go-ping/ping"
)

func main() {
    t := time.NewTicker(5 * time.Second)
    for {
        select {
        case <-t.C:
            err := checkService("google", "216.239.38.120")
            if err != nil {
                fmt.Println("notif to email, error:", err.Error())
                time.Sleep(1 * time.Hour) // to not spam email
            }
        }
    }
}

func checkService(name string, ip string) error {
    p, err := ping.NewPinger(ip)
    if err != nil {
        return err
    }
    p.Count = 3
    p.Timeout = 5 * time.Second
    err = p.Run()
    if err != nil {
        return err
    }
    stats := p.Statistics()
    if stats.PacketLoss == 100 {
        return fmt.Errorf("service %s down", name)
    }
    fmt.Printf("stats: %#v\n", stats)
    return nil
}
青柠芒果 2025-01-20 02:45:57

是的,您可以使用类似的方法将 HTTP 运行状况检查处理程序添加到您的应用程序中。然后,在执行运行状况检查的服务中,只需确保它知道要针对哪个端口运行 HTTP 检查即可。

package main

import "net/http"

func main() {
    // Start the health check endpoint and make sure not to block
    go func() {
        _ = http.ListenAndServe(":8080", http.HandlerFunc(
            func(w http.ResponseWriter, r *http.Request) {
                _, _ = w.Write([]byte("ok"))
            },
        ))
    }()
    
    // Start my application code
}

或者,如果您需要在单独的路径上公开您的健康检查路由,您可以执行类似的操作。

http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
    _, _ = w.Write([]byte("ok"))
})
_ = http.ListenAndServe(":8080", nil)

已更新
如果你想检查 goroutine 的健康状况,你可以这样做。

package main

func main() {
    crashed := make(chan struct{})
    go func() {
        defer close(crashed)
    }()

    select {
    case <-crashed:
        // Do something now that the go-routine crashed
    }
}

Yes, you can add an HTTP health check handler to your application with something like this. Then, in the service that's performing the health check, just make sure it knows which port to run the HTTP checks against.

package main

import "net/http"

func main() {
    // Start the health check endpoint and make sure not to block
    go func() {
        _ = http.ListenAndServe(":8080", http.HandlerFunc(
            func(w http.ResponseWriter, r *http.Request) {
                _, _ = w.Write([]byte("ok"))
            },
        ))
    }()
    
    // Start my application code
}

Alternatively, if you need to expose your health check route at a separate path, you can do something like this.

http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
    _, _ = w.Write([]byte("ok"))
})
_ = http.ListenAndServe(":8080", nil)

Updated
If you want to check the health of a go-routine, you can do something like this.

package main

func main() {
    crashed := make(chan struct{})
    go func() {
        defer close(crashed)
    }()

    select {
    case <-crashed:
        // Do something now that the go-routine crashed
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文