اسکلت سرور gRPC برای جاوا
با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
میتوانید سرور gRPC اسکلت ما را دانلود کنید تا به اجرای کامل سرور gRPC خود کمک کنید.
شروع کنید
یک پروژه gradle جاوا (grpc-booking-service) ایجاد کنید، در زیر src/main، یک دایرکتوری 'proto' ایجاد کنید.
تعریف خدمات رزرو و پروتکل بررسی سلامت را دانلود کنید، آنها را در زیر src/main/proto قرار دهید. این فایلها روشها و پیامهای gRPC را برای API Center و Health Check تعریف میکنند.
فایل build.gradle را به روز کنید، وابستگی ها و افزونه protobuf را برای Gradle اضافه کنید. مقدمه و راهنمای افزونه protobuf-gradle را میتوانید در اینجا بیابید.
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
دستور زیر را برای ساخت کتابخانه و تولید خودکار کد از افزونه پروتک بیلد اجرا کنید:
./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
مرجع دیگر
جز در مواردی که غیر از این ذکر شده باشد،محتوای این صفحه تحت مجوز Creative Commons Attribution 4.0 License است. نمونه کدها نیز دارای مجوز Apache 2.0 License است. برای اطلاع از جزئیات، به خطمشیهای سایت Google Developers مراجعه کنید. جاوا علامت تجاری ثبتشده Oracle و/یا شرکتهای وابسته به آن است.
تاریخ آخرین بهروزرسانی 2025-07-24 بهوقت ساعت هماهنگ جهانی.
[null,null,["تاریخ آخرین بهروزرسانی 2025-07-24 بهوقت ساعت هماهنگ جهانی."],[[["\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)."]]