Skip to content

Commit 6be591d

Browse files
author
Dave Syer
committed
Add some basic docs
1 parent b5a6232 commit 6be591d

File tree

6 files changed

+194
-9
lines changed

6 files changed

+194
-9
lines changed
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
* xref:index.adoc[Overview]
2-
* xref:concepts.adoc[GRPC Concepts]
32
* xref:getting-started.adoc[Getting Started]
3+
* xref:server.adoc[GRPC Server]
4+
* xref:client.adoc[GRPC Clients]
45
* xref:contribution-guidelines.adoc[Contribution Guidelines]
56
* xref:appendix.adoc[]
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
[[client]]
2+
= GRPC Client
3+
4+
This section describes core concepts that Spring gRPC uses on the client side.
5+
6+
== Create a `Channel`
7+
8+
You can inject a `GrpcChannelFactory` into your application configuration and use it to create a gRPC channel.
9+
The most common usage of a channel is to create a client that binds to a service.
10+
The Protobuf-generated sources in your project will contain the stub classes, and they just need to be bound to a channel.
11+
The Protobuf files will be provided by the service you are connecting to.
12+
For example, consider the `SimpleGrpc` service generated by the Protobuf tooling in your build.
13+
To bind to this service on a local server:
14+
15+
[source,java]
16+
----
17+
@Bean
18+
SimpleGrpc.SimpleBlockingStub stub(GrpcChannelFactory channels) {
19+
return SimpleGrpc.newBlockingStub(channels.createChannel("0.0.0.0:9090").build());
20+
}
21+
----
22+
23+
The `GrpcChannelFactory` creates a `ChannelBuilder` that you can customize before building the channel if necessary.
24+
25+
=== Shaded Netty Client
26+
27+
The default client implementation uses the Netty client.
28+
You can switch to a shaded Netty implementation provided by the gRPC team by adding the `grpc-netty-shaded` dependency and excluding the `grpc-netty` dependency.
29+
30+
[source,xml]
31+
----
32+
<dependency>
33+
<groupId>org.springframework.grpc</groupId>
34+
<artifactId>spring-grpc-spring-boot-starter</artifactId>
35+
<exclusions>
36+
<exclusion>
37+
<groupId>io.grpc</groupId>
38+
<artifactId>grpc-netty</artifactId>
39+
</exclusion>
40+
</exclusions>
41+
</dependency>
42+
<dependency>
43+
<groupId>io.grpc</groupId>
44+
<artifactId>grpc-netty-shaded</artifactId>
45+
</dependency>
46+
----
47+
48+
For Gradle users
49+
50+
[source,gradle]
51+
----
52+
dependencies {
53+
implementation "org.springframework.grpc:spring-grpc-spring-boot-starter"
54+
modules {
55+
module("io.grpc:grpc-netty") {
56+
replacedBy("io.grpc:grpc-netty-shaded", "Use Netty shaded instead of regular Netty")
57+
}
58+
}
59+
}
60+
----
61+
62+
== Channel Configuration
63+
64+
The default `GrpcChannelFactory` implementation can also create a "named" channel, which you can then use to extract the configuration to connect to the server.
65+
For example:
66+
67+
[source,java]
68+
----
69+
@Bean
70+
SimpleGrpc.SimpleBlockingStub stub(GrpcChannelFactory channels) {
71+
return SimpleGrpc.newBlockingStub(channels.createChannel("local").build());
72+
}
73+
----
74+
75+
then in `application.properties`:
76+
77+
[source,properties]
78+
----
79+
spring.grpc.client.channels.local.address=0.0.0.0:9090
80+
----
81+
82+
There is a default named channel (named "default") that you can configure in the same way, and then it will be used by default if there is no channel with the name specified in the channel creation.
83+
84+
Beans of type `GrpcChannelConfigurer` can be used to customize the `ChannelBuilder` before the channel is built.
85+
This can be useful for setting up security, for example.
86+
87+
== The Local Server Port
88+
89+
If you are running a gRPC server locally as part of your application, you will often want to connect to it in an integration test.
90+
It can be convenient in that case to use an ephemeral port for the server (`spring.grpc.server.port=0`) and then use the port that is allocated to connect to it.
91+
You can discover the port that the server is running on by injecting the `@LocalGrpcPort` bean into your test.
92+
The `@Bean` has to be marked as `@Lazy` to ensure that the port is available when the bean is created (it is only known when the server starts which is part of the startup process).
93+
94+
[source,java]
95+
----
96+
@Bean
97+
@Lazy
98+
SimpleGrpc.SimpleBlockingStub stub(GrpcChannelFactory channels, @LocalGrpcPort int port) {
99+
return SimpleGrpc.newBlockingStub(channels.createChannel("0.0.0.0:" + port).build());
100+
}
101+
----

spring-grpc-docs/src/main/antora/modules/ROOT/pages/concepts.adoc

Lines changed: 0 additions & 4 deletions
This file was deleted.

