健康检查与就绪探针
使用标准 gRPC 健康检查协议,让负载均衡器和编排器知道服务何时已准备好处理流量。
健康检查与就绪探针 是 CoddyKit 上的免费 gRPC & High Performance APIs 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 gRPC & High Performance APIs 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 gRPC & High Performance APIs 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Why Health Checks Matter
An orchestrator must know whether a service instance can handle requests. Sending traffic to a starting, overloaded, or broken instance causes errors.
The gRPC health checking protocol answers: is this service healthy?
The Standard Health Service
gRPC defines grpc.health.v1.Health with two methods:
Check— a one-shot status queryWatch— a stream of status updates
Status is SERVING, NOT_SERVING, or UNKNOWN.
Per-Service Status
A health request names a service. An empty name asks about the whole server; a specific name asks about one service, so you can report each independently.
Registering the Health Server (Go)
The health package provides a ready implementation you register on your server.
import 'google.golang.org/grpc/health'
import hpb 'google.golang.org/grpc/health/grpc_health_v1'
hs := health.NewServer()
hpb.RegisterHealthServer(s, hs)Setting Status
Update status as your service transitions. Mark NOT_SERVING during startup or shutdown, then SERVING when ready.
hs.SetServingStatus('', hpb.HealthCheckResponse_SERVING)
hs.SetServingStatus('my.pkg.Orders', hpb.HealthCheckResponse_SERVING)Readiness vs Liveness
Two distinct ideas:
- Liveness: is the process alive? Restart if not.
- Readiness: can it serve now? Remove from rotation if not, but do not restart.
Health status maps cleanly to readiness.
Kubernetes gRPC Probes
Kubernetes supports native gRPC probes. Point them at your health service.
readinessProbe:
grpc:
port: 9090
service: my.pkg.OrdersProbing with grpc_health_probe
The grpc_health_probe binary lets you check health from a shell or older Kubernetes versions.
grpc_health_probe -addr=localhost:9090 -service=my.pkg.OrdersWatch for Live Updates
Smart load balancers use Watch to receive a stream of status changes, reacting instantly when an instance flips to NOT_SERVING instead of polling.
Graceful Shutdown
Before stopping, set status to NOT_SERVING and wait. Load balancers drain you out, in-flight requests finish, and clients avoid hitting a dying instance.
Tying Health to Dependencies
Report NOT_SERVING when a critical dependency (database, cache) is unreachable, so traffic routes only to instances that can actually do work.
Quick Check
Test your health-check knowledge.
Recap
You learned gRPC health checking:
- The standard
Healthservice offersCheckandWatch - Status is per-service:
SERVING,NOT_SERVING,UNKNOWN - Map health to readiness; pair with liveness probes
- Kubernetes and
grpc_health_probeconsume it - Flip to
NOT_SERVINGfor graceful drain and failed dependencies
常见问题解答
「健康检查与就绪探针」课时是免费的吗?
是的 — 「健康检查与就绪探针」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 gRPC & High Performance APIs 课程的其余内容,请升级到 CoddyKit PRO。 gRPC & High Performance APIs 课程共包含 4 节课。
「健康检查与就绪探针」这节课中我会学到什么?
使用标准 gRPC 健康检查协议,让负载均衡器和编排器知道服务何时已准备好处理流量。 你通过在浏览器中直接运行的动手代码来练习 gRPC & High Performance APIs,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 gRPC & High Performance APIs 需要有经验吗?
无需任何先前经验。CoddyKit 上的 gRPC & High Performance APIs 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「健康检查与就绪探针」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 gRPC & High Performance APIs 课中编写并运行代码吗?
能。每节 gRPC & High Performance APIs 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。