Skip to content

Commit ec8a153

Browse files
committed
- group rpc implementation
- update documentation - added missing grpc module-info
1 parent d02c15a commit ec8a153

File tree

75 files changed

+244
-157
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+244
-157
lines changed

docs/asciidoc/gRPC.adoc

Lines changed: 65 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
== gRPC
1+
==== gRPC
22

33
The `jooby-grpc` module provides first-class, native support for https://grpc.io/[gRPC].
44

55
Unlike traditional setups that require spinning up a separate gRPC server on a different port (often forcing a specific transport like Netty), this module embeds the `grpc-java` engine directly into Jooby.
66

77
By using a custom native bridge, it allows you to run strictly-typed gRPC services alongside your standard REST API routes on the **exact same port**. It bypasses the standard HTTP/1.1 pipeline in favor of a highly optimized, native interceptor tailored for HTTP/2 multiplexing, reactive backpressure, and zero-copy byte framing. It works natively across Undertow, Netty, and Jetty.
88

9-
=== Dependency
9+
===== Usage
10+
11+
gRPC strictly requires HTTP/2. Before installing the module, ensure your application is configured to use a supported server with HTTP/2 enabled.
1012

1113
[source, xml, role="primary"]
1214
.Maven
@@ -24,11 +26,8 @@ By using a custom native bridge, it allows you to run strictly-typed gRPC servic
2426
implementation 'io.jooby:jooby-grpc:${jooby.version}'
2527
----
2628

27-
=== Usage
28-
29-
gRPC strictly requires HTTP/2. Before installing the module, ensure your application is configured to use a supported server with HTTP/2 enabled.
30-
31-
[source, java]
29+
[source, java, role="primary"]
30+
.Java
3231
----
3332
import io.jooby.Jooby;
3433
import io.jooby.ServerOptions;
@@ -44,21 +43,37 @@ public class App extends Jooby {
4443
4544
get("/api/health", ctx -> "OK"); // <3>
4645
}
47-
48-
public static void main(String[] args) {
49-
runApp(args, App::new);
50-
}
5146
}
5247
----
48+
49+
[source, kotlin, role="secondary"]
50+
.Kotlin
51+
----
52+
import io.jooby.ServerOptions
53+
import io.jooby.grpc.GrpcModule
54+
import io.jooby.kt.Kooby
55+
56+
class App : Kooby({
57+
serverOptions = ServerOptions().setHttp2(true) // <1>
58+
59+
install(GrpcModule( // <2>
60+
GreeterService()
61+
))
62+
63+
get("/api/health") { "OK" } // <3>
64+
})
65+
----
66+
5367
<1> Enable HTTP/2 on your server.
5468
<2> Install the module and explicitly register your services.
5569
<3> Standard REST routes still work on the exact same port!
5670

57-
=== Dependency Injection
71+
===== Dependency Injection
5872

5973
If your gRPC services require external dependencies (like database repositories), you can register the service classes instead of pre-instantiated objects. The module will automatically provision them using your active Dependency Injection framework (e.g., Guice, Spring).
6074

61-
[source, java]
75+
[source, java, role="primary"]
76+
.Java
6277
----
6378
import io.jooby.Jooby;
6479
import io.jooby.di.GuiceModule;
@@ -74,19 +89,38 @@ public class App extends Jooby {
7489
}
7590
}
7691
----
92+
93+
[source, kotlin, role="secondary"]
94+
.Kotlin
95+
----
96+
import io.jooby.di.GuiceModule
97+
import io.jooby.grpc.GrpcModule
98+
import io.jooby.kt.Kooby
99+
100+
class App : Kooby({
101+
install(GuiceModule())
102+
103+
install(GrpcModule(
104+
GreeterService::class.java // <1>
105+
))
106+
})
107+
----
77108
<1> Pass the class references. The DI framework will instantiate them.
78109

79110
WARNING: gRPC services are registered as **Singletons**. Ensure your service implementations are thread-safe and do not hold request-scoped state in instance variables. Heavy blocking operations will safely run on background workers, protecting the native server's I/O event loops.
80111

81-
=== Server Reflection
112+
===== Server Reflection
82113

83114
If you want to use tools like `grpcurl` or Postman to interact with your services without providing the `.proto` files, you can easily enable gRPC Server Reflection.
84115

