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

gRPC+ProtocolBuffersでの通信処理を実装して感じたこと / Things I...

Dais-33
March 06, 2019

gRPC+ProtocolBuffersでの通信処理を実装して感じたこと / Things I felt using grpc

実際の開発でgRPC+ProtocloBuffersを導入したので、導入方法の簡単な説明と実際に導入してどんな事を感じたか、どんな事が起きたかをまとめました。

発表時はSkipした実装の詳細な説明が載っているので、実装の際に参考にしていただければと思います。

Dais-33

March 06, 2019
Tweet

More Decks by Dais-33

Other Decks in Technology

Transcript

  1. dais-sasa • #%!"  ' • Android  • 

    $&( • MotionLayout #yappliculture
  2. dais-sasa • 48*/'0 # %; • Android& • &"!& )7

    • ., $&MotionLayout -( :9563   12 + #yappliculture
  3. 1.  2. gRPCProtocolBuffers  3. Android !" 4. 

     5.   6.  #yappliculture
  4. •4*)>?$7  ;%  •JSON+10E& "/A5. @6  D8'2 

    •! #:-<=(9 B gRPC<= 3+F,C  #yappliculture
  5. .proto  (Go)) Android (Java) iOS (swiftObjective-C) gRPC  $*'&

    • $*'&  • (%  !#Entity • (% " !    
  6. build.gradle() buildscript { repositories { jcenter() mavenCentral() } dependencies {

    classpath 'com.android.tools.build:gradle:3.2.1' classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.0' } } #yappliculture
  7. build.gradle(app) apply plugin: 'com.google.protobuf' dependencies { ~  ~ def

    grpc_version = "1.17.1" api "io.grpc:grpc-okhttp:$grpc_version" api "io.grpc:grpc-protobuf-lite:$grpc_version" api "io.grpc:grpc-stub:$grpc_version" api 'javax.annotation:javax.annotation-api:1.3.2' } #yappliculture
  8. protobuf { protoc { artifact = "com.google.protobuf:protoc:3.1.0" } plugins {

    javalite { artifact = "com.google.protobuf:protoc-gen-javalite:3.0.0" } grpc { artifact = "io.grpc:protoc-gen-grpc-java:1.17.1" } } generateProtoTasks { all().each { task -> task.plugins { javalite {} grpc { option 'lite' } } } } } #yappliculture
  9. protobuf { protoc { artifact = "com.google.protobuf:protoc:3.1.0" } plugins {

    javalite { artifact = "com.google.protobuf:protoc-gen-javalite:3.0.0" } grpc { artifact = "io.grpc:protoc-gen-grpc-java:1.17.1" } } generateProtoTasks { all().each { task -> task.plugins { javalite {} grpc { option 'lite' } } } } }   block block.proto      skip #yappliculture
  10. protobuf { protoc { artifact = "com.google.protobuf:protoc:3.1.0" } plugins {

    javalite { artifact = "com.google.protobuf:protoc-gen-javalite:3.0.0" } grpc { artifact = "io.grpc:protoc-gen-grpc-java:1.17.1" } } generateProtoTasks { all().each { task -> task.plugins { javalite {} grpc { option 'lite' } } } } }        path = '/usr/local/bin/protoc’ skip #yappliculture
  11. protobuf { protoc { artifact = "com.google.protobuf:protoc:3.1.0" } plugins {

    javalite { artifact = "com.google.protobuf:protoc-gen-javalite:3.0.0" } grpc { artifact = "io.grpc:protoc-gen-grpc-java:1.17.1" } } generateProtoTasks { all().each { task -> task.plugins { javalite {} grpc { option 'lite' } } } } }  #+&' gRPC   ! $"&'*,  Java ( plugin  )% skip #yappliculture
  12. protobuf { protoc { artifact = "com.google.protobuf:protoc:3.1.0" } plugins {

    javalite { artifact = "com.google.protobuf:protoc-gen-javalite:3.0.0" } grpc { artifact = "io.grpc:protoc-gen-grpc-java:1.17.1" } } generateProtoTasks { all().each { task -> task.plugins { javalite {} grpc { option 'lite' } } } } } grpc block    (grpc-java) skip #yappliculture
  13. protobuf { protoc { artifact = "com.google.protobuf:protoc:3.1.0" } plugins {

    javalite { artifact = "com.google.protobuf:protoc-gen-javalite:3.0.0" } grpc { artifact = "io.grpc:protoc-gen-grpc-java:1.17.1" } } generateProtoTasks { all().each { task -> task.plugins { javalite {} grpc { option 'lite' } } } } } grpc-javaAndroid   javalite blockAndroid   skip #yappliculture
  14. protobuf { protoc { artifact = "com.google.protobuf:protoc:3.1.0" } plugins {

    javalite { artifact = "com.google.protobuf:protoc-gen-javalite:3.0.0" } grpc { artifact = "io.grpc:protoc-gen-grpc-java:1.17.1" } } generateProtoTasks { all().each { task -> task.plugins { javalite {} grpc { option 'lite' } } } } }    all(): protoc .plugins:    option:    skip #yappliculture
  15. syntax = "proto3"; message CreateRequest { enum Sex { INVALID

    = 0; //  MALE = 1; //  FEMALE = 2; //  } int32 age = 1; //  string name = 2; //  Sex sex = 3; //  } message CreateResponse { User user = 1; // User  } /*  */ service UserService { //   rpc Create(CreateRequest) returns (CreateResponse); /*  */ }  user.proto
  16. public final class UserOuterClass { public static final class CreateRequest

    … { public enum Sex … { INVALID, MALE, FEMALE } private int age_; public int getAge() private String name_; public String getName() private Sex sex_; public Sex getSex() public Builder newBuilder() public Builder toBuilder() public static final class Builder … { public Builder setAge(int value) public Builder setName(String value) public Builder setSex(Sex value) } } }
  17. public final class UserServiceGrpc { public static UserServiceBlockingStub newBlockingStub(Channel channel)

    public static UserServiceFutureStub newFutureStub(Channel channel) public static final class UserServiceBlockingStub … { public CreateResponse create(CreateRequest request) } public static final class UserServiceFutureStub … { public CreateResponse create(CreateRequest request) } } #yappliculture
  18. •$% #) 1. Channel!( 2. Channel" Stub!( 3. Request!( 4.

    Stub,Request' + *& . -  #   #yappliculture
  19. // Channel val channel = ManagedChannelBuilder.forAddress("10.0.2.2", 443) .build() // UserServiceStub

    val stub = UserServiceGrpc.newBlockingStub(channel) // Request val createRequest = CreateRequest.newBuilder() .setAge(33) .setName("dais-sasa") .setSex(Sex.MALE) .build() //  val createResult = stub.create(createRequest) #yappliculture
  20. // Channel)0 val channel = ManagedChannelBuilder.forAddress("10.0.2.2", 443) .build() // UserService

    Stub)0 val stub = UserServiceGrpc.newBlockingStub(channel); // Request)0 val createRequest = CreateRequest.newBuilder() .setAge(33) .setName(”dais-sasa") .setSex(Sex.MALE) .build(); // +& val createResult = stub.create(createRequest) -#( %0 5  279$"8 ; !    usePlaintext(): http6/& userAgent(agent: String): userAgent9$ 143 *7forTarget(target: String)  "8 .'target !  #, dns:///: skip #yappliculture
  21. // Channel $ val channel = ManagedChannelBuilder.forAddress("10.0.2.2", 443) .build() //

    UserService Stub $ val stub = UserServiceGrpc.newBlockingStub(channel) // Request $ val createRequest = CreateRequest.newBuilder() .setAge(33) .setName(”dais-sasa") .setSex(Sex.MALE) .build(); // " val createResult = stub.create(createRequest); ServiceGrpc  &#!  .proto service block %$ newStub(channel: Cnannel) Stub $()%$) BlockingStub '&# FutureStub ('&# skip #yappliculture
  22. // Channel val channel = ManagedChannelBuilder.forAddress("10.0.2.2", 443) .build() // UserServiceStub

    val stub = UserServiceGrpc.newBlockingStub(channel); // Request val createRequest = CreateRequest.newBuilder() .setAge(33) .setName(”dais-sasa") .setSex(Sex.MALE) .build() //  val createResult = stub.create(createRequest) Entity  .proto  message block skip #yappliculture
  23. •#$ !"  & %   Google' ( 

       •    skip #yappliculture
  24. API)$'*$.4 View3  View#(&:Class3hgadlk;kjralghal;;eur(+;9<-6) (,1  )   >

    Proto%!"(8 (5=7 Java%!"(0)   …(2+;9<-6) /> #yappliculture
  25. API)$'*$.4 View3  View#(&:Class3hgadlk;kjralghal;;eur(+;9<-6) (,1  )   >

    Proto%!"(8 (5=7 Java%!"(0)   …(2+;9<-6) /> #yappliculture
  26. Y8#+.;>61!    > proto*"$/PR ?  (PR 5)

    !&)= A > PR9:3%-0( 47 (9:3%-0( 5) OK@2PR9:C<B,'  #yappliculture
  27. Y9$,/=A72"   > proto+#%0PR!B  (PR!6) "'*@ D 

     > PR:<4&.1)!58 (:<4&.1)!6) OKC3PR:<F>E-(   ;? #yappliculture
  28. !*(  , +   %$ ↓  .-

    #/ ' "&) #yappliculture
  29. syntax = “proto3”; message User { Prof prof = 1;

    //  } message Prof { Address address = 1; //  } message Address { string prefectures = 1; //  } #yappliculture
  30. gRPC Entity // Builder  val userBuilder = user.toBuilder() val

    profBuilder = user.prof.toBuilder() val addressBuilder = user.prof.address.toBuilder() //   addressBuilder.prefectures = "" profBuilder.address = addressBuilder.build() userBuilder.prof = profBuilder.build() // API   userBuilder.build() #yappliculture
  31. •%2/ '-:3 •48+1 object 8+* object 7 object  &!#60

     %2/ 1$  "( .9    5,) #yappliculture
  32.  ALPN      •  !

     • No ALPN negotiated $  ALPN     ALPN "# $ openssl s_client -alpn h2 -connect <domain>:<port> #yappliculture