Builder addService(ServerServiceDefinition service) { services.put(service.getServiceDescriptor().getName(), service); return this; }
@Override public void destroy() throws Exception { log.info("Shutting down gRPC server ..."); server.getServices().forEach(def->healthStatusManager.clearStatus(def.getServiceDescriptor().getName())); Optional.ofNullable(server).ifPresent(Server::shutdown); log.info("gRPC server stopped."); }
for (ServerServiceDefinition service : grpcService.services()) { map.computeIfAbsent( service.getServiceDescriptor().getName(), s -> { final FileDescriptor fileDescriptor = ((ProtoFileDescriptorSupplier) fileDescriptor.getServices().stream() .filter(sd -> sd.getFullName().equals( service.getServiceDescriptor().getName())) .findFirst() .orElseThrow(IllegalStateException::new); final String serviceName = service.getServiceDescriptor().getName(); map.get(serviceName).endpoint( new EndpointInfoBuilder(serviceConfig.virtualHost().hostnamePattern(),
@Override public void run(String... args) throws Exception { log.info("Starting gRPC Server ..."); Collection<ServerInterceptor> globalInterceptors = getBeanNamesByTypeWithAnnotation(GRpcGlobalInterceptor.class, ServerInterceptor.class) .map(name -> applicationContext.getBeanFactory().getBean(name, ServerInterceptor.class)) .collect(Collectors.toList()); // Adding health service serverBuilder.addService(healthStatusManager.getHealthService()); // find and register all GRpcService-enabled beans getBeanNamesByTypeWithAnnotation(GRpcService.class, BindableService.class) .forEach(name -> { BindableService srv = applicationContext.getBeanFactory().getBean(name, BindableService.class); ServerServiceDefinition serviceDefinition = srv.bindService(); GRpcService gRpcServiceAnn = applicationContext.findAnnotationOnBean(name, GRpcService.class); serviceDefinition = bindInterceptors(serviceDefinition, gRpcServiceAnn, globalInterceptors); serverBuilder.addService(serviceDefinition); String serviceName = serviceDefinition.getServiceDescriptor().getName(); healthStatusManager.setStatus(serviceName, HealthCheckResponse.ServingStatus.SERVING); log.info("'{}' service has been registered.", srv.getClass().getName()); }); if (gRpcServerProperties.isEnableReflection()) { serverBuilder.addService(ProtoReflectionService.newInstance()); log.info("'{}' service has been registered.", ProtoReflectionService.class.getName()); } configurer.configure(serverBuilder); server = serverBuilder.build().start(); applicationContext.publishEvent(new GRpcServerInitializedEvent(server)); log.info("gRPC Server started, listening on port {}.", server.getPort()); startDaemonAwaitThread(); }
@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); } }
@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); } }
@Override public Collection<GrpcServiceDefinition> findGrpcServices() { Collection<String> beanNames = Arrays.asList(this.applicationContext.getBeanNamesForAnnotation(GrpcService.class)); List<GrpcServiceDefinition> definitions = Lists.newArrayListWithCapacity(beanNames.size()); GlobalServerInterceptorRegistry globalServerInterceptorRegistry = applicationContext.getBean(GlobalServerInterceptorRegistry.class); List<ServerInterceptor> globalInterceptorList = globalServerInterceptorRegistry.getServerInterceptors(); for (String beanName : beanNames) { BindableService bindableService = this.applicationContext.getBean(beanName, BindableService.class); ServerServiceDefinition serviceDefinition = bindableService.bindService(); GrpcService grpcServiceAnnotation = applicationContext.findAnnotationOnBean(beanName, GrpcService.class); serviceDefinition = bindInterceptors(serviceDefinition, grpcServiceAnnotation, globalInterceptorList); definitions.add(new GrpcServiceDefinition(beanName, bindableService.getClass(), serviceDefinition)); log.debug("Found gRPC service: " + serviceDefinition.getServiceDescriptor().getName() + ", bean: " + beanName + ", class: " + bindableService.getClass().getName()); } return definitions; }
@Override public void destroy() { for (final GrpcServiceDefinition grpcServiceDefinition : this.serviceList) { final String serviceName = grpcServiceDefinition.getDefinition().getServiceDescriptor().getName(); this.healthStatusManager.clearStatus(serviceName); } }
@Bean @Lazy InfoContributor grpcInfoContributor(final GrpcServerProperties properties, final Collection<BindableService> grpcServices, final HealthStatusManager healthStatusManager) { final Map<String, Object> details = new LinkedHashMap<>(); details.put("port", properties.getPort()); if (properties.isReflectionServiceEnabled()) { // Only expose services via web-info if we do the same via grpc. final Map<String, List<String>> services = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); details.put("services", services); final List<BindableService> mutableGrpcServiceList = new ArrayList<>(grpcServices); mutableGrpcServiceList.add(ProtoReflectionService.newInstance()); if (properties.isHealthServiceEnabled()) { mutableGrpcServiceList.add(healthStatusManager.getHealthService()); } for (final BindableService grpcService : mutableGrpcServiceList) { final ServiceDescriptor serviceDescriptor = grpcService.bindService().getServiceDescriptor(); final List<String> methods = collectMethodNamesForService(serviceDescriptor); services.put(serviceDescriptor.getName(), methods); } } return new SimpleInfoContributor("grpc.server", details); }
/** * Configures the services that should be served by the server. * * @param builder The server builder to configure. */ protected void configureServices(final T builder) { // support health check if (this.properties.isHealthServiceEnabled()) { builder.addService(this.healthStatusManager.getHealthService()); } if (this.properties.isReflectionServiceEnabled()) { builder.addService(ProtoReflectionService.newInstance()); } for (final GrpcServiceDefinition service : this.serviceList) { final String serviceName = service.getDefinition().getServiceDescriptor().getName(); log.info("Registered gRPC service: " + serviceName + ", bean: " + service.getBeanName() + ", class: " + service.getBeanClazz().getName()); builder.addService(service.getDefinition()); this.healthStatusManager.setStatus(serviceName, HealthCheckResponse.ServingStatus.SERVING); } }
/** * Registers a service. * * @return the previously registered service with the same service descriptor name if exists, * otherwise {@code null}. */ @Nullable public ServerServiceDefinition addService(ServerServiceDefinition service) { return services.put(service.getServiceDescriptor().getName(), service); }
/** * Removes a registered service * * @return true if the service was found to be removed. */ public boolean removeService(ServerServiceDefinition service) { return services.remove(service.getServiceDescriptor().getName(), service); }
Builder addService(ServerServiceDefinition service) { services.put(service.getServiceDescriptor().getName(), service); return this; }
/** * Add the service to this grpc handler registry. * * @param service grpc service definition * @return registry builder */ public Builder addService(ServerServiceDefinition service) { services.put( service.getServiceDescriptor().getName(), service); return this; }
private Builder(ServiceDescriptor serviceDescriptor) { this.serviceDescriptor = checkNotNull(serviceDescriptor, "serviceDescriptor"); this.serviceName = serviceDescriptor.getName(); }
Builder addService(ServerServiceDefinition service) { services.put(service.getServiceDescriptor().getName(), service); return this; }
/** * Registers a service. * * @return the previously registered service with the same service descriptor name if exists, * otherwise {@code null}. */ @Nullable public ServerServiceDefinition addService(ServerServiceDefinition service) { return services.put(service.getServiceDescriptor().getName(), service); }
/** * Removes a registered service * * @return true if the service was found to be removed. */ public boolean removeService(ServerServiceDefinition service) { return services.remove(service.getServiceDescriptor().getName(), service); }
@Override public void destroy() throws Exception { log.info("Shutting down gRPC server ..."); server.getServices().forEach(def->healthStatusManager.clearStatus(def.getServiceDescriptor().getName())); Optional.ofNullable(server).ifPresent(Server::shutdown); log.info("gRPC server stopped."); }
@Override public void destroy() { for (final GrpcServiceDefinition grpcServiceDefinition : this.serviceList) { final String serviceName = grpcServiceDefinition.getDefinition().getServiceDescriptor().getName(); this.healthStatusManager.clearStatus(serviceName); } }