Skip to content

Commit

Permalink
Add servlet server starter module
Browse files Browse the repository at this point in the history
This adds a starter module that includes the Spring gRPC server
starter and the required dependencies ((`spring-boot-starter-web`
and `jakarta-grpc-servlet`) to use the servlet container as a
gRPC server.

Signed-off-by: Chris Bono <[email protected]>
  • Loading branch information
onobc committed Feb 7, 2025
1 parent 8e7fd90 commit 2b43eb2
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 14 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<module>spring-grpc-spring-boot-autoconfigure</module>
<module>spring-grpc-spring-boot-starter</module>
<module>spring-grpc-client-spring-boot-starter</module>
<module>spring-grpc-server-web-spring-boot-starter</module>
<module>samples</module>
</modules>

Expand Down
10 changes: 10 additions & 0 deletions spring-grpc-dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@
<artifactId>spring-grpc-spring-boot-starter</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.grpc</groupId>
<artifactId>spring-grpc-client-spring-boot-starter</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.grpc</groupId>
<artifactId>spring-grpc-server-web-spring-boot-starter</artifactId>
<version>${project.version}</version>
</dependency>
<!-- Utilities -->
<dependency>
<groupId>org.springframework.grpc</groupId>
Expand Down
20 changes: 6 additions & 14 deletions spring-grpc-docs/src/main/antora/modules/ROOT/pages/server.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,15 @@ dependencies {
== Servlet Server

Any servlet container can be used to run a gRPC server.
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` and the `grpc-servlet` dependnecy in your application.
Spring gRPC includes autoconfiguration that configures the server to use the servlet container if it detects that it is in a web application.
Spring gRPC also provides a convenience starter that includes the required dependencies (`spring-boot-starter-web` and the `grpc-servlet-jakarta`) for this scenario.
So all you have to do is include the `spring-boot-starter-web` dependency as follows:

[source,xml]
----
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.grpc</groupId>
<artifactId>spring-grpc-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-servlet-jakarta</artifactId>
<artifactId>spring-grpc-server-web-spring-boot-starter</artifactId>
</dependency>
----

Expand All @@ -82,9 +76,7 @@ For Gradle users
[source,gradle]
----
dependencies {
implementation "org.springframework.boot:spring-boot-starter-web"
implementation "org.springframework.grpc:spring-grpc-spring-boot-starter"
implementation "io.grpc:grpc-servlet-jakarta"
implementation "org.springframework.grpc:spring-grpc-server-web-spring-boot-starter"
}
----

Expand Down Expand Up @@ -289,4 +281,4 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
}
----

Here we allow access to the `Simple/SayHello` method to users with the `USER` role, and to the `Simple/StreamHello` method to users with the `ADMIN` role, and allow access to all gRPC-provided services (like reflection and health indicators), while disallowing access to all other methods unless authenticated.
Here we allow access to the `Simple/SayHello` method to users with the `USER` role, and to the `Simple/StreamHello` method to users with the `ADMIN` role, and allow access to all gRPC-provided services (like reflection and health indicators), while disallowing access to all other methods unless authenticated.
32 changes: 32 additions & 0 deletions spring-grpc-server-web-spring-boot-starter/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.grpc</groupId>
<artifactId>spring-grpc</artifactId>
<version>0.4.0-SNAPSHOT</version>
</parent>
<artifactId>spring-grpc-server-web-spring-boot-starter</artifactId>
<packaging>jar</packaging>
<name>Spring gRPC Servlet Server Spring Boot Starter</name>
<description>Spring gRPC Servlet Server Spring Boot Starter</description>

<dependencies>
<dependency>
<groupId>org.springframework.grpc</groupId>
<artifactId>spring-grpc-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-servlet-jakarta</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring-boot.version}</version>
</dependency>
</dependencies>

</project>

0 comments on commit 2b43eb2

Please sign in to comment.