Add autoconfig for reflection service and friends #26
Signed-off-by: CyberZujo <hara_zujo@hotmail.com>
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -64,6 +64,11 @@
|
||||
<artifactId>grpc-servlet-jakarta</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.grpc</groupId>
|
||||
<artifactId>grpc-services</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.grpc</groupId>
|
||||
<artifactId>grpc-netty-shaded</artifactId>
|
||||
@@ -103,6 +108,6 @@
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
package org.springframework.grpc.autoconfigure.server;
|
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
import io.grpc.BindableService;
|
||||
import io.grpc.protobuf.services.ProtoReflectionService;
|
||||
|
||||
/**
|
||||
* {@link EnableAutoConfiguration Auto-configuration} for gRPC Reflection service
|
||||
* <p>
|
||||
* 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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -4,6 +4,10 @@
|
||||
{
|
||||
"name": "spring.grpc.server.port",
|
||||
"defaultValue": "9090"
|
||||
},
|
||||
{
|
||||
"name": "spring.grpc.server.reflection.enabled",
|
||||
"defaultValue": "true"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user