下面列出了怎么用io.grpc.reflection.v1alpha.ServerReflectionGrpc的API类实例代码及写法,或者点击链接到github查看源代码。
@Before
public void setUp() throws Exception {
reflectionService = ProtoReflectionService.newInstance();
Server server =
InProcessServerBuilder.forName("proto-reflection-test")
.directExecutor()
.addService(reflectionService)
.addService(new ReflectableServiceGrpc.ReflectableServiceImplBase() {})
.fallbackHandlerRegistry(handlerRegistry)
.build()
.start();
grpcCleanupRule.register(server);
ManagedChannel channel =
grpcCleanupRule.register(
InProcessChannelBuilder.forName("proto-reflection-test").directExecutor().build());
stub = ServerReflectionGrpc.newStub(channel);
}
@Before
public void setUp() throws Exception {
reflectionService = ProtoReflectionService.newInstance();
server =
InProcessServerBuilder.forName("proto-reflection-test")
.directExecutor()
.addService(reflectionService)
.addService(new ReflectableServiceGrpc.ReflectableServiceImplBase() {})
.fallbackHandlerRegistry(handlerRegistry)
.build()
.start();
channel = InProcessChannelBuilder.forName("proto-reflection-test").directExecutor().build();
stub = ServerReflectionGrpc.newStub(channel);
}
/**
* Asks the remote server to list its services and completes when the server responds.
*/
public ListenableFuture<ImmutableList<String>> listServices() {
ListServicesHandler rpcHandler = new ListServicesHandler();
StreamObserver<ServerReflectionRequest> requestStream = ServerReflectionGrpc.newStub(channel)
.withDeadlineAfter(LIST_RPC_DEADLINE_MS, TimeUnit.MILLISECONDS)
.serverReflectionInfo(rpcHandler);
return rpcHandler.start(requestStream);
}
/**
* Returns a {@link FileDescriptorSet} containing all the transitive dependencies of the supplied
* service, as provided by the remote server.
*/
public ListenableFuture<FileDescriptorSet> lookupService(String serviceName) {
LookupServiceHandler rpcHandler = new LookupServiceHandler(serviceName);
StreamObserver<ServerReflectionRequest> requestStream = ServerReflectionGrpc.newStub(channel)
.withDeadlineAfter(LOOKUP_RPC_DEADLINE_MS, TimeUnit.MILLISECONDS)
.serverReflectionInfo(rpcHandler);
return rpcHandler.start(requestStream);
}
/** Asks the remote server to list its services and completes when the server responds. */
public ListenableFuture<ImmutableList<String>> listServices() {
ListServicesHandler rpcHandler = new ListServicesHandler();
StreamObserver<ServerReflectionRequest> requestStream = ServerReflectionGrpc.newStub(channel)
.withDeadlineAfter(LIST_RPC_DEADLINE_MS, TimeUnit.MILLISECONDS)
.serverReflectionInfo(rpcHandler);
return rpcHandler.start(requestStream);
}
/**
* Returns a {@link FileDescriptorSet} containing all the transitive dependencies of the supplied
* service, as provided by the remote server.
*/
public ListenableFuture<FileDescriptorSet> lookupService(String serviceName) {
LookupServiceHandler rpcHandler = new LookupServiceHandler(serviceName);
StreamObserver<ServerReflectionRequest> requestStream = ServerReflectionGrpc.newStub(channel)
.withDeadlineAfter(LOOKUP_RPC_DEADLINE_MS, TimeUnit.MILLISECONDS)
.serverReflectionInfo(rpcHandler);
return rpcHandler.start(requestStream);
}
/**
* Asks the remote server to list its services and completes when the server responds.
*/
public ListenableFuture<ImmutableList<String>> listServices() {
ListServicesHandler rpcHandler = new ListServicesHandler();
StreamObserver<ServerReflectionRequest> requestStream = ServerReflectionGrpc.newStub(channel)
.withDeadlineAfter(LIST_RPC_DEADLINE_MS, TimeUnit.MILLISECONDS)
.serverReflectionInfo(rpcHandler);
return rpcHandler.start(requestStream);
}
/**
* Returns a {@link FileDescriptorSet} containing all the transitive dependencies of the supplied
* service, as provided by the remote server.
*/
public ListenableFuture<FileDescriptorSet> lookupService(String serviceName) {
LookupServiceHandler rpcHandler = new LookupServiceHandler(serviceName);
StreamObserver<ServerReflectionRequest> requestStream = ServerReflectionGrpc.newStub(channel)
.withDeadlineAfter(LOOKUP_RPC_DEADLINE_MS, TimeUnit.MILLISECONDS)
.serverReflectionInfo(rpcHandler);
return rpcHandler.start(requestStream);
}
@Test
public void testReflection() throws InterruptedException {
List<String> discoveredServiceNames = new ArrayList<>();
ServerReflectionRequest request = ServerReflectionRequest.newBuilder().setListServices("services").setHost("localhost").build();
CountDownLatch latch = new CountDownLatch(1);
ServerReflectionGrpc.newStub(channel).serverReflectionInfo(new StreamObserver<ServerReflectionResponse>() {
@Override
public void onNext(ServerReflectionResponse value) {
List<ServiceResponse> serviceList = value.getListServicesResponse().getServiceList();
for (ServiceResponse serviceResponse : serviceList) {
discoveredServiceNames.add(serviceResponse.getName());
}
}
@Override
public void onError(Throwable t) {
}
@Override
public void onCompleted() {
latch.countDown();
}
}).onNext(request);
latch.await(3, TimeUnit.SECONDS);
assertFalse(discoveredServiceNames.isEmpty());
}
static Server newServer(int httpPort, int httpsPort) throws Exception {
final HelloRequest exampleRequest = HelloRequest.newBuilder().setName("Armeria").build();
final HttpServiceWithRoutes grpcService =
GrpcService.builder()
.addService(new HelloServiceImpl())
// See https://github.com/grpc/grpc-java/blob/master/documentation/server-reflection-tutorial.md
.addService(ProtoReflectionService.newInstance())
.supportedSerializationFormats(GrpcSerializationFormats.values())
.enableUnframedRequests(true)
// You can set useBlockingTaskExecutor(true) in order to execute all gRPC
// methods in the blockingTaskExecutor thread pool.
// .useBlockingTaskExecutor(true)
.build();
return Server.builder()
.http(httpPort)
.https(httpsPort)
.tlsSelfSigned()
.service(grpcService)
// You can access the documentation service at http://127.0.0.1:8080/docs.
// See https://line.github.io/armeria/server-docservice.html for more information.
.serviceUnder("/docs", DocService.builder()
.exampleRequestForMethod(HelloServiceGrpc.SERVICE_NAME,
"Hello", exampleRequest)
.exampleRequestForMethod(HelloServiceGrpc.SERVICE_NAME,
"LazyHello", exampleRequest)
.exampleRequestForMethod(HelloServiceGrpc.SERVICE_NAME,
"BlockingHello", exampleRequest)
.exclude(DocServiceFilter.ofServiceName(
ServerReflectionGrpc.SERVICE_NAME))
.build())
.build();
}
static Server newServer(int httpPort, int httpsPort) throws Exception {
final HelloRequest exampleRequest = HelloRequest.newBuilder().setName("Armeria").build();
final HttpServiceWithRoutes grpcService =
GrpcService.builder()
.addService(new HelloServiceImpl())
// See https://github.com/grpc/grpc-java/blob/master/documentation/server-reflection-tutorial.md
.addService(ProtoReflectionService.newInstance())
.supportedSerializationFormats(GrpcSerializationFormats.values())
.enableUnframedRequests(true)
// You can set useBlockingTaskExecutor(true) in order to execute all gRPC
// methods in the blockingTaskExecutor thread pool.
// .useBlockingTaskExecutor(true)
.build();
return Server.builder()
.http(httpPort)
.https(httpsPort)
.tlsSelfSigned()
.service(grpcService)
// You can access the documentation service at http://127.0.0.1:8080/docs.
// See https://armeria.dev/docs/server-docservice for more information.
.serviceUnder("/docs",
DocService.builder()
.exampleRequestForMethod(
HelloServiceGrpc.SERVICE_NAME,
"Hello", exampleRequest)
.exampleRequestForMethod(
HelloServiceGrpc.SERVICE_NAME,
"LazyHello", exampleRequest)
.exampleRequestForMethod(
HelloServiceGrpc.SERVICE_NAME,
"BlockingHello", exampleRequest)
.exclude(DocServiceFilter.ofServiceName(
ServerReflectionGrpc.SERVICE_NAME))
.build())
.build();
}
static Server newServer(int httpPort, int httpsPort) throws Exception {
final HelloRequest exampleRequest = HelloRequest.newBuilder().setName("Armeria").build();
final HttpServiceWithRoutes grpcService =
GrpcService.builder()
.addService(new HelloServiceImpl())
// See https://github.com/grpc/grpc-java/blob/master/documentation/server-reflection-tutorial.md
.addService(ProtoReflectionService.newInstance())
.supportedSerializationFormats(GrpcSerializationFormats.values())
.enableUnframedRequests(true)
// You can set useBlockingTaskExecutor(true) in order to execute all gRPC
// methods in the blockingTaskExecutor thread pool.
// .useBlockingTaskExecutor(true)
.build();
return Server.builder()
.http(httpPort)
.https(httpsPort)
.tlsSelfSigned()
.service(grpcService)
// You can access the documentation service at http://127.0.0.1:8080/docs.
// See https://armeria.dev/docs/server-docservice for more information.
.serviceUnder("/docs",
DocService.builder()
.exampleRequestForMethod(HelloServiceGrpc.SERVICE_NAME,
"Hello", exampleRequest)
.exampleRequestForMethod(HelloServiceGrpc.SERVICE_NAME,
"LazyHello", exampleRequest)
.exampleRequestForMethod(HelloServiceGrpc.SERVICE_NAME,
"BlockingHello", exampleRequest)
.exclude(DocServiceFilter.ofServiceName(
ServerReflectionGrpc.SERVICE_NAME))
.build())
.build();
}
@Test
void reflectionService() throws Exception {
final ServerReflectionStub stub = ServerReflectionGrpc.newStub(channel);
final AtomicReference<ServerReflectionResponse> response = new AtomicReference<>();
final StreamObserver<ServerReflectionRequest> request = stub.serverReflectionInfo(
new StreamObserver<ServerReflectionResponse>() {
@Override
public void onNext(ServerReflectionResponse value) {
response.set(value);
}
@Override
public void onError(Throwable t) {}
@Override
public void onCompleted() {}
});
request.onNext(ServerReflectionRequest.newBuilder()
.setListServices("")
.build());
request.onCompleted();
await().untilAsserted(
() -> {
assertThat(response).doesNotHaveValue(null);
// Instead of making this test depend on every other one, just check that there is at
// least two services returned corresponding to UnitTestService and
// ProtoReflectionService.
assertThat(response.get().getListServicesResponse().getServiceList())
.hasSizeGreaterThanOrEqualTo(2);
});
}
@Test
public void sharedServiceBetweenServers()
throws IOException, ExecutionException, InterruptedException {
Server anotherServer = InProcessServerBuilder.forName("proto-reflection-test-2")
.directExecutor()
.addService(reflectionService)
.addService(new AnotherReflectableServiceGrpc.AnotherReflectableServiceImplBase() {})
.build()
.start();
grpcCleanupRule.register(anotherServer);
ManagedChannel anotherChannel = grpcCleanupRule.register(
InProcessChannelBuilder.forName("proto-reflection-test-2").directExecutor().build());
ServerReflectionGrpc.ServerReflectionStub stub2 = ServerReflectionGrpc.newStub(anotherChannel);
ServerReflectionRequest request =
ServerReflectionRequest.newBuilder().setHost(TEST_HOST).setListServices("services").build();
StreamRecorder<ServerReflectionResponse> responseObserver = StreamRecorder.create();
StreamObserver<ServerReflectionRequest> requestObserver =
stub2.serverReflectionInfo(responseObserver);
requestObserver.onNext(request);
requestObserver.onCompleted();
List<ServiceResponse> response =
responseObserver.firstValue().get().getListServicesResponse().getServiceList();
assertEquals(new HashSet<>(
Arrays.asList(
ServiceResponse.newBuilder()
.setName("grpc.reflection.v1alpha.ServerReflection")
.build(),
ServiceResponse.newBuilder()
.setName("grpc.reflection.testing.AnotherReflectableService")
.build())),
new HashSet<>(response));
}