blob: 952325b4461cd056c0b63577fdb8a85be045146b [file] [log] [blame] [view]
Abhishek Kumar2f22e842015-02-20 16:39:32 -08001# gRPC C++ Hello World Tutorial
2
3### Install gRPC
4Make 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
9The example code for this and our other examples lives in the `grpc-common`
10GitHub repository. Clone this repository to your local machine by running the
11following command:
12
13
14```sh
15$ git clone https://github.com/google/grpc-common.git
16```
17
18Change your current directory to grpc-common/cpp/helloworld
19
20```sh
21$ cd grpc-common/cpp/helloworld/
22```
23
24### Defining a service
25
26The first step in creating our example is to define a *service*: an RPC
27service specifies the methods that can be called remotely with their parameters
28and return types. As you saw in the
29[overview](#protocolbuffers) above, gRPC does this using [protocol
30buffers](https://developers.google.com/protocol-buffers/docs/overview). We
31use the protocol buffers interface definition language (IDL) to define our
32service methods, and define the parameters and return
33types as protocol buffer message types. Both the client and the
34server use interface code generated from the service definition.
35
36Here'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`
38service has one method, `hello`, that lets the server receive a single
39`HelloRequest`
40message from the remote client containing the user's name, then send back
41a greeting in a single `HelloReply`. This is the simplest type of RPC you
42can specify in gRPC - we'll look at some other types later in this document.
43
44```
45syntax = "proto3";
46
47option java_package = "ex.grpc";
48
49package helloworld;
50
51// The greeting service definition.
52service Greeter {
53 // Sends a greeting
54 rpc SayHello (HelloRequest) returns (HelloReply) {}
55}
56
57// The request message containing the user's name.
58message HelloRequest {
59 string name = 1;
60}
61
62// The response message containing the greetings
63message HelloReply {
64 string message = 1;
65}
66
67```
68
69<a name="generating"></a>
70### Generating gRPC code
71
72Once 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
74our application. The generated code contains both stub code for clients to
75use and an abstract interface for servers to implement, both with the method
76defined in our `Greeting` service.
77
78To generate the client and server side interfaces:
79
80```sh
81$ make helloworld.pb.cc
82```
83Which 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
91This 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
95This 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).