blob: 7722ada214f03581098e8d495f26b30cead1e7b3 [file] [log] [blame] [view]
Kun Zhangd32bb472015-05-06 16:15:53 -07001gRPC-Java - An RPC library and framework
2========================================
3
Eric Gribkoff785ac472018-04-30 10:28:41 -07004gRPC-Java works with JDK 6. gRPC-Java clients are supported on Android API
5levels 14 and up (Ice Cream Sandwich and later). Deploying gRPC servers on an
6Android device is not supported.
Eric Gribkoffb9400842018-01-17 10:18:33 -08007
8TLS usage typically requires using Java 8, or Play Services Dynamic Security
9Provider on Android. Please see the [Security Readme](SECURITY.md).
ejonad4531da2014-12-15 10:24:42 -080010
nmittlerb7a72c12015-09-24 12:03:23 -070011<table>
12 <tr>
13 <td><b>Homepage:</b></td>
Mehrdad Afshari8ce0bc22017-07-10 22:48:17 +000014 <td><a href="https://grpc.io/">grpc.io</a></td>
nmittlerb7a72c12015-09-24 12:03:23 -070015 </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 Anderson67ff1702016-12-06 09:27:10 -080022[![Join the chat at https://gitter.im/grpc/grpc](https://badges.gitter.im/grpc/grpc.svg)](https://gitter.im/grpc/grpc?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
nmittlerb7a72c12015-09-24 12:03:23 -070023[![Build Status](https://travis-ci.org/grpc/grpc-java.svg?branch=master)](https://travis-ci.org/grpc/grpc-java)
24[![Coverage Status](https://coveralls.io/repos/grpc/grpc-java/badge.svg?branch=master&service=github)](https://coveralls.io/github/grpc/grpc-java?branch=master)
25
Eric Anderson631e46c2018-08-14 08:02:22 -070026Getting Started
27---------------
28
29For a guided tour, take a look at the [quick start
30guide](https://grpc.io/docs/quickstart/java.html) or the more explanatory [gRPC
31basics](https://grpc.io/docs/tutorials/basic/java.html).
32
33The [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)
35are standalone projects that showcase the usage of gRPC.
36
Eric Anderson1eb16d92015-05-26 14:22:32 -070037Download
38--------
39
Eric Andersona62e9762016-06-14 10:58:38 -070040Download [the JARs][]. Or for Maven with non-Android, add to your `pom.xml`:
Eric Anderson1eb16d92015-05-26 14:22:32 -070041```xml
42<dependency>
43 <groupId>io.grpc</groupId>
Eric Andersonb64cde12018-08-02 16:00:12 -070044 <artifactId>grpc-netty-shaded</artifactId>
Carl Mastrangelo128f54c2018-07-31 12:19:20 -070045 <version>1.14.0</version>
Eric Andersona62e9762016-06-14 10:58:38 -070046</dependency>
47<dependency>
48 <groupId>io.grpc</groupId>
49 <artifactId>grpc-protobuf</artifactId>
Carl Mastrangelo128f54c2018-07-31 12:19:20 -070050 <version>1.14.0</version>
Eric Andersona62e9762016-06-14 10:58:38 -070051</dependency>
52<dependency>
53 <groupId>io.grpc</groupId>
54 <artifactId>grpc-stub</artifactId>
Carl Mastrangelo128f54c2018-07-31 12:19:20 -070055 <version>1.14.0</version>
Eric Anderson1eb16d92015-05-26 14:22:32 -070056</dependency>
57```
58
Eric Andersona62e9762016-06-14 10:58:38 -070059Or for Gradle with non-Android, add to your dependencies:
Eric Anderson1eb16d92015-05-26 14:22:32 -070060```gradle
Eric Andersonb64cde12018-08-02 16:00:12 -070061compile 'io.grpc:grpc-netty-shaded:1.14.0'
Carl Mastrangelo128f54c2018-07-31 12:19:20 -070062compile 'io.grpc:grpc-protobuf:1.14.0'
63compile 'io.grpc:grpc-stub:1.14.0'
Eric Anderson1eb16d92015-05-26 14:22:32 -070064```
65
Eric Andersonb64cde12018-08-02 16:00:12 -070066For Android client, use `grpc-okhttp` instead of `grpc-netty-shaded` and
Eric Anderson631e46c2018-08-14 08:02:22 -070067`grpc-protobuf-lite` instead of `grpc-protobuf`:
Xudong Ma996ea2f2015-10-07 11:03:41 -070068```gradle
Carl Mastrangelo128f54c2018-07-31 12:19:20 -070069compile 'io.grpc:grpc-okhttp:1.14.0'
70compile 'io.grpc:grpc-protobuf-lite:1.14.0'
71compile 'io.grpc:grpc-stub:1.14.0'
Xudong Ma996ea2f2015-10-07 11:03:41 -070072```
73
Eric Anderson4e8cf922016-05-12 13:00:47 -070074[the JARs]:
Carl Mastrangelo128f54c2018-07-31 12:19:20 -070075http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22io.grpc%22%20AND%20v%3A%221.14.0%22
Eric Anderson1eb16d92015-05-26 14:22:32 -070076
77Development snapshots are available in [Sonatypes's snapshot
78repository](https://oss.sonatype.org/content/repositories/snapshots/).
79
Eric Anderson631e46c2018-08-14 08:02:22 -070080Generated Code
81--------------
82
Eric Anderson52f17052016-01-27 10:33:32 -080083For protobuf-based codegen, you can put your proto files in the `src/main/proto`
84and `src/test/proto` directories along with an appropriate plugin.
85
Eric Anderson1eb16d92015-05-26 14:22:32 -070086For protobuf-based codegen integrated with the Maven build system, you can use
Eric Anderson06697b52016-11-14 21:04:22 -080087[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 Anderson1eb16d92015-05-26 14:22:32 -070090```xml
91<build>
92 <extensions>
93 <extension>
94 <groupId>kr.motd.maven</groupId>
95 <artifactId>os-maven-plugin</artifactId>
Carl Mastrangelo21750042017-07-18 10:44:37 -070096 <version>1.5.0.Final</version>
Eric Anderson1eb16d92015-05-26 14:22:32 -070097 </extension>
98 </extensions>
99 <plugins>
100 <plugin>
Eric Anderson41c3f782016-02-16 15:29:24 -0800101 <groupId>org.xolstice.maven.plugins</groupId>
102 <artifactId>protobuf-maven-plugin</artifactId>
Jorg Heymans2094bb42018-04-04 01:51:56 +0200103 <version>0.5.1</version>
Eric Anderson1eb16d92015-05-26 14:22:32 -0700104 <configuration>
Kun Zhang86d64122018-01-05 16:40:20 -0800105 <protocArtifact>com.google.protobuf:protoc:3.5.1-1:exe:${os.detected.classifier}</protocArtifact>
Eric Anderson1eb16d92015-05-26 14:22:32 -0700106 <pluginId>grpc-java</pluginId>
Carl Mastrangelo128f54c2018-07-31 12:19:20 -0700107 <pluginArtifact>io.grpc:protoc-gen-grpc-java:1.14.0:exe:${os.detected.classifier}</pluginArtifact>
Eric Anderson1eb16d92015-05-26 14:22:32 -0700108 </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 Anderson41c3f782016-02-16 15:29:24 -0800122[protobuf-maven-plugin]: https://www.xolstice.org/protobuf-maven-plugin/
Eric Anderson1eb16d92015-05-26 14:22:32 -0700123
124For protobuf-based codegen integrated with the Gradle build system, you can use
125[protobuf-gradle-plugin][]:
126```gradle
127apply plugin: 'com.google.protobuf'
128
129buildscript {
130 repositories {
131 mavenCentral()
132 }
133 dependencies {
Eric Andersonc42d5bb2018-08-03 09:43:32 -0700134 classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.5'
Eric Anderson1eb16d92015-05-26 14:22:32 -0700135 }
136}
137
Kun Zhang8bd8ed82015-07-22 09:19:52 -0700138protobuf {
139 protoc {
Kun Zhang86d64122018-01-05 16:40:20 -0800140 artifact = "com.google.protobuf:protoc:3.5.1-1"
Kun Zhang8bd8ed82015-07-22 09:19:52 -0700141 }
142 plugins {
143 grpc {
Carl Mastrangelo128f54c2018-07-31 12:19:20 -0700144 artifact = 'io.grpc:protoc-gen-grpc-java:1.14.0'
Kun Zhang8bd8ed82015-07-22 09:19:52 -0700145 }
146 }
147 generateProtoTasks {
148 all()*.plugins {
149 grpc {}
Eric Anderson1eb16d92015-05-26 14:22:32 -0700150 }
151 }
152}
Eric Anderson1eb16d92015-05-26 14:22:32 -0700153```
154
155[protobuf-gradle-plugin]: https://github.com/google/protobuf-gradle-plugin
156
Eric Anderson631e46c2018-08-14 08:02:22 -0700157API Stability
158-------------
159
160APIs annotated with `@Internal` are for internal use by the gRPC library and
161should not be used by gRPC users. APIs annotated with `@ExperimentalApi` are
162subject to change in future releases, and library code that other projects
163may depend on should not use these APIs.
164
165We 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)
168to check for usages of `@ExperimentalApi` and `@Internal` in any library code
169that depends on gRPC. It may also be used to check for `@Internal` usage or
170unintended `@ExperimentalApi` consumption in non-library code.
171
Eric Andersonfb28ad22015-01-29 15:00:58 -0800172How to Build
173------------
174
Eric Anderson1b1c6462015-08-14 08:49:56 -0700175If you are making changes to gRPC-Java, see the [compiling
176instructions](COMPILING.md).
Kun Zhang9805e272015-05-12 17:03:19 -0700177
Eric Anderson631e46c2018-08-14 08:02:22 -0700178High-level Components
179---------------------
Eric Andersonfb28ad22015-01-29 15:00:58 -0800180
Eric Anderson631e46c2018-08-14 08:02:22 -0700181At a high level there are three distinct layers to the library: *Stub*,
182*Channel*, and *Transport*.
ejonad4531da2014-12-15 10:24:42 -0800183
Eric Andersonfb28ad22015-01-29 15:00:58 -0800184### Stub
ejonad4531da2014-12-15 10:24:42 -0800185
Kun Zhangfc85a402015-08-05 16:48:15 -0700186The Stub layer is what is exposed to most developers and provides type-safe
187bindings to whatever datamodel/IDL/interface you are adapting. gRPC comes with
188a [plugin](https://github.com/google/grpc-java/blob/master/compiler) to the
189protocol-buffers compiler that generates Stub interfaces out of `.proto` files,
Eric Anderson631e46c2018-08-14 08:02:22 -0700190but bindings to other datamodel/IDL are easy and encouraged.
ejonad4531da2014-12-15 10:24:42 -0800191
Eric Andersonfb28ad22015-01-29 15:00:58 -0800192### Channel
ejonad4531da2014-12-15 10:24:42 -0800193
Kun Zhangfc85a402015-08-05 16:48:15 -0700194The Channel layer is an abstraction over Transport handling that is suitable for
195interception/decoration and exposes more behavior to the application than the
196Stub layer. It is intended to be easy for application frameworks to use this
Eric Anderson631e46c2018-08-14 08:02:22 -0700197layer to address cross-cutting concerns such as logging, monitoring, auth, etc.
ejonad4531da2014-12-15 10:24:42 -0800198
Eric Andersonfb28ad22015-01-29 15:00:58 -0800199### Transport
ejonad4531da2014-12-15 10:24:42 -0800200
mekka99907942015-08-07 23:28:06 -0700201The Transport layer does the heavy lifting of putting and taking bytes off the
Kun Zhangfc85a402015-08-05 16:48:15 -0700202wire. The interfaces to it are abstract just enough to allow plugging in of
Eric Anderson631e46c2018-08-14 08:02:22 -0700203different implementations. Note the transport layer API is considered internal
204to gRPC and has weaker API guarantees than the core API under package `io.grpc`.
Kun Zhangfc85a402015-08-05 16:48:15 -0700205
mekka99907942015-08-07 23:28:06 -0700206gRPC comes with three Transport implementations:
Kun Zhangfc85a402015-08-05 16:48:15 -0700207
Eric Anderson631e46c2018-08-14 08:02:22 -07002081. The Netty-based transport is the main transport implementation based on
Kun Zhangfc85a402015-08-05 16:48:15 -0700209 [Netty](http://netty.io). It is for both the client and the server.
Eric Anderson631e46c2018-08-14 08:02:22 -07002102. The OkHttp-based transport is a lightweight transport based on
Kun Zhangfc85a402015-08-05 16:48:15 -0700211 [OkHttp](http://square.github.io/okhttp/). It is mainly for use on Android
mekka99907942015-08-07 23:28:06 -0700212 and is for client only.
Eric Anderson631e46c2018-08-14 08:02:22 -07002133. 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.