您可以下載 Skeleton gRPC 伺服器,協助您開始實作完整的 gRPC 伺服器。
立即開始
在 src/main 下方建立 Java Gradle 專案 (grpc-booking-service),建立「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 建構外掛程式自動產生程式碼:
./gradlew build
如要在伺服器上啟用 TLS,請在 src/main/certificates/ 下方提供下列檔案:
- server_cert_chain.pem:您的伺服器憑證鏈結 (PEM 格式)
- 伺服器憑證鏈的 server_private_key.pem 私密金鑰必須是 PKCS#8 私密金鑰
- trusted_client_roots.pem 是驗證用戶端時信任的根憑證,您可以選擇透過 Mozilla 等授權取得這組信任的根憑證,或是安裝 Google 網際網路主管機關 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
其他參考資料
如需其他建構工具,請造訪 gRPC-java 並下載範例,查看 grpc-java/examples。
git clone -b v1.9.0 https://github.com/grpc/grpc-java