blob: d2eb2023c6cc693508909f5fcaf3910dbe1c03b8 [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>
Mehrdad Afshari8ce0bc22017-07-10 22:48:17 +000011 <td><a href="https://grpc.io/">grpc.io</a></td>
nmittlerb7a72c12015-09-24 12:03:23 -070012 </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
Eric Anderson67ff1702016-12-06 09:27:10 -080019[![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 -070020[![Build Status](https://travis-ci.org/grpc/grpc-java.svg?branch=master)](https://travis-ci.org/grpc/grpc-java)
21[![Coverage Status](https://coveralls.io/repos/grpc/grpc-java/badge.svg?branch=master&service=github)](https://coveralls.io/github/grpc/grpc-java?branch=master)
22
Eric Anderson1eb16d92015-05-26 14:22:32 -070023Download
24--------
25
Eric Andersona62e9762016-06-14 10:58:38 -070026Download [the JARs][]. Or for Maven with non-Android, add to your `pom.xml`:
Eric Anderson1eb16d92015-05-26 14:22:32 -070027```xml
28<dependency>
29 <groupId>io.grpc</groupId>
Eric Andersona62e9762016-06-14 10:58:38 -070030 <artifactId>grpc-netty</artifactId>
Carl Mastrangelo7d1aae12017-11-28 14:28:35 -080031 <version>1.8.0</version>
Eric Andersona62e9762016-06-14 10:58:38 -070032</dependency>
33<dependency>
34 <groupId>io.grpc</groupId>
35 <artifactId>grpc-protobuf</artifactId>
Carl Mastrangelo7d1aae12017-11-28 14:28:35 -080036 <version>1.8.0</version>
Eric Andersona62e9762016-06-14 10:58:38 -070037</dependency>
38<dependency>
39 <groupId>io.grpc</groupId>
40 <artifactId>grpc-stub</artifactId>
Carl Mastrangelo7d1aae12017-11-28 14:28:35 -080041 <version>1.8.0</version>
Eric Anderson1eb16d92015-05-26 14:22:32 -070042</dependency>
43```
44
Eric Andersona62e9762016-06-14 10:58:38 -070045Or for Gradle with non-Android, add to your dependencies:
Eric Anderson1eb16d92015-05-26 14:22:32 -070046```gradle
Carl Mastrangelo7d1aae12017-11-28 14:28:35 -080047compile 'io.grpc:grpc-netty:1.8.0'
48compile 'io.grpc:grpc-protobuf:1.8.0'
49compile 'io.grpc:grpc-stub:1.8.0'
Eric Anderson1eb16d92015-05-26 14:22:32 -070050```
51
Eric Andersona62e9762016-06-14 10:58:38 -070052For Android client, use `grpc-okhttp` instead of `grpc-netty` and
Eric Andersonaa33c592016-07-29 12:38:03 -070053`grpc-protobuf-lite` or `grpc-protobuf-nano` instead of `grpc-protobuf`:
Xudong Ma996ea2f2015-10-07 11:03:41 -070054```gradle
Carl Mastrangelo7d1aae12017-11-28 14:28:35 -080055compile 'io.grpc:grpc-okhttp:1.8.0'
56compile 'io.grpc:grpc-protobuf-lite:1.8.0'
57compile 'io.grpc:grpc-stub:1.8.0'
Xudong Ma996ea2f2015-10-07 11:03:41 -070058```
59
Eric Anderson4e8cf922016-05-12 13:00:47 -070060[the JARs]:
Carl Mastrangelo7d1aae12017-11-28 14:28:35 -080061http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22io.grpc%22%20AND%20v%3A%221.8.0%22
Eric Anderson1eb16d92015-05-26 14:22:32 -070062
63Development snapshots are available in [Sonatypes's snapshot
64repository](https://oss.sonatype.org/content/repositories/snapshots/).
65
Eric Anderson52f17052016-01-27 10:33:32 -080066For protobuf-based codegen, you can put your proto files in the `src/main/proto`
67and `src/test/proto` directories along with an appropriate plugin.
68
Eric Anderson1eb16d92015-05-26 14:22:32 -070069For protobuf-based codegen integrated with the Maven build system, you can use
Eric Anderson06697b52016-11-14 21:04:22 -080070[protobuf-maven-plugin][] (Eclipse and NetBeans users should also look at
71`os-maven-plugin`'s
72[IDE documentation](https://github.com/trustin/os-maven-plugin#issues-with-eclipse-m2e-or-other-ides)):
Eric Anderson1eb16d92015-05-26 14:22:32 -070073```xml
74<build>
75 <extensions>
76 <extension>
77 <groupId>kr.motd.maven</groupId>
78 <artifactId>os-maven-plugin</artifactId>
Carl Mastrangelo21750042017-07-18 10:44:37 -070079 <version>1.5.0.Final</version>
Eric Anderson1eb16d92015-05-26 14:22:32 -070080 </extension>
81 </extensions>
82 <plugins>
83 <plugin>
Eric Anderson41c3f782016-02-16 15:29:24 -080084 <groupId>org.xolstice.maven.plugins</groupId>
85 <artifactId>protobuf-maven-plugin</artifactId>
86 <version>0.5.0</version>
Eric Anderson1eb16d92015-05-26 14:22:32 -070087 <configuration>
Kun Zhang86d64122018-01-05 16:40:20 -080088 <protocArtifact>com.google.protobuf:protoc:3.5.1-1:exe:${os.detected.classifier}</protocArtifact>
Eric Anderson1eb16d92015-05-26 14:22:32 -070089 <pluginId>grpc-java</pluginId>
Carl Mastrangelo7d1aae12017-11-28 14:28:35 -080090 <pluginArtifact>io.grpc:protoc-gen-grpc-java:1.8.0:exe:${os.detected.classifier}</pluginArtifact>
Eric Anderson1eb16d92015-05-26 14:22:32 -070091 </configuration>
92 <executions>
93 <execution>
94 <goals>
95 <goal>compile</goal>
96 <goal>compile-custom</goal>
97 </goals>
98 </execution>
99 </executions>
100 </plugin>
101 </plugins>
102</build>
103```
104
Eric Anderson41c3f782016-02-16 15:29:24 -0800105[protobuf-maven-plugin]: https://www.xolstice.org/protobuf-maven-plugin/
Eric Anderson1eb16d92015-05-26 14:22:32 -0700106
107For protobuf-based codegen integrated with the Gradle build system, you can use
108[protobuf-gradle-plugin][]:
109```gradle
Kun Zhang8bd8ed82015-07-22 09:19:52 -0700110apply plugin: 'java'
Eric Anderson1eb16d92015-05-26 14:22:32 -0700111apply plugin: 'com.google.protobuf'
112
113buildscript {
114 repositories {
115 mavenCentral()
116 }
117 dependencies {
Eric Anderson446f0cb2016-05-07 11:24:15 -0700118 // ASSUMES GRADLE 2.12 OR HIGHER. Use plugin version 0.7.5 with earlier
119 // gradle versions
Carl Mastrangelod34260a2017-07-14 12:35:14 -0700120 classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.1'
Eric Anderson1eb16d92015-05-26 14:22:32 -0700121 }
122}
123
Kun Zhang8bd8ed82015-07-22 09:19:52 -0700124protobuf {
125 protoc {
Kun Zhang86d64122018-01-05 16:40:20 -0800126 artifact = "com.google.protobuf:protoc:3.5.1-1"
Kun Zhang8bd8ed82015-07-22 09:19:52 -0700127 }
128 plugins {
129 grpc {
Carl Mastrangelo7d1aae12017-11-28 14:28:35 -0800130 artifact = 'io.grpc:protoc-gen-grpc-java:1.8.0'
Kun Zhang8bd8ed82015-07-22 09:19:52 -0700131 }
132 }
133 generateProtoTasks {
134 all()*.plugins {
135 grpc {}
Eric Anderson1eb16d92015-05-26 14:22:32 -0700136 }
137 }
138}
Eric Anderson1eb16d92015-05-26 14:22:32 -0700139```
140
141[protobuf-gradle-plugin]: https://github.com/google/protobuf-gradle-plugin
142
Eric Andersonfb28ad22015-01-29 15:00:58 -0800143How to Build
144------------
145
Eric Anderson1b1c6462015-08-14 08:49:56 -0700146If you are making changes to gRPC-Java, see the [compiling
147instructions](COMPILING.md).
Kun Zhang9805e272015-05-12 17:03:19 -0700148
Eric Andersonfb28ad22015-01-29 15:00:58 -0800149Navigating Around the Source
150----------------------------
151
mekka99907942015-08-07 23:28:06 -0700152Here's a quick readers' guide to the code to help folks get started. At a high
Kun Zhangfc85a402015-08-05 16:48:15 -0700153level there are three distinct layers to the library: __Stub__, __Channel__ &
154__Transport__.
ejonad4531da2014-12-15 10:24:42 -0800155
Eric Andersonfb28ad22015-01-29 15:00:58 -0800156### Stub
ejonad4531da2014-12-15 10:24:42 -0800157
Kun Zhangfc85a402015-08-05 16:48:15 -0700158The Stub layer is what is exposed to most developers and provides type-safe
159bindings to whatever datamodel/IDL/interface you are adapting. gRPC comes with
160a [plugin](https://github.com/google/grpc-java/blob/master/compiler) to the
161protocol-buffers compiler that generates Stub interfaces out of `.proto` files,
162but bindings to other datamodel/IDL should be trivial to add and are welcome.
ejonad4531da2014-12-15 10:24:42 -0800163
164#### Key Interfaces
165
nmittlerf8314582015-01-27 10:25:39 -0800166[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 -0800167
Eric Andersonfb28ad22015-01-29 15:00:58 -0800168### Channel
ejonad4531da2014-12-15 10:24:42 -0800169
Kun Zhangfc85a402015-08-05 16:48:15 -0700170The Channel layer is an abstraction over Transport handling that is suitable for
171interception/decoration and exposes more behavior to the application than the
172Stub layer. It is intended to be easy for application frameworks to use this
173layer to address cross-cutting concerns such as logging, monitoring, auth etc.
174Flow-control is also exposed at this layer to allow more sophisticated
175applications to interact with it directly.
ejonad4531da2014-12-15 10:24:42 -0800176
177#### Common
178
nmittlerf8314582015-01-27 10:25:39 -0800179* [Metadata - headers & trailers](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/Metadata.java)
180* [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 -0800181
182#### Client
nmittlerf8314582015-01-27 10:25:39 -0800183* [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 -0700184* [Client Call](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/ClientCall.java)
nmittlerf8314582015-01-27 10:25:39 -0800185* [Client Interceptor](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/ClientInterceptor.java)
ejonad4531da2014-12-15 10:24:42 -0800186
187#### Server
nmittlerf8314582015-01-27 10:25:39 -0800188* [Server call handler - analog to Channel on server](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/ServerCallHandler.java)
189* [Server Call](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/ServerCall.java)
ejonad4531da2014-12-15 10:24:42 -0800190
191
Eric Andersonfb28ad22015-01-29 15:00:58 -0800192### Transport
ejonad4531da2014-12-15 10:24:42 -0800193
mekka99907942015-08-07 23:28:06 -0700194The Transport layer does the heavy lifting of putting and taking bytes off the
Kun Zhangfc85a402015-08-05 16:48:15 -0700195wire. The interfaces to it are abstract just enough to allow plugging in of
196different implementations. Transports are modeled as `Stream` factories. The
197variation in interface between a server Stream and a client Stream exists to
198codify their differing semantics for cancellation and error reporting.
199
200Note the transport layer API is considered internal to gRPC and has weaker API
201guarantees than the core API under package `io.grpc`.
202
mekka99907942015-08-07 23:28:06 -0700203gRPC comes with three Transport implementations:
Kun Zhangfc85a402015-08-05 16:48:15 -0700204
2051. The [Netty-based](https://github.com/google/grpc-java/blob/master/netty)
206 transport is the main transport implementation based on
207 [Netty](http://netty.io). It is for both the client and the server.
2082. The [OkHttp-based](https://github.com/google/grpc-java/blob/master/okhttp)
209 transport is a lightweight transport based on
210 [OkHttp](http://square.github.io/okhttp/). It is mainly for use on Android
mekka99907942015-08-07 23:28:06 -0700211 and is for client only.
Kun Zhangfc85a402015-08-05 16:48:15 -07002123. The
Kun Zhange1bd6ef2015-08-11 11:17:39 -0700213 [inProcess](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/inprocess)
Kun Zhangfc85a402015-08-05 16:48:15 -0700214 transport is for when a server is in the same process as the client. It is
215 useful for testing.
ejonad4531da2014-12-15 10:24:42 -0800216
217#### Common
218
Kun Zhange1bd6ef2015-08-11 11:17:39 -0700219* [Stream](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/internal/Stream.java)
220* [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 -0800221
222#### Client
223
Kun Zhange1bd6ef2015-08-11 11:17:39 -0700224* [Client Stream](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/internal/ClientStream.java)
225* [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 -0800226
227#### Server
228
Kun Zhange1bd6ef2015-08-11 11:17:39 -0700229* [Server Stream](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/internal/ServerStream.java)
230* [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 -0800231
232
Kun Zhangd0d2c1e2017-01-18 09:06:45 -0800233Examples
234--------
ejonad4531da2014-12-15 10:24:42 -0800235
Kun Zhangd0d2c1e2017-01-18 09:06:45 -0800236The [examples](https://github.com/grpc/grpc-java/tree/master/examples)
237and the
238[Android example](https://github.com/grpc/grpc-java/tree/master/examples/android) are standalone projects that
239showcase the usage of gRPC.