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

F0bc859dd1f5725158d04e63289c6634?s=47 Dais-33
March 06, 2019

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

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

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

F0bc859dd1f5725158d04e63289c6634?s=128

Dais-33

March 06, 2019
Tweet

Transcript

  1.  dais-sasa | Yappli, inc. gRPC+ProtocolBuffers    #yappliculture

  2. About us #yappliculture

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

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

    • ., $&MotionLayout -( :9563   12 + #yappliculture
  5. None
  6.  #yappliculture

  7. 1.  2. gRPCProtocolBuffers  3. Android !" 4. 

     5.   6.  #yappliculture
  8.  #yappliculture

  9. gRPC+ProtocolBuffers #yappliculture

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

    •! #:-<=(9 B gRPC<= 3+F,C  #yappliculture
  11.     #yappliculture

  12. API"#)*  $&(,    %API'-+   !

       #yappliculture
  13.  %!$ %!    "#   

    #yappliculture
  14. gRPC ProtocolBuffers  #yappliculture

  15. gRPC #yappliculture

  16. •Google" RPC    •Protocol Buffers   !

     #yappliculture
  17. ProtocolBuffers #yappliculture

  18. •Google*2  0-(/)  %$"# •.proto"&4 #1+  •(JSON 3

    ),' !!. #yappliculture
  19. gRPC+ProtocolBuffers      #yappliculture

  20. .proto #yappliculture

  21. .proto  (Go)) Android (Java) iOS (swiftObjective-C) gRPC  $*'&

    • $*'&  • (%  !#Entity • (% " !    
  22. Android  #yappliculture

  23.  #yappliculture

  24. 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
  25. 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
  26. 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
  27. 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
  28. 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
  29. 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
  30. 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
  31. 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
  32. 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
  33.  build.gradle    •https://qiita.com/kobaken0029/items/8e2945 f6b4ed6a2fa5ae •https://github.com/google/protobuf-gradle- plugin #yappliculture

  34.  #yappliculture

  35. 1. app/src/main/proto.proto   2.    3. app/build/generated/source/proto

     #yappliculture
  36. 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
  37. 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) } } }
  38. 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
  39.  #yappliculture

  40. •$% #) 1. Channel!( 2. Channel" Stub!( 3. Request!( 4.

    Stub,Request' + *& . -  #   #yappliculture
  41. // 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
  42. // 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
  43. // 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
  44. // 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
  45.  #yappliculture

  46.    #yappliculture

  47. • • •      #yappliculture

  48.  skip #yappliculture

  49. •Channel Stub&$! # •Channel Stub &   •"%gRPC 

     skip #yappliculture
  50.   skip #yappliculture

  51. •#$ !"  & %   Google' ( 

       •    skip #yappliculture
  52.       #yappliculture

  53.         #yappliculture

  54.  #yappliculture

  55. API   View   View Class hgadlk;kjralghal;;eur() #yappliculture

  56. API $'View&  View+Class& hgadlk;kjralghal;;eur(!,*-#() ("%   ) 

    > Proto) #yappliculture
  57. API'"%(",1View0 View!&$7Class0hgadlk;kjralghal;;eur()869+3) (*. )   > Proto# &5 (2:4Java#

    &-)    …(/)869+3) #yappliculture
  58. API)$'*$.4 View3  View#(&:Class3hgadlk;kjralghal;;eur(+;9<-6) (,1  )   >

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

    Proto%!"(8 (5=7 Java%!"(0)   …(2+;9<-6) /> #yappliculture
  60.  #yappliculture

  61. Y     > protoPR #yappliculture

  62. Y0%'35.*    > proto$(PR6  (PR-) !#4 7

      > PR12+ &)",/ #yappliculture
  63. Y8#+.;>61!    > proto*"$/PR ?  (PR 5)

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

     > PR:<4&.1)!58 (:<4&.1)!6) OKC3PR:<F>E-(   ;? #yappliculture
  65. $&',27B? •.proto *. !)9 A> <- •API )(%+", 15= 3/8@:7

     •.proto * #;06  4 #yappliculture
  66.    #yappliculture

  67. •.proto  %   •API!Entity.proto '   •Android

    8.0"TLS&# $& %   #yappliculture
  68. .proto      #yappliculture

  69. %1, proto- .0&*"5  #3()24 $/' ↓ +!(  

    24  #yappliculture
  70. gRPC.proto$)2<86  '& !";5/# 2</ Ø%(((..proto$)37  41-%(#' ;5 Ø0>:*

    #'  proto$)+=, ((937   #yappliculture
  71. API Entity .proto    #yappliculture

  72. !*(  , +   %$ ↓  .-

    #/ ' "&) #yappliculture
  73.  User     #yappliculture

  74. Entity user.prof.address.prefectures = "" #yappliculture

  75. syntax = “proto3”; message User { Prof prof = 1;

    //  } message Prof { Address address = 1; //  } message Address { string prefectures = 1; //  } #yappliculture
  76. 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
  77. •gRPC " Entitysetter   •Entity !Builder Builder #$ 

    #yappliculture
  78. •%2/ '-:3 •48+1 object 8+* object 7 object  &!#60

     %2/ 1$  "( .9    5,) #yappliculture
  79. Android 8.0 TLS     #yappliculture

  80. )& usePlaintext() $'(%#  ↓   '"  Android

    8.0!  #yappliculture
  81. •Android 8.0.5&+"*$ (-ALPN3 •%9ALPN;/0  61<7)>  • 4)'! ALPN

    ,#:2  Android 8.0.5 ?<7) =8  #yappliculture
  82.  ALPN      •  !

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

  84. •/# 0gRPC *5  ". •$!)4'3 +(1,26 -1& gRPC+ProtocolBuffers *

     %  #yappliculture
  85. Fin.