com.linecorp.armeria.server.grpc.GrpcService Java Examples

The following examples show how to use com.linecorp.armeria.server.grpc.GrpcService. 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: ArmeriaAutoConfigurationTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Bean
public GrpcServiceRegistrationBean helloGrpcService() {
    return new GrpcServiceRegistrationBean()
            .setServiceName("helloGrpcService")
            .setService(GrpcService.builder()
                                   .addService(new HelloGrpcService())
                                   .supportedSerializationFormats(GrpcSerializationFormats.values())
                                   .enableUnframedRequests(true)
                                   .build())
            .setDecorators(LoggingService.newDecorator())
            .addExampleRequests(HelloServiceGrpc.SERVICE_NAME,
                                "Hello",
                                HelloRequest.newBuilder().setName("Armeria").build())
            .addExampleHeaders(HelloServiceGrpc.SERVICE_NAME, "x-additional-header", "headerVal")
            .addExampleHeaders(HelloServiceGrpc.SERVICE_NAME, "Hello", "x-additional-header",
                               "headerVal");
}
 
Example #2
Source File: StackdriverMockServer.java    From zipkin-gcp with Apache License 2.0 5 votes vote down vote up
public StackdriverMockServer() {
  this.server = Server.builder()
      .service(GrpcService.builder()
          .addService(new Service())
          .build())
      .tlsSelfSigned()
      .build();
}
 
Example #3
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 #4
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 #5
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 #6
Source File: DownstreamSimpleBenchmark.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Override
protected void setUp() throws Exception {
    server = Server.builder()
                   .serviceUnder("/",
                                 GrpcService.builder()
                                            .addService(new GithubApiService()).build())
                                            .requestTimeout(Duration.ZERO)
                                            .meterRegistry(NoopMeterRegistry.get())
                                            .build();
    server.start().join();
    final String url = "gproto+http://127.0.0.1:" + port() + '/';
    githubApiClient = Clients.newClient(url, GithubServiceBlockingStub.class);
    githubApiFutureClient = Clients.newClient(url, GithubServiceFutureStub.class);
}
 
Example #7
Source File: GrpcDocServiceTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Override
protected void configure(ServerBuilder sb) throws Exception {
    if (TestUtil.isDocServiceDemoMode()) {
        sb.http(8080);
    }
    sb.serviceUnder("/test",
                    GrpcService.builder()
                               .addService(new TestService())
                               .supportedSerializationFormats(GrpcSerializationFormats.values())
                               .enableUnframedRequests(true)
                               .build());
    sb.serviceUnder("/docs/",
                    DocService.builder()
                              .exampleRequestForMethod(
                                    TestServiceGrpc.SERVICE_NAME,
                                    "UnaryCall",
                                    SimpleRequest.newBuilder()
                                                 .setPayload(
                                                     Payload.newBuilder()
                                                            .setBody(ByteString.copyFromUtf8("world")))
                                                 .build())
                              .injectedScript(INJECTED_HEADER_PROVIDER1, INJECTED_HEADER_PROVIDER2)
                              .injectedScriptSupplier((ctx, req) -> INJECTED_HEADER_PROVIDER3)
                              .exclude(DocServiceFilter.ofMethodName(
                                                TestServiceGrpc.SERVICE_NAME,
                                                "EmptyCall"))
                              .build()
                              .decorate(LoggingService.newDecorator()));
    sb.serviceUnder("/excludeAll/",
                    DocService.builder()
                              .exclude(DocServiceFilter.ofGrpc())
                              .build());
    sb.serviceUnder("/",
                    GrpcService.builder()
                               .addService(mock(ReconnectServiceImplBase.class))
                               .build());
}
 
Example #8
Source File: GrpcDocServicePluginTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
private static Map<String, ServiceInfo> services(DocServiceFilter include, DocServiceFilter exclude) {
    final ServerBuilder serverBuilder = Server.builder();

    // The case where a GrpcService is added to ServerBuilder without a prefix.
    final HttpServiceWithRoutes prefixlessService =
            GrpcService.builder()
                       .addService(mock(TestServiceImplBase.class))
                       .build();
    serverBuilder.service(prefixlessService);

    // The case where a GrpcService is added to ServerBuilder with a prefix.
    serverBuilder.service(
            Route.builder().pathPrefix("/test").build(),
            GrpcService.builder().addService(mock(UnitTestServiceImplBase.class)).build());

    // Another GrpcService with a different prefix.
    serverBuilder.service(
            Route.builder().pathPrefix("/reconnect").build(),
            GrpcService.builder().addService(mock(ReconnectServiceImplBase.class)).build());

    // Make sure all services and their endpoints exist in the specification.
    final ServiceSpecification specification = generator.generateSpecification(
            ImmutableSet.copyOf(serverBuilder.build().serviceConfigs()),
            unifyFilter(include, exclude));
    return specification
            .services()
            .stream()
            .collect(toImmutableMap(ServiceInfo::name, Function.identity()));
}
 
Example #9
Source File: GrpcStatusCauseTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Override
protected void configure(ServerBuilder sb) throws Exception {
    sb.serviceUnder("/",
                    GrpcService.builder()
                               .addService(new TestServiceImpl())
                               .build());
}
 