spring-grpc-docs/src/main/antora/modules/ROOT/pages/getting-started.adoc

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

spring-grpc-docs/src/main/antora/modules/ROOT/pages/index.adoc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33

44
The `Spring gRPC` project aims to streamline the development of gRPC applications.
55

6-
The xref:concepts.adoc[concepts section] provides a high-level overview of gRPC concepts and their representation in Spring gRPC.
7-
86
The xref:getting-started.adoc[Getting Started] section shows you how to create your first gRPC application.
97
Subsequent sections delve into each component and common use cases with a code-focused approach.
8+
9+
The xref:server.adoc[server section] provides a high-level overview of gRPC servers and their representation in Spring gRPC.
10+
11+
The xref:server.adoc[client section] does the same for gRPC clients. A gRPC application can be a client and a server at the same time.
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
[[server]]
2+
= GRPC Server
3+
4+
This section describes core concepts that Spring gRPC uses on the server side. We recommend reading it closely to understand the ideas behind how Spring gRPC is implemented.
5+
You only need to provide one or more beans of type `BindableService` to create a gRPC server, provided the classpath contains an implementation of a `Server`. The `BindableService` is a gRPC service that can be bound to a server.
6+
The `Server` is the gRPC server that listens for incoming requests and routes them to the appropriate service implementation.
7+
8+
== Create a `BindableService`
9+
10+
To create a gRPC server, you need to provide one or more beans of type `BindableService`.
11+
There are some `BindableServices` available off the shelf that you could include in your application (an example is the reflection service from the `grpc-services` artifact which allows clients to browse the metadata of your services and download the Portobuf files).
12+
Very commonly, you will create your own `BindableService` by extending the generated service implementation from your Protobuf file.
13+
The easiest way to do it is to simply add a Spring `@Service` annotation to the implementation class and have it picked up by the `@ComponentScan` in your Spring Boot application.
14+
15+
== Netty Server
16+
17+
If you use the `spring-grpc-spring-boot-starter` dependency on its own, the `Server` is a Netty-based implementation.
18+
You can configure common features of the server by using the `grpc.server` prefix in `application.properties` or `application.yml`.
19+
For instance, to set the port to listen on, use `spring.grpc.server.port` (defaults to 9090).
20+
For more specialized configuration, you can provide a `ServerBuilderCustomizer` bean to customize the `ServerBuilder` before it is used to create the server.
21+
22+
=== Shaded Netty
23+
24+
You can switch to a shaded Netty provided by the gRPC team by adding the `grpc-netty-shaded` dependency and excluding the `grpc-netty` dependency.
25+
26+
[source,xml]
27+
----
28+
<dependency>
29+
<groupId>org.springframework.grpc</groupId>
30+
<artifactId>spring-grpc-spring-boot-starter</artifactId>
31+
<exclusions>
32+
<exclusion>
33+
<groupId>io.grpc</groupId>
34+
<artifactId>grpc-netty</artifactId>
35+
</exclusion>
36+
</exclusions>
37+
</dependency>
38+
<dependency>
39+
<groupId>io.grpc</groupId>
40+
<artifactId>grpc-netty-shaded</artifactId>
41+
</dependency>
42+
----
43+
44+
For Gradle users
45+
46+
[source,gradle]
47+
----
48+
dependencies {
49+
implementation "org.springframework.grpc:spring-grpc-spring-boot-starter"
50+
modules {
51+
module("io.grpc:grpc-netty") {
52+
replacedBy("io.grpc:grpc-netty-shaded", "Use Netty shaded instead of regular Netty")
53+
}
54+
}
55+
}
56+
----
57+
58+
== Servlet Server
59+
60+
Any servlet container can be used to run a gRPC server.
61+
Spring gRPC includes autoconfiguration that configures the server to use the servlet container if it detects that it is in a web application, so all you have to do is include `spring-boot-starter-web` in your application.
62+
63+
[source,xml]
64+
----
65+
<dependency>
66+
<groupId>org.springframework.boot</groupId>
67+
<artifactId>spring-boot-starter-web</artifactId>
68+
</dependency>
69+
<dependency>
70+
<groupId>org.springframework.grpc</groupId>
71+
<artifactId>spring-grpc-spring-boot-starter</artifactId>
72+
</dependency>
73+
----
74+
75+
For Gradle users
76+
77+
[source,gradle]
78+
----
79+
dependencies {
80+
implementation "org.springframework.boot:spring-boot-starter-web"
81+
implementation "org.springframework.grpc:spring-grpc-spring-boot-starter"
82+
}
83+
----
84+
85+
The `spring.grpc.server.*` properties will be ignored in facour of the regular `server.*` properties in this case.
86+
The servlet that is created is mapped to process HTTP POST requests to the paths defined by the registered services, as `/<service-name>/*`.
87+
Clients can connect to the server using that path, which is what any gRPC client library will do.

0 commit comments

Comments
 (0)