blob: 4a38afa9e98f0b8ba6567f76c1b56d206b762fd7 [file] [log] [blame] [view]
Kun Zhangd32bb472015-05-06 16:15:53 -07001gRPC-Java - An RPC library and framework
2========================================
3
Eric Anderson66ce0f22015-08-11 11:39:07 -07004[![Build Status](https://travis-ci.org/grpc/grpc-java.svg?branch=master)](https://travis-ci.org/grpc/grpc-java)
5[![Coverage Status](https://coveralls.io/repos/grpc/grpc-java/badge.svg?branch=master&service=github)](https://coveralls.io/github/grpc/grpc-java?branch=master)
6
Eric Anderson65291322015-05-15 11:57:05 -07007gRPC-Java works with JDK 6. TLS usage typically requires using Java 8, or Play
nmittler24830092015-05-28 13:12:56 -07008Services Dynamic Security Provider on Android. Please see the [Security
9Readme](SECURITY.md).
ejonad4531da2014-12-15 10:24:42 -080010
Eric Anderson1eb16d92015-05-26 14:22:32 -070011Download
12--------
13
14Download [the JAR][]. Or for Maven, add to your `pom.xml`:
15```xml
16<dependency>
17 <groupId>io.grpc</groupId>
18 <artifactId>grpc-all</artifactId>
Carl Mastrangelo3c924fd2015-08-13 15:49:22 -070019 <version>0.8.0</version>
Eric Anderson1eb16d92015-05-26 14:22:32 -070020</dependency>
21```
22
23Or for Gradle, add to your dependencies:
24```gradle
Carl Mastrangelo3c924fd2015-08-13 15:49:22 -070025compile 'io.grpc:grpc-all:0.8.0'
Eric Anderson1eb16d92015-05-26 14:22:32 -070026```
27
Carl Mastrangelo3c924fd2015-08-13 15:49:22 -070028[the JAR]: https://search.maven.org/remote_content?g=io.grpc&a=grpc-all&v=0.8.0
Eric Anderson1eb16d92015-05-26 14:22:32 -070029
30Development snapshots are available in [Sonatypes's snapshot
31repository](https://oss.sonatype.org/content/repositories/snapshots/).
32
33For protobuf-based codegen integrated with the Maven build system, you can use
34[maven-protoc-plugin][]:
35```xml
Kun Zhang7a716272015-06-30 18:13:27 -070036<pluginRepositories>
37 <pluginRepository>
38 <releases>
39 <updatePolicy>never</updatePolicy>
40 </releases>
41 <snapshots>
42 <enabled>false</enabled>
43 </snapshots>
44 <id>central</id>
45 <name>Central Repository</name>
46 <url>https://repo.maven.apache.org/maven2</url>
47 </pluginRepository>
48 <pluginRepository>
49 <id>protoc-plugin</id>
50 <url>https://dl.bintray.com/sergei-ivanov/maven/</url>
51 </pluginRepository>
52</pluginRepositories>
Eric Anderson1eb16d92015-05-26 14:22:32 -070053<build>
54 <extensions>
55 <extension>
56 <groupId>kr.motd.maven</groupId>
57 <artifactId>os-maven-plugin</artifactId>
58 <version>1.2.3.Final</version>
59 </extension>
60 </extensions>
61 <plugins>
62 <plugin>
63 <groupId>com.google.protobuf.tools</groupId>
64 <artifactId>maven-protoc-plugin</artifactId>
65 <version>0.4.2</version>
66 <configuration>
Kun Zhang8bd8ed82015-07-22 09:19:52 -070067 <!--
68 The version of protoc must match protobuf-java. If you don't depend on
69 protobuf-java directly, you will be transitively depending on the
70 protobuf-java version that grpc depends on.
71 -->
Carl Mastrangelo3c924fd2015-08-13 15:49:22 -070072 <protocArtifact>com.google.protobuf:protoc:3.0.0-alpha-3.1:exe:${os.detected.classifier}</protocArtifact>
Eric Anderson1eb16d92015-05-26 14:22:32 -070073 <pluginId>grpc-java</pluginId>
Carl Mastrangelo3c924fd2015-08-13 15:49:22 -070074 <pluginArtifact>io.grpc:protoc-gen-grpc-java:0.8.0:exe:${os.detected.classifier}</pluginArtifact>
Eric Anderson1eb16d92015-05-26 14:22:32 -070075 </configuration>
76 <executions>
77 <execution>
78 <goals>
79 <goal>compile</goal>
80 <goal>compile-custom</goal>
81 </goals>
82 </execution>
83 </executions>
84 </plugin>
85 </plugins>
86</build>
87```
88
89[maven-protoc-plugin]: http://sergei-ivanov.github.io/maven-protoc-plugin/
90
91For protobuf-based codegen integrated with the Gradle build system, you can use
92[protobuf-gradle-plugin][]:
93```gradle
Kun Zhang8bd8ed82015-07-22 09:19:52 -070094apply plugin: 'java'
Eric Anderson1eb16d92015-05-26 14:22:32 -070095apply plugin: 'com.google.protobuf'
96
97buildscript {
98 repositories {
99 mavenCentral()
100 }
101 dependencies {
Kun Zhang137b2ef2015-08-03 11:10:22 -0700102 classpath 'com.google.protobuf:protobuf-gradle-plugin:0.6.1'
Eric Anderson1eb16d92015-05-26 14:22:32 -0700103 }
104}
105
Kun Zhang8bd8ed82015-07-22 09:19:52 -0700106protobuf {
107 protoc {
108 // The version of protoc must match protobuf-java. If you don't depend on
109 // protobuf-java directly, you will be transitively depending on the
110 // protobuf-java version that grpc depends on.
Carl Mastrangelo3c924fd2015-08-13 15:49:22 -0700111 artifact = "com.google.protobuf:protoc:3.0.0-alpha-3.1"
Kun Zhang8bd8ed82015-07-22 09:19:52 -0700112 }
113 plugins {
114 grpc {
Carl Mastrangelo3c924fd2015-08-13 15:49:22 -0700115 artifact = 'io.grpc:protoc-gen-grpc-java:0.8.0'
Kun Zhang8bd8ed82015-07-22 09:19:52 -0700116 }
117 }
118 generateProtoTasks {
119 all()*.plugins {
120 grpc {}
Eric Anderson1eb16d92015-05-26 14:22:32 -0700121 }
122 }
123}
Eric Anderson1eb16d92015-05-26 14:22:32 -0700124```
125
126[protobuf-gradle-plugin]: https://github.com/google/protobuf-gradle-plugin
127
Eric Andersonfb28ad22015-01-29 15:00:58 -0800128How to Build
129------------
130
Eric Anderson1b1c6462015-08-14 08:49:56 -0700131If you are making changes to gRPC-Java, see the [compiling
132instructions](COMPILING.md).
Kun Zhang9805e272015-05-12 17:03:19 -0700133
Eric Andersonfb28ad22015-01-29 15:00:58 -0800134Navigating Around the Source
135----------------------------
136
mekka99907942015-08-07 23:28:06 -0700137Here's a quick readers' guide to the code to help folks get started. At a high
Kun Zhangfc85a402015-08-05 16:48:15 -0700138level there are three distinct layers to the library: __Stub__, __Channel__ &
139__Transport__.
ejonad4531da2014-12-15 10:24:42 -0800140
Eric Andersonfb28ad22015-01-29 15:00:58 -0800141### Stub
ejonad4531da2014-12-15 10:24:42 -0800142
Kun Zhangfc85a402015-08-05 16:48:15 -0700143The Stub layer is what is exposed to most developers and provides type-safe
144bindings to whatever datamodel/IDL/interface you are adapting. gRPC comes with
145a [plugin](https://github.com/google/grpc-java/blob/master/compiler) to the
146protocol-buffers compiler that generates Stub interfaces out of `.proto` files,
147but bindings to other datamodel/IDL should be trivial to add and are welcome.
ejonad4531da2014-12-15 10:24:42 -0800148
149#### Key Interfaces
150
nmittlerf8314582015-01-27 10:25:39 -0800151[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 -0800152
Eric Andersonfb28ad22015-01-29 15:00:58 -0800153### Channel
ejonad4531da2014-12-15 10:24:42 -0800154
Kun Zhangfc85a402015-08-05 16:48:15 -0700155The Channel layer is an abstraction over Transport handling that is suitable for
156interception/decoration and exposes more behavior to the application than the
157Stub layer. It is intended to be easy for application frameworks to use this
158layer to address cross-cutting concerns such as logging, monitoring, auth etc.
159Flow-control is also exposed at this layer to allow more sophisticated
160applications to interact with it directly.
ejonad4531da2014-12-15 10:24:42 -0800161
162#### Common
163
nmittlerf8314582015-01-27 10:25:39 -0800164* [Metadata - headers & trailers](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/Metadata.java)
165* [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 -0800166
167#### Client
nmittlerf8314582015-01-27 10:25:39 -0800168* [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 -0700169* [Client Call](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/ClientCall.java)
nmittlerf8314582015-01-27 10:25:39 -0800170* [Client Interceptor](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/ClientInterceptor.java)
ejonad4531da2014-12-15 10:24:42 -0800171
172#### Server
nmittlerf8314582015-01-27 10:25:39 -0800173* [Server call handler - analog to Channel on server](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/ServerCallHandler.java)
174* [Server Call](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/ServerCall.java)
ejonad4531da2014-12-15 10:24:42 -0800175
176
Eric Andersonfb28ad22015-01-29 15:00:58 -0800177### Transport
ejonad4531da2014-12-15 10:24:42 -0800178
mekka99907942015-08-07 23:28:06 -0700179The Transport layer does the heavy lifting of putting and taking bytes off the
Kun Zhangfc85a402015-08-05 16:48:15 -0700180wire. The interfaces to it are abstract just enough to allow plugging in of
181different implementations. Transports are modeled as `Stream` factories. The
182variation in interface between a server Stream and a client Stream exists to
183codify their differing semantics for cancellation and error reporting.
184
185Note the transport layer API is considered internal to gRPC and has weaker API
186guarantees than the core API under package `io.grpc`.
187
mekka99907942015-08-07 23:28:06 -0700188gRPC comes with three Transport implementations:
Kun Zhangfc85a402015-08-05 16:48:15 -0700189
1901. The [Netty-based](https://github.com/google/grpc-java/blob/master/netty)
191 transport is the main transport implementation based on
192 [Netty](http://netty.io). It is for both the client and the server.
1932. The [OkHttp-based](https://github.com/google/grpc-java/blob/master/okhttp)
194 transport is a lightweight transport based on
195 [OkHttp](http://square.github.io/okhttp/). It is mainly for use on Android
mekka99907942015-08-07 23:28:06 -0700196 and is for client only.
Kun Zhangfc85a402015-08-05 16:48:15 -07001973. The
Kun Zhange1bd6ef2015-08-11 11:17:39 -0700198 [inProcess](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/inprocess)
Kun Zhangfc85a402015-08-05 16:48:15 -0700199 transport is for when a server is in the same process as the client. It is
200 useful for testing.
ejonad4531da2014-12-15 10:24:42 -0800201
202#### Common
203
Kun Zhange1bd6ef2015-08-11 11:17:39 -0700204* [Stream](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/internal/Stream.java)
205* [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 -0800206
207#### Client
208
Kun Zhange1bd6ef2015-08-11 11:17:39 -0700209* [Client Stream](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/internal/ClientStream.java)
210* [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 -0800211
212#### Server
213
Kun Zhange1bd6ef2015-08-11 11:17:39 -0700214* [Server Stream](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/internal/ServerStream.java)
215* [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 -0800216
217
Eric Andersonfb28ad22015-01-29 15:00:58 -0800218### Examples
ejonad4531da2014-12-15 10:24:42 -0800219
Kun Zhangfc85a402015-08-05 16:48:15 -0700220Tests showing how these layers are composed to execute calls using protobuf
221messages can be found here
222https://github.com/google/grpc-java/tree/master/interop-testing/src/main/java/io/grpc/testing/integration