22
33import io .grpc .ServerBuilder ;
44import io .grpc .inprocess .InProcessServerBuilder ;
5- import io .grpc .netty .shaded .io .grpc .netty .NettyServerBuilder ;
65import io .grpc .services .HealthStatusManager ;
76import lombok .extern .slf4j .Slf4j ;
87import org .lognet .springboot .grpc .GRpcServerBuilderConfigurer ;
1615import org .springframework .boot .autoconfigure .condition .ConditionalOnBean ;
1716import org .springframework .boot .autoconfigure .condition .ConditionalOnExpression ;
1817import org .springframework .boot .autoconfigure .condition .ConditionalOnMissingBean ;
19- import org .springframework .boot .autoconfigure .condition .ConditionalOnProperty ;
2018import org .springframework .boot .autoconfigure .validation .ValidationAutoConfiguration ;
2119import org .springframework .boot .context .properties .ConfigurationPropertiesBinding ;
2220import org .springframework .boot .context .properties .EnableConfigurationProperties ;
2826import java .io .IOException ;
2927import java .net .InetSocketAddress ;
3028import java .util .Optional ;
31- import java .util .concurrent .TimeUnit ;
3229import java .util .function .Consumer ;
3330
3431/**
3835@ AutoConfigureOrder
3936@ AutoConfigureAfter (ValidationAutoConfiguration .class )
4037@ ConditionalOnBean (annotation = GRpcService .class )
41- @ EnableConfigurationProperties (GRpcServerProperties .class )
42- @ Import (GRpcValidationConfiguration .class )
38+ @ EnableConfigurationProperties ({ GRpcServerProperties .class } )
39+ @ Import ({ GRpcValidationConfiguration .class , PureNettyConfiguration . class , ShadedNettyConfiguration . class } )
4340@ Slf4j
4441public class GRpcAutoConfiguration {
4542
4643 @ Autowired
4744 private GRpcServerProperties grpcServerProperties ;
4845
4946 @ Bean
50- @ ConditionalOnProperty (value = "grpc.enabled" , havingValue = "true" , matchIfMissing = true )
51- public GRpcServerRunner grpcServerRunner (@ Qualifier ("grpcInternalConfigurator" ) Consumer <ServerBuilder <?>> configurator ) {
52- ServerBuilder <?> serverBuilder = Optional .ofNullable (grpcServerProperties .getNettyServer ())
53- .<ServerBuilder <?>> map (n ->{
54- final NettyServerBuilder builder = Optional .ofNullable (n .getPrimaryListenAddress ())
55- .map (NettyServerBuilder ::forAddress )
56- .orElse (NettyServerBuilder .forPort (grpcServerProperties .getRunningPort ()));
57-
58- Optional .ofNullable (n .getAdditionalListenAddresses ())
59- .ifPresent (l ->l .forEach (builder ::addListenAddress ));
60-
61- Optional .ofNullable (n .getFlowControlWindow ())
62- .ifPresent (builder ::flowControlWindow );
63-
64- Optional .ofNullable (n .getInitialFlowControlWindow ())
65- .ifPresent (builder ::initialFlowControlWindow );
66-
67- Optional .ofNullable (n .getKeepAliveTime ())
68- .ifPresent (t ->builder .keepAliveTime (t .toMillis (), TimeUnit .MILLISECONDS ));
69-
70- Optional .ofNullable (n .getKeepAliveTimeout ())
71- .ifPresent (t ->builder .keepAliveTimeout (t .toMillis (), TimeUnit .MILLISECONDS ));
72-
73- Optional .ofNullable (n .getPermitKeepAliveTime ())
74- .ifPresent (t ->builder .permitKeepAliveTime (t .toMillis (), TimeUnit .MILLISECONDS ));
75-
76-
77- Optional .ofNullable (n .getMaxConnectionAge ())
78- .ifPresent (t ->builder .maxConnectionAge (t .toMillis (), TimeUnit .MILLISECONDS ));
79-
80- Optional .ofNullable (n .getMaxConnectionAgeGrace ())
81- .ifPresent (t ->builder .maxConnectionAgeGrace (t .toMillis (), TimeUnit .MILLISECONDS ));
82-
83- Optional .ofNullable (n .getMaxConnectionIdle ())
84- .ifPresent (t ->builder .maxConnectionIdle (t .toMillis (), TimeUnit .MILLISECONDS ));
85-
86- Optional .ofNullable (n .getMaxConcurrentCallsPerConnection ())
87- .ifPresent (builder ::maxConcurrentCallsPerConnection );
88-
89- Optional .ofNullable (n .getPermitKeepAliveWithoutCalls ())
90- .ifPresent (builder ::permitKeepAliveWithoutCalls );
91-
92- Optional .ofNullable (n .getMaxInboundMessageSize ())
93- .ifPresent (s ->builder .maxInboundMessageSize ((int )s .toBytes ()));
94-
95- Optional .ofNullable (n .getMaxInboundMetadataSize ())
96- .ifPresent (s ->builder .maxInboundMetadataSize ((int )s .toBytes ()));
97-
98-
99- return builder ;
100-
101- })
102- .orElse (ServerBuilder .forPort (grpcServerProperties .getRunningPort ()));
47+ @ OnGrpcServerEnabled
48+ public GRpcServerRunner grpcServerRunner (@ Qualifier ("grpcInternalConfigurator" ) Consumer <ServerBuilder <?>> configurator ,ServerBuilder <?> serverBuilder ) {
10349 return new GRpcServerRunner (configurator , serverBuilder );
10450 }
10551
52+
10653 @ Bean
10754 @ ConditionalOnExpression ("#{environment.getProperty('grpc.inProcessServerName','')!=''}" )
10855 public GRpcServerRunner grpcInprocessServerRunner (@ Qualifier ("grpcInternalConfigurator" ) Consumer <ServerBuilder <?>> configurator ) {
@@ -122,6 +69,7 @@ public GRpcServerBuilderConfigurer serverBuilderConfigurer() {
12269 }
12370
12471 @ Bean (name = "grpcInternalConfigurator" )
72+ @ OnGrpcServerEnabled
12573 public Consumer <ServerBuilder <?>> configurator (GRpcServerBuilderConfigurer configurer ) {
12674 return serverBuilder -> {
12775 if (grpcServerProperties .isEnabled ()) {
0 commit comments