gRPC-Server-Skelett für Java
Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
Sie können unseren gRPC-Server-Prototyp herunterladen, um mit der Implementierung Ihres vollständigen gRPC-Servers zu beginnen.
Jetzt starten
Erstellen Sie ein Java-Gradle-Projekt (grpc-booking-service) und unter „src/main“ ein Verzeichnis „proto“.
Laden Sie die Definition des Buchungsdienstes und das Protokoll für die Systemdiagnose herunter und platzieren Sie sie unter „src/main/proto“. In diesen Dateien werden die gRPC-Methoden und ‑Nachrichten für die Actions Center API und die Systemdiagnose definiert.
Aktualisieren Sie die Datei build.gradle und fügen Sie Abhängigkeiten und das Protobuf-Plug-in für Gradle hinzu. Eine Einführung und Anleitung für das protobuf-gradle-plugin finden Sie hier.
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
Führen Sie den folgenden Befehl aus, um die Bibliothek zu erstellen und Code automatisch aus dem protoc-Build-Plug-in zu generieren:
./gradlew build
Zum Aktivieren von TLS auf dem Server sind unter src/main/certificates/ die folgenden Dateien erforderlich:
- server_cert_chain.pem: Ihre Serverzertifikatskette im PEM-Format
- server_private_key.pem: Ihr privater Schlüssel für die Serverzertifikatskette. Muss der PKCS#8-private Schlüssel sein.
- trusted_client_roots.pem: die Root-Zertifikate, die bei der Authentifizierung von Clients als vertrauenswürdig eingestuft werden. Sie können diese vertrauenswürdigen Root-Zertifikate von einer Zertifizierungsstelle wie Mozilla beziehen oder die von der Google Internet Authority G2 derzeit empfohlenen Root-Zertifikate installieren. In letzterem Fall müssen Sie das Root-Zertifikat möglicherweise manuell aktualisieren.
Rufen Sie den Beispielcode aus diesem Repository ab:
git clone https://maps-booking.googlesource.com/java-maps-booking-grpc-server-skeleton
Platzieren Sie BookingService.java unter src/main/java/ext/maps/booking/partner/v2 und Health.java unter src/main/java/grpc/health/v1. Folgen Sie in beiden Dateien den TODOs, um Ihre Implementierungen abzuschließen.
Aktualisieren Sie die Datei „gradle.build“, um die Generierung der Server-Ausführbare anzugeben. Fügen Sie dazu den folgenden Code hinzu:
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
}
Kompilieren Sie den Server:
./gradlew installDist
Buchungsserver ausführen:
./build/install/grpc-booking-service/bin/booking-service
Endgültige Verzeichnisstruktur
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
Weitere Referenz
Sofern nicht anders angegeben, sind die Inhalte dieser Seite unter der Creative Commons Attribution 4.0 License und Codebeispiele unter der Apache 2.0 License lizenziert. Weitere Informationen finden Sie in den Websiterichtlinien von Google Developers. Java ist eine eingetragene Marke von Oracle und/oder seinen Partnern.
Zuletzt aktualisiert: 2025-07-26 (UTC).
[null,null,["Zuletzt aktualisiert: 2025-07-26 (UTC)."],[[["\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)."]]