3 回答

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超8個(gè)贊
如果您不想使用該庫(kù),您可以像這樣實(shí)現(xiàn)健康檢查:
import (
? ? "google.golang.org/grpc/health"
? ? "google.golang.org/grpc/health/grpc_health_v1"
)
grpcServer := grpc.NewServer()
grpc_health_v1.RegisterHealthServer(grpcServer, health.NewServer())

TA貢獻(xiàn)1779條經(jīng)驗(yàn) 獲得超6個(gè)贊
您現(xiàn)在應(yīng)該實(shí)現(xiàn)該邏輯,并在您的服務(wù)集中注冊(cè)該服務(wù)。
自述文件建議手動(dòng)將服務(wù)注冊(cè)到運(yùn)行狀況檢查服務(wù)中,以便您可以將服務(wù)列表作為參數(shù)傳遞來(lái)創(chuàng)建運(yùn)行狀況檢查服務(wù)。
我建議為您的服務(wù)定義一個(gè)接口,以便健康檢查服務(wù)可以以相同的方式處理所有這些服務(wù):
type HealthMeter interface {
? ? GetHealth() mysample.HealthCheckResponse_ServingStatus
? ? WatchHealth() <- chan mysample.HealthCheckResponse_ServingStatus
}
// implement service "A" and "B" as both interfaces (gRPC server AND HealthMeter)
grpcServer := grpc.NewServer()
srvA := servers.NewServiceA()
srvB := servers.NewServiceB()
healthSrv := servers.NewHealthCheckServer(map[string]servers.HealthMeter{
? ? "serviceA": srvA,
? ? "serviceB": srvB,
})
mysample.RegisterServiceAServer(grpcServer, srvA)
mysample.RegisterServiceBServer(grpcServer, srvB)
mysample.RegisterHealthServer(grpcServer, healthSrv)
我希望它可以幫助你。

TA貢獻(xiàn)1911條經(jīng)驗(yàn) 獲得超7個(gè)贊
在 grpc 包中實(shí)現(xiàn)健康檢查示例的最簡(jiǎn)單方法
/*
?*
?* Copyright 2020 gRPC authors.
?*
?* Licensed under the Apache License, Version 2.0 (the "License");
?* you may not use this file except in compliance with the License.
?* You may obtain a copy of the License at
?*
?*? ? ?http://www.apache.org/licenses/LICENSE-2.0
?*
?* Unless required by applicable law or agreed to in writing, software
?* distributed under the License is distributed on an "AS IS" BASIS,
?* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
?* See the License for the specific language governing permissions and
?* limitations under the License.
?*
?*/
// Binary server is an example server.
package main
import (
? ? "context"
? ? "flag"
? ? "fmt"
? ? "log"
? ? "net"
? ? "time"
? ? "google.golang.org/grpc"
? ? pb "google.golang.org/grpc/examples/features/proto/echo"
? ? "google.golang.org/grpc/health"
? ? healthgrpc "google.golang.org/grpc/health/grpc_health_v1"
? ? healthpb "google.golang.org/grpc/health/grpc_health_v1"
)
var (
? ? port? = flag.Int("port", 50051, "the port to serve on")
? ? sleep = flag.Duration("sleep", time.Second*5, "duration between changes in health")
? ? system = "" // empty string represents the health of the system
)
type echoServer struct {
? ? pb.UnimplementedEchoServer
}
func (e *echoServer) UnaryEcho(ctx context.Context, req *pb.EchoRequest) (*pb.EchoResponse, error) {
? ? return &pb.EchoResponse{
? ? ? ? Message: fmt.Sprintf("hello from localhost:%d", *port),
? ? }, nil
}
var _ pb.EchoServer = &echoServer{}
func main() {
? ? flag.Parse()
? ? lis, err := net.Listen("tcp", fmt.Sprintf(":%d", *port))
? ? if err != nil {
? ? ? ? log.Fatalf("failed to listen: %v", err)
? ? }
? ? s := grpc.NewServer()
? ? healthcheck := health.NewServer()
? ? healthgrpc.RegisterHealthServer(s, healthcheck)
? ? pb.RegisterEchoServer(s, &echoServer{})
? ? go func() {
? ? ? ? // asynchronously inspect dependencies and toggle serving status as needed
? ? ? ? next := healthpb.HealthCheckResponse_SERVING
? ? ? ? for {
? ? ? ? ? ? healthcheck.SetServingStatus(system, next)
? ? ? ? ? ? if next == healthpb.HealthCheckResponse_SERVING {
? ? ? ? ? ? ? ? next = healthpb.HealthCheckResponse_NOT_SERVING
? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? next = healthpb.HealthCheckResponse_SERVING
? ? ? ? ? ? }
? ? ? ? ? ? time.Sleep(*sleep)
? ? ? ? }
? ? }()
? ? if err := s.Serve(lis); err != nil {
? ? ? ? log.Fatalf("failed to serve: %v", err)
? ? }
}
- 3 回答
- 0 關(guān)注
- 211 瀏覽
添加回答
舉報(bào)