Created
January 15, 2021 17:58
-
-
Save rponte/c0e97d7f539ebc144526bdcdc5ef4244 to your computer and use it in GitHub Desktop.
GRPC: simple example of GRPC Health Checking Protocol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package br.com.zup.edu | |
| import io.grpc.health.v1.HealthCheckRequest | |
| import io.grpc.health.v1.HealthCheckResponse | |
| import io.grpc.health.v1.HealthGrpc | |
| import io.grpc.stub.StreamObserver | |
| import javax.inject.Singleton | |
| @Singleton | |
| class HealthCheckerService: HealthGrpc.HealthImplBase() { | |
| override fun check(request: HealthCheckRequest?, responseObserver: StreamObserver<HealthCheckResponse>?) { | |
| responseObserver?.onNext(HealthCheckResponse.newBuilder() | |
| .setStatus(HealthCheckResponse.ServingStatus.SERVING) | |
| .build()) | |
| responseObserver?.onCompleted() | |
| } | |
| override fun watch(request: HealthCheckRequest?, responseObserver: StreamObserver<HealthCheckResponse>?) { | |
| for (i in 0 until 10 ) { | |
| val status = when (i % 2) { | |
| 0 -> HealthCheckResponse.ServingStatus.SERVING | |
| else -> HealthCheckResponse.ServingStatus.NOT_SERVING | |
| } | |
| Thread.sleep(1000) | |
| responseObserver?.onNext(HealthCheckResponse.newBuilder() | |
| .setStatus(status) | |
| .build()) | |
| } | |
| responseObserver?.onCompleted() | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
health.protosuggested by Google