如果服务没有HTTP服务器,请编写健康检查端点。

huangapple go评论81阅读模式
英文:

write health check endpoints if the service has no HTTP server

问题

我想为两个不同的服务编写健康检查端点,但问题是它们没有HTTP服务器。

如果我可以编写健康检查端点,我该如何进行。或者说,使用Golang编写健康检查端点是否必须要有一个HTTP服务器。

英文:

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.

答案1

得分: 1

是的,你可以使用类似以下的代码向你的应用程序添加一个HTTP健康检查处理程序。然后,在执行健康检查的服务中,确保它知道要对哪个端口运行HTTP检查。

package main

import "net/http"

func main() {
	// 启动健康检查端点,并确保不阻塞
	go func() {
		_ = http.ListenAndServe(":8080", http.HandlerFunc(
			func(w http.ResponseWriter, r *http.Request) {
				_, _ = w.Write([]byte("ok"))
			},
		))
	}()
	
	// 启动我的应用程序代码
}

或者,如果你需要在单独的路径上公开健康检查路由,可以这样做。

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

更新
如果你想检查一个Go协程的健康状态,可以这样做。

package main

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

	select {
	case <-crashed:
		// 在Go协程崩溃后执行一些操作
	}
}
英文:

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 &quot;net/http&quot;

func main() {
	// Start the health check endpoint and make sure not to block
	go func() {
		_ = http.ListenAndServe(&quot;:8080&quot;, http.HandlerFunc(
			func(w http.ResponseWriter, r *http.Request) {
				_, _ = w.Write([]byte(&quot;ok&quot;))
			},
		))
	}()
	
	// 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(&quot;/health&quot;, func(w http.ResponseWriter, r *http.Request) {
	_, _ = w.Write([]byte(&quot;ok&quot;))
})
_ = http.ListenAndServe(&quot;:8080&quot;, 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 &lt;-crashed:
		// Do something now that the go-routine crashed
	}
}

答案2

得分: 0

不强制要求拥有一个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) // 避免发送过多的电子邮件
			}
		}
	}
}

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 (
	&quot;fmt&quot;
	&quot;time&quot;

	&quot;github.com/go-ping/ping&quot;
)

func main() {
	t := time.NewTicker(5 * time.Second)
	for {
		select {
		case &lt;-t.C:
			err := checkService(&quot;google&quot;, &quot;216.239.38.120&quot;)
			if err != nil {
				fmt.Println(&quot;notif to email, error:&quot;, 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(&quot;service %s down&quot;, name)
	}
	fmt.Printf(&quot;stats: %#v\n&quot;, stats)
	return nil
}

huangapple
  • 本文由 发表于 2022年3月11日 05:47:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/71431198.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定