Dubbo-go provides a built-in health check service based on the Triple protocol to help users check the health status of their services.
Start service in dubbo-go-samples/health_check/go-server
. You can use the client code below to check the status of the “greet.GreetService”.
package main
import (
"context"
"dubbo.apache.org/dubbo-go/v3/client"
_ "dubbo.apache.org/dubbo-go/v3/imports"
health "dubbo.apache.org/dubbo-go/v3/protocol/triple/health/triple_health"
"github.com/dubbogo/gost/log/logger"
)
func main() {
cli, err := client.NewClient(
client.WithClientURL("tri://127.0.0.1:20000"),
)
if err != nil {
panic(err)
}
svc, err := health.NewHealth(cli)
if err != nil {
panic(err)
}
check, err := svc.Check(context.Background(), &health.HealthCheckRequest{Service: "greet.GreetService"})
if err != nil {
logger.Error(err)
} else {
logger.Info("greet.GreetService's health", check.String())
}
watch, err := svc.Watch(context.Background(), &health.HealthCheckRequest{Service: "greet.GreetService"})
if err != nil {
logger.Error(err)
} else {
if watch.Recv() {
logger.Info("greet.GreetService's health", watch.Msg().String())
}
}
}
After starting, the following output will be displayed:
[greet.GreetService's health status:SERVING] [greet.GreetService's health status:SERVING]
Start service in dubbo-go-samples/health_check/go-server
. You can make the following HTTP request to check the status of the “greet.GreetService”:
POST /grpc.health.v1.Health/Check Host: 127.0.0.1:20000 Content-Type: application/json {"service":"greet.GreetService"}
The response will be:
{ "status": "SERVING" }