blob: 3e9b6c3e3c0a1c05ac62e1a92991b381ae1fb230 [file] [log] [blame] [view]
Kun Zhangd32bb472015-05-06 16:15:53 -07001gRPC-Java - An RPC library and framework
2========================================
3
Eric Anderson65291322015-05-15 11:57:05 -07004gRPC-Java works with JDK 6. TLS usage typically requires using Java 8, or Play
nmittler24830092015-05-28 13:12:56 -07005Services Dynamic Security Provider on Android. Please see the [Security
6Readme](SECURITY.md).
ejonad4531da2014-12-15 10:24:42 -08007
nmittlerb7a72c12015-09-24 12:03:23 -07008<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[![Build Status](https://travis-ci.org/grpc/grpc-java.svg?branch=master)](https://travis-ci.org/grpc/grpc-java)
20[![Coverage Status](https://coveralls.io/repos/grpc/grpc-java/badge.svg?branch=master&service=github)](https://coveralls.io/github/grpc/grpc-java?branch=master)
21
Eric Anderson1eb16d92015-05-26 14:22:32 -070022Download
23--------
24
Eric Andersona62e9762016-06-14 10:58:38 -070025Download [the JARs][]. Or for Maven with non-Android, add to your `pom.xml`:
Eric Anderson1eb16d92015-05-26 14:22:32 -070026```xml
27<dependency>
28 <groupId>io.grpc</groupId>
Eric Andersona62e9762016-06-14 10:58:38 -070029 <artifactId>grpc-netty</artifactId>
Sky Ao841c7c42016-07-26 00:58:43 +080030 <version>0.15.0</version>
Eric Andersona62e9762016-06-14 10:58:38 -070031</dependency>
32<dependency>
33 <groupId>io.grpc</groupId>
34 <artifactId>grpc-protobuf</artifactId>
Sky Ao841c7c42016-07-26 00:58:43 +080035 <version>0.15.0</version>
Eric Andersona62e9762016-06-14 10:58:38 -070036</dependency>
37<dependency>
38 <groupId>io.grpc</groupId>
39 <artifactId>grpc-stub</artifactId>
Sky Ao841c7c42016-07-26 00:58:43 +080040 <version>0.15.0</version>
Eric Anderson1eb16d92015-05-26 14:22:32 -070041</dependency>
42```
43
Eric Andersona62e9762016-06-14 10:58:38 -070044Or for Gradle with non-Android, add to your dependencies:
Eric Anderson1eb16d92015-05-26 14:22:32 -070045```gradle
Sky Ao841c7c42016-07-26 00:58:43 +080046compile 'io.grpc:grpc-netty:0.15.0'
47compile 'io.grpc:grpc-protobuf:0.15.0'
48compile 'io.grpc:grpc-stub:0.15.0'
Eric Anderson1eb16d92015-05-26 14:22:32 -070049```
50
Eric Andersona62e9762016-06-14 10:58:38 -070051For Android client, use `grpc-okhttp` instead of `grpc-netty` and
52`grpc-protobuf-nano` or `grpc-protobuf-lite` instead of `grpc-protobuf`:
Xudong Ma996ea2f2015-10-07 11:03:41 -070053```gradle
Sky Ao841c7c42016-07-26 00:58:43 +080054compile 'io.grpc:grpc-okhttp:0.15.0'
55compile 'io.grpc:grpc-protobuf-nano:0.15.0'
56compile 'io.grpc:grpc-stub:0.15.0'
Xudong Ma996ea2f2015-10-07 11:03:41 -070057```
58
Eric Anderson4e8cf922016-05-12 13:00:47 -070059[the JARs]:
60http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22io.grpc%22%20AND%20v%3A%220.14.0%22
Eric Anderson1eb16d92015-05-26 14:22:32 -070061
62Development snapshots are available in [Sonatypes's snapshot
63repository](https://oss.sonatype.org/content/repositories/snapshots/).
64
Eric Anderson52f17052016-01-27 10:33:32 -080065For protobuf-based codegen, you can put your proto files in the `src/main/proto`
66and `src/test/proto` directories along with an appropriate plugin.
67
Eric Anderson1eb16d92015-05-26 14:22:32 -070068For protobuf-based codegen integrated with the Maven build system, you can use
Eric Anderson41c3f782016-02-16 15:29:24 -080069[protobuf-maven-plugin][]:
Eric Anderson1eb16d92015-05-26 14:22:32 -070070```xml
71<build>
72 <extensions>
73 <extension>
74 <groupId>kr.motd.maven</groupId>
75 <artifactId>os-maven-plugin</artifactId>
Eric Anderson41c3f782016-02-16 15:29:24 -080076 <version>1.4.1.Final</version>
Eric Anderson1eb16d92015-05-26 14:22:32 -070077 </extension>
78 </extensions>
79 <plugins>
80 <plugin>
Eric Anderson41c3f782016-02-16 15:29:24 -080081 <groupId>org.xolstice.maven.plugins</groupId>
82 <artifactId>protobuf-maven-plugin</artifactId>
83 <version>0.5.0</version>
Eric Anderson1eb16d92015-05-26 14:22:32 -070084 <configuration>
Kun Zhang8bd8ed82015-07-22 09:19:52 -070085 <!--
86 The version of protoc must match protobuf-java. If you don't depend on
87 protobuf-java directly, you will be transitively depending on the
88 protobuf-java version that grpc depends on.
89 -->
Sky Ao841c7c42016-07-26 00:58:43 +080090 <protocArtifact>com.google.protobuf:protoc:3.0.0-beta-3:exe:${os.detected.classifier}</protocArtifact>
Eric Anderson1eb16d92015-05-26 14:22:32 -070091 <pluginId>grpc-java</pluginId>
Sky Ao841c7c42016-07-26 00:58:43 +080092 <pluginArtifact>io.grpc:protoc-gen-grpc-java:0.15.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
122 classpath 'com.google.protobuf:protobuf-gradle-plugin:0.7.7'
Eric Anderson1eb16d92015-05-26 14:22:32 -0700123 }
124}
125
Kun Zhang8bd8ed82015-07-22 09:19:52 -0700126protobuf {
127 protoc {
128 // The version of protoc must match protobuf-java. If you don't depend on
129 // protobuf-java directly, you will be transitively depending on the
130 // protobuf-java version that grpc depends on.
Sky Ao841c7c42016-07-26 00:58:43 +0800131 artifact = "com.google.protobuf:protoc:3.0.0-beta-3"
Kun Zhang8bd8ed82015-07-22 09:19:52 -0700132 }
133 plugins {
134 grpc {
Sky Ao841c7c42016-07-26 00:58:43 +0800135 artifact = 'io.grpc:protoc-gen-grpc-java:0.15.0'
Kun Zhang8bd8ed82015-07-22 09:19:52 -0700136 }
137 }
138 generateProtoTasks {
139 all()*.plugins {
140 grpc {}
Eric Anderson1eb16d92015-05-26 14:22:32 -0700141 }
142 }
143}
Eric Anderson1eb16d92015-05-26 14:22:32 -0700144```
145
146[protobuf-gradle-plugin]: https://github.com/google/protobuf-gradle-plugin
147
Eric Andersonfb28ad22015-01-29 15:00:58 -0800148How to Build
149------------
150
Eric Anderson1b1c6462015-08-14 08:49:56 -0700151If you are making changes to gRPC-Java, see the [compiling
152instructions](COMPILING.md).
Kun Zhang9805e272015-05-12 17:03:19 -0700153
Eric Andersonfb28ad22015-01-29 15:00:58 -0800154Navigating Around the Source
155----------------------------
156
mekka99907942015-08-07 23:28:06 -0700157Here's a quick readers' guide to the code to help folks get started. At a high
Kun Zhangfc85a402015-08-05 16:48:15 -0700158level there are three distinct layers to the library: __Stub__, __Channel__ &
159__Transport__.
ejonad4531da2014-12-15 10:24:42 -0800160
Eric Andersonfb28ad22015-01-29 15:00:58 -0800161### Stub
ejonad4531da2014-12-15 10:24:42 -0800162
Kun Zhangfc85a402015-08-05 16:48:15 -0700163The Stub layer is what is exposed to most developers and provides type-safe
164bindings to whatever datamodel/IDL/interface you are adapting. gRPC comes with
165a [plugin](https://github.com/google/grpc-java/blob/master/compiler) to the
166protocol-buffers compiler that generates Stub interfaces out of `.proto` files,
167but bindings to other datamodel/IDL should be trivial to add and are welcome.
ejonad4531da2014-12-15 10:24:42 -0800168
169#### Key Interfaces
170
nmittlerf8314582015-01-27 10:25:39 -0800171[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 -0800172
Eric Andersonfb28ad22015-01-29 15:00:58 -0800173### Channel
ejonad4531da2014-12-15 10:24:42 -0800174
Kun Zhangfc85a402015-08-05 16:48:15 -0700175The Channel layer is an abstraction over Transport handling that is suitable for
176interception/decoration and exposes more behavior to the application than the
177Stub layer. It is intended to be easy for application frameworks to use this
178layer to address cross-cutting concerns such as logging, monitoring, auth etc.
179Flow-control is also exposed at this layer to allow more sophisticated
180applications to interact with it directly.
ejonad4531da2014-12-15 10:24:42 -0800181
182#### Common
183
nmittlerf8314582015-01-27 10:25:39 -0800184* [Metadata - headers & trailers](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/Metadata.java)
185* [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 -0800186
187#### Client
nmittlerf8314582015-01-27 10:25:39 -0800188* [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 -0700189* [Client Call](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/ClientCall.java)
nmittlerf8314582015-01-27 10:25:39 -0800190* [Client Interceptor](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/ClientInterceptor.java)
ejonad4531da2014-12-15 10:24:42 -0800191
192#### Server
nmittlerf8314582015-01-27 10:25:39 -0800193* [Server call handler - analog to Channel on server](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/ServerCallHandler.java)
194* [Server Call](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/ServerCall.java)
ejonad4531da2014-12-15 10:24:42 -0800195
196
Eric Andersonfb28ad22015-01-29 15:00:58 -0800197### Transport
ejonad4531da2014-12-15 10:24:42 -0800198
mekka99907942015-08-07 23:28:06 -0700199The Transport layer does the heavy lifting of putting and taking bytes off the
Kun Zhangfc85a402015-08-05 16:48:15 -0700200wire. The interfaces to it are abstract just enough to allow plugging in of
201different implementations. Transports are modeled as `Stream` factories. The
202variation in interface between a server Stream and a client Stream exists to
203codify their differing semantics for cancellation and error reporting.
204
205Note the transport layer API is considered internal to gRPC and has weaker API
206guarantees than the core API under package `io.grpc`.
207
mekka99907942015-08-07 23:28:06 -0700208gRPC comes with three Transport implementations:
Kun Zhangfc85a402015-08-05 16:48:15 -0700209
2101. The [Netty-based](https://github.com/google/grpc-java/blob/master/netty)
211 transport is the main transport implementation based on
212 [Netty](http://netty.io). It is for both the client and the server.
2132. The [OkHttp-based](https://github.com/google/grpc-java/blob/master/okhttp)
214 transport is a lightweight transport based on
215 [OkHttp](http://square.github.io/okhttp/). It is mainly for use on Android
mekka99907942015-08-07 23:28:06 -0700216 and is for client only.
Kun Zhangfc85a402015-08-05 16:48:15 -07002173. The
Kun Zhange1bd6ef2015-08-11 11:17:39 -0700218 [inProcess](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/inprocess)
Kun Zhangfc85a402015-08-05 16:48:15 -0700219 transport is for when a server is in the same process as the client. It is
220 useful for testing.
ejonad4531da2014-12-15 10:24:42 -0800221
222#### Common
223
Kun Zhange1bd6ef2015-08-11 11:17:39 -0700224* [Stream](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/internal/Stream.java)
225* [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 -0800226
227#### Client
228
Kun Zhange1bd6ef2015-08-11 11:17:39 -0700229* [Client Stream](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/internal/ClientStream.java)
230* [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 -0800231
232#### Server
233
Kun Zhange1bd6ef2015-08-11 11:17:39 -0700234* [Server Stream](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/internal/ServerStream.java)
235* [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 -0800236
237
Eric Andersonfb28ad22015-01-29 15:00:58 -0800238### Examples
ejonad4531da2014-12-15 10:24:42 -0800239
Kun Zhangfc85a402015-08-05 16:48:15 -0700240Tests showing how these layers are composed to execute calls using protobuf
241messages can be found here
242https://github.com/google/grpc-java/tree/master/interop-testing/src/main/java/io/grpc/testing/integration