85116
Include the `grpc-services` dependency in your build, and register the v1 reflection service alongside your own:
86117

87-
[source, java]
118+
[source, java, role="primary"]
119+
.Java
88120
----
89121
import io.grpc.protobuf.services.ProtoReflectionServiceV1;
122+
import io.jooby.Jooby;
123+
import io.jooby.grpc.GrpcModule;
90124
91125
public class App extends Jooby {
92126
{
@@ -97,9 +131,24 @@ public class App extends Jooby {
97131
}
98132
}
99133
----
134+
135+
[source, kotlin, role="secondary"]
136+
.Kotlin
137+
----
138+
import io.grpc.protobuf.services.ProtoReflectionServiceV1
139+
import io.jooby.grpc.GrpcModule
140+
import io.jooby.kt.Kooby
141+
142+
class App : Kooby({
143+
install(GrpcModule(
144+
GreeterService(),
145+
ProtoReflectionServiceV1.newInstance() // <1>
146+
))
147+
})
148+
----
100149
<1> Enables the modern `v1` reflection protocol for maximum compatibility with gRPC clients.
101150

102-
=== Routing & Fallbacks
151+
===== Routing & Fallbacks
103152

104153
The gRPC module intercepts requests natively before they reach Jooby's standard router.
105154

docs/asciidoc/json-rpc.adoc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ To expose a JSON-RPC endpoint, annotate your controller or service with `@JsonRp
1111
.JSON-RPC
1212
[source,java,role="primary"]
1313
----
14-
import io.jooby.jsonrpc.JsonRpc;
14+
import io.jooby.rpc.jsonrpc.JsonRpc;
1515
1616
@JsonRpc("movies")
1717
public class MovieService {
@@ -28,7 +28,7 @@ public class MovieService {
2828
.Kotlin
2929
[source,kotlin,role="secondary"]
3030
----
31-
import io.jooby.jsonrpc.JsonRpc
31+
import io.jooby.rpc.jsonrpc.JsonRpc
3232
3333
@JsonRpc("movies")
3434
class MovieService {
@@ -41,7 +41,7 @@ class MovieService {
4141
}
4242
----
4343

44-
When the Jooby APT detects the `@JsonRpc` annotation, it generates a class ending in `Rpc_` (e.g., `MovieServiceRpc_`) that implements the `io.jooby.jsonrpc.JsonRpcService` interface.
44+
When the Jooby APT detects the `@JsonRpc` annotation, it generates a class ending in `Rpc_` (e.g., `MovieServiceRpc_`) that implements the `io.jooby.rpc.jsonrpc.JsonRpcService` interface.
4545

4646
The annotation dictates how the protocol methods are named and exposed:
4747

docs/asciidoc/rpc.adoc

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
1-
=== RPC
1+
=== Remote Procedure Calls (RPC)
2+
3+
While standard REST architectures focus on mapping HTTP methods to resources, Remote Procedure Call (RPC) architectures are designed around executing explicit actions or functions. RPC is often the preferred choice for strictly-typed client-server contracts, high-performance microservice communication, and scenarios where a strict resource-based REST model feels overly restrictive.
4+
5+
Jooby provides first-class support for several RPC protocols. You can run any of these seamlessly alongside your standard HTTP routes on the exact same server instance, sharing the same underlying thread pools and I/O event loops.
6+
7+
Depending on your client ecosystem and performance requirements, Jooby supports:
8+
9+
* **gRPC**: A high-performance, strictly-typed protocol utilizing Protocol Buffers and HTTP/2 multiplexing. Ideal for backend-to-backend communication and polyglot microservices.
10+
* **JSON-RPC**: A lightweight, stateless, and simple JSON-encoded protocol. Excellent for rapid integrations and environments where standard JSON is preferred.
11+
* **tRPC**: A RPC implementation that provides end-to-end type safety directly between your Java backend and TypeScript clients, without requiring intermediate build steps or code generation.
12+
13+
include::gRPC.adoc[]
214

315
include::json-rpc.adoc[]
416

docs/asciidoc/tRPC.adoc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ NOTE: Currently, Jooby only provides the required `TrpcParser` SPI implementatio
1515
----
1616
import io.jooby.Jooby;
1717
import io.jooby.json.JacksonModule;
18-
import io.jooby.trpc.TrpcModule;
18+
import io.jooby.rpc.trpc.TrpcModule;
1919
2020
public class App extends Jooby {
2121
{
@@ -33,7 +33,7 @@ public class App extends Jooby {
3333
----
3434
import io.jooby.kt.Kooby
3535
import io.jooby.json.JacksonModule
36-
import io.jooby.trpc.TrpcModule
36+
import io.jooby.rpc.trpc.TrpcModule
3737
3838
class App : Kooby({
3939
install(JacksonModule()) // <1>
@@ -197,7 +197,7 @@ If you throw custom domain exceptions, you can map them directly to tRPC error c
197197
.Java
198198
[source,java,role="primary"]
199199
----
200-
import io.jooby.trpc.TrpcErrorCode;
200+
import io.jooby.rpc.trpc.TrpcErrorCode;
201201
202202
{
203203
install(new TrpcModule());
@@ -213,8 +213,8 @@ import io.jooby.trpc.TrpcErrorCode;
213213
[source,kotlin,role="secondary"]
214214
----
215215
import io.jooby.kt.Kooby
216-
import io.jooby.trpc.TrpcModule
217-
import io.jooby.trpc.TrpcErrorCode
216+
import io.jooby.rpc.trpc.TrpcModule
217+
import io.jooby.rpc.trpc.TrpcErrorCode
218218
219219
class App : Kooby({
220220
install(TrpcModule())

jooby/src/main/java/io/jooby/Jooby.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@
3535
import io.jooby.internal.MutedServer;
3636
import io.jooby.internal.RegistryRef;
3737
import io.jooby.internal.RouterImpl;
38-
import io.jooby.jsonrpc.JsonRpcModule;
39-
import io.jooby.jsonrpc.JsonRpcService;
4038
import io.jooby.output.OutputFactory;
4139
import io.jooby.problem.ProblemDetailsHandler;
40+
import io.jooby.rpc.jsonrpc.JsonRpcModule;
41+
import io.jooby.rpc.jsonrpc.JsonRpcService;
4242
import io.jooby.value.ValueFactory;
4343

4444
/**

jooby/src/main/java/io/jooby/GrpcExchange.java renamed to jooby/src/main/java/io/jooby/rpc/grpc/GrpcExchange.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
44
* Copyright 2014 Edgar Espina
55
*/
6-
package io.jooby;
6+
package io.jooby.rpc.grpc;
77

88
import java.nio.ByteBuffer;
99
import java.util.Map;

jooby/src/main/java/io/jooby/GrpcProcessor.java renamed to jooby/src/main/java/io/jooby/rpc/grpc/GrpcProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
44
* Copyright 2014 Edgar Espina
55
*/
6-
package io.jooby;
6+
package io.jooby.rpc.grpc;
77

88
import java.nio.ByteBuffer;
99
import java.util.concurrent.Flow;

jooby/src/main/java/io/jooby/jsonrpc/JsonRpcDecoder.java renamed to jooby/src/main/java/io/jooby/rpc/jsonrpc/JsonRpcDecoder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
44
* Copyright 2014 Edgar Espina
55
*/
6-
package io.jooby.jsonrpc;
6+
package io.jooby.rpc.jsonrpc;
77

88
/**
99
* A pre-resolved decoder used at runtime to deserialize JSON-RPC parameter nodes into complex Java

jooby/src/main/java/io/jooby/jsonrpc/JsonRpcErrorCode.java renamed to jooby/src/main/java/io/jooby/rpc/jsonrpc/JsonRpcErrorCode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
44
* Copyright 2014 Edgar Espina
55
*/
6-
package io.jooby.jsonrpc;
6+
package io.jooby.rpc.jsonrpc;
77

88
import io.jooby.StatusCode;
99

jooby/src/main/java/io/jooby/jsonrpc/JsonRpcException.java renamed to jooby/src/main/java/io/jooby/rpc/jsonrpc/JsonRpcException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
44
* Copyright 2014 Edgar Espina
55
*/
6-
package io.jooby.jsonrpc;
6+
package io.jooby.rpc.jsonrpc;
77

88
/**
99
* Exception thrown when a JSON-RPC error occurs during routing, parsing, or execution.

0 commit comments

Comments
 (0)