Skip to content

Commit 2d0392c

Browse files
dadadomjvmlet
authored andcommitted
Expose gRPC port as local.grpc.port. (#59)
If a value of 0 is provided as the port to the ServerBuilder, the server picks a random port on startup. This change makes that port available under the property local.grpc.port so it can be used in unit tests.
1 parent 87bdc5b commit 2d0392c

File tree

4 files changed

+51
-11
lines changed

4 files changed

+51
-11
lines changed

README.adoc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,16 @@ dependencies {
2727
2828
* Start by https://github.com/google/protobuf-gradle-plugin[generating] stub and server interface(s) from your `.proto` file(s).
2929
* Annotate your server interface implementation(s) with `@org.lognet.springboot.grpc.GRpcService`
30-
* Optionally configure the server port in your `application.yml/properties`. Default port is `6565`
30+
* Optionally configure the server port in your `application.yml/properties`. Default port is `6565`.
31+
* 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`.
3132
3233
[source,yaml]
3334
----
3435
grpc:
3536
port: 6565
3637
----
3738
38-
The starter supports also the `in-process server`, which should be used for testing purposes :
39+
The starter supports also the `in-process server`, which should be used for testing purposes :
3940
4041
[source,yaml]
4142
----

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ public void run(String... args) throws Exception {
7979

8080
});
8181

82-
8382
configurer.configure(serverBuilder);
8483
server = serverBuilder.build().start();
8584

@@ -88,6 +87,18 @@ public void run(String... args) throws Exception {
8887

8988
}
9089

90+
/**
91+
* Return the port the server is actually running on.
92+
*
93+
* @return the port the server is actually running on or 0, if the server is not initialized yet
94+
*/
95+
public int getRunningPort() {
96+
if (server == null) {
97+
return 0;
98+
}
99+
return server.getPort();
100+
}
101+
91102
private ServerServiceDefinition bindInterceptors(ServerServiceDefinition serviceDefinition, GRpcService gRpcService, Collection<ServerInterceptor> globalInterceptors) {
92103

93104

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

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import io.grpc.ServerBuilder;
44
import io.grpc.inprocess.InProcessServerBuilder;
55
import io.grpc.services.HealthStatusManager;
6-
import lombok.Getter;
76
import org.lognet.springboot.grpc.GRpcServerBuilderConfigurer;
87
import org.lognet.springboot.grpc.GRpcServerRunner;
98
import org.lognet.springboot.grpc.GRpcService;
@@ -13,12 +12,17 @@
1312
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
1413
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
1514
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
16-
import org.springframework.boot.context.properties.ConfigurationProperties;
1715
import org.springframework.boot.context.properties.EnableConfigurationProperties;
1816
import org.springframework.context.ApplicationContext;
17+
import org.springframework.context.ConfigurableApplicationContext;
1918
import org.springframework.context.annotation.Bean;
20-
import org.springframework.context.annotation.Configuration;
21-
import org.springframework.stereotype.Component;
19+
import org.springframework.core.env.MapPropertySource;
20+
import org.springframework.core.env.MutablePropertySources;
21+
import org.springframework.core.env.PropertySource;
22+
import org.springframework.util.SocketUtils;
23+
24+
import java.util.HashMap;
25+
import java.util.Map;
2226

2327
/**
2428
* Created by alexf on 25-Jan-16.
@@ -29,17 +33,34 @@
2933
@EnableConfigurationProperties(GRpcServerProperties.class)
3034
public class GRpcAutoConfiguration {
3135

32-
36+
@Autowired
37+
private ApplicationContext applicationContext;
3338

3439
@Autowired
3540
private GRpcServerProperties grpcServerProperties;
3641

3742

3843

3944
@Bean
40-
@ConditionalOnProperty(value = "grpc.enabled",havingValue = "true",matchIfMissing = true)
41-
public GRpcServerRunner grpcServerRunner(GRpcServerBuilderConfigurer configurer){
42-
return new GRpcServerRunner(configurer, ServerBuilder.forPort(grpcServerProperties.getPort()));
45+
@ConditionalOnProperty(value = "grpc.enabled", havingValue = "true", matchIfMissing = true)
46+
public GRpcServerRunner grpcServerRunner(GRpcServerBuilderConfigurer configurer) {
47+
int port = grpcServerProperties.getPort();
48+
49+
GRpcServerRunner gRpcServerRunner = new GRpcServerRunner(configurer, ServerBuilder.forPort(port));
50+
51+
if (applicationContext instanceof ConfigurableApplicationContext) {
52+
int runningPort = gRpcServerRunner.getRunningPort();
53+
54+
MutablePropertySources sources = ((ConfigurableApplicationContext) applicationContext).getEnvironment().getPropertySources();
55+
PropertySource<?> source = sources.get("server.ports");
56+
if (source == null) {
57+
source = new MapPropertySource("server.ports", new HashMap<String, Object>());
58+
sources.addFirst(source);
59+
}
60+
((Map<String, Object>) source.getSource()).put("local.grpc.port", runningPort);
61+
}
62+
63+
return gRpcServerRunner;
4364
}
4465

4566
@Bean

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.springframework.boot.context.properties.ConfigurationProperties;
66
import org.springframework.context.annotation.Bean;
77
import org.springframework.stereotype.Component;
8+
import org.springframework.util.SocketUtils;
89

910
/**
1011
* Created by alexf on 26-Jan-16.
@@ -19,6 +20,12 @@ public class GRpcServerProperties {
1920
*/
2021
private int port = 6565;
2122

23+
public int getPort() {
24+
if (port == 0) {
25+
port = SocketUtils.findAvailableTcpPort(40000, 50000);
26+
}
27+
return port;
28+
}
2229

2330
/**
2431
* Enables the embedded grpc server.

0 commit comments

Comments
 (0)