Skip to content

Commit b3271be

Browse files
author
Dave Syer
committed
Add explicit support for running on separate port
If the user wants a second port for gRPC that should be possible (and was the only way with the old ecosystem projects). This change adds a webflux sample and support for spring.grpc.server.{servlet,reactive}.enabled to be explicitly "false" to disable the embedded services. N.B. embedded services are not yet supported in webflux so the flag has no effect, except to disable gRPC completely if it is "true".
1 parent 133bfec commit b3271be

File tree

18 files changed

+961
-32
lines changed

18 files changed

+961
-32
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package org.springframework.grpc.sample;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.apache.commons.logging.Log;
6+
import org.apache.commons.logging.LogFactory;
7+
import org.junit.jupiter.api.Test;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.boot.builder.SpringApplicationBuilder;
10+
import org.springframework.boot.test.context.SpringBootTest;
11+
import org.springframework.boot.test.context.TestConfiguration;
12+
import org.springframework.context.annotation.Bean;
13+
import org.springframework.context.annotation.Lazy;
14+
import org.springframework.grpc.client.GrpcChannelFactory;
15+
import org.springframework.grpc.sample.proto.HelloReply;
16+
import org.springframework.grpc.sample.proto.HelloRequest;
17+
import org.springframework.grpc.sample.proto.SimpleGrpc;
18+
import org.springframework.grpc.test.LocalGrpcPort;
19+
import org.springframework.test.annotation.DirtiesContext;
20+
21+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
22+
properties = { "spring.grpc.server.servlet.enabled=false", "spring.grpc.server.port=0" })
23+
public class ListenOnTwoPortsTests {
24+
25+
private static Log log = LogFactory.getLog(ListenOnTwoPortsTests.class);
26+
27+
public static void main(String[] args) {
28+
new SpringApplicationBuilder(GrpcServerApplication.class, ExtraConfiguration.class)
29+
.run("--spring.grpc.server.servlet.enabled=false", "--spring.grpc.server.port=9091");
30+
}
31+
32+
@Autowired
33+
private SimpleGrpc.SimpleBlockingStub stub;
34+
35+
@Test
36+
@DirtiesContext
37+
void contextLoads() {
38+
}
39+
40+
@Test
41+
@DirtiesContext
42+
void serverResponds() {
43+
log.info("Testing");
44+
HelloReply response = stub.sayHello(HelloRequest.newBuilder().setName("Alien").build());
45+
assertEquals("Hello ==> Alien", response.getMessage());
46+
}
47+
48+
@TestConfiguration
49+
static class ExtraConfiguration {
50+
51+
@Bean
52+
@Lazy
53+
SimpleGrpc.SimpleBlockingStub stub(GrpcChannelFactory channels, @LocalGrpcPort int port) {
54+
return SimpleGrpc.newBlockingStub(channels.createChannel("0.0.0.0:" + port).build());
55+
}
56+
57+
}
58+
59+
}

samples/grpc-webflux/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-webflux/build.gradle

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
plugins {
2+
id 'java'
3+
id 'org.springframework.boot' version '3.4.0'
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.3.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.3.0-SNAPSHOT'
28+
}
29+
}
30+
31+
dependencies {
32+
implementation 'org.springframework.grpc:spring-grpc-spring-boot-starter'
33+
implementation 'org.springframework.boot:spring-boot-starter-webflux'
34+
implementation 'io.grpc:grpc-services'
35+
testImplementation 'org.springframework.boot:spring-boot-starter-test'
36+
testImplementation 'org.springframework.grpc:spring-grpc-test'
37+
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
38+
}
39+
40+
tasks.named('test') {
41+
useJUnitPlatform()
42+
}
43+
44+
protobuf {
45+
protoc {
46+
artifact = "com.google.protobuf:protoc:${dependencyManagement.importedProperties['protobuf-java.version']}"
47+
}
48+
plugins {
49+
grpc {
50+
artifact = "io.grpc:protoc-gen-grpc-java:${dependencyManagement.importedProperties['grpc.version']}"
51+
}
52+
}
53+
generateProtoTasks {
54+
all()*.plugins {
55+
grpc {
56+
option 'jakarta_omit'
57+
option '@generated=omit'
58+
}
59+
}
60+
}
61+
}
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)