blob: c2053975322b9727b6be9884cb720d32f87ece7f [file] [log] [blame] [view]
Kun Zhangd32bb472015-05-06 16:15:53 -07001gRPC-Java - An RPC library and framework
2========================================
3
4[![Build Status](https://travis-ci.org/grpc/grpc-java.svg?branch=master)](https://travis-ci.org/grpc/grpc-java)
ejonad4531da2014-12-15 10:24:42 -08005
Eric Andersonfb28ad22015-01-29 15:00:58 -08006How to Build
7------------
8
Kun Zhangfd0aed22015-04-30 17:29:17 -07009### Build Netty
Jakob Buchgraberc56cec72015-03-11 13:44:32 -070010grpc-java requires Netty 4.1, which is still in flux. The version we need can be
Eric Andersonb10aaf82015-02-19 13:14:00 -080011found in the lib/netty submodule, which requires Maven 3.2 or higher to build:
Eric Andersonfb28ad22015-01-29 15:00:58 -080012```
13$ git submodule update --init
14$ cd lib/netty
15$ mvn install -pl codec-http2 -am -DskipTests=true
16```
17
Eric Anderson66c55ee2015-05-07 15:07:13 -070018### Build gRPC
19grpc-java has a C++ code generation plugin for protoc. Since many Java
20developers don't have C compilers installed and don't need to modify the
21codegen, the build can skip it. To skip, create the file
22`<project-root>/gradle.properties` and add `skipCodegen=true`.
23
24Then, to build, run:
25```
26$ ./gradlew build
27```
28
29To install the artifacts to your Maven local repository for use in your own
30project, run:
31```
32$ ./gradlew install
33```
34
35How to Build Code Generation Plugin
36-----------------------------------
37This section is only necessary if you are making changes to the code
38generation. Most users only need to use `skipCodegen=true` as discussed above.
39
Kun Zhangfd0aed22015-04-30 17:29:17 -070040### Build Protobuf
Kun Zhang111f6dd2015-05-05 16:11:27 -070041The codegen plugin is C++ code and requires protobuf 3.0.0-alpha-2.
42
Kun Zhangfd0aed22015-04-30 17:29:17 -070043For Linux, Mac and MinGW:
Eric Andersonfb28ad22015-01-29 15:00:58 -080044```
45$ git clone https://github.com/google/protobuf.git
46$ cd protobuf
Eric Andersona6f5fff2015-02-26 07:53:17 -080047$ git checkout v3.0.0-alpha-2
Eric Andersonfb28ad22015-01-29 15:00:58 -080048$ ./autogen.sh
49$ ./configure
50$ make
51$ make check
52$ sudo make install
Eric Andersonfb28ad22015-01-29 15:00:58 -080053```
54
55If you are comfortable with C++ compilation and autotools, you can specify a
Kun Zhangfd0aed22015-04-30 17:29:17 -070056``--prefix`` for Protobuf and use ``-I`` in ``CXXFLAGS``, ``-L`` in
57``LDFLAGS``, ``LD_LIBRARY_PATH``, and ``PATH`` to reference it. The
58environment variables will be used when building grpc-java.
Eric Andersonfb28ad22015-01-29 15:00:58 -080059
Kun Zhangfd0aed22015-04-30 17:29:17 -070060Protobuf installs to ``/usr/local`` by default.
61
62For Visual C++, please refer to the [Protobuf README](https://github.com/google/protobuf/blob/master/vsprojects/readme.txt)
63for how to compile Protobuf.
64
65#### Linux and MinGW
66If ``/usr/local/lib`` is not in your library search path, you can add it by running:
Louis Ryan65d64cd2015-02-18 14:51:42 -080067```
68$ sudo sh -c 'echo /usr/local/lib >> /etc/ld.so.conf'
69$ sudo ldconfig
70```
71
Kun Zhangfd0aed22015-04-30 17:29:17 -070072#### Mac
73Some versions of Mac OS X (e.g., 10.10) doesn't have ``/usr/local`` in the
Kun Zhang111f6dd2015-05-05 16:11:27 -070074default search paths for header files and libraries. It will fail the build of
75the codegen. To work around this, you will need to set environment variables:
Kun Zhangfd0aed22015-04-30 17:29:17 -070076```
77$ export CXXFLAGS="-I/usr/local/include" LDFLAGS="-L/usr/local/lib"
78```
79
Kun Zhang221c5342015-04-29 18:16:15 -070080### Notes for Visual C++
81
Eric Anderson456216b2015-03-02 16:58:27 -080082When building on Windows and VC++, you need to specify project properties for
83Gradle to find protobuf:
84```
Kun Zhang221c5342015-04-29 18:16:15 -070085.\gradlew install ^
Kun Zhang2f749712015-05-06 13:10:28 -070086 -PvcProtobufInclude=C:\path\to\protobuf-3.0.0-alpha-2\src ^
87 -PvcProtobufLibs=C:\path\to\protobuf-3.0.0-alpha-2\vsprojects\Release
Eric Anderson456216b2015-03-02 16:58:27 -080088```
89
90Since specifying those properties every build is bothersome, you can instead
Kun Zhang2f749712015-05-06 13:10:28 -070091create ``<project-root>\gradle.properties`` with contents like:
Eric Anderson456216b2015-03-02 16:58:27 -080092```
Kun Zhang2f749712015-05-06 13:10:28 -070093vcProtobufInclude=C:\\path\\to\\protobuf-3.0.0-alpha-2\\src
94vcProtobufLibs=C:\\path\\to\\protobuf-3.0.0-alpha-2\\vsprojects\\Release
Eric Anderson456216b2015-03-02 16:58:27 -080095```
96
Kun Zhang221c5342015-04-29 18:16:15 -070097The build script will build the codegen for the same architecture as the Java
98runtime installed on your system. If you are using 64-bit JVM, the codegen will
99be compiled for 64-bit, that means you must have compiled Protobuf in 64-bit.
100
101### Notes for MinGW on Windows
102If you have both MinGW and VC++ installed on Windows, VC++ will be used by
Kun Zhang2f749712015-05-06 13:10:28 -0700103default. To override this default and use MinGW, add ``-PvcDisable=true``
104to your Gradle command line or add ``vcDisable=true`` to your
105``<project-root>\gradle.properties``.
Kun Zhang221c5342015-04-29 18:16:15 -0700106
Eric Andersonfb28ad22015-01-29 15:00:58 -0800107Navigating Around the Source
108----------------------------
109
ejonad4531da2014-12-15 10:24:42 -0800110Heres 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 -0700111to the library: stub, channel & transport.
ejonad4531da2014-12-15 10:24:42 -0800112
Eric Andersonfb28ad22015-01-29 15:00:58 -0800113### Stub
ejonad4531da2014-12-15 10:24:42 -0800114
115The 'stub' layer is what is exposed to most developers and provides type-safe bindings to whatever
116datamodel/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.
117
118#### Key Interfaces
119
nmittlerf8314582015-01-27 10:25:39 -0800120[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 -0800121
122
Eric Andersonfb28ad22015-01-29 15:00:58 -0800123### Channel
ejonad4531da2014-12-15 10:24:42 -0800124
125The '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.
126
127#### Common
128
nmittlerf8314582015-01-27 10:25:39 -0800129* [Metadata - headers & trailers](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/Metadata.java)
130* [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 -0800131
132#### Client
nmittlerf8314582015-01-27 10:25:39 -0800133* [Channel - client side binding](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/Channel.java)
134* [Call](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/Call.java)
135* [Client Interceptor](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/ClientInterceptor.java)
ejonad4531da2014-12-15 10:24:42 -0800136
137#### Server
nmittlerf8314582015-01-27 10:25:39 -0800138* [Server call handler - analog to Channel on server](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/ServerCallHandler.java)
139* [Server Call](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/ServerCall.java)
ejonad4531da2014-12-15 10:24:42 -0800140
141
Eric Andersonfb28ad22015-01-29 15:00:58 -0800142### Transport
ejonad4531da2014-12-15 10:24:42 -0800143
144The '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.
145
146#### Common
147
nmittlerf8314582015-01-27 10:25:39 -0800148* [Stream](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/transport/Stream.java)
149* [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 -0800150
151#### Client
152
nmittlerf8314582015-01-27 10:25:39 -0800153* [Client Stream](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/transport/ClientStream.java)
154* [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 -0800155
156#### Server
157
nmittlerf8314582015-01-27 10:25:39 -0800158* [Server Stream](https://github.com/google/grpc-java/blob/master/core/src/main/java/io/grpc/transport/ServerStream.java)
159* [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 -0800160
161
Eric Andersonfb28ad22015-01-29 15:00:58 -0800162### Examples
ejonad4531da2014-12-15 10:24:42 -0800163
nmittlerf8314582015-01-27 10:25:39 -0800164Tests 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