Skip to content

Commit 1997593

Browse files
Dave Syerdsyer
Dave Syer
authored andcommitted
Add tomcat sample (no autoconfig)
Signed-off-by: Dave Syer <dsyer@vmware.com>
1 parent 24a797e commit 1997593

File tree

9 files changed

+418
-1
lines changed

9 files changed

+418
-1
lines changed

samples/grpc-tomcat/README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Spring Boot gRPC Sample
2+
3+
This project is a copy one of the samples from the [gRPC Spring Boot Starter](https://github.com/yidongnan/grpc-spring-boot-starter/blob/master/examples/local-grpc-server/build.gradle). Build and run any way you like to run Spring Boot. E.g:
4+
5+
```
6+
$ ./mvnw spring-boot:run
7+
...
8+
. ____ _ __ _ _
9+
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
10+
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
11+
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
12+
' |____| .__|_| |_|_| |_\__, | / / / /
13+
=========|_|==============|___/=/_/_/_/
14+
:: Spring Boot :: (v3.0.0)
15+
16+
2022-12-08T05:32:24.934-08:00 INFO 551632 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication using Java 17.0.5 with PID 551632 (/home/dsyer/dev/scratch/demo/target/classes started by dsyer in /home/dsyer/dev/scratch/demo)
17+
2022-12-08T05:32:24.938-08:00 INFO 551632 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to 1 default profile: "default"
18+
2022-12-08T05:32:25.377-08:00 WARN 551632 --- [ main] ocalVariableTableParameterNameDiscoverer : Using deprecated '-debug' fallback for parameter name resolution. Compile the affected code with '-parameters' instead or avoid its introspection: net.devh.boot.grpc.server.autoconfigure.GrpcHealthServiceAutoConfiguration
19+
2022-12-08T05:32:25.416-08:00 WARN 551632 --- [ main] ocalVariableTableParameterNameDiscoverer : Using deprecated '-debug' fallback for parameter name resolution. Compile the affected code with '-parameters' instead or avoid its introspection: net.devh.boot.grpc.server.autoconfigure.GrpcServerAutoConfiguration
20+
2022-12-08T05:32:25.425-08:00 WARN 551632 --- [ main] ocalVariableTableParameterNameDiscoverer : Using deprecated '-debug' fallback for parameter name resolution. Compile the affected code with '-parameters' instead or avoid its introspection: net.devh.boot.grpc.server.autoconfigure.GrpcServerFactoryAutoConfiguration
21+
2022-12-08T05:32:25.427-08:00 INFO 551632 --- [ main] g.s.a.GrpcServerFactoryAutoConfiguration : Detected grpc-netty: Creating NettyGrpcServerFactory
22+
2022-12-08T05:32:25.712-08:00 INFO 551632 --- [ main] n.d.b.g.s.s.AbstractGrpcServerFactory : Registered gRPC service: Simple, bean: grpcServerService, class: com.example.demo.GrpcServerService
23+
2022-12-08T05:32:25.712-08:00 INFO 551632 --- [ main] n.d.b.g.s.s.AbstractGrpcServerFactory : Registered gRPC service: grpc.health.v1.Health, bean: grpcHealthService, class: io.grpc.protobuf.services.HealthServiceImpl
24+
2022-12-08T05:32:25.712-08:00 INFO 551632 --- [ main] n.d.b.g.s.s.AbstractGrpcServerFactory : Registered gRPC service: grpc.reflection.v1alpha.ServerReflection, bean: protoReflectionService, class: io.grpc.protobuf.services.ProtoReflectionService
25+
2022-12-08T05:32:25.820-08:00 INFO 551632 --- [ main] n.d.b.g.s.s.GrpcServerLifecycle : gRPC Server started, listening on address: *, port: 9090
26+
2022-12-08T05:32:25.831-08:00 INFO 551632 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 1.264 seconds (process running for 1.623)
27+
```
28+
29+
The server starts by default on port 9090. Test with [gRPCurl](https://github.com/fullstorydev/grpcurl):
30+
31+
```
32+
$ grpcurl -d '{"name":"Hi"}' -plaintext localhost:9090 Simple.SayHello
33+
{
34+
"message": "Hello ==\u003e Hi"
35+
}
36+
```
37+
38+
## Native Image
39+
40+
The app compiles to a native image if the JVM is GraalVM:
41+
42+
```
43+
$ ./mvnw -Pnative native:compile
44+
$ ./target/demo
45+
. ____ _ __ _ _
46+
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
47+
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
48+
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
49+
' |____| .__|_| |_|_| |_\__, | / / / /
50+
=========|_|==============|___/=/_/_/_/
51+
:: Spring Boot :: (v3.0.0)
52+
53+
2022-12-08T05:36:54.365-08:00 INFO 554359 --- [ main] com.example.demo.DemoApplication : Starting AOT-processed DemoApplication using Java 17.0.5 with PID 554359 (/home/dsyer/dev/scratch/demo/target/demo started by dsyer in /home/dsyer/dev/scratch/demo)
54+
2022-12-08T05:36:54.366-08:00 INFO 554359 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to 1 default profile: "default"
55+
2022-12-08T05:36:54.377-08:00 INFO 554359 --- [ main] g.s.a.GrpcServerFactoryAutoConfiguration : Detected grpc-netty: Creating NettyGrpcServerFactory
56+
2022-12-08T05:36:54.392-08:00 INFO 554359 --- [ main] n.d.b.g.s.s.AbstractGrpcServerFactory : Registered gRPC service: Simple, bean: grpcServerService, class: com.example.demo.GrpcServerService
57+
2022-12-08T05:36:54.392-08:00 INFO 554359 --- [ main] n.d.b.g.s.s.AbstractGrpcServerFactory : Registered gRPC service: grpc.health.v1.Health, bean: grpcHealthService, class: io.grpc.protobuf.services.HealthServiceImpl
58+
2022-12-08T05:36:54.392-08:00 INFO 554359 --- [ main] n.d.b.g.s.s.AbstractGrpcServerFactory : Registered gRPC service: grpc.reflection.v1alpha.ServerReflection, bean: protoReflectionService, class: io.grpc.protobuf.services.ProtoReflectionService
59+
2022-12-08T05:36:54.396-08:00 INFO 554359 --- [ main] n.d.b.g.s.s.GrpcServerLifecycle : gRPC Server started, listening on address: *, port: 9090
60+
2022-12-08T05:36:54.396-08:00 INFO 554359 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 0.046 seconds (process running for 0.052)
61+
```

samples/grpc-tomcat/pom.xml

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>org.springframework.boot</groupId>
8+
<artifactId>spring-boot-starter-parent</artifactId>
9+
<version>3.3.3</version>
10+
<relativePath /> <!-- lookup parent from repository -->
11+
</parent>
12+
<groupId>org.springframework.grpc</groupId>
13+
<artifactId>grpc-tomcat-sample</artifactId>
14+
<version>0.1.0-SNAPSHOT</version>
15+
<name>Spring gRPC Server Sample</name>
16+
<description>Demo project for Spring gRPC</description>
17+
<url />
18+
<licenses>
19+
<license />
20+
</licenses>
21+
<developers>
22+
<developer />
23+
</developers>
24+
<scm>
25+
<connection />
26+
<developerConnection />
27+
<tag />
28+
<url />
29+
</scm>
30+
<properties>
31+
<java.version>17</java.version>
32+
<spring-javaformat-maven-plugin.version>0.0.39</spring-javaformat-maven-plugin.version>
33+
<protobuf-java.version>3.25.5</protobuf-java.version>
34+
<grpc.version>1.63.2</grpc.version>
35+
</properties>
36+
<dependencyManagement>
37+
<dependencies>
38+
<dependency>
39+
<groupId>org.springframework.grpc</groupId>
40+
<artifactId>spring-grpc-dependencies</artifactId>
41+
<version>0.1.0-SNAPSHOT</version>
42+
<type>pom</type>
43+
<scope>import</scope>
44+
</dependency>
45+
</dependencies>
46+
</dependencyManagement>
47+
<dependencies>
48+
<dependency>
49+
<groupId>org.springframework.grpc</groupId>
50+
<artifactId>spring-grpc-spring-boot-starter</artifactId>
51+
</dependency>
52+
<dependency>
53+
<groupId>org.springframework.boot</groupId>
54+
<artifactId>spring-boot-starter-web</artifactId>
55+
</dependency>
56+
<dependency>
57+
<groupId>io.grpc</groupId>
58+
<artifactId>grpc-services</artifactId>
59+
</dependency>
60+
<dependency>
61+
<groupId>io.grpc</groupId>
62+
<artifactId>grpc-servlet-jakarta</artifactId>
63+
</dependency>
64+
65+
<dependency>
66+
<groupId>org.springframework.boot</groupId>
67+
<artifactId>spring-boot-starter-test</artifactId>
68+
<scope>test</scope>
69+
</dependency>
70+
<dependency>
71+
<groupId>org.springframework.grpc</groupId>
72+
<artifactId>spring-grpc-test</artifactId>
73+
<scope>test</scope>
74+
</dependency>
75+
</dependencies>
76+
77+
<build>
78+
<extensions>
79+
<extension>
80+
<groupId>kr.motd.maven</groupId>
81+
<artifactId>os-maven-plugin</artifactId>
82+
<version>1.7.1</version>
83+
</extension>
84+
</extensions>
85+
<plugins>
86+
<plugin>
87+
<groupId>org.graalvm.buildtools</groupId>
88+
<artifactId>native-maven-plugin</artifactId>
89+
<configuration>
90+
<buildArgs>
91+
<buildArg>--verbose</buildArg>
92+
</buildArgs>
93+
</configuration>
94+
</plugin>
95+
<plugin>
96+
<groupId>org.springframework.boot</groupId>
97+
<artifactId>spring-boot-maven-plugin</artifactId>
98+
</plugin>
99+
<plugin>
100+
<groupId>org.apache.maven.plugins</groupId>
101+
<artifactId>maven-deploy-plugin</artifactId>
102+
<configuration>
103+
<skip>true</skip>
104+
</configuration>
105+
</plugin>
106+
<plugin>
107+
<groupId>io.spring.javaformat</groupId>
108+
<artifactId>spring-javaformat-maven-plugin</artifactId>
109+
<version>${spring-javaformat-maven-plugin.version}</version>
110+
<executions>
111+
<execution>
112+
<?m2e ignore?>
113+
<phase>validate</phase>
114+
<inherited>true</inherited>
115+
<goals>
116+
<goal>validate</goal>
117+
</goals>
118+
</execution>
119+
</executions>
120+
</plugin>
121+
<plugin>
122+
<groupId>org.xolstice.maven.plugins</groupId>
123+
<artifactId>protobuf-maven-plugin</artifactId>
124+
<version>0.6.1</version>
125+
<executions>
126+
<execution>
127+
<goals>
128+
<goal>compile</goal>
129+
<goal>compile-custom</goal>
130+
</goals>
131+
</execution>
132+
</executions>
133+
<configuration>
134+
<pluginParameter>
135+
jakarta_omit
136+
</pluginParameter>
137+
<protocArtifact>
138+
com.google.protobuf:protoc:${protobuf-java.version}:exe:${os.detected.classifier}</protocArtifact>
139+
<pluginId>grpc-java</pluginId>
140+
<pluginArtifact>
141+
io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}</pluginArtifact>
142+
</configuration>
143+
</plugin>
144+
</plugins>
145+
</build>
146+
147+
<repositories>
148+
<repository>
149+
<id>spring-milestones</id>
150+
<name>Spring Milestones</name>
151+
<url>https://repo.spring.io/milestone</url>
152+
<snapshots>
153+
<enabled>false</enabled>
154+
</snapshots>
155+
</repository>
156+
<repository>
157+
<id>spring-snapshots</id>
158+
<name>Spring Snapshots</name>
159+
<url>https://repo.spring.io/snapshot</url>
160+
<releases>
161+
<enabled>false</enabled>
162+
</releases>
163+
</repository>
164+
</repositories>
165+
<pluginRepositories>
166+
<pluginRepository>
167+
<id>spring-milestones</id>
168+
<name>Spring Milestones</name>
169+
<url>https://repo.spring.io/milestone</url>
170+
<snapshots>
171+
<enabled>false</enabled>
172+
</snapshots>
173+
</pluginRepository>
174+
<pluginRepository>
175+
<id>spring-snapshots</id>
176+
<name>Spring Snapshots</name>
177+
<url>https://repo.spring.io/snapshot</url>
178+
<releases>
179+
<enabled>false</enabled>
180+
</releases>
181+
</pluginRepository>
182+
</pluginRepositories>
183+
184+
185+
</project>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package org.springframework.grpc.sample;
2+
3+
import java.util.List;
4+
import java.util.stream.Collectors;
5+
6+
import org.springframework.boot.SpringApplication;
7+
import org.springframework.boot.autoconfigure.SpringBootApplication;
8+
import org.springframework.boot.web.servlet.ServletRegistrationBean;
9+
import org.springframework.context.annotation.Bean;
10+
import org.springframework.grpc.autoconfigure.server.GrpcServerAutoConfiguration;
11+
12+
import io.grpc.BindableService;
13+
import io.grpc.protobuf.services.ProtoReflectionService;
14+
import io.grpc.servlet.jakarta.GrpcServlet;
15+
16+
@SpringBootApplication(exclude = GrpcServerAutoConfiguration.class)
17+
public class GrpcServerApplication {
18+
19+
public static void main(String[] args) {
20+
SpringApplication.run(GrpcServerApplication.class, args);
21+
}
22+
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+
33+
@Bean
34+
public BindableService serverReflection() {
35+
return ProtoReflectionService.newInstance();
36+
}
37+
38+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package org.springframework.grpc.sample;
2+
3+
import org.apache.commons.logging.Log;
4+
import org.apache.commons.logging.LogFactory;
5+
import org.springframework.grpc.sample.proto.HelloReply;
6+
import org.springframework.grpc.sample.proto.HelloRequest;
7+
import org.springframework.grpc.sample.proto.SimpleGrpc;
8+
import org.springframework.scheduling.annotation.Async;
9+
import org.springframework.stereotype.Service;
10+
11+
import io.grpc.stub.StreamObserver;
12+
13+
@Service
14+
public class GrpcServerService extends SimpleGrpc.SimpleImplBase {
15+
16+
private static Log log = LogFactory.getLog(GrpcServerService.class);
17+
18+
@Override
19+
public void sayHello(HelloRequest req, StreamObserver<HelloReply> responseObserver) {
20+
log.info("Hello " + req.getName());
21+
HelloReply reply = HelloReply.newBuilder().setMessage("Hello ==> " + req.getName()).build();
22+
responseObserver.onNext(reply);
23+
responseObserver.onCompleted();
24+
}
25+
26+
@Override
27+
@Async
28+
public void streamHello(HelloRequest req, StreamObserver<HelloReply> responseObserver) {
29+
log.info("Hello " + req.getName());
30+
int count = 0;
31+
while (count < 10) {
32+
HelloReply reply = HelloReply.newBuilder().setMessage("Hello(" + count + ") ==> " + req.getName()).build();
33+
responseObserver.onNext(reply);
34+
count++;
35+
try {
36+
Thread.sleep(1000L);
37+
}
38+
catch (InterruptedException e) {
39+
Thread.currentThread().interrupt();
40+
responseObserver.onError(e);
41+
return;
42+
}
43+
}
44+
responseObserver.onCompleted();
45+
}
46+
47+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
syntax = "proto3";
2+
3+
option java_multiple_files = true;
4+
option java_package = "org.springframework.grpc.sample.proto";
5+
option java_outer_classname = "HelloWorldProto";
6+
7+
// The greeting service definition.
8+
service Simple {
9+
// Sends a greeting
10+
rpc SayHello (HelloRequest) returns (HelloReply) {
11+
}
12+
rpc StreamHello(HelloRequest) returns (stream HelloReply) {}
13+
}
14+
15+
// The request message containing the user's name.
16+
message HelloRequest {
17+
string name = 1;
18+
}
19+
20+
// The response message containing the greetings
21+
message HelloReply {
22+
string message = 1;
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Ignored unless building in Nix (https://github.com/oracle/graal/issues/8639)
2+
Args = -ENIX_LDFLAGS -ENIX_CC_WRAPPER_TARGET_HOST_x86_64_unknown_linux_gnu
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
spring.application.name=grpc-tomcat
2+
server.port=9090
3+
server.http2.enabled=true

0 commit comments

Comments
 (0)