Spring I/O 2016 Spring 5について

Post on 07-Jan-2017

792 views 1 download

Transcript of Spring I/O 2016 Spring 5について

Tagbangers, inc.Alisa Sasaki

2016

About Spring5

Spring Framework 5.0マイルストーン

5.0 GA?

3月JDK9 release?

※JDP 9のリリースが遅れると、Spring 5.0のリリースもおそらく遅れる模様。

5.0RELEASE

12月2016 2017

5.0 RC14.3 GA

6月

5.0 M1

7月末

Spring Framework 5.0のテーマ

JDK9 & Jigsawモジュール

Servlet4.0 & HTTP/2

Reactiveアーキテクチャ

JDK9 & Jigsawモジュール

Jigsaw?

Moduleの概念をJDKに導入すること• jarの依存関係を明確にする

• パッケージの公開範囲を設定できる

• モジュール単位でアプリに必要な部分だけロード

することができる

module

package

Class & Interface

Field & Method

Purpose• Java SEおよびJDKをスモールデバイス向けにスケールダウンできるように

• Jar内のライブラリ同士のコンフリクトを防ぎたい

• 内部のAPIを壊すことなく変更したい

Spring 5のjar:Jigsaw メタデータが付帯される

モジュール名はMaven central jarのネーミングに従う(spring-context, sprint-jdbc, spring-webmvc..)

JDK9 & Jigsawモジュールこんな風に変わります

module my.app.db {

requires java.sql;

requires spring.jdbc;

}

Servlet4.0 & HTTP/2 We need to embrace HTTP/2 in the Java land as well!

プロトコルの仕様(HTTP/1.1)に起因するパフォーマンスの悪さ

・1ドメインにリクエストする数が制限されている

・リクエストの順番どおりにレスポンスを返す必要がある(HoLブロッキング)

Problem

HTTP/2 (https://http2.github.io/faq/)

バイナリフレーム・ストリームの概念などを採用

リクエストとレスポンスを多重化して高速化を図る

http://webdesignledger.com

1 request ≠ 1 response

JDK9もHTTP/2対応

Servlet4.0 & HTTP/2

HTTP/2対応を行っているServlet4.0とJDK 9 HTTP Clientのサポートにフォーカスする

Servlet4.0サーバサイドJavaへのHTTP/2サポートを提供する

• ストリーム優先度付

• サーバプッシュ

• リクエスト/レスポンスの多重化

Tomcat 8.1/9.0

Jetty 9.3

Undertow 1.3

• HTTP Client API

• HTTP/1.1からのプロトコルスイッチング

• サーバプッシュ

• HPACK(ヘッダ圧縮)パラメタ

• サーバプッシュ

• HPACK(ヘッダ圧縮)パラメタ

会場で人気のあったセッショントップ3

Keynote day 2: Designing Applications: The Reactive Way

時代の流れと経緯

Reactiveとは

データフローと時(イベント)とともに変わる振る舞いの伝播を扱う、その仕組み

Reactive Programmingに必要なツール

Reactive Programming:特に外部のリソースとのやりとりのときにおいて非同期・ノンブロッキング・関数型スタイルで記述していく方法

• Reactive Streams• Reactive API

とてもざっくりしたReactive Streams

Data Stream

この人を効率よく動かす

blocking

次、1名どうぞ!

非同期データストリームをノンブロッキングなback pressureで処理していくこと

Back pressure

さらにざっくりなReactive Streams

・Publisher・Subscriber・Subscription・Processor

Publisher Subscriber

onNextonErroronComplete

RequestCancel

Java9でjava.util.concurrent.Flowに含められる予定

Asyncな書き方を求めて

public interface UserRepository {Future<User> findById(String id) throws IOException;..

}

try {Future<User> future = repository.findById(id);User use = future.get(); //block

}catch (InterruptedException e) {

//}catch (ExecutionException e) {

//}

Ugh

違うスレッドで発生するかも

CompletableFuture<User> future = repository.findById(id);future.whenComplete(user, throwable) ‐> {

// ...}

public interface UserRepository {...CompletableFuture<List<User>> findAll();...

}

CompletableFuture→Stream(JDK8)→…

Async callback!

Userが全部集まるまでコールバックしない

Reactive Streamsを使おう

複数の値を効率よく扱えるデータごとに通知がもらえる

Stream(JDK8)を使おう…

Publisher Subscriber

onNextonErroronComplete

RequestCancel

MonoReactor Core 2.5~

Flux(旧Stream)

Flux and Mono(Reactor)

implements

最大1個の値を出力するPublisher( 単発の値を返す非同期処理用)

最大N個の値を出力するPublisher(ストリーム)

subscribe されてはじめて実行される

ReactiveAPI

https://speakerdeck.com/sdeleuze/a‐lite‐rx‐api‐for‐the‐jvm

CompletableFuture<String> future =Mono.fromCompletableFuture(someCompletableFuture).timeout(Duration.ofSeconds(30)).log("hello").toCompletableFuture();

Stream<?> future =Flux.fromStream(anotherStream)

.timeout(Duration.ofSeconds(30))

.log("hello")

.stream();

CompletableFuture→Mono

Java8 Stream→Flux

Controller with Reactive types

repository.findAll().filter(user ‐> user.getName().matches("K.*")).map(user ‐> "Name: " + user.getName()).log().subscribe(user ‐> {});

public interface UserRepository {Mono<User> findById(Long id);Flux<User> findAll();Mono<User> save(User user);

}

データフローを発生させるDefaultはすべてのデータを取得する(データ取得制限なし)

@RequestMapping("/users")public Flux<User> getUsers() {

return this.userRepository.findAll();}

Thymeleaf 3.0 Reactive FriendlinessEngine throttling

シングルスレッド・back-pressure管理・Cold observable

1process

https://github.com/thymeleaf/thymeleafsandbox-springreactive

むすびに

http://wallride.org/

Multilingual easy-to-customize CMS (OSS)

Spring FrameworkHibernateThymeleaf

WallRide

MUCHAS GRACIAS!ありがとうございました