Cấu trúc máy chủ gRPC cho Java
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Bạn có thể tải máy chủ gRPC khung của chúng tôi xuống để bắt đầu triển khai máy chủ gRPC hoàn chỉnh.
Bắt đầu
Tạo một dự án gradle java (grpc-booking-service), trong src/main, tạo một thư mục "proto".
Tải định nghĩa dịch vụ đặt phòng và giao thức kiểm tra tình trạng xuống, đặt các tệp này trong src/main/proto. Các tệp này xác định các phương thức và thông báo gRPC cho API Trung tâm hành động và tính năng Kiểm tra tình trạng.
Cập nhật tệp build.gradle, thêm các phần phụ thuộc và trình bổ trợ protobuf cho Gradle. Bạn có thể xem phần giới thiệu và hướng dẫn về trình bổ trợ protobuf-gradle tại đây.
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
Chạy lệnh sau để tạo thư viện và tự động tạo mã từ trình bổ trợ bản dựng protoc:
./gradlew build
Để bật TLS trên máy chủ, trong src/main/certificates/, bạn cần có các tệp sau:
- server_cert_chain.pem chuỗi chứng chỉ máy chủ ở định dạng PEM
- server_private_key.pem khoá riêng tư của bạn cho chuỗi chứng chỉ máy chủ, cần phải là khoá riêng tư PKCS#8
- trusted_client_roots.pem các chứng chỉ gốc đáng tin cậy khi xác thực ứng dụng, bạn có thể chọn lấy bộ gốc đáng tin cậy này từ một cơ quan như Mozilla hoặc cài đặt bộ gốc hiện được Cơ quan Internet của Google G2 đề xuất. Trong trường hợp sau, đôi khi bạn có thể phải cập nhật chứng chỉ gốc theo cách thủ công
Truy xuất mã mẫu từ kho lưu trữ này:
git clone https://maps-booking.googlesource.com/java-maps-booking-grpc-server-skeleton
đặt BookingService.java trong
src/main/java/ext/maps/booking/partner/v2, đặt Health.java trong
src/main/java/grpc/health/v1. Trong cả hai tệp, hãy làm theo TODO để hoàn tất quá trình triển khai.
Cập nhật tệp gradle.build để chỉ định quá trình tạo tệp thực thi máy chủ bằng cách thêm mã sau:
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
}
Biên dịch máy chủ:
./gradlew installDist
Chạy máy chủ đặt phòng:
./build/install/grpc-booking-service/bin/booking-service
Cấu trúc thư mục cuối cùng
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
Tài liệu tham khảo khác
Trừ phi có lưu ý khác, nội dung của trang này được cấp phép theo Giấy phép ghi nhận tác giả 4.0 của Creative Commons và các mẫu mã lập trình được cấp phép theo Giấy phép Apache 2.0. Để biết thông tin chi tiết, vui lòng tham khảo Chính sách trang web của Google Developers. Java là nhãn hiệu đã đăng ký của Oracle và/hoặc các đơn vị liên kết với Oracle.
Cập nhật lần gần đây nhất: 2025-07-26 UTC.
[null,null,["Cập nhật lần gần đây nhất: 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)."]]