Java 版 gRPC 服务器框架
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
您可以下载我们的框架 gRPC 服务器,以便开始实现完整的 gRPC 服务器。
开始使用
创建一个 Java Gradle 项目 (grpc-booking-service),在 src/main 下创建一个“proto”目录。
下载预订服务定义和健康检查协议,并将其放置在 src/main/proto 下。这些文件用于定义 Actions Center API 和健康检查的 gRPC 方法和消息。
更新 build.gradle 文件,为 Gradle 添加依赖项和 protobuf 插件。如需了解 protobuf-gradle-plugin 的简介和指南,请点击此处。
apply plugin: 'java'
apply plugin: 'com.google.protobuf'
repositories {
mavenCentral()
}
// updating the version in our release process.
def grpcVersion = '1.8.0' // CURRENT_GRPC_VERSION
def nettyTcNativeVersion = '2.0.7.Final'
dependencies {
compile "com.google.api.grpc:proto-google-common-protos:0.1.9"
compile "io.grpc:grpc-netty:${grpcVersion}"
compile "io.grpc:grpc-protobuf:${grpcVersion}"
compile "io.grpc:grpc-stub:${grpcVersion}"
compile "io.netty:netty-tcnative-boringssl-static:${nettyTcNativeVersion}"
compile "org.bouncycastle:bcmail-jdk15:1.46"
testCompile "io.grpc:grpc-testing:${grpcVersion}"
testCompile "junit:junit:4.12"
testCompile "org.mockito:mockito-core:1.9.5"
}
buildscript {
repositories {
mavenCentral()
}
dependencies {
// ASSUMES GRADLE 2.12 OR HIGHER. Use plugin version 0.7.5 with earlier
// gradle versions
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.1'
}
}
protobuf {
protoc {
artifact = 'com.google.protobuf:protoc:3.4.0'
}
plugins {
grpc {
artifact = "io.grpc:protoc-gen-grpc-java:${grpcVersion}"
}
}
generateProtoTasks {
all()*.plugins {
grpc {}
}
}
}
// Inform IDEs like IntelliJ IDEA, Eclipse or NetBeans about the generated code.
sourceSets {
main {
java {
srcDirs 'build/generated/source/proto/main/grpc'
srcDirs 'build/generated/source/proto/main/java'
}
}
}
// Generate IntelliJ IDEA's .idea & .iml project files
apply plugin: 'idea'
// Provide convenience executables for trying out the examples.
apply plugin: 'application'
startScripts.enabled = false
运行以下命令以构建库并从 protoc build 插件自动生成代码:
./gradlew build
如需在服务器上启用 TLS,您需要在 src/main/certificates/ 下创建以下文件:
- server_cert_chain.pem 采用 PEM 格式的服务器证书链
- server_private_key.pem 服务器证书链的私钥,需要是 PKCS#8 私钥
- trusted_client_roots.pem 在对客户端进行身份验证时受信任的根证书,您可以选择从 Mozilla 等授权机构获取这组受信任的根证书,也可以安装 Google Internet Authority G2 目前推荐的一组根证书。在后一种情况下,您有时可能需要手动更新根证书
从以下代码库检索示例代码:
git clone https://maps-booking.googlesource.com/java-maps-booking-grpc-server-skeleton
将 BookingService.java 放在 src/main/java/ext/maps/booking/partner/v2 下,将 Health.java 放在 src/main/java/grpc/health/v1 下。在这两个文件中,按照 TODO 完成实现。
更新 gradle.build 文件,通过添加以下代码来指定服务器可执行文件的生成版本:
task bookingService(type: CreateStartScripts) {
mainClassName = 'ext.maps.booking.partner.v2.BookingService'
applicationName = 'booking-service'
outputDir = new File(project.buildDir, 'tmp')
classpath = jar.outputs.files + project.configurations.runtime
}
applicationDistribution.into('bin') {
from(bookingService)
fileMode = 0755
}
编译服务器:
./gradlew installDist
运行预订服务器:
./build/install/grpc-booking-service/bin/booking-service
最终目录结构
src
|---main
|---certificates
|---server_cert_chain.pem
|---server_private_key.pem
|---trusted_client_roots.pem
|---java
|---ext.maps.booking.partner.v2.BookingService.java
|---grpc.health.v1.Health.java
|---proto
|---booking_service.proto
|---health.proto
|---test
其他参考
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-26。
[null,null,["最后更新时间 (UTC):2025-07-26。"],[[["\u003cp\u003eA skeleton gRPC server is available for download to aid in the implementation of a complete gRPC server.\u003c/p\u003e\n"],["\u003cp\u003eThe process involves setting up a Java Gradle project, including adding dependencies, the protobuf plugin, and defining necessary .proto files for booking service and health checks.\u003c/p\u003e\n"],["\u003cp\u003eTLS encryption can be enabled on the server using certificate chain, private key, and trusted client root files.\u003c/p\u003e\n"],["\u003cp\u003eSample code from the repository is required to implement specific service files while completing TODOs.\u003c/p\u003e\n"],["\u003cp\u003eThe build process involves using Gradle to compile, generate code, and install the distribution for running the booking server.\u003c/p\u003e\n"]]],["To begin, download the booking service and health check protocol files into the `src/main/proto` directory of your Java Gradle project. Update `build.gradle` with necessary dependencies and the protobuf plugin. Build the library with `./gradlew build`, generating code from the protocol buffers. For TLS, place server certificates and trusted client roots under `src/main/certificates`. Retrieve and modify the sample `BookingService.java` and `Health.java` files. Finally, configure the server executable in `gradle.build`, compile, and run it.\n"],null,["# gRPC Server Skeleton for Java\n\nYou can download our [skeleton gRPC server](https://maps-booking.googlesource.com/java-maps-booking-grpc-server-skeleton/)\nto help get started with implementing your complete gRPC server.\n\n### Get Started\n\n1. Create a java gradle project (grpc-booking-service), under the src/main,\n create a 'proto' directory.\n\n2. Download the [booking service\n definition](https://developers.google.com/actions-center/download/apitemplate.v2.booking_service.proto)\n and [health checking\n protocol](https://github.com/grpc/grpc/blob/master/src/proto/grpc/health/v1/health.proto),\n place them under src/main/proto. These files define the gRPC methods and\n messages for the Actions Center API and Health Check.\n\n3. Update the ***build.gradle*** file, add dependencies and the protobuf plugin\n for Gradle. The introduction and guide for protobuf-gradle-plugin can be\n found [here](https://github.com/google/protobuf-gradle-plugin).\n\n apply plugin: 'java'\n apply plugin: 'com.google.protobuf'\n\n repositories {\n mavenCentral()\n }\n\n // updating the version in our release process.\n def grpcVersion = '1.8.0' // CURRENT_GRPC_VERSION\n def nettyTcNativeVersion = '2.0.7.Final'\n\n dependencies {\n compile \"com.google.api.grpc:proto-google-common-protos:0.1.9\"\n compile \"io.grpc:grpc-netty:${grpcVersion}\"\n compile \"io.grpc:grpc-protobuf:${grpcVersion}\"\n compile \"io.grpc:grpc-stub:${grpcVersion}\"\n compile \"io.netty:netty-tcnative-boringssl-static:${nettyTcNativeVersion}\"\n compile \"org.bouncycastle:bcmail-jdk15:1.46\"\n\n testCompile \"io.grpc:grpc-testing:${grpcVersion}\"\n testCompile \"junit:junit:4.12\"\n testCompile \"org.mockito:mockito-core:1.9.5\"\n }\n\n buildscript {\n repositories {\n mavenCentral()\n }\n dependencies {\n // ASSUMES GRADLE 2.12 OR HIGHER. Use plugin version 0.7.5 with earlier\n // gradle versions\n classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.1'\n }\n }\n\n protobuf {\n protoc {\n artifact = 'com.google.protobuf:protoc:3.4.0'\n }\n plugins {\n grpc {\n artifact = \"io.grpc:protoc-gen-grpc-java:${grpcVersion}\"\n }\n }\n generateProtoTasks {\n all()*.plugins {\n grpc {}\n }\n }\n }\n\n // Inform IDEs like IntelliJ IDEA, Eclipse or NetBeans about the generated code.\n sourceSets {\n main {\n java {\n srcDirs 'build/generated/source/proto/main/grpc'\n srcDirs 'build/generated/source/proto/main/java'\n }\n }\n }\n\n // Generate IntelliJ IDEA's .idea & .iml project files\n apply plugin: 'idea'\n\n // Provide convenience executables for trying out the examples.\n apply plugin: 'application'\n\n startScripts.enabled = false\n\n4. Run the following command to build the library and auto-generate code from\n protoc build plugin:\n\n ./gradlew build\n\n5. To enable TLS on the server, under ***src/main/certificates/*** the\n following files are required:\n\n - ***server_cert_chain.pem*** your server certificate chain in PEM format\n - ***server_private_key.pem*** your private key for the server certificate chain, needs to be the PKCS#8 private key\n - ***trusted_client_roots.pem*** the root certificates that are trusted when authenticating clients, you can choose to obtain this set of trusted roots from an authority like [Mozilla](https://wiki.mozilla.org/CA:IncludedCAs), or install the [set\n of roots currently recommended by the Google Internet Authority\n G2](https://pki.goog/roots.pem). In the latter case, you may have to manually update the root certificate at times\n6. Retrieve the sample code from this repo:\n\n git clone https://maps-booking.googlesource.com/java-maps-booking-grpc-server-skeleton\n\n place the **BookingService.java** under\n *src/main/java/ext/maps/booking/partner/v2* , place **Health.java** under\n *src/main/java/grpc/health/v1* . In both files, follow the ***TODOs*** to\n complete your implementations.\n7. Update the gradle.build file to specify the generation of server executable\n by adding the following code:\n\n task bookingService(type: CreateStartScripts) {\n mainClassName = 'ext.maps.booking.partner.v2.BookingService'\n applicationName = 'booking-service'\n outputDir = new File(project.buildDir, 'tmp')\n classpath = jar.outputs.files + project.configurations.runtime\n }\n\n applicationDistribution.into('bin') {\n from(bookingService)\n fileMode = 0755\n }\n\n8. Compile the server:\n\n ./gradlew installDist\n\n9. Run the booking server:\n\n ./build/install/grpc-booking-service/bin/booking-service\n\n### Final Directory Structure\n\n src\n |---main\n |---certificates\n |---server_cert_chain.pem\n |---server_private_key.pem\n |---trusted_client_roots.pem\n |---java\n |---ext.maps.booking.partner.v2.BookingService.java\n |---grpc.health.v1.Health.java\n |---proto\n |---booking_service.proto\n |---health.proto\n |---test\n\n### Other Reference\n\n- For other building tools, visit the\n [gRPC-java](https://grpc.io/docs/quickstart/java.html) and download the\n example, check grpc-java/examples.\n\n git clone -b v1.9.0 https://github.com/grpc/grpc-java\n\n- [gRPC java Transport Security\n (TLS)](https://github.com/grpc/grpc-java/blob/master/SECURITY.md).\n\n- [gRPC API\n V2](https://developers.google.com/actions-center/reference/grpc-api-v2/slot-specification)."]]