blob: a3687999aa4f0c6e7437d76e543f5cabea9c6b1c [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
7Services Dynamic Security Provider on Android. Please see the [Auth
8Readme](AUTH-README.md).
ejonad4531da2014-12-15 10:24:42 -08009
Eric Andersonfb28ad22015-01-29 15:00:58 -080010How to Build
11------------
12
Kun Zhangfd0aed22015-04-30 17:29:17 -070013### Build Netty
Jakob Buchgraberc56cec72015-03-11 13:44:32 -070014grpc-java requires Netty 4.1, which is still in flux. The version we need can be
Eric Andersonb10aaf82015-02-19 13:14:00 -080015found in the lib/netty submodule, which requires Maven 3.2 or higher to build:
Eric Andersonfb28ad22015-01-29 15:00:58 -080016```
17$ git submodule update --init
18$ cd lib/netty
19$ mvn install -pl codec-http2 -am -DskipTests=true
20```
21
Eric Anderson66c55ee2015-05-07 15:07:13 -070022### Build gRPC
Eric Anderson65291322015-05-15 11:57:05 -070023Building requires JDK 8, as our tests use TLS.
24
Eric Anderson66c55ee2015-05-07 15:07:13 -070025grpc-java has a C++ code generation plugin for protoc. Since many Java
26developers don't have C compilers installed and don't need to modify the
27codegen, the build can skip it. To skip, create the file
28`<project-root>/gradle.properties` and add `skipCodegen=true`.
29
30Then, to build, run:
31```
32$ ./gradlew build
33```
34
35To install the artifacts to your Maven local repository for use in your own
36project, run:
37```
38$ ./gradlew install
39```
40
41How to Build Code Generation Plugin
42-----------------------------------
43This section is only necessary if you are making changes to the code
44generation. Most users only need to use `skipCodegen=true` as discussed above.
45
Kun Zhangfd0aed22015-04-30 17:29:17 -070046### Build Protobuf
Kun Zhang111f6dd2015-05-05 16:11:27 -070047The codegen plugin is C++ code and requires protobuf 3.0.0-alpha-2.
48
Kun Zhangfd0aed22015-04-30 17:29:17 -070049For Linux, Mac and MinGW:
Eric Andersonfb28ad22015-01-29 15:00:58 -080050```
51$ git clone https://github.com/google/protobuf.git
52$ cd protobuf
Eric Andersona6f5fff2015-02-26 07:53:17 -080053$ git checkout v3.0.0-alpha-2
Eric Andersonfb28ad22015-01-29 15:00:58 -080054$ ./autogen.sh
55$ ./configure
56$ make
57$ make check
58$ sudo make install
Eric Andersonfb28ad22015-01-29 15:00:58 -080059```
60
61If you are comfortable with C++ compilation and autotools, you can specify a
Kun Zhangfd0aed22015-04-30 17:29:17 -070062``--prefix`` for Protobuf and use ``-I`` in ``CXXFLAGS``, ``-L`` in
63``LDFLAGS``, ``LD_LIBRARY_PATH``, and ``PATH`` to reference it. The
64environment variables will be used when building grpc-java.
Eric Andersonfb28ad22015-01-29 15:00:58 -080065
Kun Zhangfd0aed22015-04-30 17:29:17 -070066Protobuf installs to ``/usr/local`` by default.
67
68For Visual C++, please refer to the [Protobuf README](https://github.com/google/protobuf/blob/master/vsprojects/readme.txt)
69for how to compile Protobuf.
70
71#### Linux and MinGW
72If ``/usr/local/lib`` is not in your library search path, you can add it by running:
Louis Ryan65d64cd2015-02-18 14:51:42 -080073```
74$ sudo sh -c 'echo /usr/local/lib >> /etc/ld.so.conf'
75$ sudo ldconfig
76```
77
Kun Zhangfd0aed22015-04-30 17:29:17 -070078#### Mac
79Some versions of Mac OS X (e.g., 10.10) doesn't have ``/usr/local`` in the
Kun Zhang111f6dd2015-05-05 16:11:27 -070080default search paths for header files and libraries. It will fail the build of
81the codegen. To work around this, you will need to set environment variables:
Kun Zhangfd0aed22015-04-30 17:29:17 -070082```
83$ export CXXFLAGS="-I/usr/local/include" LDFLAGS="-L/usr/local/lib"
84```
85
Kun Zhang221c5342015-04-29 18:16:15 -070086### Notes for Visual C++
87
Eric Anderson456216b2015-03-02 16:58:27 -080088When building on Windows and VC++, you need to specify project properties for
89Gradle to find protobuf:
90```
Kun Zhang221c5342015-04-29 18:16:15 -070091.\gradlew install ^
Kun Zhang2f749712015-05-06 13:10:28 -070092 -PvcProtobufInclude=C:\path\to\protobuf-3.0.0-alpha-2\src ^
93 -PvcProtobufLibs=C:\path\to\protobuf-3.0.0-alpha-2\vsprojects\Release
Eric Anderson456216b2015-03-02 16:58:27 -080094```
95
96Since specifying those properties every build is bothersome, you can instead
Kun Zhang2f749712015-05-06 13:10:28 -070097create ``<project-root>\gradle.properties`` with contents like:
Eric Anderson456216b2015-03-02 16:58:27 -080098```
Kun Zhang2f749712015-05-06 13:10:28 -070099vcProtobufInclude=C:\\path\\to\\protobuf-3.0.0-alpha-2\\src
100vcProtobufLibs=C:\\path\\to\\protobuf-3.0.0-alpha-2\\vsprojects\\Release
Eric Anderson456216b2015-03-02 16:58:27 -0800101```
102
Kun Zhang221c5342015-04-29 18:16:15 -0700103The build script will build the codegen for the same architecture as the Java
104runtime installed on your system. If you are using 64-bit JVM, the codegen will
105be compiled for 64-bit, that means you must have compiled Protobuf in 64-bit.
106
107### Notes for MinGW on Windows
108If you have both MinGW and VC++ installed on Windows, VC++ will be used by
Kun Zhang2f749712015-05-06 13:10:28 -0700109default. To override this default and use MinGW, add ``-PvcDisable=true``
110to your Gradle command line or add ``vcDisable=true`` to your
111``<project-root>\gradle.properties``.
Kun Zhang221c5342015-04-29 18:16:15 -0700112
Kun Zhang9805e272015-05-12 17:03:19 -0700113### Notes for unsupported operating systems
114The build script pulls pre-compiled ``protoc`` from Maven Central by default.
115We have built ``protoc`` binaries for popular systems, but they may not work
116for your system. If ``protoc`` cannot be downloaded or would not run, you can
117use the one that has been built by your own, by adding this property to
118``<project-root>/gradle.properties``:
119```
120protoc=/path/to/protoc
121```
122
Eric Andersonfb28ad22015-01-29 15:00:58 -0800123Navigating Around the Source
124----------------------------
125
ejonad4531da2014-12-15 10:24:42 -0800126Heres 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 -0700127to the library: stub, channel & transport.
ejonad4531da2014-12-15 10:24:42 -0800128
Eric Andersonfb28ad22015-01-29 15:00:58 -0800129### Stub
ejonad4531da2014-12-15 10:24:42 -0800130
131The 'stub' layer is what is exposed to most developers and provides type-safe bindings to whatever
132datamodel/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.
133
134#### Key Interfaces
135
nmittlerf8314582015-01-27 10:25:39 -0800136[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 -0800137
138
Eric Andersonfb28ad22015-01-29 15:00:58 -0800139### Channel
ejonad4531da2014-12-15 10:24:42 -0800140
141The '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.
142
143#### Common
144
nmittlerf8314582015-01-27 10:25:39 -0800145* [Metadata - headers & trailers](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/Metadata.java)
146* [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 -0800147
148#### Client
nmittlerf8314582015-01-27 10:25:39 -0800149* [Channel - client side binding](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/Channel.java)
150* [Call](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/Call.java)
151* [Client Interceptor](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/ClientInterceptor.java)
ejonad4531da2014-12-15 10:24:42 -0800152
153#### Server
nmittlerf8314582015-01-27 10:25:39 -0800154* [Server call handler - analog to Channel on server](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/ServerCallHandler.java)
155* [Server Call](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/ServerCall.java)
ejonad4531da2014-12-15 10:24:42 -0800156
157
Eric Andersonfb28ad22015-01-29 15:00:58 -0800158### Transport
ejonad4531da2014-12-15 10:24:42 -0800159
160The '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.
161
162#### Common
163
nmittlerf8314582015-01-27 10:25:39 -0800164* [Stream](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/transport/Stream.java)
165* [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 -0800166
167#### Client
168
nmittlerf8314582015-01-27 10:25:39 -0800169* [Client Stream](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/transport/ClientStream.java)
170* [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 -0800171
172#### Server
173
nmittlerf8314582015-01-27 10:25:39 -0800174* [Server Stream](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/transport/ServerStream.java)
175* [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 -0800176
177
Eric Andersonfb28ad22015-01-29 15:00:58 -0800178### Examples
ejonad4531da2014-12-15 10:24:42 -0800179
nmittlerf8314582015-01-27 10:25:39 -0800180Tests showing how these layers are composed to execute calls using protobuf messages can be found here https://github.com/google/grpc-java/tree/master/integration-testing/src/main/java/io/grpc/testing/integration