Skip to content

Commit 0071d63

Browse files
jorgheymansjvmlet
authored andcommitted
optionally enable server reflection (#83)
* optionally enable server reflection * add test * log after registering, not before
1 parent 837a82b commit 0071d63

File tree

5 files changed

+58
-2
lines changed

5 files changed

+58
-2
lines changed

README.adoc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ dependencies {
4040
A random port can be defined by setting the port to `0`. The actual port being used can then be retrieved from the property `local.grpc.port`, or
4141
using `@LocalRunningGrpcPort` annotation which will inject the running port (explicitly configured or randomly selected)
4242
43+
* Optionally enable server reflection, see https://github.com/grpc/grpc-java/blob/master/documentation/server-reflection-tutorial.md)
44+
45+
[source,yaml]
46+
----
47+
grpc:
48+
enableReflection: true
49+
----
50+
4351
The starter supports also the `in-process server`, which should be used for testing purposes :
4452
4553
[source,yaml]
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
security.basic.enabled=false
2-
management.security.enabled=false
2+
management.security.enabled=false
3+
grpc.enableReflection=true

grpc-spring-boot-starter-demo/src/test/java/org/lognet/springboot/grpc/DemoAppTest.java

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
import io.grpc.health.v1.HealthCheckRequest;
99
import io.grpc.health.v1.HealthCheckResponse;
1010
import io.grpc.health.v1.HealthGrpc;
11+
import io.grpc.protobuf.services.ProtoReflectionService;
12+
import io.grpc.reflection.v1alpha.ServerReflectionGrpc;
13+
import io.grpc.reflection.v1alpha.ServerReflectionRequest;
14+
import io.grpc.reflection.v1alpha.ServerReflectionResponse;
15+
import io.grpc.reflection.v1alpha.ServiceResponse;
16+
import io.grpc.stub.StreamObserver;
1117
import org.junit.Assert;
1218
import org.junit.Rule;
1319
import org.junit.Test;
@@ -26,6 +32,8 @@
2632
import org.springframework.test.context.junit4.SpringRunner;
2733

2834
import java.net.URI;
35+
import java.util.ArrayList;
36+
import java.util.List;
2937
import java.util.concurrent.ExecutionException;
3038

3139
import static org.hamcrest.CoreMatchers.containsString;
@@ -37,7 +45,7 @@
3745
* Created by alexf on 28-Jan-16.
3846
*/
3947
@RunWith(SpringRunner.class)
40-
@SpringBootTest(classes = {DemoApp.class,TestConfig.class}, webEnvironment = RANDOM_PORT)
48+
@SpringBootTest(classes = {DemoApp.class,TestConfig.class}, webEnvironment = RANDOM_PORT, properties = "grpc.enableReflection=true")
4149
public class DemoAppTest extends GrpcServerTestBase{
4250

4351
@Autowired
@@ -94,6 +102,33 @@ public void testDefaultConfigurer(){
94102
GRpcServerBuilderConfigurer.class);
95103
}
96104

105+
@Test
106+
public void testReflection() throws InterruptedException {
107+
List<String> discoveredServiceNames = new ArrayList<>();
108+
ServerReflectionRequest request = ServerReflectionRequest.newBuilder().setListServices("services").setHost("localhost").build();
109+
ServerReflectionGrpc.newStub(channel).serverReflectionInfo(new StreamObserver<ServerReflectionResponse>() {
110+
@Override
111+
public void onNext(ServerReflectionResponse value) {
112+
List<ServiceResponse> serviceList = value.getListServicesResponse().getServiceList();
113+
for (ServiceResponse serviceResponse : serviceList) {
114+
discoveredServiceNames.add(serviceResponse.getName());
115+
}
116+
}
117+
118+
@Override
119+
public void onError(Throwable t) {
120+
121+
}
122+
123+
@Override
124+
public void onCompleted() {
125+
126+
}
127+
}).onNext(request);
128+
Thread.sleep(1000l);
129+
assertFalse(discoveredServiceNames.isEmpty());
130+
}
131+
97132

98133
@Test
99134
public void testHealthCheck() throws ExecutionException, InterruptedException {

grpc-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/GRpcServerRunner.java

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

33
import io.grpc.*;
44
import io.grpc.health.v1.HealthCheckResponse;
5+
import io.grpc.protobuf.services.ProtoReflectionService;
56
import io.grpc.services.HealthStatusManager;
67
import lombok.extern.slf4j.Slf4j;
78
import org.lognet.springboot.grpc.autoconfigure.GRpcServerProperties;
@@ -80,6 +81,11 @@ public void run(String... args) throws Exception {
8081

8182
});
8283

84+
if (gRpcServerProperties.isEnableReflection()) {
85+
serverBuilder.addService(ProtoReflectionService.newInstance());
86+
log.info("'{}' service has been registered.", ProtoReflectionService.class.getName());
87+
}
88+
8389
configurer.configure(serverBuilder);
8490
server = serverBuilder.build().start();
8591
applicationContext.publishEvent(new GRpcServerInitializedEvent(server));

grpc-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/autoconfigure/GRpcServerProperties.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,11 @@ public class GRpcServerProperties {
3333
*/
3434
private String inProcessServerName;
3535

36+
/**
37+
* Enables server reflection using <a href="https://github.com/grpc/grpc-java/blob/master/documentation/server-reflection-tutorial.md">ProtoReflectionService</a>.
38+
* Available only from gRPC 1.3 or higher.
39+
*/
40+
private boolean enableReflection = false;
41+
3642

3743
}

0 commit comments

Comments
 (0)