Quick start
This guide gets you started with gRPC in Java with a simple working example.
Quick start
Prerequisites
- JDK version 7 or higher
Get the example code
The example code is part of the grpc-java repo.
-
Download the repo as a zip file and unzip it, or clone the repo:
$ git clone -b v1.42.1 https://github.com/grpc/grpc-java
-
Change to the examples directory:
$ cd grpc-java/examples
Run the example
From the examples
directory:
-
Compile the client and server
$ ./gradlew installDist
-
Run the server:
$ ./build/install/examples/bin/hello-world-server INFO: Server started, listening on 50051
-
From another terminal, run the client:
$ ./build/install/examples/bin/hello-world-client INFO: Will try to greet world ... INFO: Greeting: Hello world
Congratulations! You’ve just run a client-server application with gRPC.
Note
We’ve omitted timestamps from the client and server trace output shown in this page.Update the gRPC service
In this section you’ll update the application by adding an extra server method.
The gRPC service is defined using protocol buffers. To learn more about
how to define a service in a .proto
file see Basics tutorial. For now, all
you need to know is that both the server and the client stub have a SayHello()
RPC method that takes a HelloRequest
parameter from the client and returns a
HelloReply
from the server, and that the method is defined like this:
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}
Open src/main/proto/helloworld.proto
and add a new SayHelloAgain()
method
with the same request and response types as SayHello()
:
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
// Sends another greeting
rpc SayHelloAgain (HelloRequest) returns (HelloReply) {}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}
Remember to save the file!
Update the app
When you build the example, the build process regenerates GreeterGrpc.java
,
which contains the generated gRPC client and server classes. This also
regenerates classes for populating, serializing, and retrieving our request and
response types.
However, you still need to implement and call the new method in the hand-written parts of the example app.
Update the server
In the same directory, open
src/main/java/io/grpc/examples/helloworld/HelloWorldServer.java
. Implement the
new method like this:
private class GreeterImpl extends GreeterGrpc.GreeterImplBase {
@Override
public void sayHello(HelloRequest req, StreamObserver<HelloReply> responseObserver) {
HelloReply reply = HelloReply.newBuilder().setMessage("Hello " + req.getName()).build();
responseObserver.onNext(reply);
responseObserver.onCompleted();
}
@Override
public void sayHelloAgain(HelloRequest req, StreamObserver<HelloReply> responseObserver) {
HelloReply reply = HelloReply.newBuilder().setMessage("Hello again " + req.getName()).build();
responseObserver.onNext(reply);
responseObserver.onCompleted();
}
}
Update the client
In the same directory, open
src/main/java/io/grpc/examples/helloworld/HelloWorldClient.java
. Call the new
method like this:
public void greet(String name) {
logger.info("Will try to greet " + name + " ...");
HelloRequest request = HelloRequest.newBuilder().setName(name).build();
HelloReply response;
try {
response = blockingStub.sayHello(request);
} catch (StatusRuntimeException e) {
logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus());
return;
}
logger.info("Greeting: " + response.getMessage());
try {
response = blockingStub.sayHelloAgain(request);
} catch (StatusRuntimeException e) {
logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus());
return;
}
logger.info("Greeting: " + response.getMessage());
}
Run the updated app
Run the client and server like you did before. Execute the following commands
from the examples
directory:
-
Compile the client and server:
$ ./gradlew installDist
-
Run the server:
$ ./build/install/examples/bin/hello-world-server INFO: Server started, listening on 50051
-
From another terminal, run the client:
$ ./build/install/examples/bin/hello-world-client INFO: Will try to greet world ... INFO: Greeting: Hello world INFO: Greeting: Hello again world
What’s next
- Learn how gRPC works in Introduction to gRPC and Core concepts.
- Work through the Basics tutorial.
- Explore the API reference.