下面列出了怎么用io.grpc.health.v1.HealthGrpc的API类实例代码及写法,或者点击链接到github查看源代码。
@Inject
public GRPCServerProvider(
GRPCServerConfiguration grpcServerConfiguration,
HealthGrpc.HealthImplBase healthServiceImpl,
EventServiceGrpc.EventServiceImplBase eventServiceImpl,
MetadataServiceGrpc.MetadataServiceImplBase metadataServiceImpl,
TaskServiceGrpc.TaskServiceImplBase taskServiceImpl,
WorkflowServiceGrpc.WorkflowServiceImplBase workflowServiceImpl
) {
this.configuration = grpcServerConfiguration;
this.healthServiceImpl = healthServiceImpl;
this.eventServiceImpl = eventServiceImpl;
this.metadataServiceImpl = metadataServiceImpl;
this.taskServiceImpl = taskServiceImpl;
this.workflowServiceImpl = workflowServiceImpl;
}
@Test
public void healthServing() throws Exception {
// Generate a unique in-process server name.
String serverName = InProcessServerBuilder.generateName();
HealthCheckAggregator hca = mock(HealthCheckAggregator.class);
CompletableFuture<HealthCheckStatus> hcsf = mock(CompletableFuture.class);
HealthCheckStatus hcs = mock(HealthCheckStatus.class);
when(hcs.isHealthy()).thenReturn(true);
when(hcsf.get()).thenReturn(hcs);
when(hca.check()).thenReturn(hcsf);
HealthServiceImpl healthyService = new HealthServiceImpl(hca);
addService(serverName, healthyService);
HealthGrpc.HealthBlockingStub blockingStub = HealthGrpc.newBlockingStub(
// Create a client channel and register for automatic graceful shutdown.
grpcCleanup.register(InProcessChannelBuilder.forName(serverName).directExecutor().build()));
HealthCheckResponse reply = blockingStub.check(HealthCheckRequest.newBuilder().build());
assertEquals(HealthCheckResponse.ServingStatus.SERVING, reply.getStatus());
}
@Test
public void healthNotServing() throws Exception {
// Generate a unique in-process server name.
String serverName = InProcessServerBuilder.generateName();
HealthCheckAggregator hca = mock(HealthCheckAggregator.class);
CompletableFuture<HealthCheckStatus> hcsf = mock(CompletableFuture.class);
HealthCheckStatus hcs = mock(HealthCheckStatus.class);
when(hcs.isHealthy()).thenReturn(false);
when(hcsf.get()).thenReturn(hcs);
when(hca.check()).thenReturn(hcsf);
HealthServiceImpl healthyService = new HealthServiceImpl(hca);
addService(serverName, healthyService);
HealthGrpc.HealthBlockingStub blockingStub = HealthGrpc.newBlockingStub(
// Create a client channel and register for automatic graceful shutdown.
grpcCleanup.register(InProcessChannelBuilder.forName(serverName).directExecutor().build()));
HealthCheckResponse reply = blockingStub.check(HealthCheckRequest.newBuilder().build());
assertEquals(HealthCheckResponse.ServingStatus.NOT_SERVING, reply.getStatus());
}
@Test
public void healthException() throws Exception {
// Generate a unique in-process server name.
String serverName = InProcessServerBuilder.generateName();
HealthCheckAggregator hca = mock(HealthCheckAggregator.class);
CompletableFuture<HealthCheckStatus> hcsf = mock(CompletableFuture.class);
when(hcsf.get()).thenThrow(InterruptedException.class);
when(hca.check()).thenReturn(hcsf);
HealthServiceImpl healthyService = new HealthServiceImpl(hca);
addService(serverName, healthyService);
HealthGrpc.HealthBlockingStub blockingStub = HealthGrpc.newBlockingStub(
// Create a client channel and register for automatic graceful shutdown.
grpcCleanup.register(InProcessChannelBuilder.forName(serverName).directExecutor().build()));
thrown.expect(StatusRuntimeException.class);
thrown.expect(hasProperty("status", is(Status.INTERNAL)));
blockingStub.check(HealthCheckRequest.newBuilder().build());
}
public HealthCheckClient() {
String target = "zookeeper:///grpc.health.v1.Health";
channel = ManagedChannelBuilder.forTarget(target)
.usePlaintext()
.build();
blockingStub = HealthGrpc.newBlockingStub(channel);
}
@Override
public Health health() {
HealthGrpc.HealthImplBase healthService = (HealthGrpc.HealthImplBase) healthStatusManager
.getHealthService();
HealthCheckRequest healthcheckRequest = HealthCheckRequest.newBuilder().setService(serviceName).build();
HealthStreamObserver healthStreamObserver = new HealthStreamObserver();
healthService.check(healthcheckRequest, healthStreamObserver);
return healthStreamObserver.getHealth();
}
public HealthBlockingStub getHealthBlockingStub() {
HealthBlockingStub returnValue = null;
if (this.channel != null) {
returnValue = MetadataUtils.attachHeaders(HealthGrpc.newBlockingStub(this.channel), metadata);
}
return returnValue;
}
public HealthFutureStub getHealthFutureStub() {
HealthFutureStub returnValue = null;
if (this.channel != null) {
returnValue = MetadataUtils.attachHeaders(HealthGrpc.newFutureStub(this.channel), metadata);
}
return returnValue;
}
public HealthStub getHealthStub() {
HealthStub returnValue = null;
if (this.channel != null) {
returnValue = MetadataUtils.attachHeaders(HealthGrpc.newStub(this.channel), metadata);
}
return returnValue;
}
@Override
protected void configure() {
bind(HealthGrpc.HealthImplBase.class).to(HealthServiceImpl.class);
bind(EventServiceGrpc.EventServiceImplBase.class).to(EventServiceImpl.class);
bind(MetadataServiceGrpc.MetadataServiceImplBase.class).to(MetadataServiceImpl.class);
bind(TaskServiceGrpc.TaskServiceImplBase.class).to(TaskServiceImpl.class);
bind(WorkflowServiceGrpc.WorkflowServiceImplBase.class).to(WorkflowServiceImpl.class);
bind(GRPCServerConfiguration.class).to(GRPCServerSystemConfiguration.class);
bind(GRPCServerProvider.class);
}
@Test
public void testHealthCheck() throws ExecutionException, InterruptedException {
final HealthCheckRequest healthCheckRequest = HealthCheckRequest.newBuilder().setService(GreeterGrpc.getServiceDescriptor().getName()).build();
final HealthGrpc.HealthFutureStub healthFutureStub = HealthGrpc.newFutureStub(channel);
final HealthCheckResponse.ServingStatus servingStatus = healthFutureStub.check(healthCheckRequest).get().getStatus();
assertNotNull(servingStatus);
assertEquals(servingStatus, HealthCheckResponse.ServingStatus.SERVING);
}
HcStream() {
callCreationNanos = time.currentTimeNanos();
callServiceName = serviceName;
call = subchannel.asChannel().newCall(HealthGrpc.getWatchMethod(), CallOptions.DEFAULT);
}
@Before
public void setup() {
grpcServerRule.getServiceRegistry().addService(service);
stub = HealthGrpc.newStub(grpcServerRule.getChannel());
blockingStub = HealthGrpc.newBlockingStub(grpcServerRule.getChannel());
}
HcStream() {
stopwatch = stopwatchSupplier.get().start();
callServiceName = serviceName;
call = subchannel.asChannel().newCall(HealthGrpc.getWatchMethod(), CallOptions.DEFAULT);
}
@Before
public void setup() {
grpcServerRule.getServiceRegistry().addService(service);
stub = HealthGrpc.newStub(grpcServerRule.getChannel());
blockingStub = HealthGrpc.newBlockingStub(grpcServerRule.getChannel());
}