Skip to content

Commit c358f6e

Browse files
Dave Syerdsyer
Dave Syer
authored andcommitted
Move GrpcServlet to autoconfiguration
Signed-off-by: Dave Syer <dsyer@vmware.com>
1 parent 1997593 commit c358f6e

File tree

7 files changed

+99
-31
lines changed

7 files changed

+99
-31
lines changed

samples/grpc-tomcat/src/main/java/org/springframework/grpc/sample/GrpcServerApplication.java

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,19 @@
11
package org.springframework.grpc.sample;
22

3-
import java.util.List;
4-
import java.util.stream.Collectors;
5-
63
import org.springframework.boot.SpringApplication;
74
import org.springframework.boot.autoconfigure.SpringBootApplication;
8-
import org.springframework.boot.web.servlet.ServletRegistrationBean;
95
import org.springframework.context.annotation.Bean;
10-
import org.springframework.grpc.autoconfigure.server.GrpcServerAutoConfiguration;
116

127
import io.grpc.BindableService;
138
import io.grpc.protobuf.services.ProtoReflectionService;
14-
import io.grpc.servlet.jakarta.GrpcServlet;
159

16-
@SpringBootApplication(exclude = GrpcServerAutoConfiguration.class)
10+
@SpringBootApplication
1711
public class GrpcServerApplication {
1812

1913
public static void main(String[] args) {
2014
SpringApplication.run(GrpcServerApplication.class, args);
2115
}
2216

23-
@Bean
24-
public ServletRegistrationBean<GrpcServlet> grpcServlet(List<BindableService> services) {
25-
List<String> paths = services.stream()
26-
.map(service -> "/" + service.bindService().getServiceDescriptor().getName() + "/*")
27-
.collect(Collectors.toList());
28-
ServletRegistrationBean<GrpcServlet> servlet = new ServletRegistrationBean<>(new GrpcServlet(services));
29-
servlet.setUrlMappings(paths);
30-
return servlet;
31-
}
32-
3317
@Bean
3418
public BindableService serverReflection() {
3519
return ProtoReflectionService.newInstance();

samples/grpc-tomcat/src/test/java/org/springframework/grpc/sample/GrpcServerApplicationTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class GrpcServerApplicationTests {
2424
private static Log log = LogFactory.getLog(GrpcServerApplicationTests.class);
2525

2626
public static void main(String[] args) {
27-
new SpringApplicationBuilder(GrpcServerApplication.class, ExtraConfiguration.class).profiles("ssl").run(args);
27+
new SpringApplicationBuilder(GrpcServerApplication.class, ExtraConfiguration.class).run(args);
2828
}
2929

3030
@Autowired

spring-grpc-spring-boot-autoconfigure/pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@
4545
<version>${project.version}</version>
4646
<optional>true</optional>
4747
</dependency>
48+
<dependency>
49+
<groupId>org.apache.tomcat.embed</groupId>
50+
<artifactId>tomcat-embed-core</artifactId>
51+
<optional>true</optional>
52+
</dependency>
53+
<dependency>
54+
<groupId>io.grpc</groupId>
55+
<artifactId>grpc-servlet-jakarta</artifactId>
56+
<optional>true</optional>
57+
</dependency>
4858
<dependency>
4959
<groupId>io.grpc</groupId>
5060
<artifactId>grpc-netty-shaded</artifactId>

spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/GrpcServerAutoConfiguration.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,27 @@
1515
*/
1616
package org.springframework.grpc.autoconfigure.server;
1717

18-
import io.grpc.BindableService;
19-
20-
import io.grpc.CompressorRegistry;
21-
import io.grpc.DecompressorRegistry;
22-
import io.grpc.netty.NettyServerBuilder;
2318
import org.springframework.beans.factory.ObjectProvider;
2419
import org.springframework.boot.autoconfigure.AutoConfiguration;
25-
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
20+
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
2621
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
2722
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
2823
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2924
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
3025
import org.springframework.boot.context.properties.EnableConfigurationProperties;
3126
import org.springframework.context.ApplicationEventPublisher;
3227
import org.springframework.context.annotation.Bean;
33-
import org.springframework.context.annotation.Conditional;
3428
import org.springframework.context.annotation.Import;
35-
import org.springframework.core.Ordered;
3629
import org.springframework.grpc.autoconfigure.common.codec.GrpcCodecConfiguration;
3730
import org.springframework.grpc.server.GrpcServerFactory;
3831
import org.springframework.grpc.server.ServerBuilderCustomizer;
3932
import org.springframework.grpc.server.lifecycle.GrpcServerLifecycle;
4033

34+
import io.grpc.BindableService;
35+
import io.grpc.CompressorRegistry;
36+
import io.grpc.DecompressorRegistry;
37+
import io.grpc.netty.NettyServerBuilder;
38+
4139
/**
4240
* {@link EnableAutoConfiguration Auto-configuration} for gRPC server-side components.
4341
* <p>
@@ -48,12 +46,11 @@
4846
* @author Chris Bono
4947
*/
5048
@AutoConfiguration
51-
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
49+
@AutoConfigureAfter(GrpcServerFactoryAutoConfiguration.class)
5250
@ConditionalOnClass(BindableService.class)
5351
@ConditionalOnBean(BindableService.class)
5452
@EnableConfigurationProperties(GrpcServerProperties.class)
55-
@Import({ GrpcServerFactoryConfigurations.ShadedNettyServerFactoryConfiguration.class,
56-
GrpcServerFactoryConfigurations.NettyServerFactoryConfiguration.class, GrpcCodecConfiguration.class })
53+
@Import({ GrpcCodecConfiguration.class })
5754
public class GrpcServerAutoConfiguration {
5855

5956
private final GrpcServerProperties properties;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2024-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.grpc.autoconfigure.server;
17+
18+
import java.util.List;
19+
import java.util.stream.Collectors;
20+
21+
import org.springframework.boot.autoconfigure.AutoConfiguration;
22+
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
23+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
24+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
25+
import org.springframework.boot.autoconfigure.condition.ConditionalOnNotWebApplication;
26+
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
27+
import org.springframework.boot.web.servlet.ServletRegistrationBean;
28+
import org.springframework.context.annotation.Bean;
29+
import org.springframework.context.annotation.Configuration;
30+
import org.springframework.context.annotation.Import;
31+
import org.springframework.core.Ordered;
32+
33+
import io.grpc.BindableService;
34+
import io.grpc.servlet.jakarta.GrpcServlet;
35+
36+
/**
37+
* {@link EnableAutoConfiguration Auto-configuration} for gRPC server-side components.
38+
* <p>
39+
* gRPC must be on the classpath and at least one {@link BindableService} bean registered
40+
* in the context in order for the auto-configuration to execute.
41+
*
42+
* @author David Syer
43+
* @author Chris Bono
44+
*/
45+
@AutoConfiguration
46+
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
47+
@ConditionalOnClass(BindableService.class)
48+
public class GrpcServerFactoryAutoConfiguration {
49+
50+
@Configuration(proxyBeanMethods = false)
51+
@ConditionalOnNotWebApplication
52+
@Import({ GrpcServerFactoryConfigurations.ShadedNettyServerFactoryConfiguration.class,
53+
GrpcServerFactoryConfigurations.NettyServerFactoryConfiguration.class })
54+
static class GrpcServerFactoryConfiguration {
55+
56+
}
57+
58+
@Configuration(proxyBeanMethods = false)
59+
@ConditionalOnWebApplication
60+
static class GrpcServletConfiguration {
61+
62+
@Bean
63+
public ServletRegistrationBean<GrpcServlet> grpcServlet(List<BindableService> services) {
64+
List<String> paths = services.stream()
65+
.map(service -> "/" + service.bindService().getServiceDescriptor().getName() + "/*")
66+
.collect(Collectors.toList());
67+
ServletRegistrationBean<GrpcServlet> servlet = new ServletRegistrationBean<>(new GrpcServlet(services));
68+
servlet.setUrlMappings(paths);
69+
return servlet;
70+
}
71+
72+
}
73+
74+
}
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
org.springframework.grpc.autoconfigure.server.GrpcServerFactoryAutoConfiguration
12
org.springframework.grpc.autoconfigure.server.GrpcServerAutoConfiguration
23
org.springframework.grpc.autoconfigure.client.GrpcClientAutoConfiguration

spring-grpc-spring-boot-autoconfigure/src/test/java/org/springframework/grpc/autoconfigure/server/GrpcServerAutoConfigurationTests.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ private ApplicationContextRunner contextRunner() {
6666
when(service.bindService()).thenReturn(serviceDefinition);
6767
// NOTE: we use noop server lifecycle to avoid startup
6868
return new ApplicationContextRunner()
69-
.withConfiguration(AutoConfigurations.of(GrpcServerAutoConfiguration.class, SslAutoConfiguration.class))
69+
.withConfiguration(AutoConfigurations.of(GrpcServerAutoConfiguration.class,
70+
GrpcServerFactoryAutoConfiguration.class, SslAutoConfiguration.class))
7071
.withBean("noopServerLifecycle", GrpcServerLifecycle.class, Mockito::mock)
7172
.withBean(BindableService.class, () -> service);
7273
}
@@ -77,7 +78,8 @@ private ApplicationContextRunner contextRunnerWithLifecyle() {
7778
when(service.bindService()).thenReturn(serviceDefinition);
7879
// NOTE: we use noop server lifecycle to avoid startup
7980
return new ApplicationContextRunner()
80-
.withConfiguration(AutoConfigurations.of(GrpcServerAutoConfiguration.class, SslAutoConfiguration.class))
81+
.withConfiguration(AutoConfigurations.of(GrpcServerAutoConfiguration.class,
82+
GrpcServerFactoryAutoConfiguration.class, SslAutoConfiguration.class))
8183
.withBean(BindableService.class, () -> service);
8284
}
8385

0 commit comments

Comments
 (0)