| Kun Zhang | d32bb47 | 2015-05-06 16:15:53 -0700 | [diff] [blame] | 1 | gRPC-Java - An RPC library and framework |
| 2 | ======================================== |
| 3 | |
| Eric Anderson | 6529132 | 2015-05-15 11:57:05 -0700 | [diff] [blame] | 4 | 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] | 5 | Services Dynamic Security Provider on Android. Please see the [Security |
| 6 | Readme](SECURITY.md). |
| ejona | d4531da | 2014-12-15 10:24:42 -0800 | [diff] [blame] | 7 | |
| nmittler | b7a72c1 | 2015-09-24 12:03:23 -0700 | [diff] [blame] | 8 | <table> |
| 9 | <tr> |
| 10 | <td><b>Homepage:</b></td> |
| 11 | <td><a href="http://www.grpc.io/">www.grpc.io</a></td> |
| 12 | </tr> |
| 13 | <tr> |
| 14 | <td><b>Mailing List:</b></td> |
| 15 | <td><a href="https://groups.google.com/forum/#!forum/grpc-io">grpc-io@googlegroups.com</a></td> |
| 16 | </tr> |
| 17 | </table> |
| 18 | |
| 19 | [](https://travis-ci.org/grpc/grpc-java) |
| 20 | [](https://coveralls.io/github/grpc/grpc-java?branch=master) |
| 21 | |
| Eric Anderson | 1eb16d9 | 2015-05-26 14:22:32 -0700 | [diff] [blame] | 22 | Download |
| 23 | -------- |
| 24 | |
| 25 | Download [the JAR][]. Or for Maven, add to your `pom.xml`: |
| 26 | ```xml |
| 27 | <dependency> |
| 28 | <groupId>io.grpc</groupId> |
| 29 | <artifactId>grpc-all</artifactId> |
| Carl Mastrangelo | b4fc929 | 2016-05-02 15:07:45 -0700 | [diff] [blame] | 30 | <version>0.14.0</version> |
| Eric Anderson | 1eb16d9 | 2015-05-26 14:22:32 -0700 | [diff] [blame] | 31 | </dependency> |
| 32 | ``` |
| 33 | |
| 34 | Or for Gradle, add to your dependencies: |
| 35 | ```gradle |
| Carl Mastrangelo | b4fc929 | 2016-05-02 15:07:45 -0700 | [diff] [blame] | 36 | compile 'io.grpc:grpc-all:0.14.0' |
| Eric Anderson | 1eb16d9 | 2015-05-26 14:22:32 -0700 | [diff] [blame] | 37 | ``` |
| 38 | |
| Xudong Ma | 996ea2f | 2015-10-07 11:03:41 -0700 | [diff] [blame] | 39 | For Android client, you only need to depend on the needed sub-projects, such as: |
| 40 | ```gradle |
| Carl Mastrangelo | b4fc929 | 2016-05-02 15:07:45 -0700 | [diff] [blame] | 41 | compile 'io.grpc:grpc-okhttp:0.14.0' |
| 42 | compile 'io.grpc:grpc-protobuf-nano:0.14.0' |
| 43 | compile 'io.grpc:grpc-stub:0.14.0' |
| Xudong Ma | 996ea2f | 2015-10-07 11:03:41 -0700 | [diff] [blame] | 44 | ``` |
| 45 | |
| Carl Mastrangelo | b4fc929 | 2016-05-02 15:07:45 -0700 | [diff] [blame] | 46 | [the JAR]: https://search.maven.org/remote_content?g=io.grpc&a=grpc-all&v=0.14.0 |
| Eric Anderson | 1eb16d9 | 2015-05-26 14:22:32 -0700 | [diff] [blame] | 47 | |
| 48 | Development snapshots are available in [Sonatypes's snapshot |
| 49 | repository](https://oss.sonatype.org/content/repositories/snapshots/). |
| 50 | |
| Eric Anderson | 52f1705 | 2016-01-27 10:33:32 -0800 | [diff] [blame] | 51 | For protobuf-based codegen, you can put your proto files in the `src/main/proto` |
| 52 | and `src/test/proto` directories along with an appropriate plugin. |
| 53 | |
| Eric Anderson | 1eb16d9 | 2015-05-26 14:22:32 -0700 | [diff] [blame] | 54 | For protobuf-based codegen integrated with the Maven build system, you can use |
| Eric Anderson | 41c3f78 | 2016-02-16 15:29:24 -0800 | [diff] [blame] | 55 | [protobuf-maven-plugin][]: |
| Eric Anderson | 1eb16d9 | 2015-05-26 14:22:32 -0700 | [diff] [blame] | 56 | ```xml |
| 57 | <build> |
| 58 | <extensions> |
| 59 | <extension> |
| 60 | <groupId>kr.motd.maven</groupId> |
| 61 | <artifactId>os-maven-plugin</artifactId> |
| Eric Anderson | 41c3f78 | 2016-02-16 15:29:24 -0800 | [diff] [blame] | 62 | <version>1.4.1.Final</version> |
| Eric Anderson | 1eb16d9 | 2015-05-26 14:22:32 -0700 | [diff] [blame] | 63 | </extension> |
| 64 | </extensions> |
| 65 | <plugins> |
| 66 | <plugin> |
| Eric Anderson | 41c3f78 | 2016-02-16 15:29:24 -0800 | [diff] [blame] | 67 | <groupId>org.xolstice.maven.plugins</groupId> |
| 68 | <artifactId>protobuf-maven-plugin</artifactId> |
| 69 | <version>0.5.0</version> |
| Eric Anderson | 1eb16d9 | 2015-05-26 14:22:32 -0700 | [diff] [blame] | 70 | <configuration> |
| Kun Zhang | 8bd8ed8 | 2015-07-22 09:19:52 -0700 | [diff] [blame] | 71 | <!-- |
| 72 | The version of protoc must match protobuf-java. If you don't depend on |
| 73 | protobuf-java directly, you will be transitively depending on the |
| 74 | protobuf-java version that grpc depends on. |
| 75 | --> |
| Eric Anderson | 85e68db | 2016-02-18 12:54:10 -0800 | [diff] [blame] | 76 | <protocArtifact>com.google.protobuf:protoc:3.0.0-beta-2:exe:${os.detected.classifier}</protocArtifact> |
| Eric Anderson | 1eb16d9 | 2015-05-26 14:22:32 -0700 | [diff] [blame] | 77 | <pluginId>grpc-java</pluginId> |
| Carl Mastrangelo | b4fc929 | 2016-05-02 15:07:45 -0700 | [diff] [blame] | 78 | <pluginArtifact>io.grpc:protoc-gen-grpc-java:0.14.0:exe:${os.detected.classifier}</pluginArtifact> |
| Eric Anderson | 1eb16d9 | 2015-05-26 14:22:32 -0700 | [diff] [blame] | 79 | </configuration> |
| 80 | <executions> |
| 81 | <execution> |
| 82 | <goals> |
| 83 | <goal>compile</goal> |
| 84 | <goal>compile-custom</goal> |
| 85 | </goals> |
| 86 | </execution> |
| 87 | </executions> |
| 88 | </plugin> |
| 89 | </plugins> |
| 90 | </build> |
| 91 | ``` |
| 92 | |
| Eric Anderson | 41c3f78 | 2016-02-16 15:29:24 -0800 | [diff] [blame] | 93 | [protobuf-maven-plugin]: https://www.xolstice.org/protobuf-maven-plugin/ |
| Eric Anderson | 1eb16d9 | 2015-05-26 14:22:32 -0700 | [diff] [blame] | 94 | |
| 95 | For protobuf-based codegen integrated with the Gradle build system, you can use |
| 96 | [protobuf-gradle-plugin][]: |
| 97 | ```gradle |
| Kun Zhang | 8bd8ed8 | 2015-07-22 09:19:52 -0700 | [diff] [blame] | 98 | apply plugin: 'java' |
| Eric Anderson | 1eb16d9 | 2015-05-26 14:22:32 -0700 | [diff] [blame] | 99 | apply plugin: 'com.google.protobuf' |
| 100 | |
| 101 | buildscript { |
| 102 | repositories { |
| 103 | mavenCentral() |
| 104 | } |
| 105 | dependencies { |
| Eric Anderson | 446f0cb | 2016-05-07 11:24:15 -0700 | [diff] [blame] | 106 | // ASSUMES GRADLE 2.12 OR HIGHER. Use plugin version 0.7.5 with earlier |
| 107 | // gradle versions |
| 108 | classpath 'com.google.protobuf:protobuf-gradle-plugin:0.7.7' |
| Eric Anderson | 1eb16d9 | 2015-05-26 14:22:32 -0700 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
| Kun Zhang | 8bd8ed8 | 2015-07-22 09:19:52 -0700 | [diff] [blame] | 112 | protobuf { |
| 113 | protoc { |
| 114 | // The version of protoc must match protobuf-java. If you don't depend on |
| 115 | // protobuf-java directly, you will be transitively depending on the |
| 116 | // protobuf-java version that grpc depends on. |
| Eric Anderson | 85e68db | 2016-02-18 12:54:10 -0800 | [diff] [blame] | 117 | artifact = "com.google.protobuf:protoc:3.0.0-beta-2" |
| Kun Zhang | 8bd8ed8 | 2015-07-22 09:19:52 -0700 | [diff] [blame] | 118 | } |
| 119 | plugins { |
| 120 | grpc { |
| Carl Mastrangelo | b4fc929 | 2016-05-02 15:07:45 -0700 | [diff] [blame] | 121 | artifact = 'io.grpc:protoc-gen-grpc-java:0.14.0' |
| Kun Zhang | 8bd8ed8 | 2015-07-22 09:19:52 -0700 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | generateProtoTasks { |
| 125 | all()*.plugins { |
| 126 | grpc {} |
| Eric Anderson | 1eb16d9 | 2015-05-26 14:22:32 -0700 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | } |
| Eric Anderson | 1eb16d9 | 2015-05-26 14:22:32 -0700 | [diff] [blame] | 130 | ``` |
| 131 | |
| 132 | [protobuf-gradle-plugin]: https://github.com/google/protobuf-gradle-plugin |
| 133 | |
| Eric Anderson | fb28ad2 | 2015-01-29 15:00:58 -0800 | [diff] [blame] | 134 | How to Build |
| 135 | ------------ |
| 136 | |
| Eric Anderson | 1b1c646 | 2015-08-14 08:49:56 -0700 | [diff] [blame] | 137 | If you are making changes to gRPC-Java, see the [compiling |
| 138 | instructions](COMPILING.md). |
| Kun Zhang | 9805e27 | 2015-05-12 17:03:19 -0700 | [diff] [blame] | 139 | |
| Eric Anderson | fb28ad2 | 2015-01-29 15:00:58 -0800 | [diff] [blame] | 140 | Navigating Around the Source |
| 141 | ---------------------------- |
| 142 | |
| mekka | 9990794 | 2015-08-07 23:28:06 -0700 | [diff] [blame] | 143 | Here's a quick readers' guide to the code to help folks get started. At a high |
| Kun Zhang | fc85a40 | 2015-08-05 16:48:15 -0700 | [diff] [blame] | 144 | level there are three distinct layers to the library: __Stub__, __Channel__ & |
| 145 | __Transport__. |
| ejona | d4531da | 2014-12-15 10:24:42 -0800 | [diff] [blame] | 146 | |
| Eric Anderson | fb28ad2 | 2015-01-29 15:00:58 -0800 | [diff] [blame] | 147 | ### Stub |
| ejona | d4531da | 2014-12-15 10:24:42 -0800 | [diff] [blame] | 148 | |
| Kun Zhang | fc85a40 | 2015-08-05 16:48:15 -0700 | [diff] [blame] | 149 | The Stub layer is what is exposed to most developers and provides type-safe |
| 150 | bindings to whatever datamodel/IDL/interface you are adapting. gRPC comes with |
| 151 | a [plugin](https://github.com/google/grpc-java/blob/master/compiler) to the |
| 152 | protocol-buffers compiler that generates Stub interfaces out of `.proto` files, |
| 153 | but bindings to other datamodel/IDL should be trivial to add and are welcome. |
| ejona | d4531da | 2014-12-15 10:24:42 -0800 | [diff] [blame] | 154 | |
| 155 | #### Key Interfaces |
| 156 | |
| nmittler | f831458 | 2015-01-27 10:25:39 -0800 | [diff] [blame] | 157 | [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] | 158 | |
| Eric Anderson | fb28ad2 | 2015-01-29 15:00:58 -0800 | [diff] [blame] | 159 | ### Channel |
| ejona | d4531da | 2014-12-15 10:24:42 -0800 | [diff] [blame] | 160 | |
| Kun Zhang | fc85a40 | 2015-08-05 16:48:15 -0700 | [diff] [blame] | 161 | The Channel layer is an abstraction over Transport handling that is suitable for |
| 162 | interception/decoration and exposes more behavior to the application than the |
| 163 | Stub layer. It is intended to be easy for application frameworks to use this |
| 164 | layer to address cross-cutting concerns such as logging, monitoring, auth etc. |
| 165 | Flow-control is also exposed at this layer to allow more sophisticated |
| 166 | applications to interact with it directly. |
| ejona | d4531da | 2014-12-15 10:24:42 -0800 | [diff] [blame] | 167 | |
| 168 | #### Common |
| 169 | |
| nmittler | f831458 | 2015-01-27 10:25:39 -0800 | [diff] [blame] | 170 | * [Metadata - headers & trailers](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/Metadata.java) |
| 171 | * [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] | 172 | |
| 173 | #### Client |
| nmittler | f831458 | 2015-01-27 10:25:39 -0800 | [diff] [blame] | 174 | * [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] | 175 | * [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] | 176 | * [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] | 177 | |
| 178 | #### Server |
| nmittler | f831458 | 2015-01-27 10:25:39 -0800 | [diff] [blame] | 179 | * [Server call handler - analog to Channel on server](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/ServerCallHandler.java) |
| 180 | * [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] | 181 | |
| 182 | |
| Eric Anderson | fb28ad2 | 2015-01-29 15:00:58 -0800 | [diff] [blame] | 183 | ### Transport |
| ejona | d4531da | 2014-12-15 10:24:42 -0800 | [diff] [blame] | 184 | |
| mekka | 9990794 | 2015-08-07 23:28:06 -0700 | [diff] [blame] | 185 | The Transport layer does the heavy lifting of putting and taking bytes off the |
| Kun Zhang | fc85a40 | 2015-08-05 16:48:15 -0700 | [diff] [blame] | 186 | wire. The interfaces to it are abstract just enough to allow plugging in of |
| 187 | different implementations. Transports are modeled as `Stream` factories. The |
| 188 | variation in interface between a server Stream and a client Stream exists to |
| 189 | codify their differing semantics for cancellation and error reporting. |
| 190 | |
| 191 | Note the transport layer API is considered internal to gRPC and has weaker API |
| 192 | guarantees than the core API under package `io.grpc`. |
| 193 | |
| mekka | 9990794 | 2015-08-07 23:28:06 -0700 | [diff] [blame] | 194 | gRPC comes with three Transport implementations: |
| Kun Zhang | fc85a40 | 2015-08-05 16:48:15 -0700 | [diff] [blame] | 195 | |
| 196 | 1. The [Netty-based](https://github.com/google/grpc-java/blob/master/netty) |
| 197 | transport is the main transport implementation based on |
| 198 | [Netty](http://netty.io). It is for both the client and the server. |
| 199 | 2. The [OkHttp-based](https://github.com/google/grpc-java/blob/master/okhttp) |
| 200 | transport is a lightweight transport based on |
| 201 | [OkHttp](http://square.github.io/okhttp/). It is mainly for use on Android |
| mekka | 9990794 | 2015-08-07 23:28:06 -0700 | [diff] [blame] | 202 | and is for client only. |
| Kun Zhang | fc85a40 | 2015-08-05 16:48:15 -0700 | [diff] [blame] | 203 | 3. The |
| Kun Zhang | e1bd6ef | 2015-08-11 11:17:39 -0700 | [diff] [blame] | 204 | [inProcess](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/inprocess) |
| Kun Zhang | fc85a40 | 2015-08-05 16:48:15 -0700 | [diff] [blame] | 205 | transport is for when a server is in the same process as the client. It is |
| 206 | useful for testing. |
| ejona | d4531da | 2014-12-15 10:24:42 -0800 | [diff] [blame] | 207 | |
| 208 | #### Common |
| 209 | |
| Kun Zhang | e1bd6ef | 2015-08-11 11:17:39 -0700 | [diff] [blame] | 210 | * [Stream](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/internal/Stream.java) |
| 211 | * [Stream Listener](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/internal/StreamListener.java) |
| ejona | d4531da | 2014-12-15 10:24:42 -0800 | [diff] [blame] | 212 | |
| 213 | #### Client |
| 214 | |
| Kun Zhang | e1bd6ef | 2015-08-11 11:17:39 -0700 | [diff] [blame] | 215 | * [Client Stream](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/internal/ClientStream.java) |
| 216 | * [Client Stream Listener](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/internal/ClientStreamListener.java) |
| ejona | d4531da | 2014-12-15 10:24:42 -0800 | [diff] [blame] | 217 | |
| 218 | #### Server |
| 219 | |
| Kun Zhang | e1bd6ef | 2015-08-11 11:17:39 -0700 | [diff] [blame] | 220 | * [Server Stream](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/internal/ServerStream.java) |
| 221 | * [Server Stream Listener](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/internal/ServerStreamListener.java) |
| ejona | d4531da | 2014-12-15 10:24:42 -0800 | [diff] [blame] | 222 | |
| 223 | |
| Eric Anderson | fb28ad2 | 2015-01-29 15:00:58 -0800 | [diff] [blame] | 224 | ### Examples |
| ejona | d4531da | 2014-12-15 10:24:42 -0800 | [diff] [blame] | 225 | |
| Kun Zhang | fc85a40 | 2015-08-05 16:48:15 -0700 | [diff] [blame] | 226 | Tests showing how these layers are composed to execute calls using protobuf |
| 227 | messages can be found here |
| 228 | https://github.com/google/grpc-java/tree/master/interop-testing/src/main/java/io/grpc/testing/integration |