chore: 增加健康检测功能
This commit is contained in:
71
app/services/sales-api/handlers/debug/checkgrp/checkgrp.go
Normal file
71
app/services/sales-api/handlers/debug/checkgrp/checkgrp.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package checkgrp
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"go.uber.org/zap"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
type Handlers struct {
|
||||
Build string
|
||||
Log *zap.SugaredLogger
|
||||
}
|
||||
|
||||
func (h Handlers) Readiness(w http.ResponseWriter, r *http.Request) {
|
||||
data := struct {
|
||||
Status string `json:"status"`
|
||||
}{
|
||||
Status: "ok",
|
||||
}
|
||||
statusCode := http.StatusOK
|
||||
if err := response(w, statusCode, data); err != nil {
|
||||
h.Log.Errorw("readiness", "ERROR", err)
|
||||
}
|
||||
h.Log.Infow("readiness", "statusCode", statusCode, "method", r.Method, "path",
|
||||
r.URL.Path, "remoteAddr", r.RemoteAddr)
|
||||
}
|
||||
|
||||
func (h Handlers) Liveliness(w http.ResponseWriter, r *http.Request) {
|
||||
host, err := os.Hostname()
|
||||
if err != nil {
|
||||
host = "unavailable"
|
||||
}
|
||||
data := struct {
|
||||
Status string `json:"status"`
|
||||
Build string `json:"build"`
|
||||
Host string `json:"host"`
|
||||
Pod string `json:"pod"`
|
||||
PodIP string `json:"pod_ip"`
|
||||
Node string `json:"node"`
|
||||
Namespace string `json:"namespace"`
|
||||
}{
|
||||
Status: "up",
|
||||
Build: h.Build,
|
||||
Host: host,
|
||||
Pod: os.Getenv("KUBERNETES_PODNAME"),
|
||||
PodIP: os.Getenv("KUBERNETES_NAMESPACE_POD_IP"),
|
||||
Node: os.Getenv("KUBERNETES_NODENAME"),
|
||||
Namespace: os.Getenv("KUBERNETES_NAMESPACE"),
|
||||
}
|
||||
statusCode := http.StatusOK
|
||||
if err := response(w, statusCode, data); err != nil {
|
||||
h.Log.Errorw("liveness", "ERROR", err)
|
||||
}
|
||||
h.Log.Infow("readiness", "statusCode", statusCode, "method", r.Method, "path",
|
||||
r.URL.Path, "remoteAddr", r.RemoteAddr)
|
||||
}
|
||||
|
||||
func response(w http.ResponseWriter, statusCode int, data interface{}) error {
|
||||
jsonData, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
w.Header().Set("Content-type", "application/json")
|
||||
w.WriteHeader(statusCode)
|
||||
|
||||
if _, err := w.Write(jsonData); err != nil {
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
55
app/services/sales-api/handlers/handlers.go
Normal file
55
app/services/sales-api/handlers/handlers.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"expvar"
|
||||
"git.hongxiaowei.com/xiaowei/service/app/services/sales-api/handlers/debug/checkgrp"
|
||||
"github.com/dimfeld/httptreemux/v5"
|
||||
"go.uber.org/zap"
|
||||
"net/http"
|
||||
"net/http/pprof"
|
||||
"os"
|
||||
)
|
||||
|
||||
func DebugStandardLibaryMux() *http.ServeMux {
|
||||
mux := http.NewServeMux()
|
||||
|
||||
mux.HandleFunc("/debug/pprof/", pprof.Index)
|
||||
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
|
||||
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
|
||||
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
|
||||
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
|
||||
mux.Handle("/debug/vars", expvar.Handler())
|
||||
return mux
|
||||
}
|
||||
|
||||
func DebugMux(build string, log *zap.SugaredLogger) http.Handler {
|
||||
mux := DebugStandardLibaryMux()
|
||||
cgh := checkgrp.Handlers{
|
||||
Build: build,
|
||||
Log: log,
|
||||
}
|
||||
mux.HandleFunc("/debug/readiness", cgh.Readiness)
|
||||
mux.HandleFunc("/debug/liveliness", cgh.Liveliness)
|
||||
return mux
|
||||
}
|
||||
|
||||
type APIMuxConfig struct {
|
||||
Shutdown chan os.Signal
|
||||
Log *zap.SugaredLogger
|
||||
}
|
||||
|
||||
func APIMux(cfg APIMuxConfig) *httptreemux.ContextMux {
|
||||
mux := httptreemux.NewContextMux()
|
||||
|
||||
h := func(w http.ResponseWriter, r *http.Request) {
|
||||
status := struct {
|
||||
Status string
|
||||
}{
|
||||
Status: "ok",
|
||||
}
|
||||
json.NewEncoder(w).Encode(&status)
|
||||
}
|
||||
mux.Handle(http.MethodGet, "/test", h)
|
||||
return mux
|
||||
}
|
||||
@@ -1,31 +1,155 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"go.uber.org/automaxprocs/maxprocs"
|
||||
"log"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"runtime"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"git.hongxiaowei.com/xiaowei/service/app/services/sales-api/handlers"
|
||||
|
||||
"github.com/ardanlabs/conf"
|
||||
"go.uber.org/automaxprocs/maxprocs"
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
)
|
||||
|
||||
var build = "develop"
|
||||
|
||||
func main() {
|
||||
|
||||
// Set the correct number of threads for the service
|
||||
// based on what is available either by the machine or quoras.
|
||||
if _, err := maxprocs.Set(); err != nil {
|
||||
log.Println(err)
|
||||
log, err := initLog("SALES-API")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
g := runtime.GOMAXPROCS(0)
|
||||
log.Printf("starting service build[%s] CPU[%d]", build, g)
|
||||
defer log.Println("service ended")
|
||||
if err := run(log); err != nil {
|
||||
log.Errorw("err", err)
|
||||
}
|
||||
}
|
||||
func run(log *zap.SugaredLogger) error {
|
||||
if _, err := maxprocs.Set(); err != nil {
|
||||
return fmt.Errorf("maxprocs:%w", err)
|
||||
}
|
||||
log.Infow("startup", "GOMAXPROCS", runtime.GOMAXPROCS(0))
|
||||
//configuration
|
||||
cfg := struct {
|
||||
conf.Version
|
||||
Web struct {
|
||||
APIHost string `conf:"default:0.0.0.0:3000"`
|
||||
DebugHost string `conf:"default:0.0.0.0:4000"`
|
||||
ReadTimeout time.Duration `conf:"default:5s"`
|
||||
WriteTimeout time.Duration `conf:"default:10s"`
|
||||
IdleTimeout time.Duration `conf:"default:10s"`
|
||||
ShutDownTimeout time.Duration `conf:"default:10s"`
|
||||
}
|
||||
}{
|
||||
Version: conf.Version{
|
||||
SVN: build,
|
||||
Desc: "copyright information here",
|
||||
},
|
||||
}
|
||||
const prefix = "SALES"
|
||||
help, err := conf.ParseOSArgs(prefix, &cfg)
|
||||
if err != nil {
|
||||
if errors.Is(err, conf.ErrHelpWanted) {
|
||||
fmt.Println(help)
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("parsing config:%w", err)
|
||||
}
|
||||
log.Infow("starting service", "version", build)
|
||||
defer log.Infow("shutdown complete")
|
||||
|
||||
out, err := conf.String(&cfg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
log.Infow("startup", "config", out)
|
||||
// =========================================================
|
||||
// Start Debug Service
|
||||
log.Infow("startup", "status", "debug router started", "host", cfg.Web.DebugHost)
|
||||
debugMux := handlers.DebugMux(build, log)
|
||||
go func() {
|
||||
if err := http.ListenAndServe(cfg.Web.DebugHost, debugMux); err != nil {
|
||||
log.Errorw("shutdown", "status",
|
||||
"debug router closed", "host", cfg.Web.DebugHost, "ERROR", err)
|
||||
}
|
||||
}()
|
||||
log.Infow("startup", "status", "initializing API support")
|
||||
|
||||
// Make a channel to listen for an interrupt or terminate signal from ths OS.
|
||||
// Use a buffered channel because the signal package requires it
|
||||
shutdown := make(chan os.Signal, 1)
|
||||
signal.Notify(shutdown, syscall.SIGINT, syscall.SIGTERM)
|
||||
<-shutdown
|
||||
|
||||
log.Println("stopping service")
|
||||
// Construct the mux for the API calls
|
||||
apiMux := handlers.APIMux(handlers.APIMuxConfig{
|
||||
Shutdown: nil,
|
||||
Log: nil,
|
||||
})
|
||||
|
||||
// Construct a server to service the requests against the mux
|
||||
api := http.Server{
|
||||
Addr: cfg.Web.APIHost,
|
||||
Handler: apiMux,
|
||||
ReadTimeout: cfg.Web.ReadTimeout,
|
||||
WriteTimeout: cfg.Web.WriteTimeout,
|
||||
IdleTimeout: cfg.Web.IdleTimeout,
|
||||
ErrorLog: zap.NewStdLog(log.Desugar()),
|
||||
}
|
||||
|
||||
//Make a channel to listen for errors coming from the listener.Use a
|
||||
// bufferd channel so the goroutine can exit if we don't collect this error
|
||||
serverErrors := make(chan error, 1)
|
||||
|
||||
// Start the service listening for api requests
|
||||
go func() {
|
||||
log.Infow("startup", "status", "api router started", "host", api.Addr)
|
||||
serverErrors <- api.ListenAndServe()
|
||||
}()
|
||||
|
||||
// =====================================================
|
||||
// Shutdown
|
||||
|
||||
// Blocking main and waiting for shutdown
|
||||
select {
|
||||
case err := <-serverErrors:
|
||||
return fmt.Errorf("server err:%w", err)
|
||||
case sig := <-shutdown:
|
||||
log.Infow("shutdown", "status", "shutdown started", "signal", sig)
|
||||
defer log.Infow("shutdown", "status", "shutdown complete", "signal", sig)
|
||||
|
||||
// Give outstanding requests a deadline for completion
|
||||
ctx, cancel := context.WithTimeout(context.Background(), cfg.Web.ShutDownTimeout)
|
||||
defer cancel()
|
||||
|
||||
if err := api.Shutdown(ctx); err != nil {
|
||||
api.Close()
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func initLog(service string) (*zap.SugaredLogger, error) {
|
||||
config := zap.NewProductionConfig()
|
||||
config.OutputPaths = []string{"stdout"}
|
||||
config.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
|
||||
config.DisableStacktrace = true
|
||||
config.InitialFields = map[string]interface{}{
|
||||
"service": service,
|
||||
}
|
||||
|
||||
log, err := config.Build()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
return log.Sugar(), err
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user