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