|
|
11 years ago | |
|---|---|---|
| java | 11 years ago | |
| protos | 11 years ago | |
| .gitignore | 11 years ago | |
| LICENSE | 11 years ago | |
| README.md | 11 years ago |
Welcome to the developer documentation for gRPC, a language-neutral, platform-neutral remote procedure call (RPC) system developed at Google.
This document introduces you to gRPC with a quick overview and a simple Hello World example. More documentation is coming soon!
Now that you know a bit more about gRPC, the easiest way to see how it works is to look at a simple example. Our Hello World walks you through the construction of a simple gRPC client-server application, showing you how to:
The complete code for the example is available in the grpc-common GitHub repository. You can
work along with the example and hack on the code in the comfort of your own
computer, giving you hands-on practice of really writing
gRPC code. We use the Git versioning system for source code management:
however, you don't need to know anything about Git to follow along other
than how to install and run a few git commands.
This is an introductory example rather than a comprehensive tutorial, so don't worry if you're not a Go or Java developer - complete tutorials and reference documentation for all gRPC languages are coming soon.
The rest of this page explains how to set up your local machine to work with the example code. If you just want to read the example, you can go straight to the next step.
You can download and install Git from http://git-scm.com/download. Once installed you should have access to the git command line tool. The main commands that you will need to use are:
The example code for this and our other examples lives in the grpc-common GitHub repository. Clone this repository to your local machine by running the
following command:
git clone https://github.com/google/grpc-common.git
Change your current directory to grpc-common/java
cd grpc-common/java
Java gRPC is designed to work with both Java 7 and Java 8 - our example uses Java 8. See Install Java 8 for instructions if you need to install Java 8.
To simplify building and managing gRPC's dependencies, the Java client and server are structured as a standard Maven project. See Install Maven for instructions.
Go gRPC requires Go 1.4, the latest version of Go. See Install Go for instructions.
gRPC uses the latest version of the protocol buffer compiler, protoc.
Having protoc installed isn't strictly necessary to follow along with this example, as all the generated code is checked into the Git repository. However, if you want to experiment with generating the code yourself, download and install protoc from its Git repo
The first step in creating our example is to define a service: an RPC service specifies the methods that can be called remotely with their parameters and return types. In gRPC, we use the protocol buffers interface definition language (IDL) to define our service methods, and the parameters and return types are defined as protocol buffer message types. Both the client and the server use interface code generated from the service definition. If you're not familiar with protocol buffers, you can find out more in the Protocol Buffers Developer Guide.
Here's our example service definition, defined using protocol buffers IDL in
helloworld.proto should we link to the version in the Java subdirectory or the one in the common protos directory?. The Greeting service
has one method, hello, that lets the server receive a single HelloRequest
message from the remote client containing the user's name, then send back
a greeting in a HelloReply.
syntax = "proto3";
option java_package = "ex.grpc";
package helloworld;
// The request message containing the user's name.
message HelloRequest {
optional string name = 1;
}
// The response message containing the greetings
message HelloReply {
optional string message = 1;
}
// The greeting service definition.
service Greeting {
// Sends a greeting
rpc hello (HelloRequest) returns (HelloReply) {
}
}
Once we've defined our service, we use the protocol buffer compiler
protoc to generate the special client and server code we need to create
our application - right now we're going to generate Java code, though you
can generate gRPC code in any gRPC-supported language (as you'll see later
in this example). The generated code contains both stub code for clients to
use and an abstract interface for servers to implement, both with the method
defined in our Greeting service. A stub is code that initiates contact
with a gRPC service running remotely via the internet. [can probably define
this up in "what is gRPC"?]
(If you didn't install protoc on your system and are working along with
the example, you can skip this step and move
onto the next one where we examine the generated code.)
As this is our first time using gRPC, we need to build the protobuf plugin that generates our RPC
classes. By default protoc just generates code for reading and writing
protocol buffers, so you need to use plugins to add additional features
to generated code. As we're creating Java code, we use the gRPC Java plugin.
To build the plugin:
$ pushd external/grpc_java
$ make java_plugin
$ popd
To use it to generate the code:
$ mkdir -p src/main/java
$ protoc -I . helloworld.proto
--plugin=protoc-gen-grpc=external/grpc_java/bins/opt/java_plugin \
--grpc_out=src/main/java \
--java_out=src/main/java
This generates the following classes, which contain all the generated code we need to create our example:
Helloworld.java, which has all the protocol buffer code to populate, serialize, and retrieve our HelloRequest and HelloReply message typesGreetingsGrpc.java, which contains (along with some other useful code):
an interface for Greetings servers to implement
public static interface Greetings {
public void hello(ex.grpc.Helloworld.HelloRequest request,
com.google.net.stubby.stub.StreamObserver<ex.grpc.Helloworld.HelloReply> responseObserver);
}
stub classes that clients can use to talk to a Greetings server.
public static class GreetingsStub extends
com.google.net.stubby.stub.AbstractStub<GreetingsStub, GreetingsServiceDescriptor>
implements Greetings {
...
}
Now let's write some code! First we'll create a server application to implement our service. Note that we're not going to go into a lot of detail about how to create a server in this section More detailed information will be in the tutorial for your chosen language (coming soon).
Our server application has two classes:
a simple service implementation GreetingsImpl.java.
a server that hosts the service implementation and allows access over the network: GreetingsServer.java.
GreetingsImpl.java implements the behaviour we require of our GreetingService. There are a number of important features of gRPC being used here:
public void hello(Helloworld.HelloRequest req,
StreamObserver<Helloworld.HelloReply> responseObserver) {
Helloworld.HelloReply reply = Helloworld.HelloReply.newBuilder().setMessage(
"Hello " + req.getName()).build();
responseObserver.onValue(reply);
responseObserver.onCompleted();
}
GreetingsImpl that implements a generated interface GreetingsGrpc.GreetingsGreetingsGrpc.Greetings declares the method hello that was declared in the proto IDLhello's signature is typesafe:
hello(Helloworld.HelloRequest req, StreamObserver responseObserver)
hello takes two parameters:
Helloworld.HelloRequest: the request
StreamObserver<Helloworld.HelloReply>: a response observer, an interface to be called with the response valueGreetingsServer.java shows the other main feature required to provde the gRPC service; how to allow a service implementation to be accessed from the network.
private void start() throws Exception {
server = NettyServerBuilder.forPort(port)
.addService(GreetingsGrpc.bindService(new GreetingsImpl()))
.build();
server.startAsync();
server.awaitRunning(5, TimeUnit.SECONDS);
}
GreetingsServer that holds a ServerImpl that will run the serverstart method, GreetingServer binds the GreetingsService implementation to a port and begins running itstop method that takes care of shutting down the service and cleaning up when the program exitsOnce we've implemented everything, we use Maven to build the server:
$ mvn package
We'll look at using a client to access the server in the next section.
Client-side gRPC is pretty simple. In this step, we'll use the generated code to write a simple client that can access the Greetings server we created in the previous section. You can see the complete client code in GreetingsClient.java.
Again, we're not going to go into much detail about how to implement a client - we'll leave that for the tutorial.
. The internet address is configured in the client constructor. gRPC Channel is the abstraction over transport handling; its constructor accepts the host name and port of the service. The channel in turn is used to construct the Stub.
private final ChannelImpl channel;
private final GreetingGrpc.GreetingBlockingStub blockingStub;
public HelloClient(String host, int port) {
channel = NettyChannelBuilder.forAddress(host, port)
.negotiationType(NegotiationType.PLAINTEXT)
.build();
blockingStub = GreetingGrpc.newBlockingStub(channel);
}
The greet method uses the stub to contact the service and obtain a greeting. It:
prints out the greeting
public void greet(String name) {
logger.debug("Will try to greet " + name + " ...");
try {
Helloworld.HelloRequest request = Helloworld.HelloRequest.newBuilder().setName(name).build();
Helloworld.HelloReply reply = blockingStub.hello(request);
logger.info("Greeting: " + reply.getMessage());
} catch (RuntimeException e) {
logger.log(Level.WARNING, "RPC failed", e);
return;
}
}
The main method puts together the example so that it can be run from a command line.
/* Access a service running on the local machine on port 50051 */
HelloClient client = new HelloClient("localhost", 50051);
String user = "world";
if (args.length > 1) {
user = args[1];
}
client.greet(user);
This is the same as before: our client and server are part of the same maven package so the same command builds both.
$ mvn package
The client uses a blocking stub. This means that the RPC call waits for the server to respond, and will either return a response or raise an exception.
gRPC Java has other kinds of stubs that make non-blocking calls to the server, where the response is returned asynchronously. Usage of these stubs is a more advanced topic and will be described in later steps.
We've added simple shell scripts to simplifying running the examples. Now that they are built, you can run the server with:
$ ./run_greetings_server.sh
and in another terminal window confirm that it receives a message.
$ ./run_greetings_client.sh