| Eric Anderson | 6529132 | 2015-05-15 11:57:05 -0700 | [diff] [blame] | 1 | [](https://travis-ci.org/grpc/grpc-java) |
| 2 | |
| Kun Zhang | d32bb47 | 2015-05-06 16:15:53 -0700 | [diff] [blame] | 3 | gRPC-Java - An RPC library and framework |
| 4 | ======================================== |
| 5 | |
| Eric Anderson | 6529132 | 2015-05-15 11:57:05 -0700 | [diff] [blame] | 6 | gRPC-Java works with JDK 6. TLS usage typically requires using Java 8, or Play |
| nmittler | 2483009 | 2015-05-28 13:12:56 -0700 | [diff] [blame] | 7 | Services Dynamic Security Provider on Android. Please see the [Security |
| 8 | Readme](SECURITY.md). |
| ejona | d4531da | 2014-12-15 10:24:42 -0800 | [diff] [blame] | 9 | |
| Eric Anderson | 1eb16d9 | 2015-05-26 14:22:32 -0700 | [diff] [blame] | 10 | Download |
| 11 | -------- |
| 12 | |
| 13 | Download [the JAR][]. Or for Maven, add to your `pom.xml`: |
| 14 | ```xml |
| 15 | <dependency> |
| 16 | <groupId>io.grpc</groupId> |
| 17 | <artifactId>grpc-all</artifactId> |
| 18 | <version>0.7.0</version> |
| 19 | </dependency> |
| 20 | ``` |
| 21 | |
| 22 | Or for Gradle, add to your dependencies: |
| 23 | ```gradle |
| 24 | compile 'io.grpc:grpc-all:0.7.0' |
| 25 | ``` |
| 26 | |
| 27 | [the JAR]: https://search.maven.org/remote_content?g=io.grpc&a=grpc-all&v=0.7.0 |
| 28 | |
| 29 | Development snapshots are available in [Sonatypes's snapshot |
| 30 | repository](https://oss.sonatype.org/content/repositories/snapshots/). |
| 31 | |
| 32 | For protobuf-based codegen integrated with the Maven build system, you can use |
| 33 | [maven-protoc-plugin][]: |
| 34 | ```xml |
| 35 | <build> |
| 36 | <extensions> |
| 37 | <extension> |
| 38 | <groupId>kr.motd.maven</groupId> |
| 39 | <artifactId>os-maven-plugin</artifactId> |
| 40 | <version>1.2.3.Final</version> |
| 41 | </extension> |
| 42 | </extensions> |
| 43 | <plugins> |
| 44 | <plugin> |
| 45 | <groupId>com.google.protobuf.tools</groupId> |
| 46 | <artifactId>maven-protoc-plugin</artifactId> |
| 47 | <version>0.4.2</version> |
| 48 | <configuration> |
| 49 | <protocArtifact>com.google.protobuf:protoc:3.0.0-alpha-2:exe:${os.detected.classifier}</protocArtifact> |
| 50 | <pluginId>grpc-java</pluginId> |
| 51 | <pluginArtifact>io.grpc:protoc-gen-grpc-java:0.7.0:exe:${os.detected.classifier}</pluginArtifact> |
| 52 | </configuration> |
| 53 | <executions> |
| 54 | <execution> |
| 55 | <goals> |
| 56 | <goal>compile</goal> |
| 57 | <goal>compile-custom</goal> |
| 58 | </goals> |
| 59 | </execution> |
| 60 | </executions> |
| 61 | </plugin> |
| 62 | </plugins> |
| 63 | </build> |
| 64 | ``` |
| 65 | |
| 66 | [maven-protoc-plugin]: http://sergei-ivanov.github.io/maven-protoc-plugin/ |
| 67 | |
| 68 | For protobuf-based codegen integrated with the Gradle build system, you can use |
| 69 | [protobuf-gradle-plugin][]: |
| 70 | ```gradle |
| 71 | apply plugin: 'com.google.protobuf' |
| 72 | |
| 73 | buildscript { |
| 74 | repositories { |
| 75 | mavenCentral() |
| 76 | } |
| 77 | dependencies { |
| 78 | classpath 'com.google.protobuf:protobuf-gradle-plugin:0.4.1' |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | sourceSets { |
| 83 | main { |
| 84 | proto { |
| 85 | plugins { |
| 86 | grpc { } |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | protocDep = "com.google.protobuf:protoc:3.0.0-alpha-2" |
| 93 | protobufNativeCodeGenPluginDeps = ["grpc:io.grpc:protoc-gen-grpc-java:0.7.0"] |
| 94 | ``` |
| 95 | |
| 96 | [protobuf-gradle-plugin]: https://github.com/google/protobuf-gradle-plugin |
| 97 | |
| Eric Anderson | fb28ad2 | 2015-01-29 15:00:58 -0800 | [diff] [blame] | 98 | How to Build |
| 99 | ------------ |
| Eric Anderson | 1eb16d9 | 2015-05-26 14:22:32 -0700 | [diff] [blame] | 100 | This section is only necessary if you are making changes to gRPC-Java. |
| Eric Anderson | fb28ad2 | 2015-01-29 15:00:58 -0800 | [diff] [blame] | 101 | |
| Kun Zhang | fd0aed2 | 2015-04-30 17:29:17 -0700 | [diff] [blame] | 102 | ### Build Netty |
| Jakob Buchgraber | c56cec7 | 2015-03-11 13:44:32 -0700 | [diff] [blame] | 103 | 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] | 104 | 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] | 105 | ``` |
| 106 | $ git submodule update --init |
| 107 | $ cd lib/netty |
| 108 | $ mvn install -pl codec-http2 -am -DskipTests=true |
| 109 | ``` |
| 110 | |
| Eric Anderson | 66c55ee | 2015-05-07 15:07:13 -0700 | [diff] [blame] | 111 | ### Build gRPC |
| Eric Anderson | 6529132 | 2015-05-15 11:57:05 -0700 | [diff] [blame] | 112 | Building requires JDK 8, as our tests use TLS. |
| 113 | |
| Eric Anderson | 66c55ee | 2015-05-07 15:07:13 -0700 | [diff] [blame] | 114 | grpc-java has a C++ code generation plugin for protoc. Since many Java |
| 115 | developers don't have C compilers installed and don't need to modify the |
| 116 | codegen, the build can skip it. To skip, create the file |
| 117 | `<project-root>/gradle.properties` and add `skipCodegen=true`. |
| 118 | |
| 119 | Then, to build, run: |
| 120 | ``` |
| 121 | $ ./gradlew build |
| 122 | ``` |
| 123 | |
| 124 | To install the artifacts to your Maven local repository for use in your own |
| 125 | project, run: |
| 126 | ``` |
| 127 | $ ./gradlew install |
| 128 | ``` |
| 129 | |
| 130 | How to Build Code Generation Plugin |
| 131 | ----------------------------------- |
| 132 | This section is only necessary if you are making changes to the code |
| 133 | generation. Most users only need to use `skipCodegen=true` as discussed above. |
| 134 | |
| Kun Zhang | fd0aed2 | 2015-04-30 17:29:17 -0700 | [diff] [blame] | 135 | ### Build Protobuf |
| Kun Zhang | 111f6dd | 2015-05-05 16:11:27 -0700 | [diff] [blame] | 136 | The codegen plugin is C++ code and requires protobuf 3.0.0-alpha-2. |
| 137 | |
| Kun Zhang | fd0aed2 | 2015-04-30 17:29:17 -0700 | [diff] [blame] | 138 | For Linux, Mac and MinGW: |
| Eric Anderson | fb28ad2 | 2015-01-29 15:00:58 -0800 | [diff] [blame] | 139 | ``` |
| 140 | $ git clone https://github.com/google/protobuf.git |
| 141 | $ cd protobuf |
| Eric Anderson | a6f5fff | 2015-02-26 07:53:17 -0800 | [diff] [blame] | 142 | $ git checkout v3.0.0-alpha-2 |
| Eric Anderson | fb28ad2 | 2015-01-29 15:00:58 -0800 | [diff] [blame] | 143 | $ ./autogen.sh |
| 144 | $ ./configure |
| 145 | $ make |
| 146 | $ make check |
| 147 | $ sudo make install |
| Eric Anderson | fb28ad2 | 2015-01-29 15:00:58 -0800 | [diff] [blame] | 148 | ``` |
| 149 | |
| 150 | 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] | 151 | ``--prefix`` for Protobuf and use ``-I`` in ``CXXFLAGS``, ``-L`` in |
| 152 | ``LDFLAGS``, ``LD_LIBRARY_PATH``, and ``PATH`` to reference it. The |
| 153 | environment variables will be used when building grpc-java. |
| Eric Anderson | fb28ad2 | 2015-01-29 15:00:58 -0800 | [diff] [blame] | 154 | |
| Kun Zhang | fd0aed2 | 2015-04-30 17:29:17 -0700 | [diff] [blame] | 155 | Protobuf installs to ``/usr/local`` by default. |
| 156 | |
| 157 | For Visual C++, please refer to the [Protobuf README](https://github.com/google/protobuf/blob/master/vsprojects/readme.txt) |
| 158 | for how to compile Protobuf. |
| 159 | |
| 160 | #### Linux and MinGW |
| 161 | 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] | 162 | ``` |
| 163 | $ sudo sh -c 'echo /usr/local/lib >> /etc/ld.so.conf' |
| 164 | $ sudo ldconfig |
| 165 | ``` |
| 166 | |
| Kun Zhang | fd0aed2 | 2015-04-30 17:29:17 -0700 | [diff] [blame] | 167 | #### Mac |
| 168 | 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] | 169 | default search paths for header files and libraries. It will fail the build of |
| 170 | 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] | 171 | ``` |
| 172 | $ export CXXFLAGS="-I/usr/local/include" LDFLAGS="-L/usr/local/lib" |
| 173 | ``` |
| 174 | |
| Kun Zhang | 221c534 | 2015-04-29 18:16:15 -0700 | [diff] [blame] | 175 | ### Notes for Visual C++ |
| 176 | |
| Eric Anderson | 456216b | 2015-03-02 16:58:27 -0800 | [diff] [blame] | 177 | When building on Windows and VC++, you need to specify project properties for |
| 178 | Gradle to find protobuf: |
| 179 | ``` |
| Kun Zhang | 221c534 | 2015-04-29 18:16:15 -0700 | [diff] [blame] | 180 | .\gradlew install ^ |
| Kun Zhang | 2f74971 | 2015-05-06 13:10:28 -0700 | [diff] [blame] | 181 | -PvcProtobufInclude=C:\path\to\protobuf-3.0.0-alpha-2\src ^ |
| 182 | -PvcProtobufLibs=C:\path\to\protobuf-3.0.0-alpha-2\vsprojects\Release |
| Eric Anderson | 456216b | 2015-03-02 16:58:27 -0800 | [diff] [blame] | 183 | ``` |
| 184 | |
| 185 | Since specifying those properties every build is bothersome, you can instead |
| Kun Zhang | 2f74971 | 2015-05-06 13:10:28 -0700 | [diff] [blame] | 186 | create ``<project-root>\gradle.properties`` with contents like: |
| Eric Anderson | 456216b | 2015-03-02 16:58:27 -0800 | [diff] [blame] | 187 | ``` |
| Kun Zhang | 2f74971 | 2015-05-06 13:10:28 -0700 | [diff] [blame] | 188 | vcProtobufInclude=C:\\path\\to\\protobuf-3.0.0-alpha-2\\src |
| 189 | vcProtobufLibs=C:\\path\\to\\protobuf-3.0.0-alpha-2\\vsprojects\\Release |
| Eric Anderson | 456216b | 2015-03-02 16:58:27 -0800 | [diff] [blame] | 190 | ``` |
| 191 | |
| Kun Zhang | 221c534 | 2015-04-29 18:16:15 -0700 | [diff] [blame] | 192 | The build script will build the codegen for the same architecture as the Java |
| 193 | runtime installed on your system. If you are using 64-bit JVM, the codegen will |
| 194 | be compiled for 64-bit, that means you must have compiled Protobuf in 64-bit. |
| 195 | |
| 196 | ### Notes for MinGW on Windows |
| 197 | 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] | 198 | default. To override this default and use MinGW, add ``-PvcDisable=true`` |
| 199 | to your Gradle command line or add ``vcDisable=true`` to your |
| 200 | ``<project-root>\gradle.properties``. |
| Kun Zhang | 221c534 | 2015-04-29 18:16:15 -0700 | [diff] [blame] | 201 | |
| Kun Zhang | 9805e27 | 2015-05-12 17:03:19 -0700 | [diff] [blame] | 202 | ### Notes for unsupported operating systems |
| 203 | The build script pulls pre-compiled ``protoc`` from Maven Central by default. |
| 204 | We have built ``protoc`` binaries for popular systems, but they may not work |
| 205 | for your system. If ``protoc`` cannot be downloaded or would not run, you can |
| 206 | use the one that has been built by your own, by adding this property to |
| 207 | ``<project-root>/gradle.properties``: |
| 208 | ``` |
| 209 | protoc=/path/to/protoc |
| 210 | ``` |
| 211 | |
| Eric Anderson | fb28ad2 | 2015-01-29 15:00:58 -0800 | [diff] [blame] | 212 | Navigating Around the Source |
| 213 | ---------------------------- |
| 214 | |
| ejona | d4531da | 2014-12-15 10:24:42 -0800 | [diff] [blame] | 215 | 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] | 216 | to the library: stub, channel & transport. |
| ejona | d4531da | 2014-12-15 10:24:42 -0800 | [diff] [blame] | 217 | |
| Eric Anderson | fb28ad2 | 2015-01-29 15:00:58 -0800 | [diff] [blame] | 218 | ### Stub |
| ejona | d4531da | 2014-12-15 10:24:42 -0800 | [diff] [blame] | 219 | |
| 220 | The 'stub' layer is what is exposed to most developers and provides type-safe bindings to whatever |
| 221 | 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. |
| 222 | |
| 223 | #### Key Interfaces |
| 224 | |
| nmittler | f831458 | 2015-01-27 10:25:39 -0800 | [diff] [blame] | 225 | [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] | 226 | |
| 227 | |
| Eric Anderson | fb28ad2 | 2015-01-29 15:00:58 -0800 | [diff] [blame] | 228 | ### Channel |
| ejona | d4531da | 2014-12-15 10:24:42 -0800 | [diff] [blame] | 229 | |
| 230 | 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. |
| 231 | |
| 232 | #### Common |
| 233 | |
| nmittler | f831458 | 2015-01-27 10:25:39 -0800 | [diff] [blame] | 234 | * [Metadata - headers & trailers](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/Metadata.java) |
| 235 | * [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] | 236 | |
| 237 | #### Client |
| nmittler | f831458 | 2015-01-27 10:25:39 -0800 | [diff] [blame] | 238 | * [Channel - client side binding](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/Channel.java) |
| Kun Zhang | 2ee4d02 | 2015-06-04 16:39:25 -0700 | [diff] [blame^] | 239 | * [Client Call](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/ClientCall.java) |
| nmittler | f831458 | 2015-01-27 10:25:39 -0800 | [diff] [blame] | 240 | * [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] | 241 | |
| 242 | #### Server |
| nmittler | f831458 | 2015-01-27 10:25:39 -0800 | [diff] [blame] | 243 | * [Server call handler - analog to Channel on server](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/ServerCallHandler.java) |
| 244 | * [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] | 245 | |
| 246 | |
| Eric Anderson | fb28ad2 | 2015-01-29 15:00:58 -0800 | [diff] [blame] | 247 | ### Transport |
| ejona | d4531da | 2014-12-15 10:24:42 -0800 | [diff] [blame] | 248 | |
| 249 | 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. |
| 250 | |
| 251 | #### Common |
| 252 | |
| nmittler | f831458 | 2015-01-27 10:25:39 -0800 | [diff] [blame] | 253 | * [Stream](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/transport/Stream.java) |
| 254 | * [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] | 255 | |
| 256 | #### Client |
| 257 | |
| nmittler | f831458 | 2015-01-27 10:25:39 -0800 | [diff] [blame] | 258 | * [Client Stream](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/transport/ClientStream.java) |
| 259 | * [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] | 260 | |
| 261 | #### Server |
| 262 | |
| nmittler | f831458 | 2015-01-27 10:25:39 -0800 | [diff] [blame] | 263 | * [Server Stream](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/transport/ServerStream.java) |
| 264 | * [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] | 265 | |
| 266 | |
| Eric Anderson | fb28ad2 | 2015-01-29 15:00:58 -0800 | [diff] [blame] | 267 | ### Examples |
| ejona | d4531da | 2014-12-15 10:24:42 -0800 | [diff] [blame] | 268 | |
| Eric Anderson | e03d5c0 | 2015-05-20 13:23:11 -0700 | [diff] [blame] | 269 | 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/interop-testing/src/main/java/io/grpc/testing/integration |