Skip to content

Commit 2519474

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

File tree

13 files changed

+184
-83
lines changed

13 files changed

+184
-83
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.lognet.springboot.grpc;
22

3+
import org.hamcrest.CoreMatchers;
34
import org.junit.Assert;
45
import org.junit.runner.RunWith;
56
import org.lognet.springboot.grpc.autoconfigure.GRpcServerProperties;
@@ -8,6 +9,7 @@
89
import org.springframework.boot.test.context.SpringBootTest;
910
import org.springframework.test.context.junit4.SpringRunner;
1011

12+
import static org.junit.Assert.assertThat;
1113
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE;
1214

1315
@RunWith(SpringRunner.class)
@@ -23,7 +25,7 @@ protected int getPort() {
2325

2426
@Override
2527
protected void beforeGreeting() {
26-
Assert.assertEquals(0, gRpcServerProperties.getPort());
28+
assertThat( gRpcServerProperties.getPort(), CoreMatchers.nullValue(Integer.class));
2729
Assert.assertEquals(GRpcServerProperties.DEFAULT_GRPC_PORT, runningPort);
2830
}
2931
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ protected int getPort() {
4545

4646
@Override
4747
protected void beforeGreeting() {
48-
Assert.assertEquals(randomPort, gRpcServerProperties.getPort());
48+
Assert.assertEquals(randomPort, gRpcServerProperties.getPort().intValue());
4949
Assert.assertEquals(randomPort, runningPort);
5050
}
5151
}

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

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

33
import org.junit.Assert;
44
import org.junit.runner.RunWith;
5-
import org.lognet.springboot.grpc.context.LocalRunningGrpcPort;
65
import org.lognet.springboot.grpc.demo.DemoApp;
7-
import org.springframework.beans.factory.annotation.Value;
86
import org.springframework.boot.test.context.SpringBootTest;
97
import org.springframework.test.context.junit4.SpringRunner;
108

@@ -14,20 +12,9 @@
1412
@SpringBootTest(classes = {DemoApp.class}, webEnvironment = NONE, properties = "grpc.port=0")
1513
public class RandomGrpcPortTest extends GrpcServerTestBase {
1614

17-
@Value("${local.grpc.port}")
18-
int port;
19-
20-
@LocalRunningGrpcPort
21-
int runningPort;
22-
23-
@Override
24-
protected int getPort() {
25-
return port;
26-
}
27-
2815
@Override
2916
protected void beforeGreeting() {
30-
Assert.assertEquals(0,gRpcServerProperties.getPort());
31-
Assert.assertEquals(port,runningPort);
17+
Assert.assertEquals(0,gRpcServerProperties.getPort().intValue());
18+
3219
}
3320
}

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import org.lognet.springboot.grpc.GRpcServerBuilderConfigurer;
77
import org.lognet.springboot.grpc.GRpcServerRunner;
88
import org.lognet.springboot.grpc.GRpcService;
9-
import org.lognet.springboot.grpc.context.LocalRunningGrpcPort;
109
import org.springframework.beans.factory.annotation.Autowired;
1110
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
1211
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
@@ -22,21 +21,17 @@
2221

2322
@AutoConfigureOrder
2423
@ConditionalOnBean(annotation = GRpcService.class)
25-
@EnableConfigurationProperties(GRpcServerProperties.class)
24+
@EnableConfigurationProperties (GRpcServerProperties.class)
2625
public class GRpcAutoConfiguration {
2726

28-
@LocalRunningGrpcPort
29-
private int port;
3027

3128
@Autowired
3229
private GRpcServerProperties grpcServerProperties;
3330

34-
35-
3631
@Bean
3732
@ConditionalOnProperty(value = "grpc.enabled", havingValue = "true", matchIfMissing = true)
3833
public GRpcServerRunner grpcServerRunner(GRpcServerBuilderConfigurer configurer) {
39-
return new GRpcServerRunner(configurer, ServerBuilder.forPort(port));
34+
return new GRpcServerRunner(configurer, ServerBuilder.forPort(grpcServerProperties.getRunningPort()));
4035
}
4136

4237
@Bean

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,32 @@
11
package org.lognet.springboot.grpc.autoconfigure;
22

3+
import lombok.AccessLevel;
34
import lombok.Getter;
45
import lombok.Setter;
56
import org.springframework.boot.context.properties.ConfigurationProperties;
7+
import org.springframework.stereotype.Component;
8+
import org.springframework.util.SocketUtils;
9+
10+
import java.util.Optional;
611

712
/**
813
* Created by alexf on 26-Jan-16.
914
*/
1015

1116
@ConfigurationProperties("grpc")
1217
@Getter @Setter
18+
@Component("grpcProperties")
1319
public class GRpcServerProperties {
1420
public static final int DEFAULT_GRPC_PORT = 6565;
1521
/**
1622
* gRPC server port
1723
*
1824
*/
19-
private int port = 0;
25+
private Integer port = null;
26+
27+
@Setter(AccessLevel.NONE)
28+
@Getter(AccessLevel.NONE)
29+
private Integer runningPort= null;
2030

2131
/**
2232
* Enables the embedded grpc server.
@@ -37,5 +47,18 @@ public class GRpcServerProperties {
3747
*/
3848
private boolean enableReflection = false;
3949

50+
public Integer getRunningPort(){
51+
if ( null == runningPort) {
52+
synchronized (this) {
53+
if (null==runningPort) {
54+
runningPort = Optional.ofNullable(port)
55+
.map(p -> 0 == p ? SocketUtils.findAvailableTcpPort() : p)
56+
.orElse(DEFAULT_GRPC_PORT);
57+
}
58+
}
59+
}
60+
return runningPort;
61+
62+
}
4063

4164
}

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

Lines changed: 0 additions & 37 deletions
This file was deleted.

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
ElementType.ANNOTATION_TYPE })
99
@Retention(RetentionPolicy.RUNTIME)
1010
@Documented
11-
@Value("#{environment.containsProperty('grpc.port') && '0' != environment.getProperty('grpc.port') ? environment.getProperty('grpc.port'): ${" + LocalRunningGrpcPort.propertyName + "}}")
11+
@Value("#{@'grpc-org.lognet.springboot.grpc.autoconfigure.GRpcServerProperties'.getRunningPort()}")
1212
public @interface LocalRunningGrpcPort {
13-
String propertyName = "local.grpc.port";
1413
}
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
2-
org.lognet.springboot.grpc.autoconfigure.GRpcAutoConfiguration
3-
org.springframework.boot.env.EnvironmentPostProcessor=\
4-
org.lognet.springboot.grpc.context.GRpcServerEnvironment
2+
org.lognet.springboot.grpc.autoconfigure.GRpcAutoConfiguration
3+
Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,67 @@
11
package org.lognet.springboot.grpc;
22

