Skip to content

Commit 42b5470

Browse files
author
Dave Syer
committed
Add sample with Shaded Netty
1 parent 6be591d commit 42b5470

File tree

27 files changed

+1405
-25
lines changed

27 files changed

+1405
-25
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,6 @@ As shown in the snippet below this can then be followed by version-less declarat
8585
```gradle
8686
dependencies {
8787
implementation platform("org.springframework.ai:spring-grpc-dependencies:1.0.0-SNAPSHOT")
88-
// Replace the following with the starter dependencies of specific modules you wish to use
89-
implementation 'org.springframework.ai:spring-grpc-openai'
9088
}
9189
```
9290

57.4 KB
Binary file not shown.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.8.6/apache-maven-3.8.6-bin.zip
2+
wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Getting Started
2+
3+
### Reference Documentation
4+
For further reference, please consider the following sections:
5+
6+
* [Official Apache Maven documentation](https://maven.apache.org/guides/index.html)
7+
* [Spring Boot Maven Plugin Reference Guide](https://docs.spring.io/spring-boot/docs/3.0.0/maven-plugin/reference/html/)
8+
* [Create an OCI image](https://docs.spring.io/spring-boot/docs/3.0.0/maven-plugin/reference/html/#build-image)
9+
* [GraalVM Native Image Support](https://docs.spring.io/spring-boot/docs/3.0.0/reference/html/native-image.html#native-image)
10+
* [Spring Web](https://docs.spring.io/spring-boot/docs/3.0.0/reference/htmlsingle/#web)
11+
* [Spring Boot Actuator](https://docs.spring.io/spring-boot/docs/3.0.0/reference/htmlsingle/#actuator)
12+
13+
### Guides
14+
The following guides illustrate how to use some features concretely:
15+
16+
* [Building a RESTful Web Service](https://spring.io/guides/gs/rest-service/)
17+
* [Serving Web Content with Spring MVC](https://spring.io/guides/gs/serving-web-content/)
18+
* [Building REST services with Spring](https://spring.io/guides/tutorials/rest/)
19+
* [Building a RESTful Web Service with Spring Boot Actuator](https://spring.io/guides/gs/actuator-service/)
20+
21+
### Additional Links
22+
These additional references should also help you:
23+
24+
* [Configure AOT settings in Build Plugin](https://docs.spring.io/spring-boot/docs/3.0.0/maven-plugin/reference/htmlsingle/#aot)
25+
26+
## GraalVM Native Support
27+
28+
This project has been configured to let you generate either a lightweight container or a native executable.
29+
It is also possible to run your tests in a native image.
30+
31+
### Lightweight Container with Cloud Native Buildpacks
32+
If you're already familiar with Spring Boot container images support, this is the easiest way to get started.
33+
Docker should be installed and configured on your machine prior to creating the image.
34+
35+
To create the image, run the following goal:
36+
37+
```
38+
$ ./mvnw spring-boot:build-image -Pnative
39+
```
40+
41+
Then, you can run the app like any other container:
42+
43+
```
44+
$ docker run --rm -p 8080:8080 demo:0.0.1-SNAPSHOT
45+
```
46+
47+
### Executable with Native Build Tools
48+
Use this option if you want to explore more options such as running your tests in a native image.
49+
The GraalVM `native-image` compiler should be installed and configured on your machine.
50+
51+
NOTE: GraalVM 22.3+ is required.
52+
53+
To create the executable, run the following goal:
54+
55+
```
56+
$ ./mvnw native:compile -Pnative
57+
```
58+
59+
Then, you can run the app as follows:
60+
```
61+
$ target/demo
62+
```
63+
64+
You can also run your existing tests suite in a native image.
65+
This is an efficient way to validate the compatibility of your application.
66+
67+
To run your existing tests in a native image, run the following goal:
68+
69+
```
70+
$ ./mvnw test -PnativeTest
71+
```
72+
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
```
62+
63+
The patches needed to make it work in native were a reflection hint (upstream change: https://github.com/oracle/graalvm-reachability-metadata/pull/148) and some autoconfig metadata (upstream change: https://github.com/yidongnan/grpc-spring-boot-starter/pull/775).
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
plugins {
2+
id 'java'
3+
id 'org.springframework.boot' version '3.3.4'
4+
id 'io.spring.dependency-management' version '1.1.6'
5+
id 'org.graalvm.buildtools.native' version '0.10.3'
6+
id 'com.google.protobuf' version '0.9.4'
7+
}
8+
9+
group = 'com.example'
10+
version = '0.1.0-SNAPSHOT'
11+
12+
java {
13+
toolchain {
14+
languageVersion = JavaLanguageVersion.of(17)
15+
}
16+
}
17+
18+
repositories {
19+
mavenLocal()
20+
mavenCentral()
21+
maven { url 'https://repo.spring.io/milestone' }
22+
maven { url 'https://repo.spring.io/snapshot' }
23+
}
24+
25+
dependencyManagement {
26+
imports {
27+
mavenBom 'org.springframework.grpc:spring-grpc-dependencies:0.1.0-SNAPSHOT'
28+
}
29+
}
30+
31+
dependencies {
32+
implementation 'org.springframework.grpc:spring-grpc-spring-boot-starter'
33+
implementation 'io.grpc:grpc-netty-shaded'
34+
implementation 'io.grpc:grpc-services'
35+
modules {
36+
module("io.grpc:grpc-netty") {
37+
replacedBy("io.grpc:grpc-netty-shaded", "Use Netty shaded instead of regular Netty")
38+
}
39+
}
40+
testImplementation 'org.springframework.boot:spring-boot-starter-test'
41+
testImplementation 'org.springframework.grpc:spring-grpc-test'
42+
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
43+
}
44+
45+
tasks.named('test') {
46+
useJUnitPlatform()
47+
}
48+
49+
protobuf {
50+
protoc {
51+
artifact = "com.google.protobuf:protoc:${dependencyManagement.importedProperties['protobuf-java.version']}"
52+
}
53+
plugins {
54+
grpc {
55+
artifact = "io.grpc:protoc-gen-grpc-java:${dependencyManagement.importedProperties['grpc.version']}"
56+
}
57+
}
58+
generateProtoTasks {
59+
all()*.plugins {
60+
grpc {
61+
option 'jakarta_omit'
62+
}
63+
}
64+
}
65+
}
Binary file not shown.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
distributionBase=GRADLE_USER_HOME
2+
distributionPath=wrapper/dists
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.2-bin.zip
4+
networkTimeout=10000
5+
validateDistributionUrl=true
6+
zipStoreBase=GRADLE_USER_HOME
7+
zipStorePath=wrapper/dists

0 commit comments

Comments
 (0)