commit | 1b8cb243f6f4666abbc46be90cfd930f1c35f0fa | [log] [tgz] |
---|---|---|
author | Christopher Cawdrey <ccawdrey@google.com> | Mon Feb 26 18:58:04 2018 -0800 |
committer | Christopher Cawdrey <ccawdrey@google.com> | Mon Feb 26 18:58:04 2018 -0800 |
tree | fe0fe468ea8b24aa23a87f154c65dcc3a743e14f |
Initial commit of sample java grpc server
Create a java gradle project (grpc-booking-service), under the src/main, create a ‘proto’ directory.
Download the booking service definition and health checking protocol, place them under src/main/proto. These files define the gRPC methods and messages for the Reserve with Google API and Health Check.
Update the build.gradle file, add dependencies and the protobuf plugin for Gradle. The introduction and guide for protobuf-gradle-plugin can be found here.
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
Run the following command to build the library and auto-generate code from protoc build plugin:
./gradlew build
To enable TLS on the server, under src/main/certificates/ the following files are required:
Retrieve the sample code, place the BookingService.java under src/main/java/ext/maps/booking/partner/v2, place Health.java under src/main/java/grpc/health/v1. In both files, follow the TODOs to complete your implementations.
Update the gradle.build file to specify the generation of server executable by adding the following code:
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 }
Compile the server:
./gradlew installDist
Run the booking server:
./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
For other building tools, visit the gRPC-java and download the example, check grpc-java/examples.
git clone -b v1.9.0 https://github.com/grpc/grpc-java