Upgrade to Pro — share decks privately, control downloads, hide ads and more …

gRPC on Android @DroidconSF Report

TakuSemba
November 20, 2017

gRPC on Android @DroidconSF Report

TakuSemba

November 20, 2017
Tweet

More Decks by TakuSemba

Other Decks in Technology

Transcript

  1. message CoffeeResponse { int32 price = 1; string name =

    2; string message = 3; } Response //coffee.proto
  2. Service service Coffee { rpc Order (CoffeeRequest) returns (CoffeeResponse) {}

    } service Coffee { rpc Order (stream CoffeeRequest) returns (CoffeeResponse) {} } service Coffee { rpc Order (CoffeeRequest) returns (stream CoffeeResponse) {} } service Coffee { rpc Order (stream CoffeeRequest) returns (stream CoffeeResponse) {} }
  3. syntax = "proto3"; option java_package = "com.takusemba.grpc.android.protos"; option go_package =

    "protos"; package Coffee; message CoffeeRequest { string name = 1; } message CoffeeResult { string message = 1; } service Coffee { rpc Order (CoffeeRequest) returns (CoffeeResult) {} } //coffee.proto
  4. ->% curl -OL https://github.com/google/protobuf/releases/download/v3.5.0/ protobuf-java-3.5.0.tar.gz ->% tar -zxvf protobuf-java-3.5.0.tar.gz ->%

    cd protobuf-3.5.0 ->% ./configure ->% make ->% sudo make install ->% protoc —version // -> libprotoc 3.5.0 :) //protoc
  5. ->% git clone [email protected]:grpc/grpc-java.git ->% cd grpc-java ->% git checkout

    -b v1.7.0 ->% cd compiler ->% make ->% ../gradlew java_pluginExecutable ->% cp build/exe/java_plugin/protoc-gen-grpc-java /usr/local/bin //protoc-gen-grpc-java
  6. ->% protoc coffee.proto --grpc-java_out=lite:. --java_out=. --plugin=protoc-gen- grpc-java=/usr/local/bin/protoc-gen-grpc-java //generate proto files

    for client ->% protoc --go_out=plugins=grpc:. coffee.proto //generate proto files for server Generate Proto Files
  7. gRPC on Android classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.2' implementation "io.grpc:grpc-okhttp:1.7.0" implementation "io.grpc:grpc-protobuf:1.7.0" implementation

    "io.grpc:grpc-stub:1.7.0" implementation "javax.annotation:javax.annotation-api:1.2" //build.gradle //app/build.gradle
  8. gRPC on Android val channel = ManagedChannelBuilder.forAddress("10.0.2.2", 8080) .usePlaintext(true) .build()

    val stub = CoffeeGrpc.newBlockingStub(channel) val request = CoffeeOuterClass.CoffeeRequest.newBuilder() .setName(“hot coffee") .build() Single .create<CoffeeOuterClass.CoffeeResponse> { it.onSuccess(stub.order(request)) }.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe( { /** do something */ }, { /** error handling */ } )
  9. gRPC on Android val channel = ManagedChannelBuilder.forAddress("10.0.2.2", 8080) .usePlaintext(true) .build()

    val stub = CoffeeGrpc.newBlockingStub(channel) val request = CoffeeOuterClass.CoffeeRequest.newBuilder() .setName(“hot coffee") .build() Single .create<CoffeeOuterClass.CoffeeResponse> { it.onSuccess(stub.order(request)) }.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe( { /** do something */ }, { /** error handling */ } )
  10. gRPC on Android val channel = ManagedChannelBuilder.forAddress("10.0.2.2", 8080) .usePlaintext(true) .build()

    val stub = CoffeeGrpc.newBlockingStub(channel) val request = CoffeeOuterClass.CoffeeRequest.newBuilder() .setName(“hot coffee") .build() Single .create<CoffeeOuterClass.CoffeeResponse> { it.onSuccess(stub.order(request)) }.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe( { /** do something */ }, { /** error handling */ } )
  11. gRPC on Android val channel = ManagedChannelBuilder.forAddress("10.0.2.2", 8080) .usePlaintext(true) .build()

    val stub = CoffeeGrpc.newBlockingStub(channel) val request = CoffeeOuterClass.CoffeeRequest.newBuilder() .setName(“hot coffee") .build() Single .create<CoffeeOuterClass.CoffeeResponse> { it.onSuccess(stub.order(request)) }.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe( { /** do something */ }, { /** error handling */ } )