Skip to content

Commit d61af1a

Browse files
CyberZujoDave Syer
authored andcommitted
Add autoconfig for reflection service and friends #26
Signed-off-by: CyberZujo <[email protected]>
1 parent ee994e0 commit d61af1a

File tree

9 files changed

+83
-20
lines changed

9 files changed

+83
-20
lines changed

samples/grpc-server/src/main/java/org/springframework/grpc/sample/GrpcServerApplication.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22

33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
5-
import org.springframework.context.annotation.Bean;
6-
7-
import io.grpc.BindableService;
8-
import io.grpc.protobuf.services.ProtoReflectionService;
95

106
@SpringBootApplication
117
public class GrpcServerApplication {
@@ -14,9 +10,4 @@ public static void main(String[] args) {
1410
SpringApplication.run(GrpcServerApplication.class, args);
1511
}
1612

17-
@Bean
18-
public BindableService serverReflection() {
19-
return ProtoReflectionService.newInstance();
20-
}
21-
2213
}

samples/grpc-server/src/test/java/org/springframework/grpc/sample/GrpcServerApplicationTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class GrpcServerApplicationTests {
2424
private static Log log = LogFactory.getLog(GrpcServerApplicationTests.class);
2525

2626
public static void main(String[] args) {
27-
new SpringApplicationBuilder(GrpcServerApplication.class, ExtraConfiguration.class).profiles("ssl").run(args);
27+
new SpringApplicationBuilder(GrpcServerApplication.class, ExtraConfiguration.class).run(args);
2828
}
2929

3030
@Autowired

samples/grpc-tomcat/src/main/java/org/springframework/grpc/sample/GrpcServerApplication.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22

33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
5-
import org.springframework.context.annotation.Bean;
6-
7-
import io.grpc.BindableService;
8-
import io.grpc.protobuf.services.ProtoReflectionService;
95

106
@SpringBootApplication
117
public class GrpcServerApplication {
@@ -14,9 +10,4 @@ public static void main(String[] args) {
1410
SpringApplication.run(GrpcServerApplication.class, args);
1511
}
1612

17-
@Bean
18-
public BindableService serverReflection() {
19-
return ProtoReflectionService.newInstance();
20-
}
21-
2213
}

spring-grpc-docs/src/main/antora/modules/ROOT/partials/_configprops.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
|spring.grpc.server.max-inbound-message-size | `+++4194304B+++` | Maximum message size allowed to be received by the server (default 4MiB).
2929
|spring.grpc.server.max-inbound-metadata-size | `+++8192B+++` | Maximum metadata size allowed to be received by the server (default 8KiB).
3030
|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.
31+
|spring.grpc.server.reflection.enabled | `+++true+++` |
3132
|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.
3233
|spring.grpc.server.ssl.bundle | | SSL bundle name.
3334
|spring.grpc.server.ssl.client-auth | | Client authentication mode.

spring-grpc-spring-boot-autoconfigure/pom.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@
6464
<artifactId>grpc-servlet-jakarta</artifactId>
6565
<optional>true</optional>
6666
</dependency>
67+
<dependency>
68+
<groupId>io.grpc</groupId>
69+
<artifactId>grpc-services</artifactId>
70+
<optional>true</optional>
71+
</dependency>
6772
<dependency>
6873
<groupId>io.grpc</groupId>
6974
<artifactId>grpc-netty-shaded</artifactId>
@@ -103,6 +108,6 @@
103108
<scope>test</scope>
104109
</dependency>
105110

106-
</dependencies>
111+
</dependencies>
107112

108113
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.springframework.grpc.autoconfigure.server;
2+
3+
import org.springframework.boot.autoconfigure.AutoConfiguration;
4+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
5+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
6+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
7+
import org.springframework.context.annotation.Bean;
8+
9+
import io.grpc.BindableService;
10+
import io.grpc.protobuf.services.ProtoReflectionService;
11+
12+
/**
13+
* {@link EnableAutoConfiguration Auto-configuration} for gRPC Reflection service
14+
* <p>
15+
* This auto-configuration is enabled by default. To disable it, set the configuration
16+
* flag {spring.grpc.server.reflection.enabled=false} in your application properties.
17+
*
18+
* @author Haris Zujo
19+
*/
20+
@AutoConfiguration(before = GrpcServerFactoryAutoConfiguration.class)
21+
@ConditionalOnClass(ProtoReflectionService.class)
22+
@ConditionalOnProperty(name = "spring.grpc.server.reflection.enabled", havingValue = "true", matchIfMissing = true)
23+
public class GrpcServerReflectionAutoConfiguration {
24+
25+
@Bean
26+
public BindableService serverReflection() {
27+
return ProtoReflectionService.newInstance();
28+
}
29+
30+
}

spring-grpc-spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
{
55
"name": "spring.grpc.server.port",
66
"defaultValue": "9090"
7+
},
8+
{
9+
"name": "spring.grpc.server.reflection.enabled",
10+
"defaultValue": "true"
711
}
812
]
913
}

spring-grpc-spring-boot-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ org.springframework.grpc.autoconfigure.server.GrpcServerFactoryAutoConfiguration
22
org.springframework.grpc.autoconfigure.server.GrpcServerAutoConfiguration
33
org.springframework.grpc.autoconfigure.server.GrpcServerObservationAutoConfiguration
44
org.springframework.grpc.autoconfigure.client.GrpcClientAutoConfiguration
5+
org.springframework.grpc.autoconfigure.server.GrpcServerReflectionAutoConfiguration
6+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.springframework.grpc.autoconfigure.server;
2+
3+
import io.grpc.BindableService;
4+
import org.junit.jupiter.api.Test;
5+
import org.mockito.Mockito;
6+
import org.springframework.boot.autoconfigure.AutoConfigurations;
7+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
8+
import org.springframework.grpc.server.lifecycle.GrpcServerLifecycle;
9+
10+
import static org.assertj.core.api.Assertions.assertThat;
11+
12+
public class GrpcServerReflectionAutoConfigurationTests {
13+
14+
private ApplicationContextRunner contextRunner() {
15+
return new ApplicationContextRunner()
16+
.withConfiguration(AutoConfigurations.of(GrpcServerReflectionAutoConfiguration.class))
17+
.withBean("noopServerLifecylcle", GrpcServerLifecycle.class, Mockito::mock);
18+
}
19+
20+
@Test
21+
void whenReflectionEnabledFlagNotPresentThenCreateDefaultBean() {
22+
this.contextRunner().run((context -> assertThat(context).hasSingleBean(BindableService.class)));
23+
}
24+
25+
@Test
26+
void whenReflectionEnabledThenCreateBean() {
27+
this.contextRunner()
28+
.withPropertyValues("spring.grpc.server.reflection.enabled=true")
29+
.run((context) -> assertThat(context).hasSingleBean(BindableService.class));
30+
}
31+
32+
@Test
33+
void whenReflectionDisabledThenSkipBeanCreation() {
34+
this.contextRunner()
35+
.withPropertyValues("spring.grpc.server.reflection.enabled=false")
36+
.run((context) -> assertThat(context).doesNotHaveBean(BindableService.class));
37+
}
38+
39+
}

0 commit comments

Comments
 (0)