| Kun Zhang | d32bb47 | 2015-05-06 16:15:53 -0700 | [diff] [blame] | 1 | gRPC-Java - An RPC library and framework |
| 2 | ======================================== |
| 3 | |
| Eric Gribkoff | 785ac47 | 2018-04-30 10:28:41 -0700 | [diff] [blame] | 4 | gRPC-Java works with JDK 6. gRPC-Java clients are supported on Android API |
| 5 | levels 14 and up (Ice Cream Sandwich and later). Deploying gRPC servers on an |
| 6 | Android device is not supported. |
| Eric Gribkoff | b940084 | 2018-01-17 10:18:33 -0800 | [diff] [blame] | 7 | |
| 8 | TLS usage typically requires using Java 8, or Play Services Dynamic Security |
| 9 | Provider on Android. Please see the [Security Readme](SECURITY.md). |
| ejona | d4531da | 2014-12-15 10:24:42 -0800 | [diff] [blame] | 10 | |
| nmittler | b7a72c1 | 2015-09-24 12:03:23 -0700 | [diff] [blame] | 11 | <table> |
| 12 | <tr> |
| 13 | <td><b>Homepage:</b></td> |
| Mehrdad Afshari | 8ce0bc2 | 2017-07-10 22:48:17 +0000 | [diff] [blame] | 14 | <td><a href="https://grpc.io/">grpc.io</a></td> |
| nmittler | b7a72c1 | 2015-09-24 12:03:23 -0700 | [diff] [blame] | 15 | </tr> |
| 16 | <tr> |
| 17 | <td><b>Mailing List:</b></td> |
| 18 | <td><a href="https://groups.google.com/forum/#!forum/grpc-io">grpc-io@googlegroups.com</a></td> |
| 19 | </tr> |
| 20 | </table> |
| 21 | |
| Eric Anderson | 67ff170 | 2016-12-06 09:27:10 -0800 | [diff] [blame] | 22 | [](https://gitter.im/grpc/grpc?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) |
| nmittler | b7a72c1 | 2015-09-24 12:03:23 -0700 | [diff] [blame] | 23 | [](https://travis-ci.org/grpc/grpc-java) |
| 24 | [](https://coveralls.io/github/grpc/grpc-java?branch=master) |
| 25 | |
| Eric Anderson | 1eb16d9 | 2015-05-26 14:22:32 -0700 | [diff] [blame] | 26 | Download |
| 27 | -------- |
| 28 | |
| Eric Anderson | a62e976 | 2016-06-14 10:58:38 -0700 | [diff] [blame] | 29 | Download [the JARs][]. Or for Maven with non-Android, add to your `pom.xml`: |
| Eric Anderson | 1eb16d9 | 2015-05-26 14:22:32 -0700 | [diff] [blame] | 30 | ```xml |
| 31 | <dependency> |
| 32 | <groupId>io.grpc</groupId> |
| Eric Anderson | a62e976 | 2016-06-14 10:58:38 -0700 | [diff] [blame] | 33 | <artifactId>grpc-netty</artifactId> |
| Carl Mastrangelo | 128f54c | 2018-07-31 12:19:20 -0700 | [diff] [blame] | 34 | <version>1.14.0</version> |
| Eric Anderson | a62e976 | 2016-06-14 10:58:38 -0700 | [diff] [blame] | 35 | </dependency> |
| 36 | <dependency> |
| 37 | <groupId>io.grpc</groupId> |
| 38 | <artifactId>grpc-protobuf</artifactId> |
| Carl Mastrangelo | 128f54c | 2018-07-31 12:19:20 -0700 | [diff] [blame] | 39 | <version>1.14.0</version> |
| Eric Anderson | a62e976 | 2016-06-14 10:58:38 -0700 | [diff] [blame] | 40 | </dependency> |
| 41 | <dependency> |
| 42 | <groupId>io.grpc</groupId> |
| 43 | <artifactId>grpc-stub</artifactId> |
| Carl Mastrangelo | 128f54c | 2018-07-31 12:19:20 -0700 | [diff] [blame] | 44 | <version>1.14.0</version> |
| Eric Anderson | 1eb16d9 | 2015-05-26 14:22:32 -0700 | [diff] [blame] | 45 | </dependency> |
| 46 | ``` |
| 47 | |
| Eric Anderson | a62e976 | 2016-06-14 10:58:38 -0700 | [diff] [blame] | 48 | Or for Gradle with non-Android, add to your dependencies: |
| Eric Anderson | 1eb16d9 | 2015-05-26 14:22:32 -0700 | [diff] [blame] | 49 | ```gradle |
| Carl Mastrangelo | 128f54c | 2018-07-31 12:19:20 -0700 | [diff] [blame] | 50 | compile 'io.grpc:grpc-netty:1.14.0' |
| 51 | compile 'io.grpc:grpc-protobuf:1.14.0' |
| 52 | compile 'io.grpc:grpc-stub:1.14.0' |
| Eric Anderson | 1eb16d9 | 2015-05-26 14:22:32 -0700 | [diff] [blame] | 53 | ``` |
| 54 | |
| Eric Anderson | a62e976 | 2016-06-14 10:58:38 -0700 | [diff] [blame] | 55 | For Android client, use `grpc-okhttp` instead of `grpc-netty` and |
| Eric Anderson | aa33c59 | 2016-07-29 12:38:03 -0700 | [diff] [blame] | 56 | `grpc-protobuf-lite` or `grpc-protobuf-nano` instead of `grpc-protobuf`: |
| Xudong Ma | 996ea2f | 2015-10-07 11:03:41 -0700 | [diff] [blame] | 57 | ```gradle |
| Carl Mastrangelo | 128f54c | 2018-07-31 12:19:20 -0700 | [diff] [blame] | 58 | compile 'io.grpc:grpc-okhttp:1.14.0' |
| 59 | compile 'io.grpc:grpc-protobuf-lite:1.14.0' |
| 60 | compile 'io.grpc:grpc-stub:1.14.0' |
| Xudong Ma | 996ea2f | 2015-10-07 11:03:41 -0700 | [diff] [blame] | 61 | ``` |
| 62 | |
| Eric Anderson | 4e8cf92 | 2016-05-12 13:00:47 -0700 | [diff] [blame] | 63 | [the JARs]: |
| Carl Mastrangelo | 128f54c | 2018-07-31 12:19:20 -0700 | [diff] [blame] | 64 | http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22io.grpc%22%20AND%20v%3A%221.14.0%22 |
| Eric Anderson | 1eb16d9 | 2015-05-26 14:22:32 -0700 | [diff] [blame] | 65 | |
| 66 | Development snapshots are available in [Sonatypes's snapshot |
| 67 | repository](https://oss.sonatype.org/content/repositories/snapshots/). |
| 68 | |
| Eric Anderson | 52f1705 | 2016-01-27 10:33:32 -0800 | [diff] [blame] | 69 | For protobuf-based codegen, you can put your proto files in the `src/main/proto` |
| 70 | and `src/test/proto` directories along with an appropriate plugin. |
| 71 | |
| Eric Anderson | 1eb16d9 | 2015-05-26 14:22:32 -0700 | [diff] [blame] | 72 | For protobuf-based codegen integrated with the Maven build system, you can use |
| Eric Anderson | 06697b5 | 2016-11-14 21:04:22 -0800 | [diff] [blame] | 73 | [protobuf-maven-plugin][] (Eclipse and NetBeans users should also look at |
| 74 | `os-maven-plugin`'s |
| 75 | [IDE documentation](https://github.com/trustin/os-maven-plugin#issues-with-eclipse-m2e-or-other-ides)): |
| Eric Anderson | 1eb16d9 | 2015-05-26 14:22:32 -0700 | [diff] [blame] | 76 | ```xml |
| 77 | <build> |
| 78 | <extensions> |
| 79 | <extension> |
| 80 | <groupId>kr.motd.maven</groupId> |
| 81 | <artifactId>os-maven-plugin</artifactId> |
| Carl Mastrangelo | 2175004 | 2017-07-18 10:44:37 -0700 | [diff] [blame] | 82 | <version>1.5.0.Final</version> |
| Eric Anderson | 1eb16d9 | 2015-05-26 14:22:32 -0700 | [diff] [blame] | 83 | </extension> |
| 84 | </extensions> |
| 85 | <plugins> |
| 86 | <plugin> |
| Eric Anderson | 41c3f78 | 2016-02-16 15:29:24 -0800 | [diff] [blame] | 87 | <groupId>org.xolstice.maven.plugins</groupId> |
| 88 | <artifactId>protobuf-maven-plugin</artifactId> |
| Jorg Heymans | 2094bb4 | 2018-04-04 01:51:56 +0200 | [diff] [blame] | 89 | <version>0.5.1</version> |
| Eric Anderson | 1eb16d9 | 2015-05-26 14:22:32 -0700 | [diff] [blame] | 90 | <configuration> |
| Kun Zhang | 86d6412 | 2018-01-05 16:40:20 -0800 | [diff] [blame] | 91 | <protocArtifact>com.google.protobuf:protoc:3.5.1-1:exe:${os.detected.classifier}</protocArtifact> |
| Eric Anderson | 1eb16d9 | 2015-05-26 14:22:32 -0700 | [diff] [blame] | 92 | <pluginId>grpc-java</pluginId> |
| Carl Mastrangelo | 128f54c | 2018-07-31 12:19:20 -0700 | [diff] [blame] | 93 | <pluginArtifact>io.grpc:protoc-gen-grpc-java:1.14.0:exe:${os.detected.classifier}</pluginArtifact> |
| Eric Anderson | 1eb16d9 | 2015-05-26 14:22:32 -0700 | [diff] [blame] | 94 | </configuration> |
| 95 | <executions> |
| 96 | <execution> |
| 97 | <goals> |
| 98 | <goal>compile</goal> |
| 99 | <goal>compile-custom</goal> |
| 100 | </goals> |
| 101 | </execution> |
| 102 | </executions> |
| 103 | </plugin> |
| 104 | </plugins> |
| 105 | </build> |
| 106 | ``` |
| 107 | |
| Eric Anderson | 41c3f78 | 2016-02-16 15:29:24 -0800 | [diff] [blame] | 108 | [protobuf-maven-plugin]: https://www.xolstice.org/protobuf-maven-plugin/ |
| Eric Anderson | 1eb16d9 | 2015-05-26 14:22:32 -0700 | [diff] [blame] | 109 | |
| 110 | For protobuf-based codegen integrated with the Gradle build system, you can use |
| 111 | [protobuf-gradle-plugin][]: |
| 112 | ```gradle |
| Kun Zhang | 8bd8ed8 | 2015-07-22 09:19:52 -0700 | [diff] [blame] | 113 | apply plugin: 'java' |
| Eric Anderson | 1eb16d9 | 2015-05-26 14:22:32 -0700 | [diff] [blame] | 114 | apply plugin: 'com.google.protobuf' |
| 115 | |
| 116 | buildscript { |
| 117 | repositories { |
| 118 | mavenCentral() |
| 119 | } |
| 120 | dependencies { |
| Eric Anderson | 446f0cb | 2016-05-07 11:24:15 -0700 | [diff] [blame] | 121 | // ASSUMES GRADLE 2.12 OR HIGHER. Use plugin version 0.7.5 with earlier |
| 122 | // gradle versions |
| Eric Anderson | 71bad37 | 2018-01-11 14:27:50 -0800 | [diff] [blame] | 123 | classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.3' |
| Eric Anderson | 1eb16d9 | 2015-05-26 14:22:32 -0700 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
| Kun Zhang | 8bd8ed8 | 2015-07-22 09:19:52 -0700 | [diff] [blame] | 127 | protobuf { |
| 128 | protoc { |
| Kun Zhang | 86d6412 | 2018-01-05 16:40:20 -0800 | [diff] [blame] | 129 | artifact = "com.google.protobuf:protoc:3.5.1-1" |
| Kun Zhang | 8bd8ed8 | 2015-07-22 09:19:52 -0700 | [diff] [blame] | 130 | } |
| 131 | plugins { |
| 132 | grpc { |
| Carl Mastrangelo | 128f54c | 2018-07-31 12:19:20 -0700 | [diff] [blame] | 133 | artifact = 'io.grpc:protoc-gen-grpc-java:1.14.0' |
| Kun Zhang | 8bd8ed8 | 2015-07-22 09:19:52 -0700 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | generateProtoTasks { |
| 137 | all()*.plugins { |
| 138 | grpc {} |
| Eric Anderson | 1eb16d9 | 2015-05-26 14:22:32 -0700 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | } |
| Eric Anderson | 1eb16d9 | 2015-05-26 14:22:32 -0700 | [diff] [blame] | 142 | ``` |
| 143 | |
| 144 | [protobuf-gradle-plugin]: https://github.com/google/protobuf-gradle-plugin |
| 145 | |
| Eric Anderson | fb28ad2 | 2015-01-29 15:00:58 -0800 | [diff] [blame] | 146 | How to Build |
| 147 | ------------ |
| 148 | |
| Eric Anderson | 1b1c646 | 2015-08-14 08:49:56 -0700 | [diff] [blame] | 149 | If you are making changes to gRPC-Java, see the [compiling |
| 150 | instructions](COMPILING.md). |
| Kun Zhang | 9805e27 | 2015-05-12 17:03:19 -0700 | [diff] [blame] | 151 | |
| Eric Anderson | fb28ad2 | 2015-01-29 15:00:58 -0800 | [diff] [blame] | 152 | Navigating Around the Source |
| 153 | ---------------------------- |
| 154 | |
| mekka | 9990794 | 2015-08-07 23:28:06 -0700 | [diff] [blame] | 155 | 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] | 156 | level there are three distinct layers to the library: __Stub__, __Channel__ & |
| 157 | __Transport__. |
| 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 | ### Stub |
| 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 Stub layer is what is exposed to most developers and provides type-safe |
| 162 | bindings to whatever datamodel/IDL/interface you are adapting. gRPC comes with |
| 163 | a [plugin](https://github.com/google/grpc-java/blob/master/compiler) to the |
| 164 | protocol-buffers compiler that generates Stub interfaces out of `.proto` files, |
| 165 | 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] | 166 | |
| 167 | #### Key Interfaces |
| 168 | |
| nmittler | f831458 | 2015-01-27 10:25:39 -0800 | [diff] [blame] | 169 | [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] | 170 | |
| Eric Anderson | fb28ad2 | 2015-01-29 15:00:58 -0800 | [diff] [blame] | 171 | ### Channel |
| ejona | d4531da | 2014-12-15 10:24:42 -0800 | [diff] [blame] | 172 | |
| Kun Zhang | fc85a40 | 2015-08-05 16:48:15 -0700 | [diff] [blame] | 173 | The Channel layer is an abstraction over Transport handling that is suitable for |
| 174 | interception/decoration and exposes more behavior to the application than the |
| 175 | Stub layer. It is intended to be easy for application frameworks to use this |
| 176 | layer to address cross-cutting concerns such as logging, monitoring, auth etc. |
| 177 | Flow-control is also exposed at this layer to allow more sophisticated |
| 178 | applications to interact with it directly. |
| ejona | d4531da | 2014-12-15 10:24:42 -0800 | [diff] [blame] | 179 | |
| 180 | #### Common |
| 181 | |
| nmittler | f831458 | 2015-01-27 10:25:39 -0800 | [diff] [blame] | 182 | * [Metadata - headers & trailers](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/Metadata.java) |
| 183 | * [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] | 184 | |
| 185 | #### Client |
| nmittler | f831458 | 2015-01-27 10:25:39 -0800 | [diff] [blame] | 186 | * [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] | 187 | * [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] | 188 | * [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] | 189 | |
| 190 | #### Server |
| nmittler | f831458 | 2015-01-27 10:25:39 -0800 | [diff] [blame] | 191 | * [Server call handler - analog to Channel on server](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/ServerCallHandler.java) |
| 192 | * [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] | 193 | |
| 194 | |
| Eric Anderson | fb28ad2 | 2015-01-29 15:00:58 -0800 | [diff] [blame] | 195 | ### Transport |
| ejona | d4531da | 2014-12-15 10:24:42 -0800 | [diff] [blame] | 196 | |
| mekka | 9990794 | 2015-08-07 23:28:06 -0700 | [diff] [blame] | 197 | 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] | 198 | wire. The interfaces to it are abstract just enough to allow plugging in of |
| 199 | different implementations. Transports are modeled as `Stream` factories. The |
| 200 | variation in interface between a server Stream and a client Stream exists to |
| 201 | codify their differing semantics for cancellation and error reporting. |
| 202 | |
| 203 | Note the transport layer API is considered internal to gRPC and has weaker API |
| 204 | guarantees than the core API under package `io.grpc`. |
| 205 | |
| mekka | 9990794 | 2015-08-07 23:28:06 -0700 | [diff] [blame] | 206 | gRPC comes with three Transport implementations: |
| Kun Zhang | fc85a40 | 2015-08-05 16:48:15 -0700 | [diff] [blame] | 207 | |
| 208 | 1. The [Netty-based](https://github.com/google/grpc-java/blob/master/netty) |
| 209 | transport is the main transport implementation based on |
| 210 | [Netty](http://netty.io). It is for both the client and the server. |
| 211 | 2. The [OkHttp-based](https://github.com/google/grpc-java/blob/master/okhttp) |
| 212 | transport is a lightweight transport based on |
| 213 | [OkHttp](http://square.github.io/okhttp/). It is mainly for use on Android |
| mekka | 9990794 | 2015-08-07 23:28:06 -0700 | [diff] [blame] | 214 | and is for client only. |
| Kun Zhang | fc85a40 | 2015-08-05 16:48:15 -0700 | [diff] [blame] | 215 | 3. The |
| Kun Zhang | e1bd6ef | 2015-08-11 11:17:39 -0700 | [diff] [blame] | 216 | [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] | 217 | transport is for when a server is in the same process as the client. It is |
| 218 | useful for testing. |
| ejona | d4531da | 2014-12-15 10:24:42 -0800 | [diff] [blame] | 219 | |
| 220 | #### Common |
| 221 | |
| Kun Zhang | e1bd6ef | 2015-08-11 11:17:39 -0700 | [diff] [blame] | 222 | * [Stream](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/internal/Stream.java) |
| 223 | * [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] | 224 | |
| 225 | #### Client |
| 226 | |
| Kun Zhang | e1bd6ef | 2015-08-11 11:17:39 -0700 | [diff] [blame] | 227 | * [Client Stream](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/internal/ClientStream.java) |
| 228 | * [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] | 229 | |
| 230 | #### Server |
| 231 | |
| Kun Zhang | e1bd6ef | 2015-08-11 11:17:39 -0700 | [diff] [blame] | 232 | * [Server Stream](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/internal/ServerStream.java) |
| 233 | * [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] | 234 | |
| 235 | |
| Kun Zhang | d0d2c1e | 2017-01-18 09:06:45 -0800 | [diff] [blame] | 236 | Examples |
| 237 | -------- |
| ejona | d4531da | 2014-12-15 10:24:42 -0800 | [diff] [blame] | 238 | |
| Kun Zhang | d0d2c1e | 2017-01-18 09:06:45 -0800 | [diff] [blame] | 239 | The [examples](https://github.com/grpc/grpc-java/tree/master/examples) |
| 240 | and the |
| 241 | [Android example](https://github.com/grpc/grpc-java/tree/master/examples/android) are standalone projects that |
| 242 | showcase the usage of gRPC. |
| Shohei Kamimori | 5af2515 | 2018-03-29 07:34:18 +0900 | [diff] [blame] | 243 | |
| 244 | Tools |
| 245 | ----- |
| 246 | |
| 247 | APIs annotated with `@Internal` are for internal use by the gRPC library and |
| 248 | should not be used by gRPC users. APIs annotated with `@ExperimentalApi` are |
| 249 | subject to change in future releases, and library code that other projects |
| 250 | may depend on should not use these APIs. We recommend using the |
| 251 | [grpc-java-api-checker](https://github.com/grpc/grpc-java-api-checker) |
| 252 | (an [Error Prone](https://github.com/google/error-prone) plugin) |
| 253 | to check for usages of `@ExperimentalApi` and `@Internal` in any library code |
| 254 | that depends on gRPC. It may also be used to check for `@Internal` usage or |
| 255 | unintended `@ExperimentalApi` consumption in non-library code. |