| 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 | 631e46c | 2018-08-14 08:02:22 -0700 | [diff] [blame] | 26 | Getting Started |
| 27 | --------------- |
| 28 | |
| 29 | For a guided tour, take a look at the [quick start |
| 30 | guide](https://grpc.io/docs/quickstart/java.html) or the more explanatory [gRPC |
| 31 | basics](https://grpc.io/docs/tutorials/basic/java.html). |
| 32 | |
| 33 | The [examples](https://github.com/grpc/grpc-java/tree/v1.14.0/examples) and the |
| 34 | [Android example](https://github.com/grpc/grpc-java/tree/v1.14.0/examples/android) |
| 35 | are standalone projects that showcase the usage of gRPC. |
| 36 | |
| Eric Anderson | 1eb16d9 | 2015-05-26 14:22:32 -0700 | [diff] [blame] | 37 | Download |
| 38 | -------- |
| 39 | |
| Eric Anderson | a62e976 | 2016-06-14 10:58:38 -0700 | [diff] [blame] | 40 | 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] | 41 | ```xml |
| 42 | <dependency> |
| 43 | <groupId>io.grpc</groupId> |
| Eric Anderson | b64cde1 | 2018-08-02 16:00:12 -0700 | [diff] [blame] | 44 | <artifactId>grpc-netty-shaded</artifactId> |
| Carl Mastrangelo | 128f54c | 2018-07-31 12:19:20 -0700 | [diff] [blame] | 45 | <version>1.14.0</version> |
| Eric Anderson | a62e976 | 2016-06-14 10:58:38 -0700 | [diff] [blame] | 46 | </dependency> |
| 47 | <dependency> |
| 48 | <groupId>io.grpc</groupId> |
| 49 | <artifactId>grpc-protobuf</artifactId> |
| Carl Mastrangelo | 128f54c | 2018-07-31 12:19:20 -0700 | [diff] [blame] | 50 | <version>1.14.0</version> |
| Eric Anderson | a62e976 | 2016-06-14 10:58:38 -0700 | [diff] [blame] | 51 | </dependency> |
| 52 | <dependency> |
| 53 | <groupId>io.grpc</groupId> |
| 54 | <artifactId>grpc-stub</artifactId> |
| Carl Mastrangelo | 128f54c | 2018-07-31 12:19:20 -0700 | [diff] [blame] | 55 | <version>1.14.0</version> |
| Eric Anderson | 1eb16d9 | 2015-05-26 14:22:32 -0700 | [diff] [blame] | 56 | </dependency> |
| 57 | ``` |
| 58 | |
| Eric Anderson | a62e976 | 2016-06-14 10:58:38 -0700 | [diff] [blame] | 59 | Or for Gradle with non-Android, add to your dependencies: |
| Eric Anderson | 1eb16d9 | 2015-05-26 14:22:32 -0700 | [diff] [blame] | 60 | ```gradle |
| Eric Anderson | b64cde1 | 2018-08-02 16:00:12 -0700 | [diff] [blame] | 61 | compile 'io.grpc:grpc-netty-shaded:1.14.0' |
| Carl Mastrangelo | 128f54c | 2018-07-31 12:19:20 -0700 | [diff] [blame] | 62 | compile 'io.grpc:grpc-protobuf:1.14.0' |
| 63 | compile 'io.grpc:grpc-stub:1.14.0' |
| Eric Anderson | 1eb16d9 | 2015-05-26 14:22:32 -0700 | [diff] [blame] | 64 | ``` |
| 65 | |
| Eric Anderson | b64cde1 | 2018-08-02 16:00:12 -0700 | [diff] [blame] | 66 | For Android client, use `grpc-okhttp` instead of `grpc-netty-shaded` and |
| Eric Anderson | 631e46c | 2018-08-14 08:02:22 -0700 | [diff] [blame] | 67 | `grpc-protobuf-lite` instead of `grpc-protobuf`: |
| Xudong Ma | 996ea2f | 2015-10-07 11:03:41 -0700 | [diff] [blame] | 68 | ```gradle |
| Carl Mastrangelo | 128f54c | 2018-07-31 12:19:20 -0700 | [diff] [blame] | 69 | compile 'io.grpc:grpc-okhttp:1.14.0' |
| 70 | compile 'io.grpc:grpc-protobuf-lite:1.14.0' |
| 71 | compile 'io.grpc:grpc-stub:1.14.0' |
| Xudong Ma | 996ea2f | 2015-10-07 11:03:41 -0700 | [diff] [blame] | 72 | ``` |
| 73 | |
| Eric Anderson | 4e8cf92 | 2016-05-12 13:00:47 -0700 | [diff] [blame] | 74 | [the JARs]: |
| Carl Mastrangelo | 128f54c | 2018-07-31 12:19:20 -0700 | [diff] [blame] | 75 | 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] | 76 | |
| 77 | Development snapshots are available in [Sonatypes's snapshot |
| 78 | repository](https://oss.sonatype.org/content/repositories/snapshots/). |
| 79 | |
| Eric Anderson | 631e46c | 2018-08-14 08:02:22 -0700 | [diff] [blame] | 80 | Generated Code |
| 81 | -------------- |
| 82 | |
| Eric Anderson | 52f1705 | 2016-01-27 10:33:32 -0800 | [diff] [blame] | 83 | For protobuf-based codegen, you can put your proto files in the `src/main/proto` |
| 84 | and `src/test/proto` directories along with an appropriate plugin. |
| 85 | |
| Eric Anderson | 1eb16d9 | 2015-05-26 14:22:32 -0700 | [diff] [blame] | 86 | 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] | 87 | [protobuf-maven-plugin][] (Eclipse and NetBeans users should also look at |
| 88 | `os-maven-plugin`'s |
| 89 | [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] | 90 | ```xml |
| 91 | <build> |
| 92 | <extensions> |
| 93 | <extension> |
| 94 | <groupId>kr.motd.maven</groupId> |
| 95 | <artifactId>os-maven-plugin</artifactId> |
| Carl Mastrangelo | 2175004 | 2017-07-18 10:44:37 -0700 | [diff] [blame] | 96 | <version>1.5.0.Final</version> |
| Eric Anderson | 1eb16d9 | 2015-05-26 14:22:32 -0700 | [diff] [blame] | 97 | </extension> |
| 98 | </extensions> |
| 99 | <plugins> |
| 100 | <plugin> |
| Eric Anderson | 41c3f78 | 2016-02-16 15:29:24 -0800 | [diff] [blame] | 101 | <groupId>org.xolstice.maven.plugins</groupId> |
| 102 | <artifactId>protobuf-maven-plugin</artifactId> |
| Jorg Heymans | 2094bb4 | 2018-04-04 01:51:56 +0200 | [diff] [blame] | 103 | <version>0.5.1</version> |
| Eric Anderson | 1eb16d9 | 2015-05-26 14:22:32 -0700 | [diff] [blame] | 104 | <configuration> |
| Kun Zhang | 86d6412 | 2018-01-05 16:40:20 -0800 | [diff] [blame] | 105 | <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] | 106 | <pluginId>grpc-java</pluginId> |
| Carl Mastrangelo | 128f54c | 2018-07-31 12:19:20 -0700 | [diff] [blame] | 107 | <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] | 108 | </configuration> |
| 109 | <executions> |
| 110 | <execution> |
| 111 | <goals> |
| 112 | <goal>compile</goal> |
| 113 | <goal>compile-custom</goal> |
| 114 | </goals> |
| 115 | </execution> |
| 116 | </executions> |
| 117 | </plugin> |
| 118 | </plugins> |
| 119 | </build> |
| 120 | ``` |
| 121 | |
| Eric Anderson | 41c3f78 | 2016-02-16 15:29:24 -0800 | [diff] [blame] | 122 | [protobuf-maven-plugin]: https://www.xolstice.org/protobuf-maven-plugin/ |
| Eric Anderson | 1eb16d9 | 2015-05-26 14:22:32 -0700 | [diff] [blame] | 123 | |
| 124 | For protobuf-based codegen integrated with the Gradle build system, you can use |
| 125 | [protobuf-gradle-plugin][]: |
| 126 | ```gradle |
| 127 | apply plugin: 'com.google.protobuf' |
| 128 | |
| 129 | buildscript { |
| 130 | repositories { |
| 131 | mavenCentral() |
| 132 | } |
| 133 | dependencies { |
| Eric Anderson | c42d5bb | 2018-08-03 09:43:32 -0700 | [diff] [blame] | 134 | classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.5' |
| Eric Anderson | 1eb16d9 | 2015-05-26 14:22:32 -0700 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | |
| Kun Zhang | 8bd8ed8 | 2015-07-22 09:19:52 -0700 | [diff] [blame] | 138 | protobuf { |
| 139 | protoc { |
| Kun Zhang | 86d6412 | 2018-01-05 16:40:20 -0800 | [diff] [blame] | 140 | artifact = "com.google.protobuf:protoc:3.5.1-1" |
| Kun Zhang | 8bd8ed8 | 2015-07-22 09:19:52 -0700 | [diff] [blame] | 141 | } |
| 142 | plugins { |
| 143 | grpc { |
| Carl Mastrangelo | 128f54c | 2018-07-31 12:19:20 -0700 | [diff] [blame] | 144 | artifact = 'io.grpc:protoc-gen-grpc-java:1.14.0' |
| Kun Zhang | 8bd8ed8 | 2015-07-22 09:19:52 -0700 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | generateProtoTasks { |
| 148 | all()*.plugins { |
| 149 | grpc {} |
| Eric Anderson | 1eb16d9 | 2015-05-26 14:22:32 -0700 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | } |
| Eric Anderson | 1eb16d9 | 2015-05-26 14:22:32 -0700 | [diff] [blame] | 153 | ``` |
| 154 | |
| 155 | [protobuf-gradle-plugin]: https://github.com/google/protobuf-gradle-plugin |
| 156 | |
| Eric Anderson | 631e46c | 2018-08-14 08:02:22 -0700 | [diff] [blame] | 157 | API Stability |
| 158 | ------------- |
| 159 | |
| 160 | APIs annotated with `@Internal` are for internal use by the gRPC library and |
| 161 | should not be used by gRPC users. APIs annotated with `@ExperimentalApi` are |
| 162 | subject to change in future releases, and library code that other projects |
| 163 | may depend on should not use these APIs. |
| 164 | |
| 165 | We recommend using the |
| 166 | [grpc-java-api-checker](https://github.com/grpc/grpc-java-api-checker) |
| 167 | (an [Error Prone](https://github.com/google/error-prone) plugin) |
| 168 | to check for usages of `@ExperimentalApi` and `@Internal` in any library code |
| 169 | that depends on gRPC. It may also be used to check for `@Internal` usage or |
| 170 | unintended `@ExperimentalApi` consumption in non-library code. |
| 171 | |
| Eric Anderson | fb28ad2 | 2015-01-29 15:00:58 -0800 | [diff] [blame] | 172 | How to Build |
| 173 | ------------ |
| 174 | |
| Eric Anderson | 1b1c646 | 2015-08-14 08:49:56 -0700 | [diff] [blame] | 175 | If you are making changes to gRPC-Java, see the [compiling |
| 176 | instructions](COMPILING.md). |
| Kun Zhang | 9805e27 | 2015-05-12 17:03:19 -0700 | [diff] [blame] | 177 | |
| Eric Anderson | 631e46c | 2018-08-14 08:02:22 -0700 | [diff] [blame] | 178 | High-level Components |
| 179 | --------------------- |
| Eric Anderson | fb28ad2 | 2015-01-29 15:00:58 -0800 | [diff] [blame] | 180 | |
| Eric Anderson | 631e46c | 2018-08-14 08:02:22 -0700 | [diff] [blame] | 181 | At a high level there are three distinct layers to the library: *Stub*, |
| 182 | *Channel*, and *Transport*. |
| ejona | d4531da | 2014-12-15 10:24:42 -0800 | [diff] [blame] | 183 | |
| Eric Anderson | fb28ad2 | 2015-01-29 15:00:58 -0800 | [diff] [blame] | 184 | ### Stub |
| ejona | d4531da | 2014-12-15 10:24:42 -0800 | [diff] [blame] | 185 | |
| Kun Zhang | fc85a40 | 2015-08-05 16:48:15 -0700 | [diff] [blame] | 186 | The Stub layer is what is exposed to most developers and provides type-safe |
| 187 | bindings to whatever datamodel/IDL/interface you are adapting. gRPC comes with |
| 188 | a [plugin](https://github.com/google/grpc-java/blob/master/compiler) to the |
| 189 | protocol-buffers compiler that generates Stub interfaces out of `.proto` files, |
| Eric Anderson | 631e46c | 2018-08-14 08:02:22 -0700 | [diff] [blame] | 190 | but bindings to other datamodel/IDL are easy and encouraged. |
| ejona | d4531da | 2014-12-15 10:24:42 -0800 | [diff] [blame] | 191 | |
| Eric Anderson | fb28ad2 | 2015-01-29 15:00:58 -0800 | [diff] [blame] | 192 | ### Channel |
| ejona | d4531da | 2014-12-15 10:24:42 -0800 | [diff] [blame] | 193 | |
| Kun Zhang | fc85a40 | 2015-08-05 16:48:15 -0700 | [diff] [blame] | 194 | The Channel layer is an abstraction over Transport handling that is suitable for |
| 195 | interception/decoration and exposes more behavior to the application than the |
| 196 | Stub layer. It is intended to be easy for application frameworks to use this |
| Eric Anderson | 631e46c | 2018-08-14 08:02:22 -0700 | [diff] [blame] | 197 | layer to address cross-cutting concerns such as logging, monitoring, auth, etc. |
| ejona | d4531da | 2014-12-15 10:24:42 -0800 | [diff] [blame] | 198 | |
| Eric Anderson | fb28ad2 | 2015-01-29 15:00:58 -0800 | [diff] [blame] | 199 | ### Transport |
| ejona | d4531da | 2014-12-15 10:24:42 -0800 | [diff] [blame] | 200 | |
| mekka | 9990794 | 2015-08-07 23:28:06 -0700 | [diff] [blame] | 201 | 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] | 202 | wire. The interfaces to it are abstract just enough to allow plugging in of |
| Eric Anderson | 631e46c | 2018-08-14 08:02:22 -0700 | [diff] [blame] | 203 | different implementations. Note the transport layer API is considered internal |
| 204 | to gRPC and has weaker API guarantees than the core API under package `io.grpc`. |
| Kun Zhang | fc85a40 | 2015-08-05 16:48:15 -0700 | [diff] [blame] | 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 | |
| Eric Anderson | 631e46c | 2018-08-14 08:02:22 -0700 | [diff] [blame] | 208 | 1. The Netty-based transport is the main transport implementation based on |
| Kun Zhang | fc85a40 | 2015-08-05 16:48:15 -0700 | [diff] [blame] | 209 | [Netty](http://netty.io). It is for both the client and the server. |
| Eric Anderson | 631e46c | 2018-08-14 08:02:22 -0700 | [diff] [blame] | 210 | 2. The OkHttp-based transport is a lightweight transport based on |
| Kun Zhang | fc85a40 | 2015-08-05 16:48:15 -0700 | [diff] [blame] | 211 | [OkHttp](http://square.github.io/okhttp/). It is mainly for use on Android |
| mekka | 9990794 | 2015-08-07 23:28:06 -0700 | [diff] [blame] | 212 | and is for client only. |
| Eric Anderson | 631e46c | 2018-08-14 08:02:22 -0700 | [diff] [blame] | 213 | 3. The in-process transport is for when a server is in the same process as the |
| 214 | client. It is useful for testing, while also being safe for production use. |