Skip to content

Commit 736b1a3

Browse files
author
Alexander Furer
committed
support pure io.grpc:grpc-netty dependency
1 parent 9e17f1f commit 736b1a3

File tree

5 files changed

+169
-58
lines changed

5 files changed

+169
-58
lines changed

grpc-spring-boot-starter/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ signing {
151151

152152

153153
dependencies {
154+
compileOnly "io.grpc:grpc-netty:${grpcVersion}"
154155
compile "io.grpc:grpc-netty-shaded:${grpcVersion}"
155156
compile "io.grpc:grpc-services:${grpcVersion}"
156157
compile(group: 'org.springframework.boot', name: 'spring-boot-starter')

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

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

33
import io.grpc.ServerBuilder;
44
import io.grpc.inprocess.InProcessServerBuilder;
5-
import io.grpc.netty.shaded.io.grpc.netty.NettyServerBuilder;
65
import io.grpc.services.HealthStatusManager;
76
import lombok.extern.slf4j.Slf4j;
87
import org.lognet.springboot.grpc.GRpcServerBuilderConfigurer;
@@ -16,7 +15,6 @@
1615
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
1716
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
1817
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
19-
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
2018
import org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration;
2119
import org.springframework.boot.context.properties.ConfigurationPropertiesBinding;
2220
import org.springframework.boot.context.properties.EnableConfigurationProperties;
@@ -28,7 +26,6 @@
2826
import java.io.IOException;
2927
import java.net.InetSocketAddress;
3028
import java.util.Optional;
31-
import java.util.concurrent.TimeUnit;
3229
import java.util.function.Consumer;
3330

3431
/**
@@ -38,71 +35,21 @@
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
4441
public 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()) {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.lognet.springboot.grpc.autoconfigure;
2+
3+
import org.springframework.beans.factory.annotation.Value;
4+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
5+
6+
import java.lang.annotation.Documented;
7+
import java.lang.annotation.ElementType;
8+
import java.lang.annotation.Retention;
9+
import java.lang.annotation.RetentionPolicy;
10+
import java.lang.annotation.Target;
11+
12+
@Retention(RetentionPolicy.RUNTIME)
13+
@Target({ ElementType.TYPE, ElementType.METHOD })
14+
@Documented
15+
@ConditionalOnProperty(value = "grpc.enabled", havingValue = "true", matchIfMissing = true)
16+
public @interface OnGrpcServerEnabled {
17+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package org.lognet.springboot.grpc.autoconfigure;
2+
3+
import io.grpc.ServerBuilder;
4+
import io.grpc.netty.NettyServerBuilder;
5+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
6+
import org.springframework.context.annotation.Bean;
7+
import org.springframework.context.annotation.Configuration;
8+
9+
import java.util.Optional;
10+
import java.util.concurrent.TimeUnit;
11+
12+
@Configuration
13+
@ConditionalOnClass(NettyServerBuilder.class)
14+
public class PureNettyConfiguration {
15+
16+
@Bean
17+
@OnGrpcServerEnabled
18+
public ServerBuilder<?> nettyServerBuilder(GRpcServerProperties grpcServerProperties){
19+
return Optional.ofNullable(grpcServerProperties.getNettyServer())
20+
.<ServerBuilder<?>> map(n->{
21+
final NettyServerBuilder builder = Optional.ofNullable(n.getPrimaryListenAddress())
22+
.map(NettyServerBuilder::forAddress)
23+
.orElse(NettyServerBuilder.forPort(grpcServerProperties.getRunningPort()));
24+
25+
26+
Optional.ofNullable(n.getAdditionalListenAddresses())
27+
.ifPresent(l->l.forEach(builder::addListenAddress));
28+
29+
Optional.ofNullable(n.getFlowControlWindow())
30+
.ifPresent(builder::flowControlWindow);
31+
32+
Optional.ofNullable(n.getInitialFlowControlWindow())
33+
.ifPresent(builder::initialFlowControlWindow);
34+
35+
Optional.ofNullable(n.getKeepAliveTime())
36+
.ifPresent(t->builder.keepAliveTime(t.toMillis(), TimeUnit.MILLISECONDS));
37+
38+
Optional.ofNullable(n.getKeepAliveTimeout())
39+
.ifPresent(t->builder.keepAliveTimeout(t.toMillis(), TimeUnit.MILLISECONDS));
40+
41+
Optional.ofNullable(n.getPermitKeepAliveTime())
42+
.ifPresent(t->builder.permitKeepAliveTime(t.toMillis(), TimeUnit.MILLISECONDS));
43+
44+
45+
Optional.ofNullable(n.getMaxConnectionAge())
46+
.ifPresent(t->builder.maxConnectionAge(t.toMillis(), TimeUnit.MILLISECONDS));
47+
48+
Optional.ofNullable(n.getMaxConnectionAgeGrace())
49+
.ifPresent(t->builder.maxConnectionAgeGrace(t.toMillis(), TimeUnit.MILLISECONDS));
50+
51+
Optional.ofNullable(n.getMaxConnectionIdle())
52+
.ifPresent(t->builder.maxConnectionIdle(t.toMillis(), TimeUnit.MILLISECONDS));
53+
54+
Optional.ofNullable(n.getMaxConcurrentCallsPerConnection())
55+
.ifPresent(builder::maxConcurrentCallsPerConnection);
56+
57+
Optional.ofNullable(n.getPermitKeepAliveWithoutCalls())
58+
.ifPresent(builder::permitKeepAliveWithoutCalls);
59+
60+
Optional.ofNullable(n.getMaxInboundMessageSize())
61+
.ifPresent(s->builder.maxInboundMessageSize((int)s.toBytes()));
62+
63+
Optional.ofNullable(n.getMaxInboundMetadataSize())
64+
.ifPresent(s->builder.maxInboundMetadataSize((int)s.toBytes()));
65+
66+
67+
return builder;
68+
69+
})
70+
.orElse(ServerBuilder.forPort(grpcServerProperties.getRunningPort()));
71+
}
72+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package org.lognet.springboot.grpc.autoconfigure;
2+
3+
import io.grpc.ServerBuilder;
4+
5+
import io.grpc.netty.shaded.io.grpc.netty.NettyServerBuilder;
6+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
7+
import org.springframework.context.annotation.Bean;
8+
import org.springframework.context.annotation.Configuration;
9+
10+
import java.util.Optional;
11+
import java.util.concurrent.TimeUnit;
12+
13+
@Configuration
14+
@ConditionalOnClass(NettyServerBuilder.class)
15+
public class ShadedNettyConfiguration {
16+
17+
@Bean
18+
@OnGrpcServerEnabled
19+
public ServerBuilder<?> nettyServerBuilder(GRpcServerProperties grpcServerProperties){
20+
return Optional.ofNullable(grpcServerProperties.getNettyServer())
21+
.<ServerBuilder<?>> map(n->{
22+
final NettyServerBuilder builder = Optional.ofNullable(n.getPrimaryListenAddress())
23+
.map(NettyServerBuilder::forAddress)
24+
.orElse(NettyServerBuilder.forPort(grpcServerProperties.getRunningPort()));
25+
26+
27+
Optional.ofNullable(n.getAdditionalListenAddresses())
28+
.ifPresent(l->l.forEach(builder::addListenAddress));
29+
30+
Optional.ofNullable(n.getFlowControlWindow())
31+
.ifPresent(builder::flowControlWindow);
32+
33+
Optional.ofNullable(n.getInitialFlowControlWindow())
34+
.ifPresent(builder::initialFlowControlWindow);
35+
36+
Optional.ofNullable(n.getKeepAliveTime())
37+
.ifPresent(t->builder.keepAliveTime(t.toMillis(), TimeUnit.MILLISECONDS));
38+
39+
Optional.ofNullable(n.getKeepAliveTimeout())
40+
.ifPresent(t->builder.keepAliveTimeout(t.toMillis(), TimeUnit.MILLISECONDS));
41+
42+
Optional.ofNullable(n.getPermitKeepAliveTime())
43+
.ifPresent(t->builder.permitKeepAliveTime(t.toMillis(), TimeUnit.MILLISECONDS));
44+
45+
46+
Optional.ofNullable(n.getMaxConnectionAge())
47+
.ifPresent(t->builder.maxConnectionAge(t.toMillis(), TimeUnit.MILLISECONDS));
48+
49+
Optional.ofNullable(n.getMaxConnectionAgeGrace())
50+
.ifPresent(t->builder.maxConnectionAgeGrace(t.toMillis(), TimeUnit.MILLISECONDS));
51+
52+
Optional.ofNullable(n.getMaxConnectionIdle())
53+
.ifPresent(t->builder.maxConnectionIdle(t.toMillis(), TimeUnit.MILLISECONDS));
54+
55+
Optional.ofNullable(n.getMaxConcurrentCallsPerConnection())
56+
.ifPresent(builder::maxConcurrentCallsPerConnection);
57+
58+
Optional.ofNullable(n.getPermitKeepAliveWithoutCalls())
59+
.ifPresent(builder::permitKeepAliveWithoutCalls);
60+
61+
Optional.ofNullable(n.getMaxInboundMessageSize())
62+
.ifPresent(s->builder.maxInboundMessageSize((int)s.toBytes()));
63+
64+
Optional.ofNullable(n.getMaxInboundMetadataSize())
65+
.ifPresent(s->builder.maxInboundMetadataSize((int)s.toBytes()));
66+
67+
68+
return builder;
69+
70+
})
71+
.orElse(ServerBuilder.forPort(grpcServerProperties.getRunningPort()));
72+
}
73+
}

0 commit comments

Comments
 (0)