Example #10
Source File: GrpcFlowControlTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Override
protected void configure(ServerBuilder sb) throws Exception {
    sb.maxRequestLength(0);
    sb.requestTimeoutMillis(0);

    sb.serviceUnder("/",
                    GrpcService.builder()
                               .addService(new FlowControlService())
                               .setMaxInboundMessageSizeBytes(Integer.MAX_VALUE)
                               .build());
}
 
Example #11
Source File: GrpcMetricsIntegrationTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Override
protected void configure(ServerBuilder sb) throws Exception {
    sb.meterRegistry(registry);
    sb.service(GrpcService.builder()
                          .addService(new TestServiceImpl())
                          .enableUnframedRequests(true)
                          .build(),
               MetricCollectingService.newDecorator(MeterIdPrefixFunction.ofDefault("server")),
               LoggingService.newDecorator());
}
 
Example #12
Source File: StackdriverSpanConsumerTest.java    From zipkin-gcp with Apache License 2.0 4 votes vote down vote up
@Override protected void configure(ServerBuilder sb) {
  sb.service(GrpcService.builder()
      .addService(traceService)
      .build());
}
 
Example #13
Source File: LargePayloadBenchmark.java    From armeria with Apache License 2.0 4 votes vote down vote up
@Setup
public void setUp() {

    bindableService = new BinaryProxyImplBase() {
        @Override
        public StreamObserver<BinaryPayload> echo(StreamObserver<BinaryPayload> responseObserver) {
            return new StreamObserver<BinaryPayload>() {
                @Override
                public void onNext(BinaryPayload value) {
                    try {
                        responseObserver.onNext(value);
                    } finally {
                        if (wrapBuffer) {
                            GrpcUnsafeBufferUtil.releaseBuffer(value,
                                                               ServiceRequestContext.current());
                        }
                    }
                }

                @Override
                public void onError(Throwable t) {
                    responseObserver.onError(t);
                }

                @Override
                public void onCompleted() {
                    responseObserver.onCompleted();
                }
            };
        }
    };

    server = Server.builder()
                   .serviceUnder("/",
                                 GrpcService.builder()
                                            .addService(bindableService)
                                            .unsafeWrapRequestBuffers(wrapBuffer)
                                            .build())
                   .build();
    server.start().join();

    final String url = "gproto+http://127.0.0.1:" + server.activeLocalPort(SessionProtocol.HTTP) + '/';
    binaryProxyClient = Clients.newClient(url, BinaryProxyStub.class);
}
 
Example #14
Source File: GrpcDocServicePlugin.java    From armeria with Apache License 2.0 4 votes vote down vote up
@Override
public Set<Class<? extends Service<?, ?>>> supportedServiceTypes() {
    return ImmutableSet.of(GrpcService.class);
}
 
Example #15
Source File: GrpcClientTest.java    From armeria with Apache License 2.0 4 votes vote down vote up
@Override
protected void configure(ServerBuilder sb) {
    sb.workerGroup(EventLoopGroups.newEventLoopGroup(1), true);
    sb.maxRequestLength(MAX_MESSAGE_SIZE);
    sb.idleTimeoutMillis(0);
    sb.http(0);
    sb.https(0);
    sb.tlsSelfSigned();

    final ServerServiceDefinition interceptService =
            ServerInterceptors.intercept(
                    new TestServiceImpl(Executors.newSingleThreadScheduledExecutor()),
                    new ServerInterceptor() {
                        @Override
                        public <REQ, RESP> Listener<REQ> interceptCall(
                                ServerCall<REQ, RESP> call,
                                Metadata requestHeaders,
                                ServerCallHandler<REQ, RESP> next) {
                            final HttpHeadersBuilder fromClient = HttpHeaders.builder();
                            MetadataUtil.fillHeaders(requestHeaders, fromClient);
                            CLIENT_HEADERS_CAPTURE.set(fromClient.build());
                            return next.startCall(
                                    new SimpleForwardingServerCall<REQ, RESP>(call) {
                                        @Override
                                        public void close(Status status, Metadata trailers) {
                                            trailers.merge(requestHeaders);
                                            super.close(status, trailers);
                                        }
                                    }, requestHeaders);
                        }
                    });

    sb.serviceUnder("/",
                    GrpcService.builder()
                               .addService(interceptService)
                               .setMaxInboundMessageSizeBytes(MAX_MESSAGE_SIZE)
                               .setMaxOutboundMessageSizeBytes(MAX_MESSAGE_SIZE)
                               .useClientTimeoutHeader(false)
                               .build()
                               .decorate((client, ctx, req) -> {
                                   final HttpResponse res = client.serve(ctx, req);
                                   return new FilteredHttpResponse(res) {
                                       private boolean headersReceived;

                                       @Override
                                       protected HttpObject filter(HttpObject obj) {
                                           if (obj instanceof HttpHeaders) {
                                               if (!headersReceived) {
                                                   headersReceived = true;
                                               } else {
                                                   SERVER_TRAILERS_CAPTURE.set((HttpHeaders) obj);
                                               }
                                           }
                                           return obj;
                                       }
                                   };
                               }));
}