1gRPC-Java - An RPC library and framework 2======================================== 3 4<table> 5 <tr> 6 <td><b>Homepage:</b></td> 7 <td><a href="https://grpc.io/">grpc.io</a></td> 8 </tr> 9 <tr> 10 <td><b>Mailing List:</b></td> 11 <td><a href="https://groups.google.com/forum/#!forum/grpc-io">[email protected]</a></td> 12 </tr> 13</table> 14 15[](https://gitter.im/grpc/grpc?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) 16[](https://travis-ci.org/grpc/grpc-java) 17[](https://coveralls.io/github/grpc/grpc-java?branch=master) 18[](https://codecov.io/gh/grpc/grpc-java) 19 20Supported Platforms 21------------------- 22 23gRPC-Java supports Java 8 and later. Android minSdkVersion 19 (KitKat) and 24later are supported with [Java 8 language desugaring][android-java-8]. 25 26TLS usage on Android typically requires Play Services Dynamic Security Provider. 27Please see the [Security Readme](SECURITY.md). 28 29Older Java versions are not directly supported, but a branch remains available 30for fixes and releases. See [gRFC P5 JDK Version Support 31Policy][P5-jdk-version-support]. 32 33Java version | gRPC Branch 34------------ | ----------- 357 | 1.41.x 36 37[android-java-8]: https://developer.android.com/studio/write/java8-support#supported_features 38[P5-jdk-version-support]: https://github.com/grpc/proposal/blob/master/P5-jdk-version-support.md#proposal 39 40Getting Started 41--------------- 42 43For a guided tour, take a look at the [quick start 44guide](https://grpc.io/docs/languages/java/quickstart) or the more explanatory [gRPC 45basics](https://grpc.io/docs/languages/java/basics). 46 47The [examples](https://github.com/grpc/grpc-java/tree/v1.56.0/examples) and the 48[Android example](https://github.com/grpc/grpc-java/tree/v1.56.0/examples/android) 49are standalone projects that showcase the usage of gRPC. 50 51Download 52-------- 53 54Download [the JARs][]. Or for Maven with non-Android, add to your `pom.xml`: 55```xml 56<dependency> 57 <groupId>io.grpc</groupId> 58 <artifactId>grpc-netty-shaded</artifactId> 59 <version>1.56.0</version> 60 <scope>runtime</scope> 61</dependency> 62<dependency> 63 <groupId>io.grpc</groupId> 64 <artifactId>grpc-protobuf</artifactId> 65 <version>1.56.0</version> 66</dependency> 67<dependency> 68 <groupId>io.grpc</groupId> 69 <artifactId>grpc-stub</artifactId> 70 <version>1.56.0</version> 71</dependency> 72<dependency> <!-- necessary for Java 9+ --> 73 <groupId>org.apache.tomcat</groupId> 74 <artifactId>annotations-api</artifactId> 75 <version>6.0.53</version> 76 <scope>provided</scope> 77</dependency> 78``` 79 80Or for Gradle with non-Android, add to your dependencies: 81```gradle 82runtimeOnly 'io.grpc:grpc-netty-shaded:1.56.0' 83implementation 'io.grpc:grpc-protobuf:1.56.0' 84implementation 'io.grpc:grpc-stub:1.56.0' 85compileOnly 'org.apache.tomcat:annotations-api:6.0.53' // necessary for Java 9+ 86``` 87 88For Android client, use `grpc-okhttp` instead of `grpc-netty-shaded` and 89`grpc-protobuf-lite` instead of `grpc-protobuf`: 90```gradle 91implementation 'io.grpc:grpc-okhttp:1.56.0' 92implementation 'io.grpc:grpc-protobuf-lite:1.56.0' 93implementation 'io.grpc:grpc-stub:1.56.0' 94compileOnly 'org.apache.tomcat:annotations-api:6.0.53' // necessary for Java 9+ 95``` 96 97For [Bazel](https://bazel.build), you can either 98[use Maven](https://github.com/bazelbuild/rules_jvm_external) 99(with the GAVs from above), or use `@io_grpc_grpc_java//api` et al (see below). 100 101[the JARs]: 102https://search.maven.org/search?q=g:io.grpc%20AND%20v:1.56.0 103 104Development snapshots are available in [Sonatypes's snapshot 105repository](https://oss.sonatype.org/content/repositories/snapshots/). 106 107Generated Code 108-------------- 109 110For protobuf-based codegen, you can put your proto files in the `src/main/proto` 111and `src/test/proto` directories along with an appropriate plugin. 112 113For protobuf-based codegen integrated with the Maven build system, you can use 114[protobuf-maven-plugin][] (Eclipse and NetBeans users should also look at 115`os-maven-plugin`'s 116[IDE documentation](https://github.com/trustin/os-maven-plugin#issues-with-eclipse-m2e-or-other-ides)): 117```xml 118<build> 119 <extensions> 120 <extension> 121 <groupId>kr.motd.maven</groupId> 122 <artifactId>os-maven-plugin</artifactId> 123 <version>1.7.1</version> 124 </extension> 125 </extensions> 126 <plugins> 127 <plugin> 128 <groupId>org.xolstice.maven.plugins</groupId> 129 <artifactId>protobuf-maven-plugin</artifactId> 130 <version>0.6.1</version> 131 <configuration> 132 <protocArtifact>com.google.protobuf:protoc:3.22.3:exe:${os.detected.classifier}</protocArtifact> 133 <pluginId>grpc-java</pluginId> 134 <pluginArtifact>io.grpc:protoc-gen-grpc-java:1.56.0:exe:${os.detected.classifier}</pluginArtifact> 135 </configuration> 136 <executions> 137 <execution> 138 <goals> 139 <goal>compile</goal> 140 <goal>compile-custom</goal> 141 </goals> 142 </execution> 143 </executions> 144 </plugin> 145 </plugins> 146</build> 147``` 148 149[protobuf-maven-plugin]: https://www.xolstice.org/protobuf-maven-plugin/ 150 151For non-Android protobuf-based codegen integrated with the Gradle build system, 152you can use [protobuf-gradle-plugin][]: 153```gradle 154plugins { 155 id 'com.google.protobuf' version '0.9.1' 156} 157 158protobuf { 159 protoc { 160 artifact = "com.google.protobuf:protoc:3.22.3" 161 } 162 plugins { 163 grpc { 164 artifact = 'io.grpc:protoc-gen-grpc-java:1.56.0' 165 } 166 } 167 generateProtoTasks { 168 all()*.plugins { 169 grpc {} 170 } 171 } 172} 173``` 174 175[protobuf-gradle-plugin]: https://github.com/google/protobuf-gradle-plugin 176 177The prebuilt protoc-gen-grpc-java binary uses glibc on Linux. If you are 178compiling on Alpine Linux, you may want to use the [Alpine grpc-java package][] 179which uses musl instead. 180 181[Alpine grpc-java package]: https://pkgs.alpinelinux.org/package/edge/community/x86_64/grpc-java 182 183For Android protobuf-based codegen integrated with the Gradle build system, also 184use protobuf-gradle-plugin but specify the 'lite' options: 185 186```gradle 187plugins { 188 id 'com.google.protobuf' version '0.9.1' 189} 190 191protobuf { 192 protoc { 193 artifact = "com.google.protobuf:protoc:3.22.3" 194 } 195 plugins { 196 grpc { 197 artifact = 'io.grpc:protoc-gen-grpc-java:1.56.0' 198 } 199 } 200 generateProtoTasks { 201 all().each { task -> 202 task.builtins { 203 java { option 'lite' } 204 } 205 task.plugins { 206 grpc { option 'lite' } 207 } 208 } 209 } 210} 211 212``` 213 214For [Bazel](https://bazel.build), use the [`proto_library`](https://github.com/bazelbuild/rules_proto) 215and the [`java_proto_library`](https://bazel.build/reference/be/java#java_proto_library) (no `load()` required) 216and `load("@io_grpc_grpc_java//:java_grpc_library.bzl", "java_grpc_library")` (from this project), as in 217[this example `BUILD.bazel`](https://github.com/grpc/grpc-java/blob/master/examples/BUILD.bazel). 218 219API Stability 220------------- 221 222APIs annotated with `@Internal` are for internal use by the gRPC library and 223should not be used by gRPC users. APIs annotated with `@ExperimentalApi` are 224subject to change in future releases, and library code that other projects 225may depend on should not use these APIs. 226 227We recommend using the 228[grpc-java-api-checker](https://github.com/grpc/grpc-java-api-checker) 229(an [Error Prone](https://github.com/google/error-prone) plugin) 230to check for usages of `@ExperimentalApi` and `@Internal` in any library code 231that depends on gRPC. It may also be used to check for `@Internal` usage or 232unintended `@ExperimentalApi` consumption in non-library code. 233 234How to Build 235------------ 236 237If you are making changes to gRPC-Java, see the [compiling 238instructions](COMPILING.md). 239 240High-level Components 241--------------------- 242 243At a high level there are three distinct layers to the library: *Stub*, 244*Channel*, and *Transport*. 245 246### Stub 247 248The Stub layer is what is exposed to most developers and provides type-safe 249bindings to whatever datamodel/IDL/interface you are adapting. gRPC comes with 250a [plugin](https://github.com/google/grpc-java/blob/master/compiler) to the 251protocol-buffers compiler that generates Stub interfaces out of `.proto` files, 252but bindings to other datamodel/IDL are easy and encouraged. 253 254### Channel 255 256The Channel layer is an abstraction over Transport handling that is suitable for 257interception/decoration and exposes more behavior to the application than the 258Stub layer. It is intended to be easy for application frameworks to use this 259layer to address cross-cutting concerns such as logging, monitoring, auth, etc. 260 261### Transport 262 263The Transport layer does the heavy lifting of putting and taking bytes off the 264wire. The interfaces to it are abstract just enough to allow plugging in of 265different implementations. Note the transport layer API is considered internal 266to gRPC and has weaker API guarantees than the core API under package `io.grpc`. 267 268gRPC comes with multiple Transport implementations: 269 2701. The Netty-based HTTP/2 transport is the main transport implementation based 271 on [Netty](https://netty.io). It is not officially supported on Android. 2722. The OkHttp-based HTTP/2 transport is a lightweight transport based on 273 [Okio](https://square.github.io/okio/) and forked low-level parts of 274 [OkHttp](https://square.github.io/okhttp/). It is mainly for use on Android. 2753. The in-process transport is for when a server is in the same process as the 276 client. It is used frequently for testing, while also being safe for 277 production use. 2784. The Binder transport is for Android cross-process communication on a single 279 device. 280