diff --git a/samples/grpc-server/src/main/java/org/springframework/grpc/sample/GrpcServerApplication.java b/samples/grpc-server/src/main/java/org/springframework/grpc/sample/GrpcServerApplication.java
index d605209..91ed5dd 100644
--- a/samples/grpc-server/src/main/java/org/springframework/grpc/sample/GrpcServerApplication.java
+++ b/samples/grpc-server/src/main/java/org/springframework/grpc/sample/GrpcServerApplication.java
@@ -2,10 +2,6 @@ package org.springframework.grpc.sample;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
-import org.springframework.context.annotation.Bean;
-
-import io.grpc.BindableService;
-import io.grpc.protobuf.services.ProtoReflectionService;
@SpringBootApplication
public class GrpcServerApplication {
@@ -14,9 +10,4 @@ public class GrpcServerApplication {
SpringApplication.run(GrpcServerApplication.class, args);
}
- @Bean
- public BindableService serverReflection() {
- return ProtoReflectionService.newInstance();
- }
-
}
diff --git a/samples/grpc-server/src/test/java/org/springframework/grpc/sample/GrpcServerApplicationTests.java b/samples/grpc-server/src/test/java/org/springframework/grpc/sample/GrpcServerApplicationTests.java
index bfc73df..eae86b8 100644
--- a/samples/grpc-server/src/test/java/org/springframework/grpc/sample/GrpcServerApplicationTests.java
+++ b/samples/grpc-server/src/test/java/org/springframework/grpc/sample/GrpcServerApplicationTests.java
@@ -24,7 +24,7 @@ public class GrpcServerApplicationTests {
private static Log log = LogFactory.getLog(GrpcServerApplicationTests.class);
public static void main(String[] args) {
- new SpringApplicationBuilder(GrpcServerApplication.class, ExtraConfiguration.class).profiles("ssl").run(args);
+ new SpringApplicationBuilder(GrpcServerApplication.class, ExtraConfiguration.class).run(args);
}
@Autowired
diff --git a/samples/grpc-tomcat/src/main/java/org/springframework/grpc/sample/GrpcServerApplication.java b/samples/grpc-tomcat/src/main/java/org/springframework/grpc/sample/GrpcServerApplication.java
index d605209..91ed5dd 100644
--- a/samples/grpc-tomcat/src/main/java/org/springframework/grpc/sample/GrpcServerApplication.java
+++ b/samples/grpc-tomcat/src/main/java/org/springframework/grpc/sample/GrpcServerApplication.java
@@ -2,10 +2,6 @@ package org.springframework.grpc.sample;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
-import org.springframework.context.annotation.Bean;
-
-import io.grpc.BindableService;
-import io.grpc.protobuf.services.ProtoReflectionService;
@SpringBootApplication
public class GrpcServerApplication {
@@ -14,9 +10,4 @@ public class GrpcServerApplication {
SpringApplication.run(GrpcServerApplication.class, args);
}
- @Bean
- public BindableService serverReflection() {
- return ProtoReflectionService.newInstance();
- }
-
}
diff --git a/spring-grpc-docs/src/main/antora/modules/ROOT/partials/_configprops.adoc b/spring-grpc-docs/src/main/antora/modules/ROOT/partials/_configprops.adoc
index 3b5481c..1c7f5a4 100644
--- a/spring-grpc-docs/src/main/antora/modules/ROOT/partials/_configprops.adoc
+++ b/spring-grpc-docs/src/main/antora/modules/ROOT/partials/_configprops.adoc
@@ -28,6 +28,7 @@
|spring.grpc.server.max-inbound-message-size | `+++4194304B+++` | Maximum message size allowed to be received by the server (default 4MiB).
|spring.grpc.server.max-inbound-metadata-size | `+++8192B+++` | Maximum metadata size allowed to be received by the server (default 8KiB).
|spring.grpc.server.port | `+++9090+++` | Server port to listen on. When the value is 0, a random available port is selected. The default is 9090.
+|spring.grpc.server.reflection.enabled | `+++true+++` |
|spring.grpc.server.shutdown-grace-period | `+++30s+++` | Maximum time to wait for the server to gracefully shutdown. When the value is negative, the server waits forever. When the value is 0, the server will force shutdown immediately. The default is 30 seconds.
|spring.grpc.server.ssl.bundle | | SSL bundle name.
|spring.grpc.server.ssl.client-auth | | Client authentication mode.
diff --git a/spring-grpc-spring-boot-autoconfigure/pom.xml b/spring-grpc-spring-boot-autoconfigure/pom.xml
index eaa8fa0..44bd262 100644
--- a/spring-grpc-spring-boot-autoconfigure/pom.xml
+++ b/spring-grpc-spring-boot-autoconfigure/pom.xml
@@ -64,6 +64,11 @@
+ * This auto-configuration is enabled by default. To disable it, set the configuration + * flag {spring.grpc.server.reflection.enabled=false} in your application properties. + * + * @author Haris Zujo + */ +@AutoConfiguration(before = GrpcServerFactoryAutoConfiguration.class) +@ConditionalOnClass(ProtoReflectionService.class) +@ConditionalOnProperty(name = "spring.grpc.server.reflection.enabled", havingValue = "true", matchIfMissing = true) +public class GrpcServerReflectionAutoConfiguration { + + @Bean + public BindableService serverReflection() { + return ProtoReflectionService.newInstance(); + } + +} diff --git a/spring-grpc-spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-grpc-spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json index 7102b59..ceb06f0 100644 --- a/spring-grpc-spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-grpc-spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -4,6 +4,10 @@ { "name": "spring.grpc.server.port", "defaultValue": "9090" + }, + { + "name": "spring.grpc.server.reflection.enabled", + "defaultValue": "true" } ] } diff --git a/spring-grpc-spring-boot-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/spring-grpc-spring-boot-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports index 41b6b0d..172a191 100644 --- a/spring-grpc-spring-boot-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports +++ b/spring-grpc-spring-boot-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -2,3 +2,5 @@ org.springframework.grpc.autoconfigure.server.GrpcServerFactoryAutoConfiguration org.springframework.grpc.autoconfigure.server.GrpcServerAutoConfiguration org.springframework.grpc.autoconfigure.server.GrpcServerObservationAutoConfiguration org.springframework.grpc.autoconfigure.client.GrpcClientAutoConfiguration +org.springframework.grpc.autoconfigure.server.GrpcServerReflectionAutoConfiguration + diff --git a/spring-grpc-spring-boot-autoconfigure/src/test/java/org/springframework/grpc/autoconfigure/server/GrpcServerReflectionAutoConfigurationTests.java b/spring-grpc-spring-boot-autoconfigure/src/test/java/org/springframework/grpc/autoconfigure/server/GrpcServerReflectionAutoConfigurationTests.java new file mode 100644 index 0000000..67e3bba --- /dev/null +++ b/spring-grpc-spring-boot-autoconfigure/src/test/java/org/springframework/grpc/autoconfigure/server/GrpcServerReflectionAutoConfigurationTests.java @@ -0,0 +1,39 @@ +package org.springframework.grpc.autoconfigure.server; + +import io.grpc.BindableService; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +import org.springframework.boot.autoconfigure.AutoConfigurations; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; +import org.springframework.grpc.server.lifecycle.GrpcServerLifecycle; + +import static org.assertj.core.api.Assertions.assertThat; + +public class GrpcServerReflectionAutoConfigurationTests { + + private ApplicationContextRunner contextRunner() { + return new ApplicationContextRunner() + .withConfiguration(AutoConfigurations.of(GrpcServerReflectionAutoConfiguration.class)) + .withBean("noopServerLifecylcle", GrpcServerLifecycle.class, Mockito::mock); + } + + @Test + void whenReflectionEnabledFlagNotPresentThenCreateDefaultBean() { + this.contextRunner().run((context -> assertThat(context).hasSingleBean(BindableService.class))); + } + + @Test + void whenReflectionEnabledThenCreateBean() { + this.contextRunner() + .withPropertyValues("spring.grpc.server.reflection.enabled=true") + .run((context) -> assertThat(context).hasSingleBean(BindableService.class)); + } + + @Test + void whenReflectionDisabledThenSkipBeanCreation() { + this.contextRunner() + .withPropertyValues("spring.grpc.server.reflection.enabled=false") + .run((context) -> assertThat(context).doesNotHaveBean(BindableService.class)); + } + +}