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

初めてのgRPC mini / Beginning gRPC mini

初めてのgRPC mini / Beginning gRPC mini

2019/5/28に開催されたLINE Developer Meetup #54 in Fukuokaでの登壇資料です

53850955f15249a1a9dc49df6113e400?s=128

LINE Developers
PRO

May 28, 2019
Tweet

Transcript

  1. gRPC -mini- LINE Developer Meetup #54 in Fukuoka 2019/5/28 

    ,   LINE Developer Meetup #54 in Fukuoka 1
  2. • LINE       • (Maeda

    Kentaro) • Twitter: kencharos LINE Developer Meetup #54 in Fukuoka 2
  3. Agenda • gRPC  • Protocol Buffers  • gRPC

    Java  LINE Developer Meetup #54 in Fukuoka 3
  4.  " 80 . l !# /,6(  gRPC %9

     3: +5-; l gRPCProtocol BuffersgRPC Java $7 l ! 19 l ↑ * JJUC CCC 2019 Spring 24'<&) https://speakerdeck.com/line_developers/starting-grpc 4 LINE Developer Meetup #54 in Fukuoka
  5. Agenda • gRPC  • Protocol Buffers  • gRPC

    Java  LINE Developer Meetup #54 in Fukuoka 5
  6. 0 / I ) / / ) / / F

    P l oil l epC ) rc l 2 / R P N F nm l ( F 1 0 / C l 0 /t u g bj da F 6 0 ) CG https://grpc.io/ LINE Developer Meetup #54 in Fukuoka
  7. l e > p • - e I >c A

    B T • > tPT P c DH o B >gp I • D >c DL I BD C B C l u • + ( 2 s • L / )r l f l > RAL g I DH T 7 4/ ) LINE Developer Meetup #54 in Fukuoka
  8. l )) t n ( / 2 1 • ))

    RL )) T L l m a c i C • ) e U H HS H s C • PU C 8 )) LINE Developer Meetup #54 in Fukuoka
  9. ) PR o i hl i fi l ) I

    R nH SR l o rLR+( e g e l +( H tuwJ a yG ld i • ,1B1# ) # # # D# # # D # 0C ### l ptu i i fi • s c b LR v 9 LINE Developer Meetup #54 in Fukuoka
  10. l I L P C D l I R B

    10 proto file message ( ) service (PRC ) protoc Protocol Buffers gRPC plugin  Message  Service  Protocol Buffers  gRPC  LINE Developer Meetup #54 in Fukuoka
  11. l P T Sp A l ) // a T

    S AoC O A • O LEJ / O P b l U E P W O EJS U wA IU l / O A T S A C L / L P W • eg U t NPRH A W • L / ( + r n Wg eg S W 11 / + LO LINE Developer Meetup #54 in Fukuoka
  12. Agenda • gRPC  • Protocol Buffers  • gRPC

    Java  LINE Developer Meetup #54 in Fukuoka 12
  13. l gRPC   version 3  l  

    message, RPC  service  ! • packageoptionimport  "      ! 13 proto  syntax = "proto3"; package my.rpc; option java_package = "my.rpc"; message SomeMessage{///} service SomeService{///} LINE Developer Meetup #54 in Fukuoka
  14. l Java  1!'  message 0 8.5%*  l

    Java1! 3249*  14 message  message Person { int32 id = 1; string name = 2; Gender gender = 3; enum Gender { man = 0; woman = 1; } }   -/ int32, int64, uint32, uint64,double, , bool 6(: string : bytes :"$ enum #7 repeated $.  )+ map<key$, value$> message 1!&message  LINE Developer Meetup #54 in Fukuoka
  15. l Protocol Buffers ('.I7+/&H8?K = L9 • +/&,#*0 .-!/$ .-!

    36  • +/&H8:NM   l EA Person id150DG  , 08 96 01 (%P 15   0000 0100 212)#%6@O5@K <+/&H8 = 1 1001 0110 0000 0001 ;C1)#%$"QF>  21(%J 4B 000 0001 ++ 001 0110 -> 10010110 -> 128 + 16 + 4 + 2 -> 150 LINE Developer Meetup #54 in Fukuoka
  16. l Java  service & RPC  ' / ,$"message-%message

    ( l *"' / stream " -% ).   RPC 40 +0 l !# 16 service  service GreetingService { rpc Hello(Person) returns (Greeting) rpc HelloStream(stream Person) returns (stream Greeting) } LINE Developer Meetup #54 in Fukuoka
  17. Agenda • gRPC  • Protocol Buffers  • gRPC

    Java  LINE Developer Meetup #54 in Fukuoka 17
  18. 18 Protocol Buffers LINE Developer Meetup #54 in Fukuoka l

    Maven/Gradle Protocol Buffers   gRPC plugin $ • src/main/proto  proto " apply plugin: 'com.google.protobuf' apply plugin: "idea" protobuf { protoc { artifact = "com.google.protobuf:protoc: 3.6.1 " } plugins { grpc { // grpc plugin # artifact = "io.grpc:protoc-gen-grpc-java:1.18.1" } } generateProtoTasks { //plugin "  all()*.plugins { grpc {} // grpc plugin !$ } } ProtocolBuffrers   gRPC plugin#  gRPC plugin!$
  19. 19  LINE Developer Meetup #54 in Fukuoka l 

    build/generated   l Gradle     • https://github.com/google/prot obuf-gradle-plugin
  20. 20 RPC LINE Developer Meetup #54 in Fukuoka l RPC6#

    .;3 stream   RPC(= $ l  !RPC4:.87" 0),   • 5+  Unary& 1 • 87"*<9- '/:% RPC RPC  Unary RPC rpc Hello(In) returns (Out) 121 Server Streaming RPC rpc Hello(In) returns (stream Out) 121 Client Streaming RPC rpc Hello(stream In) returns (Out) 121 Bidirectional Streaming RPC rpc Hello(stream In) returns (stream Out) 121
  21. 21 gRPC Java   LINE Developer Meetup #54 in

    Fukuoka l 2StreamObserver  l  message  Stub gRPC Service RPC(Unary) Stream Observer Stream Observer message message onNext onCompleted onNext onCompleted message onNext
  22. 22 gRPC Java  LINE Developer Meetup #54 in Fukuoka

      Stub RPC#%proto!$/@87 gRPC Server Stub9;<   gRPC Sevice gRPC Server .&RPCProto!$/@ 87,'> Server ?F Channel #% gRPC Server 9; B5RPC+2 )C  message PRC3DA Stub-gRPC Service ( :04 Protocol Buffers  message StreamObserver #%    :04 *=1E*=6-"
  23. 23 :gRPC Server LINE Developer Meetup #54 in Fukuoka l

    ServerBuilder addService  GrpcService&+ ' •  !$ Netty TLS #% NettryServiceBuilder () • RPC"* Netty NIO ! public static void main(String[] args)throws Exception{ var builder = (NettyServerBuilder)ServerBuilder .forPort(6565) .addService(new FizBuzBasicGrpcService()); // builder.sslContext(...); var server = builder.build(); server.start(); server.awaitTermination(); }
  24. 24 :Channel LINE Developer Meetup #54 in Fukuoka l ManagedChannel

     *5 l + 3)49 RPC $1  ,' 0" /  * 8 l gRPC  TLS ,( 2%   TLS 6  & usePlaintext 7#. !- ManagedChannel createChannel() { return ManagedChannelBuilder.forAddress("localhost", 6565) .usePlaintext() .build(); }
  25. 25 :Stub LINE Developer Meetup #54 in Fukuoka l 1;65

    ServiceGrpc (33C $65 '!"   Channel : 65  l 0B-=RPC%*(&?@, • 942/+ BlockingStub  FutureStub0 var blockStub = FizBuzServiceGrpc.newBlockingStub(channel); var futureStub = FizBuzServiceGrpc.newFutureStub(channel); var stub = FizBuzServiceGrpc.newStub(channel); Stub RPC  BlockingStub unary, server side stream <.('!A8) FutureStub unary ><. (Future<T> A8) Stub 7 StreamObsever)#!
  26. 26 Unary RPC LINE Developer Meetup #54 in Fukuoka l

    #'( 7   ( 6 • 40 $&"' ,-+. • #'3 StreamObserver gRPC!%/2 5*  /2)4 Stub gRPC Service RPC18 Stream Observer Stream Observer message message onNext onCompleted onNext onCompleted
  27. 27 Unary RPC  LINE Developer Meetup #54 in Fukuoka

    // Client InputNumber input = InputNumber.newBuilder().setNum(1).build(); FizBuzAnswer a1 = blockStub.fizBuzOne(input); System.out.println(a1.getAnswer()); // Server Unary @Override public void fizBuzOne(InputNumber request, StreamObserver<FizBuzAnswer> responseObserver) { String answer = calc.calc(request.getNum()); // send response to client responseObserver.onNext(FizBuzAnswer.newBuilder().setAnswer(answer).build()); // send RPC finish to client responseObserver.onCompleted(); }
  28. 28 Server Streaming RPC LINE Developer Meetup #54 in Fukuoka

    l  * (& ) • Unary  onNext(&!# • BlockingStub  Iterator  " $' Stub gRPC Service RPC%+ Stream Observer Stream Observer message message onNext onCompleted onNext onCompleted message onNext
  29. 29 Client Streaming RPC LINE Developer Meetup #54 in Fukuoka

    l  ($& &"   ) •  !%'  Stub gRPC Service RPC#* Stream Observer Stream Observer message onNext onCompleted onCompleted message onNext message onNext
  30. 30 Bidirectional Streaming RPC LINE Developer Meetup #54 in Fukuoka

    l "# !;5704  • Ping-Pong $9$%:  6) *2/ 7+   •   onCompleted ( RPC'8  3.&-,<  Stub gRPC Service RPC1= Stream Observer Stream Observer message onNext onCompleted onCompleted message onNext message onNext message onNext
  31. var bdRequestSender = stub.fizBuzMany(new StreamObserver<FizBuzAnswer>() { @Override public void onNext(FizBuzAnswer

    value) { System.out.println("receive " + value.getAnswer()); } @Override public void onCompleted() { waitFinish.countDown(); } @Override public void onError(Throwable t) { waitFinish.countDown(); } }); // receive server data each onNext. bdRequestSender.onNext(InputNumber.newBuilder().setNum(3).build()); bdRequestSender.onNext(InputNumber.newBuilder().setNum(1).build()); bdRequestSender.onNext(InputNumber.newBuilder().setNum(80).build()); // send finish to server. bdRequestSender.onCompleted(); waitFinish.countDown(); 31 Bidirectional Streaming RPC(Stub) LINE Developer Meetup #54 in Fukuoka   
  32. @Override public StreamObserver<InputNumber> fizBuzMany(StreamObserver<FizBuzAnswer> responseObserver) { return new StreamObserver<InputNumber>() {

    @Override public void onNext(InputNumber value) { // one by one data send. var answer = calc.calc(value.getNum()); responseObserver.onNext( FizBuzAnswer.newBuilder().setAnswer(answer).build()); } @Override public void onCompleted() { responseObserver.onCompleted(); } @Override public void onError(Throwable t) { t.printStackTrace(); } }; } 32 Bidirectional Streaming RPC(Server) LINE Developer Meetup #54 in Fukuoka   
  33. 33  gRPC   LINE Developer Meetup #54 in

    Fukuoka l API $" Request-Response  Unary $ l Streaming API%  #  • !&   !& ?
  34. 34 Stream  LINE Developer Meetup #54 in Fukuoka l

    Reactive-gRPC(https://github.com/salesforce/reactive-grpc) • Salesforce gRPC4HRxJava/Reactor ; Stub, Service $)BA  • StreamObserver 2O LF6@ $) BA '%(  D ."' + -!+-.78   • 1K."' +N3 gRPC :>N2O  J9M #%*!#% l gRPC-kotlin (https://github.com/rouzwawi/grpc-kotlin) • $/&0;M $)BA  • Stream >NE=/,G5 <CI$)? 
  35. 35 Reactive-gRPC  l Unary  Single/Mono, Stream  Observable/Flux

     l  RPC   Stub, Server  Reactive    StreamObserver  @Override public Flux<FizBuzAnswer> fizBuzMany(Flux<InputNumber> request) { return request.map(InputNumber::getNum) .as(cal::calcStream) .map(a -> FizBuzAnswer.newBuilder().setAnswer(a).build()); } var stb = ReactorFizBuzServiceGrpc.newReactorStub(chan); stb.fizBuzMany( Flux.range(0,20).map(n -> InputNumber.newBuilder().setNum(n).build())) .subscribe(answer -> System.out.println(answer.getAnswer())); LINE Developer Meetup #54 in Fukuoka
  36. Agenda • LINE Developer Meetup #54 in Fukuoka 36

  37. l JSON 1 &  2000 <> 85 3B;1 100

    &2, 4- 3B0(*9 • REST - 4841 "#@ • gRPC - 1036 "#@ • 4.8 ? . HTTP/2 6:)A # =8+C '7 / • https://github.com/kencharos/grpc_bench • Java + Micronaut gRPC   %$ 37 gRPC REST %! LINE Developer Meetup #54 in Fukuoka
  38. l P B P • https://github.com/protocolbuffers/protobuf • google.protobuf.Timestamp • java.time.LocalDate

    • 10 (BigDecimal) B 38 B LINE Developer Meetup #54 in Fukuoka import "google/protobuf/timestamp.proto"; message FromTo { google.protobuf.Timestamp from = 1; google.protobuf.Timestamp to = 2; }
  39. l - 2 B https://github.com/grpc-ecosystem/grpc-gateway S 1 2 Rc ePurR

    w H fgR a tsPJ 2 R ). - h a • ( j N R aSiT RPC ySp • ) R a p v Slo • . R a tsPJ HnGO 39 - 2 B LINE Developer Meetup #54 in Fukuoka
  40. l T Jp • P J • e u b

    • cgr S W SP C R S W v iy tg a o 40 LINE Developer Meetup #54 in Fukuoka var client = new cl.FizBuzServicePromiseClient('http://localhost:8080'); var request = new message.InputNumber(); request.setNum(12); client.fizBuzOne(request, {}) .then(res => {window.alert("answer is " + res.getAnswer());})
  41. l C P • HK KT K l • HK

    KT K C l P u C e HV C - u C l P a l Cg R 41 P LINE Developer Meetup #54 in Fukuoka
  42. l pc bB • ta R R R P ir

    l o • n o • u e o • ,, o l i gB C R R v S C 42 R R LINE Developer Meetup #54 in Fukuoka
  43. l R • P R • C 43 LINE Developer

    Meetup #54 in Fukuoka public <ReqT, RespT> Listener<ReqT> interceptCall( ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) { var cred = headers.keys().stream().filter(k -> k.equals("cred")).findFirst().get(); //    var username = getUsrename(cred); // # Context  username % ! var newContext = Context.current().withValue(Context.key("username"), username); //  Context " $ RPC Context.get("username').get()  return Contexts.interceptCall(newContext, call, headers, next); }
  44. l Google7< .=-4 gPRC   >;68*9,0 l proto %(

    API % #+: &)$ %!#  l  "'  2/?5 31  44   LINE Developer Meetup #54 in Fukuoka gRPC(Golden Retriever Pan Cakes)    
  45. l   (!/ (/#%etc)LINE Financial https://linecorp.com/ja/career/position/1106 l  (!/

    (/#%etc)LINE Financial https://linecorp.com/ja/career/position/1102 l    (!/ (/#%etc)LINE Financial https://linecorp.com/ja/career/position/1423 l ' (!/ (/#%etc)LINE Financial https://linecorp.com/ja/career/position/1099 45 We are hiring!! (& $"  ) LINE Developer Meetup #54 in Fukuoka
  46. THANK YOU