io.grpc.reflection.v1alpha.ServerReflectionGrpc Java Examples

The following examples show how to use io.grpc.reflection.v1alpha.ServerReflectionGrpc. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ProtoReflectionServiceTest.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #2
Source File: ProtoReflectionServiceTest.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #3
Source File: ServerReflectionClient.java    From grpc-swagger with MIT License 5 votes vote down vote up
/**
 * 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);
}
 
Example #4
Source File: ServerReflectionClient.java    From grpc-swagger with MIT License 5 votes vote down vote up
/**
 * 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);
}
 
Example #5
Source File: ServerReflectionClient.java    From milkman with MIT License 5 votes vote down vote up
/** 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);
}
 
Example #6
Source File: ServerReflectionClient.java    From milkman with MIT License 5 votes vote down vote up
/**
 * 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);
}
 
Example #7
Source File: ServerReflectionClient.java    From grpc-swagger with MIT License 5 votes vote down vote up
/**
 * 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);
}
 
Example #8
Source File: ServerReflectionClient.java    From grpc-swagger with MIT License 5 votes vote down vote up
/**
 * 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);
}
 
Example #9
Source File: DemoAppTest.java    From grpc-spring-boot-starter with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example #10
Source File: ArmeriaGrpcServer.java    From grpc-by-example-java with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example #11
Source File: Main.java    From armeria with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example #12
Source File: Main.java    From armeria with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example #13
Source File: GrpcServiceServerTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@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);
            });
}
 
Example #14
Source File: ProtoReflectionServiceTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@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));
}