blob: 7a8e2f5898d3d4d652835352dd207b7d2d62f0a8 [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 Anderson4e8cf922016-05-12 13:00:47 -070025Download [the JARs][]. Or for Maven, add to your `pom.xml`:
Eric Anderson1eb16d92015-05-26 14:22:32 -070026```xml
27<dependency>
28 <groupId>io.grpc</groupId>
29 <artifactId>grpc-all</artifactId>
Carl Mastrangelob4fc9292016-05-02 15:07:45 -070030 <version>0.14.0</version>
Eric Anderson1eb16d92015-05-26 14:22:32 -070031</dependency>
32```
33
34Or for Gradle, add to your dependencies:
35```gradle
Carl Mastrangelob4fc9292016-05-02 15:07:45 -070036compile 'io.grpc:grpc-all:0.14.0'
Eric Anderson1eb16d92015-05-26 14:22:32 -070037```
38
Xudong Ma996ea2f2015-10-07 11:03:41 -070039For Android client, you only need to depend on the needed sub-projects, such as:
40```gradle
Carl Mastrangelob4fc9292016-05-02 15:07:45 -070041compile 'io.grpc:grpc-okhttp:0.14.0'
42compile 'io.grpc:grpc-protobuf-nano:0.14.0'
43compile 'io.grpc:grpc-stub:0.14.0'
Xudong Ma996ea2f2015-10-07 11:03:41 -070044```
45
Eric Anderson4e8cf922016-05-12 13:00:47 -070046[the JARs]:
47http://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 -070048
49Development snapshots are available in [Sonatypes's snapshot
50repository](https://oss.sonatype.org/content/repositories/snapshots/).
51
Eric Anderson52f17052016-01-27 10:33:32 -080052For protobuf-based codegen, you can put your proto files in the `src/main/proto`
53and `src/test/proto` directories along with an appropriate plugin.
54
Eric Anderson1eb16d92015-05-26 14:22:32 -070055For protobuf-based codegen integrated with the Maven build system, you can use
Eric Anderson41c3f782016-02-16 15:29:24 -080056[protobuf-maven-plugin][]:
Eric Anderson1eb16d92015-05-26 14:22:32 -070057```xml
58<build>
59 <extensions>
60 <extension>
61 <groupId>kr.motd.maven</groupId>
62 <artifactId>os-maven-plugin</artifactId>
Eric Anderson41c3f782016-02-16 15:29:24 -080063 <version>1.4.1.Final</version>
Eric Anderson1eb16d92015-05-26 14:22:32 -070064 </extension>
65 </extensions>
66 <plugins>
67 <plugin>
Eric Anderson41c3f782016-02-16 15:29:24 -080068 <groupId>org.xolstice.maven.plugins</groupId>
69 <artifactId>protobuf-maven-plugin</artifactId>
70 <version>0.5.0</version>
Eric Anderson1eb16d92015-05-26 14:22:32 -070071 <configuration>
Kun Zhang8bd8ed82015-07-22 09:19:52 -070072 <!--
73 The version of protoc must match protobuf-java. If you don't depend on
74 protobuf-java directly, you will be transitively depending on the
75 protobuf-java version that grpc depends on.
76 -->
Eric Anderson85e68db2016-02-18 12:54:10 -080077 <protocArtifact>com.google.protobuf:protoc:3.0.0-beta-2:exe:${os.detected.classifier}</protocArtifact>
Eric Anderson1eb16d92015-05-26 14:22:32 -070078 <pluginId>grpc-java</pluginId>
Carl Mastrangelob4fc9292016-05-02 15:07:45 -070079 <pluginArtifact>io.grpc:protoc-gen-grpc-java:0.14.0:exe:${os.detected.classifier}</pluginArtifact>
Eric Anderson1eb16d92015-05-26 14:22:32 -070080 </configuration>
81 <executions>
82 <execution>
83 <goals>
84 <goal>compile</goal>
85 <goal>compile-custom</goal>
86 </goals>
87 </execution>
88 </executions>
89 </plugin>
90 </plugins>
91</build>
92```
93
Eric Anderson41c3f782016-02-16 15:29:24 -080094[protobuf-maven-plugin]: https://www.xolstice.org/protobuf-maven-plugin/
Eric Anderson1eb16d92015-05-26 14:22:32 -070095
96For protobuf-based codegen integrated with the Gradle build system, you can use
97[protobuf-gradle-plugin][]:
98```gradle
Kun Zhang8bd8ed82015-07-22 09:19:52 -070099apply plugin: 'java'
Eric Anderson1eb16d92015-05-26 14:22:32 -0700100apply plugin: 'com.google.protobuf'
101
102buildscript {
103 repositories {
104 mavenCentral()
105 }
106 dependencies {
Eric Anderson446f0cb2016-05-07 11:24:15 -0700107 // ASSUMES GRADLE 2.12 OR HIGHER. Use plugin version 0.7.5 with earlier
108 // gradle versions
109 classpath 'com.google.protobuf:protobuf-gradle-plugin:0.7.7'
Eric Anderson1eb16d92015-05-26 14:22:32 -0700110 }
111}
112
Kun Zhang8bd8ed82015-07-22 09:19:52 -0700113protobuf {
114 protoc {
115 // The version of protoc must match protobuf-java. If you don't depend on
116 // protobuf-java directly, you will be transitively depending on the
117 // protobuf-java version that grpc depends on.
Eric Anderson85e68db2016-02-18 12:54:10 -0800118 artifact = "com.google.protobuf:protoc:3.0.0-beta-2"
Kun Zhang8bd8ed82015-07-22 09:19:52 -0700119 }
120 plugins {
121 grpc {
Carl Mastrangelob4fc9292016-05-02 15:07:45 -0700122 artifact = 'io.grpc:protoc-gen-grpc-java:0.14.0'
Kun Zhang8bd8ed82015-07-22 09:19:52 -0700123 }
124 }
125 generateProtoTasks {
126 all()*.plugins {
127 grpc {}
Eric Anderson1eb16d92015-05-26 14:22:32 -0700128 }
129 }
130}
Eric Anderson1eb16d92015-05-26 14:22:32 -0700131```
132
133[protobuf-gradle-plugin]: https://github.com/google/protobuf-gradle-plugin
134
Eric Andersonfb28ad22015-01-29 15:00:58 -0800135How to Build
136------------
137
Eric Anderson1b1c6462015-08-14 08:49:56 -0700138If you are making changes to gRPC-Java, see the [compiling
139instructions](COMPILING.md).
Kun Zhang9805e272015-05-12 17:03:19 -0700140
Eric Andersonfb28ad22015-01-29 15:00:58 -0800141Navigating Around the Source
142----------------------------
143
mekka99907942015-08-07 23:28:06 -0700144Here's a quick readers' guide to the code to help folks get started. At a high
Kun Zhangfc85a402015-08-05 16:48:15 -0700145level there are three distinct layers to the library: __Stub__, __Channel__ &
146__Transport__.
ejonad4531da2014-12-15 10:24:42 -0800147
Eric Andersonfb28ad22015-01-29 15:00:58 -0800148### Stub
ejonad4531da2014-12-15 10:24:42 -0800149
Kun Zhangfc85a402015-08-05 16:48:15 -0700150The Stub layer is what is exposed to most developers and provides type-safe
151bindings to whatever datamodel/IDL/interface you are adapting. gRPC comes with
152a [plugin](https://github.com/google/grpc-java/blob/master/compiler) to the
153protocol-buffers compiler that generates Stub interfaces out of `.proto` files,
154but bindings to other datamodel/IDL should be trivial to add and are welcome.
ejonad4531da2014-12-15 10:24:42 -0800155
156#### Key Interfaces
157
nmittlerf8314582015-01-27 10:25:39 -0800158[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 -0800159
Eric Andersonfb28ad22015-01-29 15:00:58 -0800160### Channel
ejonad4531da2014-12-15 10:24:42 -0800161
Kun Zhangfc85a402015-08-05 16:48:15 -0700162The Channel layer is an abstraction over Transport handling that is suitable for
163interception/decoration and exposes more behavior to the application than the
164Stub layer. It is intended to be easy for application frameworks to use this
165layer to address cross-cutting concerns such as logging, monitoring, auth etc.
166Flow-control is also exposed at this layer to allow more sophisticated
167applications to interact with it directly.
ejonad4531da2014-12-15 10:24:42 -0800168
169#### Common
170
nmittlerf8314582015-01-27 10:25:39 -0800171* [Metadata - headers & trailers](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/Metadata.java)
172* [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 -0800173
174#### Client
nmittlerf8314582015-01-27 10:25:39 -0800175* [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 -0700176* [Client Call](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/ClientCall.java)
nmittlerf8314582015-01-27 10:25:39 -0800177* [Client Interceptor](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/ClientInterceptor.java)
ejonad4531da2014-12-15 10:24:42 -0800178
179#### Server
nmittlerf8314582015-01-27 10:25:39 -0800180* [Server call handler - analog to Channel on server](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/ServerCallHandler.java)
181* [Server Call](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/ServerCall.java)
ejonad4531da2014-12-15 10:24:42 -0800182
183
Eric Andersonfb28ad22015-01-29 15:00:58 -0800184### Transport
ejonad4531da2014-12-15 10:24:42 -0800185
mekka99907942015-08-07 23:28:06 -0700186The Transport layer does the heavy lifting of putting and taking bytes off the
Kun Zhangfc85a402015-08-05 16:48:15 -0700187wire. The interfaces to it are abstract just enough to allow plugging in of
188different implementations. Transports are modeled as `Stream` factories. The
189variation in interface between a server Stream and a client Stream exists to
190codify their differing semantics for cancellation and error reporting.
191
192Note the transport layer API is considered internal to gRPC and has weaker API
193guarantees than the core API under package `io.grpc`.
194
mekka99907942015-08-07 23:28:06 -0700195gRPC comes with three Transport implementations:
Kun Zhangfc85a402015-08-05 16:48:15 -0700196
1971. The [Netty-based](https://github.com/google/grpc-java/blob/master/netty)
198 transport is the main transport implementation based on
199 [Netty](http://netty.io). It is for both the client and the server.
2002. The [OkHttp-based](https://github.com/google/grpc-java/blob/master/okhttp)
201 transport is a lightweight transport based on
202 [OkHttp](http://square.github.io/okhttp/). It is mainly for use on Android
mekka99907942015-08-07 23:28:06 -0700203 and is for client only.
Kun Zhangfc85a402015-08-05 16:48:15 -07002043. The
Kun Zhange1bd6ef2015-08-11 11:17:39 -0700205 [inProcess](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/inprocess)
Kun Zhangfc85a402015-08-05 16:48:15 -0700206 transport is for when a server is in the same process as the client. It is
207 useful for testing.
ejonad4531da2014-12-15 10:24:42 -0800208
209#### Common
210
Kun Zhange1bd6ef2015-08-11 11:17:39 -0700211* [Stream](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/internal/Stream.java)
212* [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 -0800213
214#### Client
215
Kun Zhange1bd6ef2015-08-11 11:17:39 -0700216* [Client Stream](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/internal/ClientStream.java)
217* [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 -0800218
219#### Server
220
Kun Zhange1bd6ef2015-08-11 11:17:39 -0700221* [Server Stream](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/internal/ServerStream.java)
222* [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 -0800223
224
Eric Andersonfb28ad22015-01-29 15:00:58 -0800225### Examples
ejonad4531da2014-12-15 10:24:42 -0800226
Kun Zhangfc85a402015-08-05 16:48:15 -0700227Tests showing how these layers are composed to execute calls using protobuf
228messages can be found here
229https://github.com/google/grpc-java/tree/master/interop-testing/src/main/java/io/grpc/testing/integration