GH-754 Add initial support for specifying grpc service

The newly added support allows one to actually specify which groc service to bootstrap in the event multiple services are available in the classpath

Resolves #754
This commit is contained in:
Oleg Zhurakousky
2021-10-11 20:46:43 +02:00
parent 778bf3b024
commit caf3cd79fb
6 changed files with 80 additions and 18 deletions

View File

@@ -28,13 +28,29 @@ import org.springframework.cloud.function.context.FunctionProperties;
@ConfigurationProperties(prefix = FunctionProperties.PREFIX + ".grpc")
public class FunctionGrpcProperties {
private final static String GRPC_PREFIX = FunctionProperties.PREFIX + ".grpc";
/**
* The name of function definition property.
*/
public final static String SERVICE_CLASS_NAME = GRPC_PREFIX + ".service-class-name";
/**
* Default gRPC port.
*/
public final static int GRPC_PORT = 6048;
/**
* gRPC port server will bind to. Default 6048;
*/
private int port = GRPC_PORT;
/**
* The fully qualified name of the service you wish to enable/expose.
* Setting this property ensures that only a single service is enabled/exposed,
* regardless of how many services are available on the classpath.
*/
private String serviceClassName;
/**
* Grpc Server port.
*/
@@ -45,4 +61,14 @@ public class FunctionGrpcProperties {
public void setPort(int port) {
this.port = port;
}
public String getServiceClassName() {
return serviceClassName;
}
public void setServiceClassName(String serviceClassName) {
this.serviceClassName = serviceClassName;
}
}

View File

@@ -25,6 +25,7 @@ import org.springframework.cloud.function.context.FunctionProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import com.google.protobuf.GeneratedMessageV3;
@@ -43,15 +44,24 @@ class GrpcAutoConfiguration {
@Bean
public GrpcServer grpcServer(FunctionGrpcProperties grpcProperties, BindableService[] grpcMessagingServices) {
Assert.notEmpty(grpcMessagingServices, "'grpcMessagingServices' must not be null or empty");
if (StringUtils.hasText(grpcProperties.getServiceClassName())) {
for (BindableService bindableService : grpcMessagingServices) {
if (bindableService.getClass().getName().equals(grpcProperties.getServiceClassName())) {
return new GrpcServer(grpcProperties, new BindableService[] {bindableService});
}
}
}
return new GrpcServer(grpcProperties, grpcMessagingServices);
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Bean
public BindableService grpcSpringMessageHandler(MessageHandlingHelper helper) {
return new GrpcServerMessageHandler(helper);
}
@SuppressWarnings({ "rawtypes", "unchecked" })
@Bean
public MessageHandlingHelper grpcMessageHandlingHelper(List<GrpcMessageConverter<?>> grpcConverters,
FunctionProperties funcProperties, FunctionCatalog functionCatalog) {

View File

@@ -32,6 +32,9 @@ import io.grpc.protobuf.services.ProtoReflectionService;
/**
*
* @author Oleg Zhurakousky
* @author Dave Syer
*
* @since 3.2
*
*/
class GrpcServer implements SmartLifecycle {
@@ -70,6 +73,7 @@ class GrpcServer implements SmartLifecycle {
logger.info("gRPC server is listening on port " + this.grpcProperties.getPort());
}
catch (Exception e) {
stop();
throw new IllegalStateException(e);
}
});