首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何实施grpc-go健康检查?

如何实施grpc-go健康检查?
EN

Stack Overflow用户
提问于 2019-12-16 08:27:30
回答 3查看 6.4K关注 0票数 2

我正在寻找grpc-go健康检查的文档和代码示例。

寻找问题

没有明确的答案,我可以重新使用在我的程序中实施健康检查。

EN

回答 3

Stack Overflow用户

发布于 2020-08-05 17:30:57

我建议您查看此Github项目,以了解如何构建为生产准备的gRPC服务,其中包括健康检查等。

特定于健康检查,您可以检查它是如何完成的,这里

库利用了这个更专注的项目

如果您不想使用lib,您可以像这样实现健康检查:

代码语言:javascript
复制
import (
    "google.golang.org/grpc/health"
    "google.golang.org/grpc/health/grpc_health_v1"
)
grpcServer := grpc.NewServer()
grpc_health_v1.RegisterHealthServer(grpcServer, health.NewServer())
票数 6
EN

Stack Overflow用户

发布于 2019-12-18 15:51:19

你发布的其中一个链接有一个链接到这个read:https://github.com/grpc/grpc/blob/master/doc/health-checking.md

现在您应该实现该逻辑,并在您的服务集合中注册该服务。

readme建议手动将服务注册到健康检查服务中,这样您就可以传递服务列表作为参数来创建健康检查服务。

我建议为您的服务定义一个接口,以便健康检查服务可以以同样的方式处理所有这些服务:

代码语言:javascript
复制
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)

我希望它能帮到你。

票数 4
EN

Stack Overflow用户

发布于 2022-10-24 14:58:02

在grpc包中实现健康检查示例的最简单方法

代码语言:javascript
复制
/*
 *
 * 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)
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59352845

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档