Skip to content

Commit 59909d2

Browse files
committed
Add webflux oauth2 sample
1 parent 4753de5 commit 59909d2

11 files changed

Lines changed: 542 additions & 0 deletions

File tree

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.5.3)
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.5.3)
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+
```
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
plugins {
2+
id 'java'
3+
id 'org.springframework.boot' version '3.5.3'
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.10.0-SNAPSHOT'
11+
12+
java {
13+
toolchain {
14+
languageVersion = JavaLanguageVersion.of(17)
15+
}
16+
}
17+
18+
processTestAot {
19+
enabled = false
20+
}
21+
22+
repositories {
23+
mavenCentral()
24+
maven { url 'https://repo.spring.io/milestone' }
25+
maven { url 'https://repo.spring.io/snapshot' }
26+
}
27+
28+
dependencyManagement {
29+
imports {
30+
mavenBom 'org.springframework.grpc:spring-grpc-dependencies:0.10.0-SNAPSHOT'
31+
}
32+
}
33+
34+
dependencies {
35+
implementation 'org.springframework.grpc:spring-grpc-spring-boot-starter'
36+
implementation 'org.springframework.boot:spring-boot-starter-webflux'
37+
implementation 'org.springframework.boot:spring-boot-starter-oauth2-resource-server'
38+
implementation 'org.springframework.boot:spring-boot-starter-oauth2-client'
39+
implementation 'org.springframework.boot:spring-boot-starter-security'
40+
implementation 'io.grpc:grpc-services'
41+
testImplementation 'org.springframework.boot:spring-boot-starter-test'
42+
testImplementation 'org.springframework.grpc:spring-grpc-test'
43+
testImplementation 'org.springframework.experimental.boot:spring-boot-testjars-maven:0.0.4'
44+
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
45+
}
46+
47+
test {
48+
useJUnitPlatform()
49+
testLogging.showStandardStreams = true
50+
outputs.upToDateWhen { false }
51+
}
52+
53+
protobuf {
54+
protoc {
55+
artifact = "com.google.protobuf:protoc:${dependencyManagement.importedProperties['protobuf-java.version']}"
56+
}
57+
plugins {
58+
grpc {
59+
artifact = "io.grpc:protoc-gen-grpc-java:${dependencyManagement.importedProperties['grpc.version']}"
60+
}
61+
}
62+
generateProtoTasks {
63+
all()*.plugins {
64+
grpc {
65+
option '@generated=omit'
66+
}
67+
}
68+
}
69+
}
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
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.5.3</version>
10+
<relativePath /> <!-- lookup parent from repository -->
11+
</parent>
12+
<groupId>org.springframework.grpc</groupId>
13+
<artifactId>grpc-webflux-sample-secure</artifactId>
14+
<version>0.10.0-SNAPSHOT</version>
15+
<name>Spring gRPC Webflux Sample Secure</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.43</spring-javaformat-maven-plugin.version>
33+
<protobuf-java.version>4.30.2</protobuf-java.version>
34+
<grpc.version>1.72.0</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.10.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.boot</groupId>
50+
<artifactId>spring-boot-starter-webflux</artifactId>
51+
</dependency>
52+
<dependency>
53+
<groupId>org.springframework.boot</groupId>
54+
<artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
55+
</dependency>
56+
<dependency>
57+
<groupId>org.springframework.boot</groupId>
58+
<artifactId>spring-boot-starter-security</artifactId>
59+
</dependency>
60+
<dependency>
61+
<groupId>org.springframework.grpc</groupId>
62+
<artifactId>spring-grpc-spring-boot-starter</artifactId>
63+
</dependency>
64+
<dependency>
65+
<groupId>io.grpc</groupId>
66+
<artifactId>grpc-services</artifactId>
67+
</dependency>
68+
<dependency>
69+
<groupId>org.springframework.boot</groupId>
70+
<artifactId>spring-boot-starter-oauth2-client</artifactId>
71+
<scope>test</scope>
72+
</dependency>
73+
<dependency>
74+
<groupId>org.springframework.grpc</groupId>
75+
<artifactId>spring-grpc-test</artifactId>
76+
<scope>test</scope>
77+
</dependency>
78+
<dependency>
79+
<groupId>org.springframework.experimental.boot</groupId>
80+
<artifactId>spring-boot-testjars-maven</artifactId>
81+
<version>0.0.4</version>
82+
<scope>test</scope>
83+
</dependency>
84+
</dependencies>
85+
86+
<build>
87+
<plugins>
88+
<plugin>
89+
<groupId>org.graalvm.buildtools</groupId>
90+
<artifactId>native-maven-plugin</artifactId>
91+
<configuration>
92+
<buildArgs>
93+
<buildArg>--verbose</buildArg>
94+
</buildArgs>
95+
</configuration>
96+
</plugin>
97+
<plugin>
98+
<groupId>org.springframework.boot</groupId>
99+
<artifactId>spring-boot-maven-plugin</artifactId>
100+
</plugin>
101+
<plugin>
102+
<groupId>org.apache.maven.plugins</groupId>
103+
<artifactId>maven-deploy-plugin</artifactId>
104+
<configuration>
105+
<skip>true</skip>
106+
</configuration>
107+
</plugin>
108+
<plugin>
109+
<groupId>io.spring.javaformat</groupId>
110+
<artifactId>spring-javaformat-maven-plugin</artifactId>
111+
<version>${spring-javaformat-maven-plugin.version}</version>
112+
<executions>
113+
<execution>
114+
<?m2e ignore?>
115+
<phase>validate</phase>
116+
<inherited>true</inherited>
117+
<goals>
118+
<goal>validate</goal>
119+
</goals>
120+
</execution>
121+
</executions>
122+
</plugin>
123+
<plugin>
124+
<groupId>io.github.ascopes</groupId>
125+
<artifactId>protobuf-maven-plugin</artifactId>
126+
<version>3.4.2</version>
127+
<configuration>
128+
<protocVersion>${protobuf-java.version}</protocVersion>
129+
<binaryMavenPlugins>
130+
<binaryMavenPlugin>
131+
<groupId>io.grpc</groupId>
132+
<artifactId>protoc-gen-grpc-java</artifactId>
133+
<version>${grpc.version}</version>
134+
<options>@generated=omit</options>
135+
</binaryMavenPlugin>
136+
</binaryMavenPlugins>
137+
</configuration>
138+
<executions>
139+
<execution>
140+
<goals>
141+
<goal>generate</goal>
142+
</goals>
143+
</execution>
144+
</executions>
145+
</plugin>
146+
</plugins>
147+
</build>
148+
149+
<repositories>
150+
<repository>
151+
<id>spring-milestones</id>
152+
<name>Spring Milestones</name>
153+
<url>https://repo.spring.io/milestone</url>
154+
<snapshots>
155+
<enabled>false</enabled>
156+
</snapshots>
157+
</repository>
158+
<repository>
159+
<id>spring-snapshots</id>
160+
<name>Spring Snapshots</name>
161+
<url>https://repo.spring.io/snapshot</url>
162+
<releases>
163+
<enabled>false</enabled>
164+
</releases>
165+
</repository>
166+
</repositories>
167+
<pluginRepositories>
168+
<pluginRepository>
169+
<id>spring-milestones</id>
170+
<name>Spring Milestones</name>
171+
<url>https://repo.spring.io/milestone</url>
172+
<snapshots>
173+
<enabled>false</enabled>
174+
</snapshots>
175+
</pluginRepository>
176+
<pluginRepository>
177+
<id>spring-snapshots</id>
178+
<name>Spring Snapshots</name>
179+
<url>https://repo.spring.io/snapshot</url>
180+
<releases>
181+
<enabled>false</enabled>
182+
</releases>
183+
</pluginRepository>
184+
</pluginRepositories>
185+
186+
187+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.springframework.grpc.sample;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class GrpcServerApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(GrpcServerApplication.class, args);
11+
}
12+
13+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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.stereotype.Service;
9+
10+
import io.grpc.stub.StreamObserver;
11+
12+
@Service
13+
public class GrpcServerService extends SimpleGrpc.SimpleImplBase {
14+
15+
private static Log log = LogFactory.getLog(GrpcServerService.class);
16+
17+
@Override
18+
public void sayHello(HelloRequest req, StreamObserver<HelloReply> responseObserver) {
19+
log.info("Hello " + req.getName());
20+
if (req.getName().startsWith("error")) {
21+
throw new IllegalArgumentException("Bad name: " + req.getName());
22+
}
23+
if (req.getName().startsWith("internal")) {
24+
throw new RuntimeException();
25+
}
26+
HelloReply reply = HelloReply.newBuilder().setMessage("Hello ==> " + req.getName()).build();
27+
responseObserver.onNext(reply);
28+
responseObserver.onCompleted();
29+
}
30+
31+
@Override
32+
public void streamHello(HelloRequest req, StreamObserver<HelloReply> responseObserver) {
33+
log.info("Hello " + req.getName());
34+
int count = 0;
35+
while (count < 10) {
36+
HelloReply reply = HelloReply.newBuilder().setMessage("Hello(" + count + ") ==> " + req.getName()).build();
37+
responseObserver.onNext(reply);
38+
count++;
39+
try {
40+
Thread.sleep(1000L);
41+
}
42+
catch (InterruptedException e) {
43+
Thread.currentThread().interrupt();
44+
responseObserver.onError(e);
45+
return;
46+
}
47+
}
48+
responseObserver.onCompleted();
49+
}
50+
51+
}

0 commit comments

Comments
 (0)