33
import org.junit.AfterClass;
4-
import org.junit.BeforeClass;
5-
import org.junit.Test;
4+
import org.junit.ClassRule;
5+
import org.junit.rules.TemporaryFolder;
66
import org.junit.runner.RunWith;
77
import org.lognet.springboot.grpc.demo.DemoApp;
8+
import org.springframework.beans.factory.annotation.Autowired;
89
import org.springframework.boot.SpringApplication;
910
import org.springframework.boot.test.context.SpringBootTest;
1011
import org.springframework.context.ConfigurableApplicationContext;
12+
import org.springframework.core.env.Environment;
1113
import org.springframework.test.context.junit4.SpringRunner;
1214
import org.springframework.util.SocketUtils;
1315

1416
import java.io.File;
17+
import java.io.FileOutputStream;
1518
import java.io.IOException;
19+
import java.io.OutputStream;
1620
import java.net.URISyntaxException;
17-
import java.nio.file.Paths;
1821
import java.util.Optional;
22+
import java.util.Properties;
1923

20-
import static org.junit.Assert.assertEquals;
2124

2225
@RunWith(SpringRunner.class)
2326
@SpringBootTest(classes = DemoApp.class,
2427
// Normally spring.cloud.config.enabled:true is the default but since we have the
2528
// config server on the classpath we need to set it explicitly
2629
properties = { "spring.cloud.config.enabled:true"})
27-
public class ConfigServerEnvironmentTest extends GrpcServerTestBase{
30+
public abstract class ConfigServerEnvironmentBaseTest extends GrpcServerTestBase{
2831

2932
private static int configPort = SocketUtils.findAvailableTcpPort();
3033
private static ConfigurableApplicationContext server;
3134

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();
35+
@ClassRule
36+
public static TemporaryFolder temporaryFolder = new TemporaryFolder();
3737

38+
@Autowired
39+
protected Environment environment;
3840

3941

42+
public static void startConfigServer(Properties properties) throws IOException, URISyntaxException {
43+
44+
File cfgFile = temporaryFolder.newFile("grpc-demo.properties");
45+
try(OutputStream os = new FileOutputStream(cfgFile)) {
46+
properties.store(os,null);
47+
}
48+
4049

4150
server = SpringApplication.run(org.springframework.cloud.config.server.ConfigServerApplication.class,
4251
"--server.port=" + configPort,
4352
"--spring.autoconfigure.exclude=org.lognet.springboot.grpc.autoconfigure.GRpcAutoConfiguration",
4453
"--spring.cloud.config.server.health.enabled=false",
4554
"--spring.cloud.config.server.bootstrap=false",
4655
"--spring.profiles.active=native",
47-
"--spring.cloud.config.server.native.search-locations[0]=file:"+configDir.getAbsolutePath()
56+
"--grpc.enabled=false",
57+
"--spring.cloud.config.server.native.search-locations[0]=file:"+temporaryFolder.getRoot().getAbsolutePath()
4858
);
4959
System.setProperty("config.port", "" + configPort);
5060

5161
}
5262

53-
@Test
54-
public void assertConfigServerConfiguredPort(){
55-
assertEquals(6666,getPort());
56-
}
63+
64+
5765
@AfterClass
5866
public static void close() {
5967
System.clearProperty("config.port");
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.lognet.springboot.grpc;
2+
3+
import org.junit.BeforeClass;
4+
import org.junit.Test;
5+
6+
import java.io.IOException;
7+
import java.net.URISyntaxException;
8+
import java.util.Properties;
9+
10+
import static org.junit.Assert.assertEquals;
11+
12+
13+
public class ConfigServerEnvironmentTest1 extends ConfigServerEnvironmentBaseTest{
14+
15+
16+
@BeforeClass
17+
public static void startConfigServer() throws IOException, URISyntaxException {
18+
Properties properties = new Properties();
19+
properties.put("grpc.port","6666");
20+
startConfigServer(properties);
21+
22+
}
23+
24+
25+
26+
27+
@Test
28+
public void assertConfigServerConfiguredPort(){
29+
assertEquals(6666,getPort());
30+
}
31+
32+
33+
}

0 commit comments

Comments
 (0)