Skip to content

Commit 907d66c

Browse files
author
Alexander Furer
committed
grpc.port property is properly populated by config server client, should fix 99 and 103
1 parent 1356fbf commit 907d66c

File tree

9 files changed

+77
-7
lines changed

9 files changed

+77
-7
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
buildscript {
22
ext {
33
springBoot_1_X_Version = '1.5.13.RELEASE'
4-
springBoot_2_X_Version = '2.0.3.RELEASE'
4+
springBoot_2_X_Version = '2.1.3.RELEASE'
55
grpcVersion = '1.19.0'
66
}
77
repositories {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class GRpcServerProperties {
1616
* gRPC server port
1717
*
1818
*/
19-
private int port = DEFAULT_GRPC_PORT;
19+
private int port = 0;
2020

2121
/**
2222
* Enables the embedded grpc server.

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,11 @@ public void postProcessEnvironment(ConfigurableEnvironment environment, SpringAp
2222
MutablePropertySources sources = environment.getPropertySources();
2323
Properties properties = new Properties();
2424
Integer configuredPort = environment.getProperty("grpc.port", Integer.class);
25-
properties.put("grpc.port", 0);
26-
if (null == configuredPort) {
25+
if (null == configuredPort) {
2726
properties.put(LocalRunningGrpcPort.propertyName, DEFAULT_GRPC_PORT);
2827
} else if (0 == configuredPort) {
2928
properties.put(LocalRunningGrpcPort.propertyName, SocketUtils.findAvailableTcpPort());
3029
} else {
31-
properties.put("grpc.port", configuredPort);
3230
properties.put(LocalRunningGrpcPort.propertyName, configuredPort);
3331
}
3432

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
ElementType.ANNOTATION_TYPE })
99
@Retention(RetentionPolicy.RUNTIME)
1010
@Documented
11-
@Value("${" + LocalRunningGrpcPort.propertyName + "}")
11+
@Value("#{environment.containsProperty('grpc.port') && '0' != environment.getProperty('grpc.port') ? environment.getProperty('grpc.port'): ${" + LocalRunningGrpcPort.propertyName + "}}")
1212
public @interface LocalRunningGrpcPort {
1313
String propertyName = "local.grpc.port";
1414
}

grpc-spring-boot2-starter-demo/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ dependencies {
3131
testRuntime('org.springframework.boot:spring-boot-starter-actuator')
3232
testRuntime('org.springframework.boot:spring-boot-starter-web')
3333

34+
testCompile "org.springframework.cloud:spring-cloud-config-server:2.1.1.RELEASE"
35+
testCompile "org.springframework.cloud:spring-cloud-config-client:2.1.1.RELEASE"
3436

3537
}
3638
bootJar.enabled =false
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
spring:
2+
application:
3+
name: grpc-demo
4+
cloud:
5+
config:
6+
uri: http://localhost:${config.port:8888}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package org.lognet.springboot.grpc;
2+
3+
import org.junit.AfterClass;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.lognet.springboot.grpc.demo.DemoApp;
8+
import org.springframework.boot.SpringApplication;
9+
import org.springframework.boot.test.context.SpringBootTest;
10+
import org.springframework.context.ConfigurableApplicationContext;
11+
import org.springframework.test.context.junit4.SpringRunner;
12+
import org.springframework.util.SocketUtils;
13+
14+
import java.io.File;
15+
import java.io.IOException;
16+
import java.net.URISyntaxException;
17+
import java.nio.file.Paths;
18+
import java.util.Optional;
19+
20+
import static org.junit.Assert.assertEquals;
21+
22+
@RunWith(SpringRunner.class)
23+
@SpringBootTest(classes = DemoApp.class,
24+
// Normally spring.cloud.config.enabled:true is the default but since we have the
25+
// config server on the classpath we need to set it explicitly
26+
properties = { "spring.cloud.config.enabled:true"})
27+
public class ConfigServerEnvironmentTest extends GrpcServerTestBase{
28+
29+
private static int configPort = SocketUtils.findAvailableTcpPort();
30+
private static ConfigurableApplicationContext server;
31+
32+
@BeforeClass
33+
public static void startConfigServer() throws IOException, URISyntaxException {
34+
File configDir = Paths.get(ConfigServerEnvironmentTest.class.getResource("/config-repo/grpc-demo.properties").toURI())
35+
.toFile()
36+
.getParentFile();
37+
38+
39+
40+
41+
server = SpringApplication.run(org.springframework.cloud.config.server.ConfigServerApplication.class,
42+
"--server.port=" + configPort,
43+
"--spring.autoconfigure.exclude=org.lognet.springboot.grpc.autoconfigure.GRpcAutoConfiguration",
44+
"--spring.cloud.config.server.health.enabled=false",
45+
"--spring.cloud.config.server.bootstrap=false",
46+
"--spring.profiles.active=native",
47+
"--spring.cloud.config.server.native.search-locations[0]=file:"+configDir.getAbsolutePath()
48+
);
49+
System.setProperty("config.port", "" + configPort);
50+
51+
}
52+
53+
@Test
54+
public void assertConfigServerConfiguredPort(){
55+
assertEquals(6666,getPort());
56+
}
57+
@AfterClass
58+
public static void close() {
59+
System.clearProperty("config.port");
60+
Optional.ofNullable(server).ifPresent(ConfigurableApplicationContext::close);
61+
}
62+
63+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
management.endpoints.web.base-path=/
22
management.endpoints.web.exposure.include=*
3-
3+
spring.main.allow-bean-definition-overriding=true
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
grpc.port = 6666

0 commit comments

Comments
 (0)