Here's our example service definition, defined using protocol buffers IDL in
[helloworld.proto](java/src/main/proto/helloworld.proto)_should we link to the version in the Java subdirectory or the one in the common protos directory?_. The `Greeting` service
[helloworld.proto](java/src/main/proto/helloworld.proto). 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`.
a greeting in a single `HelloReply`. This is the simplest type of RPC you
can specify in gRPC - we'll look at some other types later in this document.
```
syntax = "proto3";
@ -167,7 +168,8 @@ this up in "what is gRPC"?]
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
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.
This generates the following classes, which contain all the generated code we need to create our example:
This generates the following classes, which contain all the generated code
we need to create our example:
- [`Helloworld.java`](java/src/main/java/ex/grpc/Helloworld.java), which has all the protocol buffer code to populate, serialize, and retrieve our `HelloRequest` and `HelloReply` message types
- [`GreetingsGrpc.java`](java/src/main/java/ex/grpc/GreetingsGrpc.java), which contains (along with some other useful code):
- [`Helloworld.java`](java/src/main/java/ex/grpc/Helloworld.java), which
has all the protocol buffer code to populate, serialize, and retrieve our
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).
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](java/src/main/java/ex/grpc/GreetingsImpl.java).
- a server that hosts the service implementation and allows access over the network: [GreetingsServer.java](src/main/java/ex/grpc/GreetingsServer.java).
- a server that hosts the service implementation and allows access over the
@ -271,9 +293,16 @@ implementation to be accessed from the network.
```
- it provides a class `GreetingsServer` that holds a `ServerImpl` that will run the server
- in the `start` method, `GreetingServer` binds the `GreetingsService` implementation to a port and begins running it
- there is also a `stop` method that takes care of shutting down the service and cleaning up when the program exits
The `GreetingsServer` class has a `ServerImpl` member that actually runs the
server. To create an appropriate `ServerImpl`, we use a special `ServerBuilder`
class (in this case a `NettyServerBuilder`) in the `GreetingsServer`'s `start`
method, binding the `GreetingsService` implementation that we created to a
port. Then we start the server running: the server is now ready to receive
requests from `Greetings` service clients on our specified port. We'll cover
how all this works in a bit more detail in our language-specific documentation.
`GreetingsServer` also has a `stop` method that takes care of shutting down
the service and cleaning up when the program exits.
#### Build it
@ -288,16 +317,22 @@ We'll look at using a client to access the server in the next section.
<aname="client"></a>
### Writing a client
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](src/main/java/ex/grpc/GreetingsClient.java).
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](#server). You can see the complete client code in