Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

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 {
Expand All @@ -14,9 +10,4 @@ public static void main(String[] args) {
SpringApplication.run(GrpcServerApplication.class, args);
}

@Bean
public BindableService serverReflection() {
return ProtoReflectionService.newInstance();
}

}
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
spring.application.name=grpc-server
grpc.server.reflection.enabled=false
spring.grpc.server.reflection.enabled=true
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,11 @@

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 {

public static void main(String[] args) {
SpringApplication.run(GrpcServerApplication.class, args);
}

@Bean
public BindableService serverReflection() {
return ProtoReflectionService.newInstance();
}

}
9 changes: 5 additions & 4 deletions spring-grpc-spring-boot-autoconfigure/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,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>
Expand Down Expand Up @@ -88,10 +93,6 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-services</artifactId>
</dependency>

</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.grpc.servlet.jakarta.ServletServerBuilder;

import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
Expand All @@ -48,6 +49,7 @@
* @author Toshiaki Maki
*/
@AutoConfiguration
@AutoConfigureAfter(GrpcServerReflectionAutoConfiguration.class)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
@ConditionalOnClass(BindableService.class)
@ConditionalOnBean(BindableService.class)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
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 org.springframework.context.annotation.Configuration;

import io.grpc.BindableService;
import io.grpc.protobuf.services.ProtoReflectionService;

/**
* {@link EnableAutoConfiguration Auto-configuration} for gRPC Reflection service
* <p>
* grpc.reflection.enabled=true must be present in configuration in order for the
* spring.grpc.reflection.enabled=true must be present in configuration in order for the
* auto-configuration to execute
*
* @author Haris Zujo
*/
@Configuration
@AutoConfiguration
@ConditionalOnClass(ProtoReflectionService.class)
@ConditionalOnProperty(name = "spring.grpc.server.reflection.enabled", havingValue = "true", matchIfMissing = true)
public class GrpcServerReflectionAutoConfiguration {

@Bean
@ConditionalOnProperty(name = "grpc.server.reflection.enabled", havingValue = "true")
public BindableService serverReflection() {
return ProtoReflectionService.newInstance();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
{
"name": "spring.grpc.server.port",
"defaultValue": "9090"
},
{
"name": "spring.grpc.server.reflection.enabled",
"defaultValue": "true"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,23 @@ private ApplicationContextRunner contextRunner() {
.withBean("noopServerLifecylcle", GrpcServerLifecycle.class, Mockito::mock);
}

@Test
void whenReflectionEnabledFlagNotPresentThenCreateDefaultBean() {
this.contextRunner()
.run((context -> assertThat(context).hasSingleBean(BindableService.class)));
}

@Test
void whenReflectionEnabledThenCreateBean() {
this.contextRunner()
.withPropertyValues("grpc.server.reflection.enabled=true")
.withPropertyValues("spring.grpc.server.reflection.enabled=true")
.run((context) -> assertThat(context).hasSingleBean(BindableService.class));
}

@Test
void whenReflectionDisabledThenSkipBeanCreation() {
this.contextRunner()
.withPropertyValues("grpc.server.reflection.enabled=false")
.withPropertyValues("spring.grpc.server.reflection.enabled=false")
.run((context) -> assertThat(context).doesNotHaveBean(BindableService.class));
}

Expand Down