Skip to content

Commit 9b02a10

Browse files
author
Alexander Furer
committed
random grpc port support
1 parent 2d0392c commit 9b02a10

File tree

7 files changed

+80
-36
lines changed

7 files changed

+80
-36
lines changed

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

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import io.grpc.services.HealthStatusManager;
66
import lombok.extern.slf4j.Slf4j;
77
import org.lognet.springboot.grpc.autoconfigure.GRpcServerProperties;
8+
import org.lognet.springboot.grpc.context.GRpcServerInitializedEvent;
89
import org.springframework.beans.factory.BeanCreationException;
910
import org.springframework.beans.factory.DisposableBean;
1011
import org.springframework.beans.factory.annotation.Autowired;
@@ -81,23 +82,14 @@ public void run(String... args) throws Exception {
8182

8283
configurer.configure(serverBuilder);
8384
server = serverBuilder.build().start();
85+
applicationContext.publishEvent(new GRpcServerInitializedEvent(server));
8486

8587
log.info("gRPC Server started, listening on port {}.", server.getPort());
8688
startDaemonAwaitThread();
8789

8890
}
8991

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-
}
92+
10193

10294
private ServerServiceDefinition bindInterceptors(ServerServiceDefinition serviceDefinition, GRpcService gRpcService, Collection<ServerInterceptor> globalInterceptors) {
10395

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

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,7 @@ public class GRpcAutoConfiguration {
4444
@Bean
4545
@ConditionalOnProperty(value = "grpc.enabled", havingValue = "true", matchIfMissing = true)
4646
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;
47+
return new GRpcServerRunner(configurer, ServerBuilder.forPort(grpcServerProperties.getPort()));
6448
}
6549

6650
@Bean

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,6 @@ public class GRpcServerProperties {
2020
*/
2121
private int port = 6565;
2222

23-
public int getPort() {
24-
if (port == 0) {
25-
port = SocketUtils.findAvailableTcpPort(40000, 50000);
26-
}
27-
return port;
28-
}
29-
3023
/**
3124
* Enables the embedded grpc server.
3225
*/
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.lognet.springboot.grpc.context;
2+
3+
import io.grpc.Server;
4+
import org.springframework.context.ApplicationEvent;
5+
6+
public class GRpcServerInitializedEvent extends ApplicationEvent {
7+
/**
8+
* Create a new ApplicationEvent.
9+
*
10+
* @param source the object on which the event initially occurred (never {@code null})
11+
*/
12+
public GRpcServerInitializedEvent(Server source) {
13+
super(source);
14+
}
15+
public Server getServer(){
16+
return (Server) getSource();
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package org.lognet.springboot.grpc.context;
2+
3+
import io.grpc.ServerBuilder;
4+
import org.lognet.springboot.grpc.GRpcServerRunner;
5+
import org.springframework.boot.context.embedded.EmbeddedServletContainerInitializedEvent;
6+
import org.springframework.boot.context.embedded.ServerPortInfoApplicationContextInitializer;
7+
import org.springframework.context.ApplicationContextInitializer;
8+
import org.springframework.context.ApplicationEvent;
9+
import org.springframework.context.ApplicationListener;
10+
import org.springframework.context.ConfigurableApplicationContext;
11+
import org.springframework.core.env.MapPropertySource;
12+
import org.springframework.core.env.MutablePropertySources;
13+
import org.springframework.core.env.PropertySource;
14+
15+
import java.util.HashMap;
16+
import java.util.Map;
17+
18+
public class GRpcServerPortInfoApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
19+
@Override
20+
public void initialize(ConfigurableApplicationContext applicationContext) {
21+
applicationContext.addApplicationListener(new ApplicationListener<GRpcServerInitializedEvent>() {
22+
@Override
23+
public void onApplicationEvent(GRpcServerInitializedEvent event) {
24+
GRpcServerPortInfoApplicationContextInitializer.this.onApplicationEvent(applicationContext,event);
25+
}
26+
});
27+
}
28+
29+
private void onApplicationEvent(ConfigurableApplicationContext applicationContext,GRpcServerInitializedEvent event) {
30+
MutablePropertySources sources = applicationContext.getEnvironment().getPropertySources();
31+
PropertySource<?> source = sources.get("server.ports");
32+
if (source == null) {
33+
source = new MapPropertySource("server.ports", new HashMap<>());
34+
sources.addFirst(source);
35+
}
36+
((Map<String, Object>) source.getSource()).put("local.grpc.port", event.getServer().getPort());
37+
}
38+
39+
40+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.lognet.springboot.grpc.context;
2+
3+
import org.springframework.beans.factory.annotation.Value;
4+
5+
import java.lang.annotation.*;
6+
7+
@Target({ ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER,
8+
ElementType.ANNOTATION_TYPE })
9+
@Retention(RetentionPolicy.RUNTIME)
10+
@Documented
11+
@Value("${grpc.port !=0 ? ${grpc.port}:${local.grpc.port}}")
12+
public @interface LocalRunningGrpcPort {
13+
}
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
org.springframework.boot.autoconfigure.EnableAutoConfiguration=org.lognet.springboot.grpc.autoconfigure.GRpcAutoConfiguration
1+
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
2+
org.lognet.springboot.grpc.autoconfigure.GRpcAutoConfiguration
3+
4+
org.springframework.context.ApplicationContextInitializer=\
5+
org.lognet.springboot.grpc.context.GRpcServerPortInfoApplicationContextInitializer

0 commit comments

Comments
 (0)