Abhishek Kumar | 2f22e84 | 2015-02-20 16:39:32 -0800 | [diff] [blame^] | 1 | # gRPC C++ Hello World Tutorial |
| 2 | |
| 3 | ### Install gRPC |
| 4 | Make sure you have installed gRPC on your system. Follow the instructions here: |
| 5 | [https://github.com/grpc/grpc/blob/master/INSTALL](https://github.com/grpc/grpc/blob/master/INSTALL). |
| 6 | |
| 7 | ### Get the tutorial source code |
| 8 | |
| 9 | The example code for this and our other examples lives in the `grpc-common` |
| 10 | GitHub repository. Clone this repository to your local machine by running the |
| 11 | following command: |
| 12 | |
| 13 | |
| 14 | ```sh |
| 15 | $ git clone https://github.com/google/grpc-common.git |
| 16 | ``` |
| 17 | |
| 18 | Change your current directory to grpc-common/cpp/helloworld |
| 19 | |
| 20 | ```sh |
| 21 | $ cd grpc-common/cpp/helloworld/ |
| 22 | ``` |
| 23 | |
| 24 | ### Defining a service |
| 25 | |
| 26 | The first step in creating our example is to define a *service*: an RPC |
| 27 | service specifies the methods that can be called remotely with their parameters |
| 28 | and return types. As you saw in the |
| 29 | [overview](#protocolbuffers) above, gRPC does this using [protocol |
| 30 | buffers](https://developers.google.com/protocol-buffers/docs/overview). We |
| 31 | use the protocol buffers interface definition language (IDL) to define our |
| 32 | service methods, and define the parameters and return |
| 33 | types as protocol buffer message types. Both the client and the |
| 34 | server use interface code generated from the service definition. |
| 35 | |
| 36 | Here's our example service definition, defined using protocol buffers IDL in |
| 37 | [helloworld.proto](https://github.com/grpc/grpc-common/blob/master/protos/helloworld.proto). The `Greeting` |
| 38 | service has one method, `hello`, that lets the server receive a single |
| 39 | `HelloRequest` |
| 40 | message from the remote client containing the user's name, then send back |
| 41 | a greeting in a single `HelloReply`. This is the simplest type of RPC you |
| 42 | can specify in gRPC - we'll look at some other types later in this document. |
| 43 | |
| 44 | ``` |
| 45 | syntax = "proto3"; |
| 46 | |
| 47 | option java_package = "ex.grpc"; |
| 48 | |
| 49 | package helloworld; |
| 50 | |
| 51 | // The greeting service definition. |
| 52 | service Greeter { |
| 53 | // Sends a greeting |
| 54 | rpc SayHello (HelloRequest) returns (HelloReply) {} |
| 55 | } |
| 56 | |
| 57 | // The request message containing the user's name. |
| 58 | message HelloRequest { |
| 59 | string name = 1; |
| 60 | } |
| 61 | |
| 62 | // The response message containing the greetings |
| 63 | message HelloReply { |
| 64 | string message = 1; |
| 65 | } |
| 66 | |
| 67 | ``` |
| 68 | |
| 69 | <a name="generating"></a> |
| 70 | ### Generating gRPC code |
| 71 | |
| 72 | Once we've defined our service, we use the protocol buffer compiler |
| 73 | `protoc` to generate the special client and server code we need to create |
| 74 | our application. The generated code contains both stub code for clients to |
| 75 | use and an abstract interface for servers to implement, both with the method |
| 76 | defined in our `Greeting` service. |
| 77 | |
| 78 | To generate the client and server side interfaces: |
| 79 | |
| 80 | ```sh |
| 81 | $ make helloworld.pb.cc |
| 82 | ``` |
| 83 | Which internally invokes the proto-compiler as: |
| 84 | |
| 85 | ```sh |
| 86 | $protoc -I ../../protos/ --cpp_out=. --grpc_out=. --plugin=protoc-gen-grpc=grpc_cpp_plugin helloworld.proto |
| 87 | ``` |
| 88 | |
| 89 | ### Writing a client |
| 90 | |
| 91 | This is an incomplete tutorial. For now the reader should refer to [greeter_client.cc](https://github.com/grpc/grpc-common/blob/master/cpp/helloworld/greeter_client.cc). |
| 92 | |
| 93 | ### Writing a server |
| 94 | |
| 95 | This is an incomplete tutorial. For now the reader should refer to [greeter_server.cc](https://github.com/grpc/grpc-common/blob/master/cpp/helloworld/greeter_server.cc). |