Java源码示例:com.linecorp.armeria.server.HttpServiceWithRoutes

示例1
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();
}
 
示例2
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();
}
 
示例3
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();
}
 
示例4
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()));
}
 
示例5
@Override
public Iterable<HttpServiceWithRoutes> moreServices() {
    return ImmutableList.of(sp.newSamlService());
}
 
示例6
/**
 * Adds gRPC services to the specified {@link ServerBuilder}.
 */
public static void configureGrpcServices(
        ServerBuilder server, DocServiceBuilder docServiceBuilder,
        List<GrpcServiceRegistrationBean> beans,
        @Nullable MeterIdPrefixFunctionFactory meterIdPrefixFunctionFactory,
        @Nullable String docsPath) {
    requireNonNull(server, "server");
    requireNonNull(docServiceBuilder, "docServiceBuilder");
    requireNonNull(beans, "beans");

    final List<GrpcExampleRequest> docServiceRequests = new ArrayList<>();
    final List<GrpcExampleHeaders> docServiceHeaders = new ArrayList<>();
    beans.forEach(bean -> {
        final HttpServiceWithRoutes serviceWithRoutes = bean.getService();
        docServiceRequests.addAll(bean.getExampleRequests());
        docServiceHeaders.addAll(bean.getExampleHeaders());
        serviceWithRoutes.routes().forEach(
                route -> {
                    HttpService service = bean.getService();
                    for (Function<? super HttpService, ? extends HttpService> decorator
                            : bean.getDecorators()) {
                        service = service.decorate(decorator);
                    }
                    server.service(route,
                                   setupMetricCollectingService(service, bean,
                                                                meterIdPrefixFunctionFactory));
                }
        );
    });

    if (Strings.isNullOrEmpty(docsPath)) {
        return;
    }

    docServiceRequests.forEach(
            exampleReq -> docServiceBuilder.exampleRequestForMethod(exampleReq.getServiceType(),
                                                                    exampleReq.getMethodName(),
                                                                    exampleReq.getExampleRequest()));
    docServiceHeaders.forEach(exampleHeader -> configureExampleHeaders(docServiceBuilder,
                                                                       exampleHeader.getServiceType(),
                                                                       exampleHeader.getMethodName(),
                                                                       exampleHeader.getHeaders()));
}
 
示例7
/**
 * Creates an {@link HttpService} which handles SAML messages.
 */
public HttpServiceWithRoutes newSamlService() {
    return new SamlService(this);
}
 
示例8
/**
 * Returns additional {@link Service}s which are required for working this {@link AuthProvider}
 * well.
 */
default Iterable<HttpServiceWithRoutes> moreServices() {
    return ImmutableList.of();
}