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