blob: ac248344f2cc6cc26b885b63f70b7cd66a596b10 [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>
nmittlerd94fedc2015-06-12 07:32:50 -070019 <version>0.7.1</version>
Eric Anderson1eb16d92015-05-26 14:22:32 -070020</dependency>
21```
22
23Or for Gradle, add to your dependencies:
24```gradle
nmittlerd94fedc2015-06-12 07:32:50 -070025compile 'io.grpc:grpc-all:0.7.1'
Eric Anderson1eb16d92015-05-26 14:22:32 -070026```
27
nmittlerd94fedc2015-06-12 07:32:50 -070028[the JAR]: https://search.maven.org/remote_content?g=io.grpc&a=grpc-all&v=0.7.1
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 -->
72 <protocArtifact>com.google.protobuf:protoc:3.0.0-alpha-2:exe:${os.detected.classifier}</protocArtifact>
Eric Anderson1eb16d92015-05-26 14:22:32 -070073 <pluginId>grpc-java</pluginId>
nmittlerd94fedc2015-06-12 07:32:50 -070074 <pluginArtifact>io.grpc:protoc-gen-grpc-java:0.7.1: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.
111 artifact = "com.google.protobuf:protoc:3.0.0-alpha-2"
112 }
113 plugins {
114 grpc {
115 artifact = 'io.grpc:protoc-gen-grpc-java:0.7.1'
116 }
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------------
Eric Anderson1eb16d92015-05-26 14:22:32 -0700130This section is only necessary if you are making changes to gRPC-Java.
Eric Andersonfb28ad22015-01-29 15:00:58 -0800131
Eric Anderson65291322015-05-15 11:57:05 -0700132Building requires JDK 8, as our tests use TLS.
133
Eric Anderson66c55ee2015-05-07 15:07:13 -0700134grpc-java has a C++ code generation plugin for protoc. Since many Java
135developers don't have C compilers installed and don't need to modify the
136codegen, the build can skip it. To skip, create the file
137`<project-root>/gradle.properties` and add `skipCodegen=true`.
138
139Then, to build, run:
140```
141$ ./gradlew build
142```
143
144To install the artifacts to your Maven local repository for use in your own
145project, run:
146```
147$ ./gradlew install
148```
149
150How to Build Code Generation Plugin
151-----------------------------------
152This section is only necessary if you are making changes to the code
153generation. Most users only need to use `skipCodegen=true` as discussed above.
154
Kun Zhangfd0aed22015-04-30 17:29:17 -0700155### Build Protobuf
Kun Zhangaf9fb6d2015-06-30 16:08:46 -0700156The codegen plugin is C++ code and requires protobuf 3.0.0-alpha-3.1.
Kun Zhang111f6dd2015-05-05 16:11:27 -0700157
Kun Zhangfd0aed22015-04-30 17:29:17 -0700158For Linux, Mac and MinGW:
Eric Andersonfb28ad22015-01-29 15:00:58 -0800159```
160$ git clone https://github.com/google/protobuf.git
161$ cd protobuf
Kun Zhangaf9fb6d2015-06-30 16:08:46 -0700162$ git checkout v3.0.0-alpha-3.1
Eric Andersonfb28ad22015-01-29 15:00:58 -0800163$ ./autogen.sh
164$ ./configure
165$ make
166$ make check
167$ sudo make install
Eric Andersonfb28ad22015-01-29 15:00:58 -0800168```
169
170If you are comfortable with C++ compilation and autotools, you can specify a
Kun Zhangfd0aed22015-04-30 17:29:17 -0700171``--prefix`` for Protobuf and use ``-I`` in ``CXXFLAGS``, ``-L`` in
172``LDFLAGS``, ``LD_LIBRARY_PATH``, and ``PATH`` to reference it. The
173environment variables will be used when building grpc-java.
Eric Andersonfb28ad22015-01-29 15:00:58 -0800174
Kun Zhangfd0aed22015-04-30 17:29:17 -0700175Protobuf installs to ``/usr/local`` by default.
176
177For Visual C++, please refer to the [Protobuf README](https://github.com/google/protobuf/blob/master/vsprojects/readme.txt)
178for how to compile Protobuf.
179
180#### Linux and MinGW
181If ``/usr/local/lib`` is not in your library search path, you can add it by running:
Louis Ryan65d64cd2015-02-18 14:51:42 -0800182```
183$ sudo sh -c 'echo /usr/local/lib >> /etc/ld.so.conf'
184$ sudo ldconfig
185```
186
Kun Zhangfd0aed22015-04-30 17:29:17 -0700187#### Mac
188Some versions of Mac OS X (e.g., 10.10) doesn't have ``/usr/local`` in the
Kun Zhang111f6dd2015-05-05 16:11:27 -0700189default search paths for header files and libraries. It will fail the build of
190the codegen. To work around this, you will need to set environment variables:
Kun Zhangfd0aed22015-04-30 17:29:17 -0700191```
192$ export CXXFLAGS="-I/usr/local/include" LDFLAGS="-L/usr/local/lib"
193```
194
Kun Zhang221c5342015-04-29 18:16:15 -0700195### Notes for Visual C++
196
Eric Anderson456216b2015-03-02 16:58:27 -0800197When building on Windows and VC++, you need to specify project properties for
198Gradle to find protobuf:
199```
Kun Zhang221c5342015-04-29 18:16:15 -0700200.\gradlew install ^
Kun Zhangaf9fb6d2015-06-30 16:08:46 -0700201 -PvcProtobufInclude=C:\path\to\protobuf-3.0.0-alpha-3.1\src ^
202 -PvcProtobufLibs=C:\path\to\protobuf-3.0.0-alpha-3.1\vsprojects\Release
Eric Anderson456216b2015-03-02 16:58:27 -0800203```
204
205Since specifying those properties every build is bothersome, you can instead
Kun Zhang2f749712015-05-06 13:10:28 -0700206create ``<project-root>\gradle.properties`` with contents like:
Eric Anderson456216b2015-03-02 16:58:27 -0800207```
Kun Zhangaf9fb6d2015-06-30 16:08:46 -0700208vcProtobufInclude=C:\\path\\to\\protobuf-3.0.0-alpha-3.1\\src
209vcProtobufLibs=C:\\path\\to\\protobuf-3.0.0-alpha-3.1\\vsprojects\\Release
Eric Anderson456216b2015-03-02 16:58:27 -0800210```
211
Kun Zhang221c5342015-04-29 18:16:15 -0700212The build script will build the codegen for the same architecture as the Java
213runtime installed on your system. If you are using 64-bit JVM, the codegen will
214be compiled for 64-bit, that means you must have compiled Protobuf in 64-bit.
215
216### Notes for MinGW on Windows
217If you have both MinGW and VC++ installed on Windows, VC++ will be used by
Kun Zhang2f749712015-05-06 13:10:28 -0700218default. To override this default and use MinGW, add ``-PvcDisable=true``
219to your Gradle command line or add ``vcDisable=true`` to your
220``<project-root>\gradle.properties``.
Kun Zhang221c5342015-04-29 18:16:15 -0700221
mekka99907942015-08-07 23:28:06 -0700222### Notes for Unsupported Operating Systems
Kun Zhang9805e272015-05-12 17:03:19 -0700223The build script pulls pre-compiled ``protoc`` from Maven Central by default.
224We have built ``protoc`` binaries for popular systems, but they may not work
225for your system. If ``protoc`` cannot be downloaded or would not run, you can
226use the one that has been built by your own, by adding this property to
227``<project-root>/gradle.properties``:
228```
229protoc=/path/to/protoc
230```
231
Eric Andersonfb28ad22015-01-29 15:00:58 -0800232Navigating Around the Source
233----------------------------
234
mekka99907942015-08-07 23:28:06 -0700235Here's a quick readers' guide to the code to help folks get started. At a high
Kun Zhangfc85a402015-08-05 16:48:15 -0700236level there are three distinct layers to the library: __Stub__, __Channel__ &
237__Transport__.
ejonad4531da2014-12-15 10:24:42 -0800238
Eric Andersonfb28ad22015-01-29 15:00:58 -0800239### Stub
ejonad4531da2014-12-15 10:24:42 -0800240
Kun Zhangfc85a402015-08-05 16:48:15 -0700241The Stub layer is what is exposed to most developers and provides type-safe
242bindings to whatever datamodel/IDL/interface you are adapting. gRPC comes with
243a [plugin](https://github.com/google/grpc-java/blob/master/compiler) to the
244protocol-buffers compiler that generates Stub interfaces out of `.proto` files,
245but bindings to other datamodel/IDL should be trivial to add and are welcome.
ejonad4531da2014-12-15 10:24:42 -0800246
247#### Key Interfaces
248
nmittlerf8314582015-01-27 10:25:39 -0800249[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 -0800250
Eric Andersonfb28ad22015-01-29 15:00:58 -0800251### Channel
ejonad4531da2014-12-15 10:24:42 -0800252
Kun Zhangfc85a402015-08-05 16:48:15 -0700253The Channel layer is an abstraction over Transport handling that is suitable for
254interception/decoration and exposes more behavior to the application than the
255Stub layer. It is intended to be easy for application frameworks to use this
256layer to address cross-cutting concerns such as logging, monitoring, auth etc.
257Flow-control is also exposed at this layer to allow more sophisticated
258applications to interact with it directly.
ejonad4531da2014-12-15 10:24:42 -0800259
260#### Common
261
nmittlerf8314582015-01-27 10:25:39 -0800262* [Metadata - headers & trailers](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/Metadata.java)
263* [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 -0800264
265#### Client
nmittlerf8314582015-01-27 10:25:39 -0800266* [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 -0700267* [Client Call](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/ClientCall.java)
nmittlerf8314582015-01-27 10:25:39 -0800268* [Client Interceptor](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/ClientInterceptor.java)
ejonad4531da2014-12-15 10:24:42 -0800269
270#### Server
nmittlerf8314582015-01-27 10:25:39 -0800271* [Server call handler - analog to Channel on server](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/ServerCallHandler.java)
272* [Server Call](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/ServerCall.java)
ejonad4531da2014-12-15 10:24:42 -0800273
274
Eric Andersonfb28ad22015-01-29 15:00:58 -0800275### Transport
ejonad4531da2014-12-15 10:24:42 -0800276
mekka99907942015-08-07 23:28:06 -0700277The Transport layer does the heavy lifting of putting and taking bytes off the
Kun Zhangfc85a402015-08-05 16:48:15 -0700278wire. The interfaces to it are abstract just enough to allow plugging in of
279different implementations. Transports are modeled as `Stream` factories. The
280variation in interface between a server Stream and a client Stream exists to
281codify their differing semantics for cancellation and error reporting.
282
283Note the transport layer API is considered internal to gRPC and has weaker API
284guarantees than the core API under package `io.grpc`.
285
mekka99907942015-08-07 23:28:06 -0700286gRPC comes with three Transport implementations:
Kun Zhangfc85a402015-08-05 16:48:15 -0700287
2881. The [Netty-based](https://github.com/google/grpc-java/blob/master/netty)
289 transport is the main transport implementation based on
290 [Netty](http://netty.io). It is for both the client and the server.
2912. The [OkHttp-based](https://github.com/google/grpc-java/blob/master/okhttp)
292 transport is a lightweight transport based on
293 [OkHttp](http://square.github.io/okhttp/). It is mainly for use on Android
mekka99907942015-08-07 23:28:06 -0700294 and is for client only.
Kun Zhangfc85a402015-08-05 16:48:15 -07002953. The
mekka99907942015-08-07 23:28:06 -0700296 [inProcess](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/transport/inprocess)
Kun Zhangfc85a402015-08-05 16:48:15 -0700297 transport is for when a server is in the same process as the client. It is
298 useful for testing.
ejonad4531da2014-12-15 10:24:42 -0800299
300#### Common
301
nmittlerf8314582015-01-27 10:25:39 -0800302* [Stream](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/transport/Stream.java)
303* [Stream Listener](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/transport/StreamListener.java)
ejonad4531da2014-12-15 10:24:42 -0800304
305#### Client
306
nmittlerf8314582015-01-27 10:25:39 -0800307* [Client Stream](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/transport/ClientStream.java)
308* [Client Stream Listener](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/transport/ClientStreamListener.java)
ejonad4531da2014-12-15 10:24:42 -0800309
310#### Server
311
nmittlerf8314582015-01-27 10:25:39 -0800312* [Server Stream](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/transport/ServerStream.java)
313* [Server Stream Listener](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/transport/ServerStreamListener.java)
ejonad4531da2014-12-15 10:24:42 -0800314
315
Eric Andersonfb28ad22015-01-29 15:00:58 -0800316### Examples
ejonad4531da2014-12-15 10:24:42 -0800317
Kun Zhangfc85a402015-08-05 16:48:15 -0700318Tests showing how these layers are composed to execute calls using protobuf
319messages can be found here
320https://github.com/google/grpc-java/tree/master/interop-testing/src/main/java/io/grpc/testing/integration