| Kun Zhang | d32bb47 | 2015-05-06 16:15:53 -0700 | [diff] [blame] | 1 | gRPC-Java - An RPC library and framework |
| 2 | ======================================== |
| 3 | |
| 4 | [](https://travis-ci.org/grpc/grpc-java) |
| ejona | d4531da | 2014-12-15 10:24:42 -0800 | [diff] [blame] | 5 | |
| Eric Anderson | fb28ad2 | 2015-01-29 15:00:58 -0800 | [diff] [blame] | 6 | How to Build |
| 7 | ------------ |
| 8 | |
| Kun Zhang | fd0aed2 | 2015-04-30 17:29:17 -0700 | [diff] [blame] | 9 | ### Build Netty |
| Jakob Buchgraber | c56cec7 | 2015-03-11 13:44:32 -0700 | [diff] [blame] | 10 | grpc-java requires Netty 4.1, which is still in flux. The version we need can be |
| Eric Anderson | b10aaf8 | 2015-02-19 13:14:00 -0800 | [diff] [blame] | 11 | found in the lib/netty submodule, which requires Maven 3.2 or higher to build: |
| Eric Anderson | fb28ad2 | 2015-01-29 15:00:58 -0800 | [diff] [blame] | 12 | ``` |
| 13 | $ git submodule update --init |
| 14 | $ cd lib/netty |
| 15 | $ mvn install -pl codec-http2 -am -DskipTests=true |
| 16 | ``` |
| 17 | |
| Kun Zhang | fd0aed2 | 2015-04-30 17:29:17 -0700 | [diff] [blame] | 18 | ### Build Protobuf |
| Kun Zhang | 111f6dd | 2015-05-05 16:11:27 -0700 | [diff] [blame] | 19 | The codegen plugin is C++ code and requires protobuf 3.0.0-alpha-2. |
| 20 | |
| 21 | If you are not changing the codegen plugin, nor any of the ``.proto`` files in |
| Kun Zhang | 2f74971 | 2015-05-06 13:10:28 -0700 | [diff] [blame^] | 22 | the source tree, you can skip this chapter and add ``skipCodegen=true`` |
| 23 | to ``<project-root>/gradle.properties``. It will make the build script skip the |
| Kun Zhang | 111f6dd | 2015-05-05 16:11:27 -0700 | [diff] [blame] | 24 | build and invocation of the codegen, and use generated code that has been |
| 25 | checked in. |
| Kun Zhang | fd0aed2 | 2015-04-30 17:29:17 -0700 | [diff] [blame] | 26 | |
| 27 | For Linux, Mac and MinGW: |
| Eric Anderson | fb28ad2 | 2015-01-29 15:00:58 -0800 | [diff] [blame] | 28 | ``` |
| 29 | $ git clone https://github.com/google/protobuf.git |
| 30 | $ cd protobuf |
| Eric Anderson | a6f5fff | 2015-02-26 07:53:17 -0800 | [diff] [blame] | 31 | $ git checkout v3.0.0-alpha-2 |
| Eric Anderson | fb28ad2 | 2015-01-29 15:00:58 -0800 | [diff] [blame] | 32 | $ ./autogen.sh |
| 33 | $ ./configure |
| 34 | $ make |
| 35 | $ make check |
| 36 | $ sudo make install |
| Eric Anderson | fb28ad2 | 2015-01-29 15:00:58 -0800 | [diff] [blame] | 37 | ``` |
| 38 | |
| 39 | If you are comfortable with C++ compilation and autotools, you can specify a |
| Kun Zhang | fd0aed2 | 2015-04-30 17:29:17 -0700 | [diff] [blame] | 40 | ``--prefix`` for Protobuf and use ``-I`` in ``CXXFLAGS``, ``-L`` in |
| 41 | ``LDFLAGS``, ``LD_LIBRARY_PATH``, and ``PATH`` to reference it. The |
| 42 | environment variables will be used when building grpc-java. |
| Eric Anderson | fb28ad2 | 2015-01-29 15:00:58 -0800 | [diff] [blame] | 43 | |
| Kun Zhang | fd0aed2 | 2015-04-30 17:29:17 -0700 | [diff] [blame] | 44 | Protobuf installs to ``/usr/local`` by default. |
| 45 | |
| 46 | For Visual C++, please refer to the [Protobuf README](https://github.com/google/protobuf/blob/master/vsprojects/readme.txt) |
| 47 | for how to compile Protobuf. |
| 48 | |
| 49 | #### Linux and MinGW |
| 50 | If ``/usr/local/lib`` is not in your library search path, you can add it by running: |
| Louis Ryan | 65d64cd | 2015-02-18 14:51:42 -0800 | [diff] [blame] | 51 | ``` |
| 52 | $ sudo sh -c 'echo /usr/local/lib >> /etc/ld.so.conf' |
| 53 | $ sudo ldconfig |
| 54 | ``` |
| 55 | |
| Kun Zhang | fd0aed2 | 2015-04-30 17:29:17 -0700 | [diff] [blame] | 56 | #### Mac |
| 57 | Some versions of Mac OS X (e.g., 10.10) doesn't have ``/usr/local`` in the |
| Kun Zhang | 111f6dd | 2015-05-05 16:11:27 -0700 | [diff] [blame] | 58 | default search paths for header files and libraries. It will fail the build of |
| 59 | the codegen. To work around this, you will need to set environment variables: |
| Kun Zhang | fd0aed2 | 2015-04-30 17:29:17 -0700 | [diff] [blame] | 60 | ``` |
| 61 | $ export CXXFLAGS="-I/usr/local/include" LDFLAGS="-L/usr/local/lib" |
| 62 | ``` |
| 63 | |
| 64 | ### Build GRPC |
| 65 | On Linux, Mac or MinGW: |
| Eric Anderson | fb28ad2 | 2015-01-29 15:00:58 -0800 | [diff] [blame] | 66 | ``` |
| 67 | $ ./gradlew install |
| 68 | ``` |
| 69 | |
| Kun Zhang | 221c534 | 2015-04-29 18:16:15 -0700 | [diff] [blame] | 70 | ### Notes for Visual C++ |
| 71 | |
| Eric Anderson | 456216b | 2015-03-02 16:58:27 -0800 | [diff] [blame] | 72 | When building on Windows and VC++, you need to specify project properties for |
| 73 | Gradle to find protobuf: |
| 74 | ``` |
| Kun Zhang | 221c534 | 2015-04-29 18:16:15 -0700 | [diff] [blame] | 75 | .\gradlew install ^ |
| Kun Zhang | 2f74971 | 2015-05-06 13:10:28 -0700 | [diff] [blame^] | 76 | -PvcProtobufInclude=C:\path\to\protobuf-3.0.0-alpha-2\src ^ |
| 77 | -PvcProtobufLibs=C:\path\to\protobuf-3.0.0-alpha-2\vsprojects\Release |
| Eric Anderson | 456216b | 2015-03-02 16:58:27 -0800 | [diff] [blame] | 78 | ``` |
| 79 | |
| 80 | Since specifying those properties every build is bothersome, you can instead |
| Kun Zhang | 2f74971 | 2015-05-06 13:10:28 -0700 | [diff] [blame^] | 81 | create ``<project-root>\gradle.properties`` with contents like: |
| Eric Anderson | 456216b | 2015-03-02 16:58:27 -0800 | [diff] [blame] | 82 | ``` |
| Kun Zhang | 2f74971 | 2015-05-06 13:10:28 -0700 | [diff] [blame^] | 83 | vcProtobufInclude=C:\\path\\to\\protobuf-3.0.0-alpha-2\\src |
| 84 | vcProtobufLibs=C:\\path\\to\\protobuf-3.0.0-alpha-2\\vsprojects\\Release |
| Eric Anderson | 456216b | 2015-03-02 16:58:27 -0800 | [diff] [blame] | 85 | ``` |
| 86 | |
| Kun Zhang | 221c534 | 2015-04-29 18:16:15 -0700 | [diff] [blame] | 87 | The build script will build the codegen for the same architecture as the Java |
| 88 | runtime installed on your system. If you are using 64-bit JVM, the codegen will |
| 89 | be compiled for 64-bit, that means you must have compiled Protobuf in 64-bit. |
| 90 | |
| 91 | ### Notes for MinGW on Windows |
| 92 | If you have both MinGW and VC++ installed on Windows, VC++ will be used by |
| Kun Zhang | 2f74971 | 2015-05-06 13:10:28 -0700 | [diff] [blame^] | 93 | default. To override this default and use MinGW, add ``-PvcDisable=true`` |
| 94 | to your Gradle command line or add ``vcDisable=true`` to your |
| 95 | ``<project-root>\gradle.properties``. |
| Kun Zhang | 221c534 | 2015-04-29 18:16:15 -0700 | [diff] [blame] | 96 | |
| Eric Anderson | fb28ad2 | 2015-01-29 15:00:58 -0800 | [diff] [blame] | 97 | Navigating Around the Source |
| 98 | ---------------------------- |
| 99 | |
| ejona | d4531da | 2014-12-15 10:24:42 -0800 | [diff] [blame] | 100 | Heres a quick readers guide to the code to help folks get started. At a high level there are three distinct layers |
| Kun Zhang | a8ef36a | 2015-04-22 14:46:07 -0700 | [diff] [blame] | 101 | to the library: stub, channel & transport. |
| ejona | d4531da | 2014-12-15 10:24:42 -0800 | [diff] [blame] | 102 | |
| Eric Anderson | fb28ad2 | 2015-01-29 15:00:58 -0800 | [diff] [blame] | 103 | ### Stub |
| ejona | d4531da | 2014-12-15 10:24:42 -0800 | [diff] [blame] | 104 | |
| 105 | The 'stub' layer is what is exposed to most developers and provides type-safe bindings to whatever |
| 106 | datamodel/IDL/interface you are adapting. An example is provided of a binding to code generated by the protocol-buffers compiler but others should be trivial to add and are welcome. |
| 107 | |
| 108 | #### Key Interfaces |
| 109 | |
| nmittler | f831458 | 2015-01-27 10:25:39 -0800 | [diff] [blame] | 110 | [Stream Observer](https://github.com/google/grpc-java/blob/master/stub/src/main/java/io/grpc/stub/StreamObserver.java) |
| ejona | d4531da | 2014-12-15 10:24:42 -0800 | [diff] [blame] | 111 | |
| 112 | |
| Eric Anderson | fb28ad2 | 2015-01-29 15:00:58 -0800 | [diff] [blame] | 113 | ### Channel |
| ejona | d4531da | 2014-12-15 10:24:42 -0800 | [diff] [blame] | 114 | |
| 115 | The 'channel' layer is an abstraction over transport handling that is suitable for interception/decoration and exposes more behavior to the application than the stub layer. It is intended to be easy for application frameworks to use this layer to address cross-cutting concerns such as logging, monitoring, auth etc. Flow-control is also exposed at this layer to allow more sophisticated applications to interact with it directly. |
| 116 | |
| 117 | #### Common |
| 118 | |
| nmittler | f831458 | 2015-01-27 10:25:39 -0800 | [diff] [blame] | 119 | * [Metadata - headers & trailers](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/Metadata.java) |
| 120 | * [Status - error code namespace & handling](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/Status.java) |
| ejona | d4531da | 2014-12-15 10:24:42 -0800 | [diff] [blame] | 121 | |
| 122 | #### Client |
| nmittler | f831458 | 2015-01-27 10:25:39 -0800 | [diff] [blame] | 123 | * [Channel - client side binding](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/Channel.java) |
| 124 | * [Call](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/Call.java) |
| 125 | * [Client Interceptor](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/ClientInterceptor.java) |
| ejona | d4531da | 2014-12-15 10:24:42 -0800 | [diff] [blame] | 126 | |
| 127 | #### Server |
| nmittler | f831458 | 2015-01-27 10:25:39 -0800 | [diff] [blame] | 128 | * [Server call handler - analog to Channel on server](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/ServerCallHandler.java) |
| 129 | * [Server Call](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/ServerCall.java) |
| ejona | d4531da | 2014-12-15 10:24:42 -0800 | [diff] [blame] | 130 | |
| 131 | |
| Eric Anderson | fb28ad2 | 2015-01-29 15:00:58 -0800 | [diff] [blame] | 132 | ### Transport |
| ejona | d4531da | 2014-12-15 10:24:42 -0800 | [diff] [blame] | 133 | |
| 134 | The 'transport' layer does the heavy lifting of putting & taking bytes off the wire. The interfaces to it are abstract just enough to allow plugging in of different implementations. Transports are modeled as 'Stream' factories. The variation in interface between a server stream and a client stream exists to codify their differing semantics for cancellation and error reporting. |
| 135 | |
| 136 | #### Common |
| 137 | |
| nmittler | f831458 | 2015-01-27 10:25:39 -0800 | [diff] [blame] | 138 | * [Stream](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/transport/Stream.java) |
| 139 | * [Stream Listener](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/transport/StreamListener.java) |
| ejona | d4531da | 2014-12-15 10:24:42 -0800 | [diff] [blame] | 140 | |
| 141 | #### Client |
| 142 | |
| nmittler | f831458 | 2015-01-27 10:25:39 -0800 | [diff] [blame] | 143 | * [Client Stream](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/transport/ClientStream.java) |
| 144 | * [Client Stream Listener](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/transport/ClientStreamListener.java) |
| ejona | d4531da | 2014-12-15 10:24:42 -0800 | [diff] [blame] | 145 | |
| 146 | #### Server |
| 147 | |
| nmittler | f831458 | 2015-01-27 10:25:39 -0800 | [diff] [blame] | 148 | * [Server Stream](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/transport/ServerStream.java) |
| 149 | * [Server Stream Listener](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/transport/ServerStreamListener.java) |
| ejona | d4531da | 2014-12-15 10:24:42 -0800 | [diff] [blame] | 150 | |
| 151 | |
| Eric Anderson | fb28ad2 | 2015-01-29 15:00:58 -0800 | [diff] [blame] | 152 | ### Examples |
| ejona | d4531da | 2014-12-15 10:24:42 -0800 | [diff] [blame] | 153 | |
| nmittler | f831458 | 2015-01-27 10:25:39 -0800 | [diff] [blame] | 154 | Tests showing how these layers are composed to execute calls using protobuf messages can be found here https://github.com/google/grpc-java/tree/master/integration-testing/src/main/java/io/grpc/testing/integration |