blob: 85f4c46507ab69b085ef26ec83dc67e32d05257f [file] [log] [blame] [view]
Kun Zhangd32bb472015-05-06 16:15:53 -07001gRPC-Java - An RPC library and framework
2========================================
3
Eric Gribkoffb9400842018-01-17 10:18:33 -08004gRPC-Java works with JDK 6. On Android, gRPC-Java supports Android API levels
514 and up (Ice Cream Sandwich and later).
6
7TLS usage typically requires using Java 8, or Play Services Dynamic Security
8Provider on Android. Please see the [Security Readme](SECURITY.md).
ejonad4531da2014-12-15 10:24:42 -08009
nmittlerb7a72c12015-09-24 12:03:23 -070010<table>
11 <tr>
12 <td><b>Homepage:</b></td>
Mehrdad Afshari8ce0bc22017-07-10 22:48:17 +000013 <td><a href="https://grpc.io/">grpc.io</a></td>
nmittlerb7a72c12015-09-24 12:03:23 -070014 </tr>
15 <tr>
16 <td><b>Mailing List:</b></td>
17 <td><a href="https://groups.google.com/forum/#!forum/grpc-io">grpc-io@googlegroups.com</a></td>
18 </tr>
19</table>
20
Eric Anderson67ff1702016-12-06 09:27:10 -080021[![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 -070022[![Build Status](https://travis-ci.org/grpc/grpc-java.svg?branch=master)](https://travis-ci.org/grpc/grpc-java)
23[![Coverage Status](https://coveralls.io/repos/grpc/grpc-java/badge.svg?branch=master&service=github)](https://coveralls.io/github/grpc/grpc-java?branch=master)
24
Eric Anderson1eb16d92015-05-26 14:22:32 -070025Download
26--------
27
Eric Andersona62e9762016-06-14 10:58:38 -070028Download [the JARs][]. Or for Maven with non-Android, add to your `pom.xml`:
Eric Anderson1eb16d92015-05-26 14:22:32 -070029```xml
30<dependency>
31 <groupId>io.grpc</groupId>
Eric Andersona62e9762016-06-14 10:58:38 -070032 <artifactId>grpc-netty</artifactId>
Eric Gribkoff0c27e712018-02-15 18:54:38 -080033 <version>1.10.0</version>
Eric Andersona62e9762016-06-14 10:58:38 -070034</dependency>
35<dependency>
36 <groupId>io.grpc</groupId>
37 <artifactId>grpc-protobuf</artifactId>
Eric Gribkoff0c27e712018-02-15 18:54:38 -080038 <version>1.10.0</version>
Eric Andersona62e9762016-06-14 10:58:38 -070039</dependency>
40<dependency>
41 <groupId>io.grpc</groupId>
42 <artifactId>grpc-stub</artifactId>
Eric Gribkoff0c27e712018-02-15 18:54:38 -080043 <version>1.10.0</version>
Eric Anderson1eb16d92015-05-26 14:22:32 -070044</dependency>
45```
46
Eric Andersona62e9762016-06-14 10:58:38 -070047Or for Gradle with non-Android, add to your dependencies:
Eric Anderson1eb16d92015-05-26 14:22:32 -070048```gradle
Eric Gribkoff0c27e712018-02-15 18:54:38 -080049compile 'io.grpc:grpc-netty:1.10.0'
50compile 'io.grpc:grpc-protobuf:1.10.0'
51compile 'io.grpc:grpc-stub:1.10.0'
Eric Anderson1eb16d92015-05-26 14:22:32 -070052```
53
Eric Andersona62e9762016-06-14 10:58:38 -070054For Android client, use `grpc-okhttp` instead of `grpc-netty` and
Eric Andersonaa33c592016-07-29 12:38:03 -070055`grpc-protobuf-lite` or `grpc-protobuf-nano` instead of `grpc-protobuf`:
Xudong Ma996ea2f2015-10-07 11:03:41 -070056```gradle
Eric Gribkoff0c27e712018-02-15 18:54:38 -080057compile 'io.grpc:grpc-okhttp:1.10.0'
58compile 'io.grpc:grpc-protobuf-lite:1.10.0'
59compile 'io.grpc:grpc-stub:1.10.0'
Xudong Ma996ea2f2015-10-07 11:03:41 -070060```
61
Eric Anderson4e8cf922016-05-12 13:00:47 -070062[the JARs]:
Kun Zhang41154912018-01-09 13:28:54 -080063http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22io.grpc%22%20AND%20v%3A%221.9.0%22
Eric Anderson1eb16d92015-05-26 14:22:32 -070064
65Development snapshots are available in [Sonatypes's snapshot
66repository](https://oss.sonatype.org/content/repositories/snapshots/).
67
Eric Anderson52f17052016-01-27 10:33:32 -080068For protobuf-based codegen, you can put your proto files in the `src/main/proto`
69and `src/test/proto` directories along with an appropriate plugin.
70
Eric Anderson1eb16d92015-05-26 14:22:32 -070071For protobuf-based codegen integrated with the Maven build system, you can use
Eric Anderson06697b52016-11-14 21:04:22 -080072[protobuf-maven-plugin][] (Eclipse and NetBeans users should also look at
73`os-maven-plugin`'s
74[IDE documentation](https://github.com/trustin/os-maven-plugin#issues-with-eclipse-m2e-or-other-ides)):
Eric Anderson1eb16d92015-05-26 14:22:32 -070075```xml
76<build>
77 <extensions>
78 <extension>
79 <groupId>kr.motd.maven</groupId>
80 <artifactId>os-maven-plugin</artifactId>
Carl Mastrangelo21750042017-07-18 10:44:37 -070081 <version>1.5.0.Final</version>
Eric Anderson1eb16d92015-05-26 14:22:32 -070082 </extension>
83 </extensions>
84 <plugins>
85 <plugin>
Eric Anderson41c3f782016-02-16 15:29:24 -080086 <groupId>org.xolstice.maven.plugins</groupId>
87 <artifactId>protobuf-maven-plugin</artifactId>
88 <version>0.5.0</version>
Eric Anderson1eb16d92015-05-26 14:22:32 -070089 <configuration>
Kun Zhang86d64122018-01-05 16:40:20 -080090 <protocArtifact>com.google.protobuf:protoc:3.5.1-1:exe:${os.detected.classifier}</protocArtifact>
Eric Anderson1eb16d92015-05-26 14:22:32 -070091 <pluginId>grpc-java</pluginId>
Eric Gribkoff0c27e712018-02-15 18:54:38 -080092 <pluginArtifact>io.grpc:protoc-gen-grpc-java:1.10.0:exe:${os.detected.classifier}</pluginArtifact>
Eric Anderson1eb16d92015-05-26 14:22:32 -070093 </configuration>
94 <executions>
95 <execution>
96 <goals>
97 <goal>compile</goal>
98 <goal>compile-custom</goal>
99 </goals>
100 </execution>
101 </executions>
102 </plugin>
103 </plugins>
104</build>
105```
106
Eric Anderson41c3f782016-02-16 15:29:24 -0800107[protobuf-maven-plugin]: https://www.xolstice.org/protobuf-maven-plugin/
Eric Anderson1eb16d92015-05-26 14:22:32 -0700108
109For protobuf-based codegen integrated with the Gradle build system, you can use
110[protobuf-gradle-plugin][]:
111```gradle
Kun Zhang8bd8ed82015-07-22 09:19:52 -0700112apply plugin: 'java'
Eric Anderson1eb16d92015-05-26 14:22:32 -0700113apply plugin: 'com.google.protobuf'
114
115buildscript {
116 repositories {
117 mavenCentral()
118 }
119 dependencies {
Eric Anderson446f0cb2016-05-07 11:24:15 -0700120 // ASSUMES GRADLE 2.12 OR HIGHER. Use plugin version 0.7.5 with earlier
121 // gradle versions
Eric Anderson71bad372018-01-11 14:27:50 -0800122 classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.3'
Eric Anderson1eb16d92015-05-26 14:22:32 -0700123 }
124}
125
Kun Zhang8bd8ed82015-07-22 09:19:52 -0700126protobuf {
127 protoc {
Kun Zhang86d64122018-01-05 16:40:20 -0800128 artifact = "com.google.protobuf:protoc:3.5.1-1"
Kun Zhang8bd8ed82015-07-22 09:19:52 -0700129 }
130 plugins {
131 grpc {
Eric Gribkoff0c27e712018-02-15 18:54:38 -0800132 artifact = 'io.grpc:protoc-gen-grpc-java:1.10.0'
Kun Zhang8bd8ed82015-07-22 09:19:52 -0700133 }
134 }
135 generateProtoTasks {
136 all()*.plugins {
137 grpc {}
Eric Anderson1eb16d92015-05-26 14:22:32 -0700138 }
139 }
140}
Eric Anderson1eb16d92015-05-26 14:22:32 -0700141```
142
143[protobuf-gradle-plugin]: https://github.com/google/protobuf-gradle-plugin
144
Eric Andersonfb28ad22015-01-29 15:00:58 -0800145How to Build
146------------
147
Eric Anderson1b1c6462015-08-14 08:49:56 -0700148If you are making changes to gRPC-Java, see the [compiling
149instructions](COMPILING.md).
Kun Zhang9805e272015-05-12 17:03:19 -0700150
Eric Andersonfb28ad22015-01-29 15:00:58 -0800151Navigating Around the Source
152----------------------------
153
mekka99907942015-08-07 23:28:06 -0700154Here's a quick readers' guide to the code to help folks get started. At a high
Kun Zhangfc85a402015-08-05 16:48:15 -0700155level there are three distinct layers to the library: __Stub__, __Channel__ &
156__Transport__.
ejonad4531da2014-12-15 10:24:42 -0800157
Eric Andersonfb28ad22015-01-29 15:00:58 -0800158### Stub
ejonad4531da2014-12-15 10:24:42 -0800159
Kun Zhangfc85a402015-08-05 16:48:15 -0700160The Stub layer is what is exposed to most developers and provides type-safe
161bindings to whatever datamodel/IDL/interface you are adapting. gRPC comes with
162a [plugin](https://github.com/google/grpc-java/blob/master/compiler) to the
163protocol-buffers compiler that generates Stub interfaces out of `.proto` files,
164but bindings to other datamodel/IDL should be trivial to add and are welcome.
ejonad4531da2014-12-15 10:24:42 -0800165
166#### Key Interfaces
167
nmittlerf8314582015-01-27 10:25:39 -0800168[Stream Observer](https://github.com/google/grpc-java/blob/master/stub/src/main/java/io/grpc/stub/StreamObserver.java)
ejonad4531da2014-12-15 10:24:42 -0800169
Eric Andersonfb28ad22015-01-29 15:00:58 -0800170### Channel
ejonad4531da2014-12-15 10:24:42 -0800171
Kun Zhangfc85a402015-08-05 16:48:15 -0700172The Channel layer is an abstraction over Transport handling that is suitable for
173interception/decoration and exposes more behavior to the application than the
174Stub layer. It is intended to be easy for application frameworks to use this
175layer to address cross-cutting concerns such as logging, monitoring, auth etc.
176Flow-control is also exposed at this layer to allow more sophisticated
177applications to interact with it directly.
ejonad4531da2014-12-15 10:24:42 -0800178
179#### Common
180
nmittlerf8314582015-01-27 10:25:39 -0800181* [Metadata - headers & trailers](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/Metadata.java)
182* [Status - error code namespace & handling](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/Status.java)
ejonad4531da2014-12-15 10:24:42 -0800183
184#### Client
nmittlerf8314582015-01-27 10:25:39 -0800185* [Channel - client side binding](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/Channel.java)
Kun Zhang2ee4d022015-06-04 16:39:25 -0700186* [Client Call](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/ClientCall.java)
nmittlerf8314582015-01-27 10:25:39 -0800187* [Client Interceptor](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/ClientInterceptor.java)
ejonad4531da2014-12-15 10:24:42 -0800188
189#### Server
nmittlerf8314582015-01-27 10:25:39 -0800190* [Server call handler - analog to Channel on server](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/ServerCallHandler.java)
191* [Server Call](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/ServerCall.java)
ejonad4531da2014-12-15 10:24:42 -0800192
193
Eric Andersonfb28ad22015-01-29 15:00:58 -0800194### Transport
ejonad4531da2014-12-15 10:24:42 -0800195
mekka99907942015-08-07 23:28:06 -0700196The Transport layer does the heavy lifting of putting and taking bytes off the
Kun Zhangfc85a402015-08-05 16:48:15 -0700197wire. The interfaces to it are abstract just enough to allow plugging in of
198different implementations. Transports are modeled as `Stream` factories. The
199variation in interface between a server Stream and a client Stream exists to
200codify their differing semantics for cancellation and error reporting.
201
202Note the transport layer API is considered internal to gRPC and has weaker API
203guarantees than the core API under package `io.grpc`.
204
mekka99907942015-08-07 23:28:06 -0700205gRPC comes with three Transport implementations:
Kun Zhangfc85a402015-08-05 16:48:15 -0700206
2071. The [Netty-based](https://github.com/google/grpc-java/blob/master/netty)
208 transport is the main transport implementation based on
209 [Netty](http://netty.io). It is for both the client and the server.
2102. The [OkHttp-based](https://github.com/google/grpc-java/blob/master/okhttp)
211 transport is a lightweight transport based on
212 [OkHttp](http://square.github.io/okhttp/). It is mainly for use on Android
mekka99907942015-08-07 23:28:06 -0700213 and is for client only.
Kun Zhangfc85a402015-08-05 16:48:15 -07002143. The
Kun Zhange1bd6ef2015-08-11 11:17:39 -0700215 [inProcess](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/inprocess)
Kun Zhangfc85a402015-08-05 16:48:15 -0700216 transport is for when a server is in the same process as the client. It is
217 useful for testing.
ejonad4531da2014-12-15 10:24:42 -0800218
219#### Common
220
Kun Zhange1bd6ef2015-08-11 11:17:39 -0700221* [Stream](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/internal/Stream.java)
222* [Stream Listener](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/internal/StreamListener.java)
ejonad4531da2014-12-15 10:24:42 -0800223
224#### Client
225
Kun Zhange1bd6ef2015-08-11 11:17:39 -0700226* [Client Stream](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/internal/ClientStream.java)
227* [Client Stream Listener](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/internal/ClientStreamListener.java)
ejonad4531da2014-12-15 10:24:42 -0800228
229#### Server
230
Kun Zhange1bd6ef2015-08-11 11:17:39 -0700231* [Server Stream](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/internal/ServerStream.java)
232* [Server Stream Listener](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/internal/ServerStreamListener.java)
ejonad4531da2014-12-15 10:24:42 -0800233
234
Kun Zhangd0d2c1e2017-01-18 09:06:45 -0800235Examples
236--------
ejonad4531da2014-12-15 10:24:42 -0800237
Kun Zhangd0d2c1e2017-01-18 09:06:45 -0800238The [examples](https://github.com/grpc/grpc-java/tree/master/examples)
239and the
240[Android example](https://github.com/grpc/grpc-java/tree/master/examples/android) are standalone projects that
241showcase the usage of gRPC.