|
3 | 3 | import io.grpc.ServerBuilder; |
4 | 4 | import io.grpc.inprocess.InProcessServerBuilder; |
5 | 5 | import io.grpc.services.HealthStatusManager; |
6 | | -import lombok.Getter; |
7 | 6 | import org.lognet.springboot.grpc.GRpcServerBuilderConfigurer; |
8 | 7 | import org.lognet.springboot.grpc.GRpcServerRunner; |
9 | 8 | import org.lognet.springboot.grpc.GRpcService; |
|
13 | 12 | import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; |
14 | 13 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
15 | 14 | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
16 | | -import org.springframework.boot.context.properties.ConfigurationProperties; |
17 | 15 | import org.springframework.boot.context.properties.EnableConfigurationProperties; |
18 | 16 | import org.springframework.context.ApplicationContext; |
| 17 | +import org.springframework.context.ConfigurableApplicationContext; |
19 | 18 | 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; |
22 | 26 |
|
23 | 27 | /** |
24 | 28 | * Created by alexf on 25-Jan-16. |
|
29 | 33 | @EnableConfigurationProperties(GRpcServerProperties.class) |
30 | 34 | public class GRpcAutoConfiguration { |
31 | 35 |
|
32 | | - |
| 36 | + @Autowired |
| 37 | + private ApplicationContext applicationContext; |
33 | 38 |
|
34 | 39 | @Autowired |
35 | 40 | private GRpcServerProperties grpcServerProperties; |
36 | 41 |
|
37 | 42 |
|
38 | 43 |
|
39 | 44 | @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; |
43 | 64 | } |
44 | 65 |
|
45 | 66 | @Bean |
|
0 commit comments