1
0
mirror of https://github.com/go-kratos/kratos.git synced 2025-01-24 03:46:37 +02:00
Tony Chen e35fd9af6f
http/health: add health checker (#830)
* add health checker
2021-04-15 16:48:31 +08:00

35 lines
657 B
Go

package main
import (
"context"
"errors"
"log"
"github.com/go-kratos/kratos/v2"
"github.com/go-kratos/kratos/v2/transport/http"
"github.com/go-kratos/kratos/v2/transport/http/health"
)
func main() {
handler := health.NewHandler()
handler.AddChecker("mysql", func(ctx context.Context) error {
return nil
})
handler.AddObserver("redis", func(ctx context.Context) error {
return errors.New("connection refused")
})
httpSrv := http.NewServer(http.Address(":8000"))
httpSrv.Handle("/healthz", handler)
app := kratos.New(
kratos.Name("mux"),
kratos.Server(
httpSrv,
),
)
if err := app.Run(); err != nil {
log.Println(err)
}
}