Java 用 gRPC サーバー スケルトン

スケルトンの gRPC サーバーをダウンロードして、完全な gRPC サーバーの実装を開始できます。

使ってみる

  1. Java Gradle プロジェクト(grpc-booking-service)を作成し、src/main の下に「proto」ディレクトリを作成します。

  2. 予約サービスの定義ヘルスチェック プロトコルをダウンロードし、src/main/proto に配置します。これらのファイルは、Actions Center API とヘルスチェックの gRPC メソッドとメッセージを定義します。

  3. 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
    
  4. 次のコマンドを実行してライブラリをビルドし、protoc ビルド プラグインからコードを自動生成します。

      ./gradlew build
    
  5. サーバーで TLS を有効にするには、src/main/certificates/ に次のファイルが必要です。

    • server_cert_chain.pem PEM 形式のサーバー証明書チェーン
    • server_private_key.pem サーバー証明書チェーンの秘密鍵。PKCS#8 秘密鍵である必要があります。
    • trusted_client_roots.pem クライアントの認証時に信頼されるルート証明書。この信頼できるルートのセットは、Mozilla などの認証局から取得することも、Google Internet Authority G2 が現在推奨しているルートセットをインストールすることもできます。後者の場合、ルート証明書を手動で更新しなければならないことがあります。
  6. このリポジトリからサンプルコードを取得します。

      git clone https://maps-booking.googlesource.com/java-maps-booking-grpc-server-skeleton
    

    BookingService.javasrc/main/java/ext/maps/booking/partner/v2 に配置し、Health.javasrc/main/java/grpc/health/v1 に配置します。どちらのファイルでも、TODO に沿って実装を完了します。

  7. 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
    }
    
  8. サーバーをコンパイルします。

    ./gradlew installDist
    
  9. 予約サーバーを実行します。

    ./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

その他のリファレンス