KEMBAR78
Reactor Netty & Apache Kafka Stack #jsug | PDF
Reactor Netty &
Apache Kafka Stack
JSUG勉強会 2018年その6 SpringOne Platform 2018報告会
ヤフー株式会社 水落 啓太
2018年11月29日
自己紹介
水落 啓太(みずおち けいた)
• ヤフー株式会社所属
• 社内PaaS(Pivotal Cloud Foundry)
関連コンポーネントの開発、
社内PaaS利用者向け技術サポートなどに従事
• マイブームはSpring WebFlux
Copyright (C) 2018 Yahoo Japan Corporation. All Rights Reserved.
2
紹介するセッション
1. Reactor Netty, the Default Spring Boot 2.0 Runtime
– Violeta Georgieva@Pivotal
2. Walking up the Spring for Apache Kafka Stack
– Viktor Gamov@Confluent
– Gary Russell@Pivotal
Copyright (C) 2018 Yahoo Japan Corporation. All Rights Reserved.
3
Reactor Netty, the Default Spring
Boot 2.0 Runtime
Copyright (C) 2018 Yahoo Japan Corporation. All Rights Reserved.
4
どんなセッション?
• reactor-nettyのアップデート(0.8系)
• reactor-nettyの仕組み紹介
• reactor-nettyによるライブコーディング
Copyright (C) 2018 Yahoo Japan Corporation. All Rights Reserved.
5
Netty
• ネットワークプログラミング用フレームワーク
(!= ウェブアプリケーション向け)
• 高スループット、複雑な非同期処理が求められるミドルウェア
等の実装に高適応
• non-blocking IO
• Spring 5/Spring WebFluxのデフォルトバックエンド
• Springから見ると “メタフレームワーク” といえる
Copyright (C) 2018 Yahoo Japan Corporation. All Rights Reserved.
6
Reactor Netty
Copyright (C) 2018 Yahoo Japan Corporation. All Rights Reserved.
7引用: https://spring.io
Reactor Netty (0.8) Server API
HttpServer.create()
.port(8080)
.protocol(HttpProtocol.HTTP11)
.handle((req, res) ->
...
res.send(resBody)
)
.bindNow();
Copyright (C) 2018 Yahoo Japan Corporation. All Rights Reserved.
8
Publisher<ByteBuf>
Reactor Netty (0.8) Client API
HttpClient.create()
.port(8080)
.protocol(HttpProtocol.HTTP11)
.post()
.uri("http://...")
.send(body)
.responseContent()
.aggregate()
.asString()
.block();
Copyright (C) 2018 Yahoo Japan Corporation. All Rights Reserved.
9
Publisher<ByteBuf>
wiretap
• 通信内容をダンプする機能
HttpClient.create()
.wiretap(true)
...
13:24:36.333 [reactor-http-nio-4] DEBUG reactor.netty.http.client.HttpClient - [id:
0x78ba8f21, L:/172.16.164.183:57134 - R:www.yahoo.co.jp/183.79.250.251:80] WRITE: 113B
+-------------------------------------------------+
| 0 1 2 3 4 5 6 7 8 9 a b c d e f |
+--------+-------------------------------------------------+----------------+
|00000000| 47 45 54 20 2f 20 48 54 54 50 2f 31 2e 31 0d 0a |GET / HTTP/1.1..|
|00000010| 75 73 65 72 2d 61 67 65 6e 74 3a 20 52 65 61 63 |user-agent: Reac|
|00000020| 74 6f 72 4e 65 74 74 79 2f 30 2e 38 2e 32 2e 52 |torNetty/0.8.2.R|
|00000030| 45 4c 45 41 53 45 0d 0a 68 6f 73 74 3a 20 77 77 |ELEASE..host: ww|
|00000040| 77 2e 79 61 68 6f 6f 2e 63 6f 2e 6a 70 0d 0a 61 |w.yahoo.co.jp..a|
|00000050| 63 63 65 70 74 3a 20 2a 2f 2a 0d 0a 63 6f 6e 74 |ccept: */*..cont|
|00000060| 65 6e 74 2d 6c 65 6e 67 74 68 3a 20 30 0d 0a 0d |ent-length: 0...|
|00000070| 0a |. |
+--------+-------------------------------------------------+----------------+
Copyright (C) 2018 Yahoo Japan Corporation. All Rights Reserved.
10
Reactor Netty 0.8 Update
• Spring Boot 2.1対応
• HTTP/2対応
• Server/Clientの新Builder API
• パッケージ名/Maven Group ID変更
• 内部実装の進化
Copyright (C) 2018 Yahoo Japan Corporation. All Rights Reserved.
11
Reactor Netty 0.8の注意
• パッケージ名/Maven Group ID変更
パッケージ名
reactor.ipc.netty → reactor.netty
Group ID
io.projectreactor.ipc.netty → io.projectreactor.netty
Copyright (C) 2018 Yahoo Japan Corporation. All Rights Reserved.
12
Reactor Netty 0.8の注意(ハマりました。。)
Copyright (C) 2018 Yahoo Japan Corporation. All Rights Reserved.
13
My App
Srping Boot
/ WebFlux
2.1
cloudfoundry-
client-reactor
3.13.0
Reactor Netty
(io.projectreact
or.netty)
0.8
Reactor Netty
(io.projectreact
or.ipc.netty)
0.7
• Reactor-Nettyをバックエンドとするライブラリを使う際は注
意
???
Walking up the Spring for Apache
Kafka Stack
Copyright (C) 2018 Yahoo Japan Corporation. All Rights Reserved.
14
どんなセッション?
• Kafka本体とその周辺ライブラリ・フレームワークの解説
• SpringとKafka同梱ライブラリの連携状況など
Copyright (C) 2018 Yahoo Japan Corporation. All Rights Reserved.
15
Kafka = Streaming Platform
Copyright (C) 2018 Yahoo Japan Corporation. All Rights Reserved.
16
引用: https://www.slideshare.net/SpringCentral/walking-up-the-spring-for-apache-kafka-stack
Kafka = Streaming Platform
Copyright (C) 2018 Yahoo Japan Corporation. All Rights Reserved.
17
Producer/Consumer
• Kafkaにデータを送受信する基本的なアプリケーション
• Producer/Consumer APIより実装する
Connectors
• Kafkaとデータ連携するアプリケーション
• データソース・シンク毎(e.g. MySQL, Elasticsearch)に実装が公開さ
れており、通常新規にプログラミングする必要はない
• Connector APIにより実装される
Streaming Engine
• トピック間同士でリアルタイムにデータを変換・統合するようなアプリ
ケーション
• Streams APIより実装する
• 後述のKafka Streamsライブラリより手軽実装できる
Kafka Connectors
Copyright (C) 2018 Yahoo Japan Corporation. All Rights Reserved.
18
引用: https://www.slideshare.net/SpringCentral/walking-up-the-spring-for-apache-kafka-stack
Kafka Streams
• Kafkaを用いたストリーミング処理を書くためのライブラリ
• メッセージストリームをKStream/KTableという概念に抽象化し
てプログラミング
• Stream/Tableという名前から連想できるようにmap/join等の
処理を簡単に書ける
Copyright (C) 2018 Yahoo Japan Corporation. All Rights Reserved.
19
Kafka Streams
• Kafkaを用いたストリーミング処理を書くためのライブラリ
• メッセージストリームをKStream/KTableという概念に抽象化し
てプログラミング
• Stream/Tableという名前から連想できるようにmap/join等の
処理を簡単に書ける
Copyright (C) 2018 Yahoo Japan Corporation. All Rights Reserved.
20
Kafka Streams - join
• レーティングと映画からレーティング済み映画データを作成
KTable<Long, String> ratedMovies = ratingAverage
.join(movies, (avg, movie) ->
movie.getTitle() + "=" + avg);
• Kafkaトピックへ書き込み
ratedMovies.toStream().to("rated-movie", ...);
Copyright (C) 2018 Yahoo Japan Corporation. All Rights Reserved.
21
引用: https://content.pivotal.io/springone-platform-2018/walking-up-the-spring-for-apache-kafka-stack-2
Spring for Apache Kafka
Copyright (C) 2018 Yahoo Japan Corporation. All Rights Reserved.
22
• ここまではpure Kafkaな話
• Springでは様々なレイヤーからKafka利用をサポート
Kafka Streams & Spring Boot
• Spring BootでKafka Streamsを簡単に設定
spring:
kafka:
bootstrap-servers:
- localhost:9092
streams:
application-id: stream-app
properties:
commit.interval.ms: 1000
...
Copyright (C) 2018 Yahoo Japan Corporation. All Rights Reserved.
23
Spring for Apache Kafka
Copyright (C) 2018 Yahoo Japan Corporation. All Rights Reserved.
24
引用: https://www.slideshare.net/SpringCentral/walking-up-the-spring-for-apache-kafka-stack
Spring for Apache Kafka
Copyright (C) 2018 Yahoo Japan Corporation. All Rights Reserved.
25
• Spring For Apache Kafka
• KafkaTemplate
• @KafkaListener
• Spring Integration (Kafka Extension)
• メッセージ通信の抽象化(Message Channel, Endpoint)
• Kakfa非依存化
• Spring Cloud Streams
• Opinionated Configuration
• Kafka Streamsサポート
• Reactor Kafka
• KafkaSender.send(Publisher<... SenderRecord>)
• KafkaReceiver.receive(): Flux<ReceiverRecord<...>>
まとめ
Copyright (C) 2018 Yahoo Japan Corporation. All Rights Reserved.
26
まとめ
• Reactor Netty/Apache Kafkaに関する2セッションを紹介した
• 共にストリーミング処理に強いフレームワーク・プラット
フォームであり、実用性だけでなく技術的な面白さがある
• 興味があればぜひYouTube/Slideshareをチェックしてみてほし
いです
• ご清聴ありがとうございました
Copyright (C) 2018 Yahoo Japan Corporation. All Rights Reserved.
27

Reactor Netty & Apache Kafka Stack #jsug

  • 1.
    Reactor Netty & ApacheKafka Stack JSUG勉強会 2018年その6 SpringOne Platform 2018報告会 ヤフー株式会社 水落 啓太 2018年11月29日
  • 2.
    自己紹介 水落 啓太(みずおち けいた) •ヤフー株式会社所属 • 社内PaaS(Pivotal Cloud Foundry) 関連コンポーネントの開発、 社内PaaS利用者向け技術サポートなどに従事 • マイブームはSpring WebFlux Copyright (C) 2018 Yahoo Japan Corporation. All Rights Reserved. 2
  • 3.
    紹介するセッション 1. Reactor Netty,the Default Spring Boot 2.0 Runtime – Violeta Georgieva@Pivotal 2. Walking up the Spring for Apache Kafka Stack – Viktor Gamov@Confluent – Gary Russell@Pivotal Copyright (C) 2018 Yahoo Japan Corporation. All Rights Reserved. 3
  • 4.
    Reactor Netty, theDefault Spring Boot 2.0 Runtime Copyright (C) 2018 Yahoo Japan Corporation. All Rights Reserved. 4
  • 5.
    どんなセッション? • reactor-nettyのアップデート(0.8系) • reactor-nettyの仕組み紹介 •reactor-nettyによるライブコーディング Copyright (C) 2018 Yahoo Japan Corporation. All Rights Reserved. 5
  • 6.
    Netty • ネットワークプログラミング用フレームワーク (!= ウェブアプリケーション向け) •高スループット、複雑な非同期処理が求められるミドルウェア 等の実装に高適応 • non-blocking IO • Spring 5/Spring WebFluxのデフォルトバックエンド • Springから見ると “メタフレームワーク” といえる Copyright (C) 2018 Yahoo Japan Corporation. All Rights Reserved. 6
  • 7.
    Reactor Netty Copyright (C)2018 Yahoo Japan Corporation. All Rights Reserved. 7引用: https://spring.io
  • 8.
    Reactor Netty (0.8)Server API HttpServer.create() .port(8080) .protocol(HttpProtocol.HTTP11) .handle((req, res) -> ... res.send(resBody) ) .bindNow(); Copyright (C) 2018 Yahoo Japan Corporation. All Rights Reserved. 8 Publisher<ByteBuf>
  • 9.
    Reactor Netty (0.8)Client API HttpClient.create() .port(8080) .protocol(HttpProtocol.HTTP11) .post() .uri("http://...") .send(body) .responseContent() .aggregate() .asString() .block(); Copyright (C) 2018 Yahoo Japan Corporation. All Rights Reserved. 9 Publisher<ByteBuf>
  • 10.
    wiretap • 通信内容をダンプする機能 HttpClient.create() .wiretap(true) ... 13:24:36.333 [reactor-http-nio-4]DEBUG reactor.netty.http.client.HttpClient - [id: 0x78ba8f21, L:/172.16.164.183:57134 - R:www.yahoo.co.jp/183.79.250.251:80] WRITE: 113B +-------------------------------------------------+ | 0 1 2 3 4 5 6 7 8 9 a b c d e f | +--------+-------------------------------------------------+----------------+ |00000000| 47 45 54 20 2f 20 48 54 54 50 2f 31 2e 31 0d 0a |GET / HTTP/1.1..| |00000010| 75 73 65 72 2d 61 67 65 6e 74 3a 20 52 65 61 63 |user-agent: Reac| |00000020| 74 6f 72 4e 65 74 74 79 2f 30 2e 38 2e 32 2e 52 |torNetty/0.8.2.R| |00000030| 45 4c 45 41 53 45 0d 0a 68 6f 73 74 3a 20 77 77 |ELEASE..host: ww| |00000040| 77 2e 79 61 68 6f 6f 2e 63 6f 2e 6a 70 0d 0a 61 |w.yahoo.co.jp..a| |00000050| 63 63 65 70 74 3a 20 2a 2f 2a 0d 0a 63 6f 6e 74 |ccept: */*..cont| |00000060| 65 6e 74 2d 6c 65 6e 67 74 68 3a 20 30 0d 0a 0d |ent-length: 0...| |00000070| 0a |. | +--------+-------------------------------------------------+----------------+ Copyright (C) 2018 Yahoo Japan Corporation. All Rights Reserved. 10
  • 11.
    Reactor Netty 0.8Update • Spring Boot 2.1対応 • HTTP/2対応 • Server/Clientの新Builder API • パッケージ名/Maven Group ID変更 • 内部実装の進化 Copyright (C) 2018 Yahoo Japan Corporation. All Rights Reserved. 11
  • 12.
    Reactor Netty 0.8の注意 •パッケージ名/Maven Group ID変更 パッケージ名 reactor.ipc.netty → reactor.netty Group ID io.projectreactor.ipc.netty → io.projectreactor.netty Copyright (C) 2018 Yahoo Japan Corporation. All Rights Reserved. 12
  • 13.
    Reactor Netty 0.8の注意(ハマりました。。) Copyright(C) 2018 Yahoo Japan Corporation. All Rights Reserved. 13 My App Srping Boot / WebFlux 2.1 cloudfoundry- client-reactor 3.13.0 Reactor Netty (io.projectreact or.netty) 0.8 Reactor Netty (io.projectreact or.ipc.netty) 0.7 • Reactor-Nettyをバックエンドとするライブラリを使う際は注 意 ???
  • 14.
    Walking up theSpring for Apache Kafka Stack Copyright (C) 2018 Yahoo Japan Corporation. All Rights Reserved. 14
  • 15.
  • 16.
    Kafka = StreamingPlatform Copyright (C) 2018 Yahoo Japan Corporation. All Rights Reserved. 16 引用: https://www.slideshare.net/SpringCentral/walking-up-the-spring-for-apache-kafka-stack
  • 17.
    Kafka = StreamingPlatform Copyright (C) 2018 Yahoo Japan Corporation. All Rights Reserved. 17 Producer/Consumer • Kafkaにデータを送受信する基本的なアプリケーション • Producer/Consumer APIより実装する Connectors • Kafkaとデータ連携するアプリケーション • データソース・シンク毎(e.g. MySQL, Elasticsearch)に実装が公開さ れており、通常新規にプログラミングする必要はない • Connector APIにより実装される Streaming Engine • トピック間同士でリアルタイムにデータを変換・統合するようなアプリ ケーション • Streams APIより実装する • 後述のKafka Streamsライブラリより手軽実装できる
  • 18.
    Kafka Connectors Copyright (C)2018 Yahoo Japan Corporation. All Rights Reserved. 18 引用: https://www.slideshare.net/SpringCentral/walking-up-the-spring-for-apache-kafka-stack
  • 19.
    Kafka Streams • Kafkaを用いたストリーミング処理を書くためのライブラリ •メッセージストリームをKStream/KTableという概念に抽象化し てプログラミング • Stream/Tableという名前から連想できるようにmap/join等の 処理を簡単に書ける Copyright (C) 2018 Yahoo Japan Corporation. All Rights Reserved. 19
  • 20.
    Kafka Streams • Kafkaを用いたストリーミング処理を書くためのライブラリ •メッセージストリームをKStream/KTableという概念に抽象化し てプログラミング • Stream/Tableという名前から連想できるようにmap/join等の 処理を簡単に書ける Copyright (C) 2018 Yahoo Japan Corporation. All Rights Reserved. 20
  • 21.
    Kafka Streams -join • レーティングと映画からレーティング済み映画データを作成 KTable<Long, String> ratedMovies = ratingAverage .join(movies, (avg, movie) -> movie.getTitle() + "=" + avg); • Kafkaトピックへ書き込み ratedMovies.toStream().to("rated-movie", ...); Copyright (C) 2018 Yahoo Japan Corporation. All Rights Reserved. 21 引用: https://content.pivotal.io/springone-platform-2018/walking-up-the-spring-for-apache-kafka-stack-2
  • 22.
    Spring for ApacheKafka Copyright (C) 2018 Yahoo Japan Corporation. All Rights Reserved. 22 • ここまではpure Kafkaな話 • Springでは様々なレイヤーからKafka利用をサポート
  • 23.
    Kafka Streams &Spring Boot • Spring BootでKafka Streamsを簡単に設定 spring: kafka: bootstrap-servers: - localhost:9092 streams: application-id: stream-app properties: commit.interval.ms: 1000 ... Copyright (C) 2018 Yahoo Japan Corporation. All Rights Reserved. 23
  • 24.
    Spring for ApacheKafka Copyright (C) 2018 Yahoo Japan Corporation. All Rights Reserved. 24 引用: https://www.slideshare.net/SpringCentral/walking-up-the-spring-for-apache-kafka-stack
  • 25.
    Spring for ApacheKafka Copyright (C) 2018 Yahoo Japan Corporation. All Rights Reserved. 25 • Spring For Apache Kafka • KafkaTemplate • @KafkaListener • Spring Integration (Kafka Extension) • メッセージ通信の抽象化(Message Channel, Endpoint) • Kakfa非依存化 • Spring Cloud Streams • Opinionated Configuration • Kafka Streamsサポート • Reactor Kafka • KafkaSender.send(Publisher<... SenderRecord>) • KafkaReceiver.receive(): Flux<ReceiverRecord<...>>
  • 26.
    まとめ Copyright (C) 2018Yahoo Japan Corporation. All Rights Reserved. 26
  • 27.
    まとめ • Reactor Netty/ApacheKafkaに関する2セッションを紹介した • 共にストリーミング処理に強いフレームワーク・プラット フォームであり、実用性だけでなく技術的な面白さがある • 興味があればぜひYouTube/Slideshareをチェックしてみてほし いです • ご清聴ありがとうございました Copyright (C) 2018 Yahoo Japan Corporation. All Rights Reserved. 27