blob: 40645c39febc456ff1ceb08160f5382571ea0484 [file] [log] [blame] [view]
Eric Anderson65291322015-05-15 11:57:05 -07001[![Build Status](https://travis-ci.org/grpc/grpc-java.svg?branch=master)](https://travis-ci.org/grpc/grpc-java)
2
Kun Zhangd32bb472015-05-06 16:15:53 -07003gRPC-Java - An RPC library and framework
4========================================
5
Eric Anderson65291322015-05-15 11:57:05 -07006gRPC-Java works with JDK 6. TLS usage typically requires using Java 8, or Play
nmittler24830092015-05-28 13:12:56 -07007Services Dynamic Security Provider on Android. Please see the [Security
8Readme](SECURITY.md).
ejonad4531da2014-12-15 10:24:42 -08009
Eric Anderson1eb16d92015-05-26 14:22:32 -070010Download
11--------
12
13Download [the JAR][]. Or for Maven, add to your `pom.xml`:
14```xml
15<dependency>
16 <groupId>io.grpc</groupId>
17 <artifactId>grpc-all</artifactId>
nmittlerd94fedc2015-06-12 07:32:50 -070018 <version>0.7.1</version>
Eric Anderson1eb16d92015-05-26 14:22:32 -070019</dependency>
20```
21
22Or for Gradle, add to your dependencies:
23```gradle
nmittlerd94fedc2015-06-12 07:32:50 -070024compile 'io.grpc:grpc-all:0.7.1'
Eric Anderson1eb16d92015-05-26 14:22:32 -070025```
26
nmittlerd94fedc2015-06-12 07:32:50 -070027[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 -070028
29Development snapshots are available in [Sonatypes's snapshot
30repository](https://oss.sonatype.org/content/repositories/snapshots/).
31
32For protobuf-based codegen integrated with the Maven build system, you can use
33[maven-protoc-plugin][]:
34```xml
Kun Zhang7a716272015-06-30 18:13:27 -070035<pluginRepositories>
36 <pluginRepository>
37 <releases>
38 <updatePolicy>never</updatePolicy>
39 </releases>
40 <snapshots>
41 <enabled>false</enabled>
42 </snapshots>
43 <id>central</id>
44 <name>Central Repository</name>
45 <url>https://repo.maven.apache.org/maven2</url>
46 </pluginRepository>
47 <pluginRepository>
48 <id>protoc-plugin</id>
49 <url>https://dl.bintray.com/sergei-ivanov/maven/</url>
50 </pluginRepository>
51</pluginRepositories>
Eric Anderson1eb16d92015-05-26 14:22:32 -070052<build>
53 <extensions>
54 <extension>
55 <groupId>kr.motd.maven</groupId>
56 <artifactId>os-maven-plugin</artifactId>
57 <version>1.2.3.Final</version>
58 </extension>
59 </extensions>
60 <plugins>
61 <plugin>
62 <groupId>com.google.protobuf.tools</groupId>
63 <artifactId>maven-protoc-plugin</artifactId>
64 <version>0.4.2</version>
65 <configuration>
Kun Zhanga2511712015-06-05 14:55:26 -070066 <protocArtifact>com.google.protobuf:protoc:3.0.0-alpha-3:exe:${os.detected.classifier}</protocArtifact>
Eric Anderson1eb16d92015-05-26 14:22:32 -070067 <pluginId>grpc-java</pluginId>
nmittlerd94fedc2015-06-12 07:32:50 -070068 <pluginArtifact>io.grpc:protoc-gen-grpc-java:0.7.1:exe:${os.detected.classifier}</pluginArtifact>
Eric Anderson1eb16d92015-05-26 14:22:32 -070069 </configuration>
70 <executions>
71 <execution>
72 <goals>
73 <goal>compile</goal>
74 <goal>compile-custom</goal>
75 </goals>
76 </execution>
77 </executions>
78 </plugin>
79 </plugins>
80</build>
81```
82
83[maven-protoc-plugin]: http://sergei-ivanov.github.io/maven-protoc-plugin/
84
85For protobuf-based codegen integrated with the Gradle build system, you can use
86[protobuf-gradle-plugin][]:
87```gradle
88apply plugin: 'com.google.protobuf'
89
90buildscript {
91 repositories {
92 mavenCentral()
93 }
94 dependencies {
95 classpath 'com.google.protobuf:protobuf-gradle-plugin:0.4.1'
96 }
97}
98
99sourceSets {
100 main {
101 proto {
102 plugins {
103 grpc { }
104 }
105 }
106 }
107}
108
Kun Zhanga2511712015-06-05 14:55:26 -0700109protocDep = "com.google.protobuf:protoc:3.0.0-alpha-3"
nmittlerd94fedc2015-06-12 07:32:50 -0700110protobufNativeCodeGenPluginDeps = ["grpc:io.grpc:protoc-gen-grpc-java:0.7.1"]
Eric Anderson1eb16d92015-05-26 14:22:32 -0700111```
112
113[protobuf-gradle-plugin]: https://github.com/google/protobuf-gradle-plugin
114
Eric Andersonfb28ad22015-01-29 15:00:58 -0800115How to Build
116------------
Eric Anderson1eb16d92015-05-26 14:22:32 -0700117This section is only necessary if you are making changes to gRPC-Java.
Eric Andersonfb28ad22015-01-29 15:00:58 -0800118
Kun Zhangfd0aed22015-04-30 17:29:17 -0700119### Build Netty
Jakob Buchgraberc56cec72015-03-11 13:44:32 -0700120grpc-java requires Netty 4.1, which is still in flux. The version we need can be
Eric Andersonb10aaf82015-02-19 13:14:00 -0800121found in the lib/netty submodule, which requires Maven 3.2 or higher to build:
Eric Andersonfb28ad22015-01-29 15:00:58 -0800122```
123$ git submodule update --init
124$ cd lib/netty
125$ mvn install -pl codec-http2 -am -DskipTests=true
126```
127
Eric Anderson66c55ee2015-05-07 15:07:13 -0700128### Build gRPC
Eric Anderson65291322015-05-15 11:57:05 -0700129Building requires JDK 8, as our tests use TLS.
130
Eric Anderson66c55ee2015-05-07 15:07:13 -0700131grpc-java has a C++ code generation plugin for protoc. Since many Java
132developers don't have C compilers installed and don't need to modify the
133codegen, the build can skip it. To skip, create the file
134`<project-root>/gradle.properties` and add `skipCodegen=true`.
135
136Then, to build, run:
137```
138$ ./gradlew build
139```
140
141To install the artifacts to your Maven local repository for use in your own
142project, run:
143```
144$ ./gradlew install
145```
146
147How to Build Code Generation Plugin
148-----------------------------------
149This section is only necessary if you are making changes to the code
150generation. Most users only need to use `skipCodegen=true` as discussed above.
151
Kun Zhangfd0aed22015-04-30 17:29:17 -0700152### Build Protobuf
Kun Zhanga2511712015-06-05 14:55:26 -0700153The codegen plugin is C++ code and requires protobuf 3.0.0-alpha-3.
Kun Zhang111f6dd2015-05-05 16:11:27 -0700154
Kun Zhangfd0aed22015-04-30 17:29:17 -0700155For Linux, Mac and MinGW:
Eric Andersonfb28ad22015-01-29 15:00:58 -0800156```
157$ git clone https://github.com/google/protobuf.git
158$ cd protobuf
Kun Zhanga2511712015-06-05 14:55:26 -0700159$ git checkout v3.0.0-alpha-3
Eric Andersonfb28ad22015-01-29 15:00:58 -0800160$ ./autogen.sh
161$ ./configure
162$ make
163$ make check
164$ sudo make install
Eric Andersonfb28ad22015-01-29 15:00:58 -0800165```
166
167If you are comfortable with C++ compilation and autotools, you can specify a
Kun Zhangfd0aed22015-04-30 17:29:17 -0700168``--prefix`` for Protobuf and use ``-I`` in ``CXXFLAGS``, ``-L`` in
169``LDFLAGS``, ``LD_LIBRARY_PATH``, and ``PATH`` to reference it. The
170environment variables will be used when building grpc-java.
Eric Andersonfb28ad22015-01-29 15:00:58 -0800171
Kun Zhangfd0aed22015-04-30 17:29:17 -0700172Protobuf installs to ``/usr/local`` by default.
173
174For Visual C++, please refer to the [Protobuf README](https://github.com/google/protobuf/blob/master/vsprojects/readme.txt)
175for how to compile Protobuf.
176
177#### Linux and MinGW
178If ``/usr/local/lib`` is not in your library search path, you can add it by running:
Louis Ryan65d64cd2015-02-18 14:51:42 -0800179```
180$ sudo sh -c 'echo /usr/local/lib >> /etc/ld.so.conf'
181$ sudo ldconfig
182```
183
Kun Zhangfd0aed22015-04-30 17:29:17 -0700184#### Mac
185Some versions of Mac OS X (e.g., 10.10) doesn't have ``/usr/local`` in the
Kun Zhang111f6dd2015-05-05 16:11:27 -0700186default search paths for header files and libraries. It will fail the build of
187the codegen. To work around this, you will need to set environment variables:
Kun Zhangfd0aed22015-04-30 17:29:17 -0700188```
189$ export CXXFLAGS="-I/usr/local/include" LDFLAGS="-L/usr/local/lib"
190```
191
Kun Zhang221c5342015-04-29 18:16:15 -0700192### Notes for Visual C++
193
Eric Anderson456216b2015-03-02 16:58:27 -0800194When building on Windows and VC++, you need to specify project properties for
195Gradle to find protobuf:
196```
Kun Zhang221c5342015-04-29 18:16:15 -0700197.\gradlew install ^
Kun Zhanga2511712015-06-05 14:55:26 -0700198 -PvcProtobufInclude=C:\path\to\protobuf-3.0.0-alpha-3\src ^
199 -PvcProtobufLibs=C:\path\to\protobuf-3.0.0-alpha-3\vsprojects\Release
Eric Anderson456216b2015-03-02 16:58:27 -0800200```
201
202Since specifying those properties every build is bothersome, you can instead
Kun Zhang2f749712015-05-06 13:10:28 -0700203create ``<project-root>\gradle.properties`` with contents like:
Eric Anderson456216b2015-03-02 16:58:27 -0800204```
Kun Zhanga2511712015-06-05 14:55:26 -0700205vcProtobufInclude=C:\\path\\to\\protobuf-3.0.0-alpha-3\\src
206vcProtobufLibs=C:\\path\\to\\protobuf-3.0.0-alpha-3\\vsprojects\\Release
Eric Anderson456216b2015-03-02 16:58:27 -0800207```
208
Kun Zhang221c5342015-04-29 18:16:15 -0700209The build script will build the codegen for the same architecture as the Java
210runtime installed on your system. If you are using 64-bit JVM, the codegen will
211be compiled for 64-bit, that means you must have compiled Protobuf in 64-bit.
212
213### Notes for MinGW on Windows
214If you have both MinGW and VC++ installed on Windows, VC++ will be used by
Kun Zhang2f749712015-05-06 13:10:28 -0700215default. To override this default and use MinGW, add ``-PvcDisable=true``
216to your Gradle command line or add ``vcDisable=true`` to your
217``<project-root>\gradle.properties``.
Kun Zhang221c5342015-04-29 18:16:15 -0700218
Kun Zhang9805e272015-05-12 17:03:19 -0700219### Notes for unsupported operating systems
220The build script pulls pre-compiled ``protoc`` from Maven Central by default.
221We have built ``protoc`` binaries for popular systems, but they may not work
222for your system. If ``protoc`` cannot be downloaded or would not run, you can
223use the one that has been built by your own, by adding this property to
224``<project-root>/gradle.properties``:
225```
226protoc=/path/to/protoc
227```
228
Eric Andersonfb28ad22015-01-29 15:00:58 -0800229Navigating Around the Source
230----------------------------
231
ejonad4531da2014-12-15 10:24:42 -0800232Heres a quick readers guide to the code to help folks get started. At a high level there are three distinct layers
Kun Zhanga8ef36a2015-04-22 14:46:07 -0700233to the library: stub, channel & transport.
ejonad4531da2014-12-15 10:24:42 -0800234
Eric Andersonfb28ad22015-01-29 15:00:58 -0800235### Stub
ejonad4531da2014-12-15 10:24:42 -0800236
237The 'stub' layer is what is exposed to most developers and provides type-safe bindings to whatever
238datamodel/IDL/interface you are adapting. An example is provided of a binding to code generated by the protocol-buffers compiler but others should be trivial to add and are welcome.
239
240#### Key Interfaces
241
nmittlerf8314582015-01-27 10:25:39 -0800242[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 -0800243
244
Eric Andersonfb28ad22015-01-29 15:00:58 -0800245### Channel
ejonad4531da2014-12-15 10:24:42 -0800246
247The 'channel' layer is an abstraction over transport handling that is suitable for interception/decoration and exposes more behavior to the application than the stub layer. It is intended to be easy for application frameworks to use this layer to address cross-cutting concerns such as logging, monitoring, auth etc. Flow-control is also exposed at this layer to allow more sophisticated applications to interact with it directly.
248
249#### Common
250
nmittlerf8314582015-01-27 10:25:39 -0800251* [Metadata - headers & trailers](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/Metadata.java)
252* [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 -0800253
254#### Client
nmittlerf8314582015-01-27 10:25:39 -0800255* [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 -0700256* [Client Call](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/ClientCall.java)
nmittlerf8314582015-01-27 10:25:39 -0800257* [Client Interceptor](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/ClientInterceptor.java)
ejonad4531da2014-12-15 10:24:42 -0800258
259#### Server
nmittlerf8314582015-01-27 10:25:39 -0800260* [Server call handler - analog to Channel on server](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/ServerCallHandler.java)
261* [Server Call](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/ServerCall.java)
ejonad4531da2014-12-15 10:24:42 -0800262
263
Eric Andersonfb28ad22015-01-29 15:00:58 -0800264### Transport
ejonad4531da2014-12-15 10:24:42 -0800265
266The 'transport' layer does the heavy lifting of putting & taking bytes off the wire. The interfaces to it are abstract just enough to allow plugging in of different implementations. Transports are modeled as 'Stream' factories. The variation in interface between a server stream and a client stream exists to codify their differing semantics for cancellation and error reporting.
267
268#### Common
269
nmittlerf8314582015-01-27 10:25:39 -0800270* [Stream](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/transport/Stream.java)
271* [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 -0800272
273#### Client
274
nmittlerf8314582015-01-27 10:25:39 -0800275* [Client Stream](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/transport/ClientStream.java)
276* [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 -0800277
278#### Server
279
nmittlerf8314582015-01-27 10:25:39 -0800280* [Server Stream](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/transport/ServerStream.java)
281* [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 -0800282
283
Eric Andersonfb28ad22015-01-29 15:00:58 -0800284### Examples
ejonad4531da2014-12-15 10:24:42 -0800285
Eric Andersone03d5c02015-05-20 13:23:11 -0700286Tests showing how these layers are composed to execute calls using protobuf messages can be found here https://github.com/google/grpc-java/tree/master/interop-testing/src/main/java/io/grpc/testing/integration