gRPC

5f533179da1c82722252cbcb93e7356f?s=47 Moyuru Aizawa
September 15, 2017

 gRPC

5f533179da1c82722252cbcb93e7356f?s=128

Moyuru Aizawa

September 15, 2017
Tweet

Transcript

  1. gRPC @MoyuruAizawa

  2. Ѫᖒ๖ (Moyuru Aizawa) MoyuruAizawa Contract Developer Pairs Div. Eureka, Inc.

    FRESH! Div. CyberAgent, Inc.
  3. None
  4. ‣ Live Streaming ‣ Upcoming ‣ Live ‣ Pause ‣

    … FRESH!
  5. FRESH!

  6. Polling

  7. Polling Client Server Live Pause

  8. Polling Client Server Live Pause

  9. Polling Client Server Live Pause

  10. Polling Client Server Live Pause

  11. Polling Client Server Live Pause

  12. Polling Client Server Live Pause

  13. Polling Client Server Live Pause

  14. Polling Client Server Live Pause

  15. Polling Client Server Live Pause ແବ!

  16. Polling Client Server Live Pause ஗͍!!

  17. gRPC

  18. ‣ Google ‣ Remote Procedure Call ‣ HTTP/2 ‣ ѹॖϔομʔ

    ‣ αʔόʔϓογϡ ‣ Protocol Buffers ‣ ετϦʔϜ ‣ … gRPC
  19. gRPC

  20. gRPC Channel(OkHttp2) Binary data

  21. gRPC Serialize/Deserialize
 … FooServiceStub

  22. Plasma

  23. ‣ FRESH!։ൃ ‣ gRPC Stream, Server Sent EventΛ༻͍ͨ൚༻తͳServer Pushϛυϧ ΢ΣΞ

    ‣ ΫϥΠΞϯτ͸Plasmaʹରͯ͠subscribe͍ͨ͠EventΛૹΔ ‣ Πϕϯτൃੜ࣌ʹPlasma͕ΫϥΠΞϯτʹରͯ͠৘ใΛૹΔ ‣ https://blog.stormcat.io/post/entry/openfresh-plasma/ Plasma
  24. gRPC Client Plasma

  25. gRPC Client Plasma Connect to server

  26. gRPC Client Plasma Connect to server subscribe Event1

  27. gRPC Client Plasma Connect to server subscribe Event1 Event1

  28. gRPC Client Plasma Connect to server subscribe Event1 Event1

  29. gRPC Client Plasma Connect to server subscribe Event1 Event1 subscribe

    Event2
  30. gRPC Client Plasma Connect to server subscribe Event1 Event1 subscribe

    Event2 Event2
  31. gRPC Client Plasma Connect to server subscribe Event1 Event1 subscribe

    Event2 Event2
  32. gRPC Client Plasma Connect to server subscribe Event1 Event1 subscribe

    Event2 Event1 Event2
  33. gRPC Client Plasma Connect to server subscribe Event1 Event1 subscribe

    Event2 Event1 Event2
  34. gRPC Client Plasma Connect to server subscribe Event1 Event1 subscribe

    Event2 Event1 Event2 ແବͳϦΫΤετͷ࡟ݮ ϦΞϧλΠϜ
  35. TIPS

  36. ‣ Protobuf ‣ ProtoϑΝΠϧͷσʔλఆٛΛݩʹSerialize/Deserializeίʔυͷੜ ੒ɺσʔλΫϥεͷੜ੒ ‣ ProtoϑΝΠϧͷαʔϏεఆٛΛݩʹStubΫϥεͷੜ੒ ‣ Channel ‣

    αʔόʔͱͷίωΫγϣϯ ‣ Stub ‣ αʔϏε gRPC
  37. Kotlin and Protobuf

  38. Kotlin and Protobuf

  39. apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'com.google.protobuf' apply

    plugin: 'com.android.application' apply plugin: 'com.google.protobuf' apply plugin: 'kotlin-android' Kotlin and Protobuf
  40. Channel and Lifecycle

  41. ManagedChannel and Lifecycle onCreate

  42. ManagedChannel and Lifecycle onCreate new ManagedChannel()

  43. ManagedChannel and Lifecycle onCreate new ManagedChannel() FooServiceStubGrpc.newStub(channel)

  44. ManagedChannel and Lifecycle onCreate new ManagedChannel() FooServiceStubGrpc.newStub(channel) stub.bar()

  45. ManagedChannel and Lifecycle onCreate onDestroy channel.shutdownNow() new ManagedChannel() FooServiceStubGrpc.newStub(channel) stub.bar()

  46. ManagedChannel and Lifecycle onCreate onDestroy channel.shutdownNow() the system will simply

    kill the activity's hosting process without calling onDestroy new ManagedChannel() FooServiceStubGrpc.newStub(channel) stub.bar()
  47. ManagedChannel and Lifecycle onCreate onDestroy channel.shutdownNow() new ManagedChannel() FooServiceStubGrpc.newStub(channel) stub.bar()

    αʔόʔʹແବͳίωΫγϣϯ͕ཷ·͍ͬͯ͘ ΞϓϦ͕όοΫάϥ΢ϯυͰ΋௨৴ͯ͠͠·͏
  48. ManagedChannel and Lifecycle onCreate onDestroy channel.shutdownNow() new ManagedChannel() FooServiceStubGrpc.newStub(channel) stub.bar()

    onStart onStop
  49. ‣ onStart-onStopͰίωΫγϣϯΛுΔ ‣ ίωΫγϣϯ͕੾ΕͨStub͸࢖͑ͳ͘ͳΔͷͰɺStub΋ຖ౓ੜ੒͠௚ ͢ ‣ FRESH!Ͱ͸gRPCͰ௨৴͢Δ༻ͷΫϥεΛ࡞੒ɻαʔόʔͱͷ઀ଓ ͱɺϦΫΤετΛ·ͱΊͨɻ ManagedChannel and

    Lifecycle
  50. Reconnection

  51. ‣ Channel: αʔόʔͱͷίωΫγϣϯ ‣ ManagedChannel:αʔόʔ͕μ΢ϯ͢Δɺ୺຤͕ΦϑϥΠϯʹͳΔͳ ͲʹΑͬͯίωΫγϣϯ͕੾Εͯ΋ɺ࠶઀ଓΛߦͬͯ͘ΕΔɻ Reconnection

  52. Reconnection ͍͔ͭΒManagedChannel͕ૉૣ͘ ࠶઀ଓͯ͘͠ΕΔͱࡨ͍֮ͯͨ͠?

  53. ‣ ͔ͨ͠ʹɺ࠶઀ଓͯ͘͠ΕΔɻ ‣ ͔͠͠ɺ΍ͨΒ͕͔͔࣌ؒΔɻ(5ඵͱ͔10ඵͱ͔) ‣ ࠶઀ଓ·ͰผͷHTTP/1.1ͷAPIΛୟ͘ Plasma͕RxͰ͍͏PublishSubjectͷΑ͏ͳϛυϧ΢ΣΞͳͷͰɺ઀ଓ͕੾Ε͍ͯΔؒʹى͖ͨΠϕϯτΛ औΕͳ͍ͨΊ Reconnection

  54. ‣ ͔ͨ͠ʹɺ࠶઀ଓͯ͘͠ΕΔɻ ‣ ͔͠͠ɺ΍ͨΒ͕͔͔࣌ؒΔɻ(5ඵͱ͔10ඵͱ͔) ‣ ࠶઀ଓ·ͰผͷHTTP/1.1ͷAPIΛୟ͘ Plasma͕RxͰ͍͏PublishSubjectͷΑ͏ͳϛυϧ΢ΣΞͳͷͰɺ઀ଓ͕੾Ε͍ͯΔؒʹى͖ͨΠϕϯτΛ औΕͳ͍ͨΊ ‣ ઀ଓঢ়ଶͷมߋΛ؂ࢹ͍ͨ͠ͳ͊….

    Reconnection
  55. ‣ ͔ͨ͠ʹɺ࠶઀ଓͯ͘͠ΕΔɻ ‣ ͔͠͠ɺ΍ͨΒ͕͔͔࣌ؒΔɻ(5ඵͱ͔10ඵͱ͔) ‣ ࠶઀ଓ·ͰผͷHTTP/1.1ͷAPIΛୟ͘ Plasma͕RxͰ͍͏PublishSubjectͷΑ͏ͳϛυϧ΢ΣΞͳͷͰɺ઀ଓ͕੾Ε͍ͯΔؒʹى͖ͨΠϕϯτΛ औΕͳ͍ͨΊ ‣ ઀ଓঢ়ଶͷมߋΛ؂ࢹ͍ͨ͠ͳ͊….

    Reconnection
  56. TLS 1.2

  57. OkHttpChannelBuilder .forAddress(address, port) .sslSocketFactory(sslSocketFactory) .connectionSpec( ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS) .tlsVersions(TlsVersion.TLS_1_2) .build() ) .negotiationType(NegotiationType.TLS)

    .build() Build ManagedChannel
  58. OkHttpChannelBuilder .forAddress(address, port) .sslSocketFactory(sslSocketFactory) .connectionSpec( ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS) .tlsVersions(TlsVersion.TLS_1_2) .build() ) .negotiationType(NegotiationType.TLS)

    .build() Build ManagedChannel
  59. OkHttpChannelBuilder .forAddress(address, port) .sslSocketFactory(sslSocketFactory) .connectionSpec( ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS) .tlsVersions(TlsVersion.TLS_1_2) .build() ) .negotiationType(NegotiationType.TLS)

    .build() Build ManagedChannel Android 4.1-4.4Ͱ TLS 1.2͕ར༻Ͱ͖ͳ͍
  60. class TlsSocketFactory(private val delegate: SSLSocketFactory) : SSLSocketFactory() { override fun

    getDefaultCipherSuites(): Array<String> = delegate.defaultCipherSuites override fun createSocket(s: Socket?, host: String?, port: Int, autoClose: Boolean): Socket = delegate.createSocket(s, host, port, autoClose).let(this::patch) override fun createSocket(host: String?, port: Int): Socket = delegate.createSocket(host, port).let(this::patch) override fun createSocket(host: String?, port: Int, localHost: InetAddress?, localPort: Int): Socket = delegate.createSocket(host, port, localHost, localPort).let(this::patch) override fun createSocket(host: InetAddress?, port: Int): Socket = delegate.createSocket(host, port).let(this::patch) override fun createSocket(address: InetAddress?, port: Int, localAddress: InetAddress?, localPort: Int): Socket = delegate.createSocket(address, port, localAddress, localPort).let(this::patch) override fun getSupportedCipherSuites(): Array<String> = delegate.supportedCipherSuites private fun patch(s: Socket): Socket = s.apply { if (this is SSLSocket) enabledProtocols = arrayOf("TLSv1.2") } } Enable TLS 1.2 https://github.com/square/okhttp/issues/2372
  61. class TlsSocketFactory(private val delegate: SSLSocketFactory) : SSLSocketFactory() { override fun

    getDefaultCipherSuites(): Array<String> = delegate.defaultCipherSuites override fun createSocket(s: Socket?, host: String?, port: Int, autoClose: Boolean): Socket = delegate.createSocket(s, host, port, autoClose).let(this::patch) override fun createSocket(host: String?, port: Int): Socket = delegate.createSocket(host, port).let(this::patch) override fun createSocket(host: String?, port: Int, localHost: InetAddress?, localPort: Int): Socket = delegate.createSocket(host, port, localHost, localPort).let(this::patch) override fun createSocket(host: InetAddress?, port: Int): Socket = delegate.createSocket(host, port).let(this::patch) override fun createSocket(address: InetAddress?, port: Int, localAddress: InetAddress?, localPort: Int): Socket = delegate.createSocket(address, port, localAddress, localPort).let(this::patch) override fun getSupportedCipherSuites(): Array<String> = delegate.supportedCipherSuites private fun patch(s: Socket): Socket = s.apply { if (this is SSLSocket) enabledProtocols = arrayOf("TLSv1.2") } } Enable TLS 1.2 https://github.com/square/okhttp/issues/2372
  62. class TlsSocketFactory(private val delegate: SSLSocketFactory) : SSLSocketFactory() { override fun

    getDefaultCipherSuites(): Array<String> = delegate.defaultCipherSuites override fun createSocket(s: Socket?, host: String?, port: Int, autoClose: Boolean): Socket = delegate.createSocket(s, host, port, autoClose).let(this::patch) override fun createSocket(host: String?, port: Int): Socket = delegate.createSocket(host, port).let(this::patch) override fun createSocket(host: String?, port: Int, localHost: InetAddress?, localPort: Int): Socket = delegate.createSocket(host, port, localHost, localPort).let(this::patch) override fun createSocket(host: InetAddress?, port: Int): Socket = delegate.createSocket(host, port).let(this::patch) override fun createSocket(address: InetAddress?, port: Int, localAddress: InetAddress?, localPort: Int): Socket = delegate.createSocket(address, port, localAddress, localPort).let(this::patch) override fun getSupportedCipherSuites(): Array<String> = delegate.supportedCipherSuites private fun patch(s: Socket): Socket = s.apply { if (this is SSLSocket) enabledProtocols = arrayOf("TLSv1.2") } } Enable TLS 1.2 SSLSocketFactoryΛϥοϓͯ͠TLS 1.2Λ༗ޮʹ͢Δ https://github.com/square/okhttp/issues/2372
  63. None
  64. FRESH!

  65. FRESH!

  66. ‣ ଈ࠲ʹαʔόʔ͔Β৘ใ͕ඈΜͰ͖ͯ࠷ߴ ‣ ίωΫγϣϯʹؾΛ഑Δඞཁ͕͋Δ ‣ HTTP/1.1ʹ͸ແ͔ͬͨ֓೦ͳͷͰ׳ΕΔ·Ͱେม ‣ AndroidͰϓϩμΫγϣϯʹಋೖͨ͠࿩͕গͳ͍ ‣ ࢼߦࡨޡͷ࿈ଓ

    Conclusion
  67. Thank you