More Related Content
Similar to Serverless時代のJavaについて (20)
More from Amazon Web Services Japan (20)
Serverless時代のJavaについて
- 1. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Serverless時代の
Javaについて
AWS Presents, Battle against Massive Load
using Your Super Sonic Lambda Function!
1
@_kensh
Kensuke Shimokawa
Amazon Web Services Japan
- 2. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Who?
2
•Name
• 下川 賢介
• Kensuke Shimokawa
•Company
• Amazon Web Services Japan
•Role
• Serverless Specialist
Solutions Architect
@_kensh
- 3. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
agenda
3
•Serverless って何ですか?
•Serverless Java チューニング
•まとめ
- 4. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved. 4
Serverless って何ですか?
- 5. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Amazon では、、、
モノリス
2001 2002
マイクロサービス
+ 2 ピザルール
- 6. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
役割の違い
モノリス
(全てを担当)
マイクロサービス
(単一機能を担当)
- 7. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
マイクロサービス アーキテクチャの例
7
- 8. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved. 8
マイクロサービスの特徴
- 9. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
モノリスのデリバリーパイプライン
9
開発者 サービス
デリバリーパイプライン
- 10. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
マイクロサービスのデリバリーパイプライン
10
開発者 サービス デリバリーパイプライン
- 11. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
モノリスのスケール
11
- 12. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
マイクロサービス のスケール
12
- 13. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved. 13
Javaでマイクロサービスというと
SpringBoot ですよね?
- 14. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
起動に時間がかかる? (本日の主題でもある)
14
• コンテナやServerlessは素早くスケールさせたい。
• ポイントは適度に⼩さい単位への分割
• → よりライトウエイトに、よりロジックだけに
• → プレーンなJavaに
• → 定型機能は特化型フレームワークで
AWS Lambda + Java
という選択肢はどうですか?
- 15. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. 15
HTTPS event
Amazon API Gateway AWS Lambda
Serverlessにするとマイクロサービスが作りやすい
- 16. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved. 16
サービスで考慮すること
- 17. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
サービス運用者の気持ち
17
インフラを
管理したくない
サービスを
スケールさせたい
コスト効率を上げたい
高可用性や
セキュリティ要件
を満たしたい
- 18. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved. 18
AWS Lambdaについて
- 19. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
AWS Lambda
19
- 20. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
(注意)Java Lambda Expressionのことではありません!
20
() -> System.out.println("Zero parameter lambda");
- 21. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
AWS Lambda
21
インフラの
プロビジョニング不要
自動でスケール
使った分だけの
お支払い
高可用性かつ安全
- 22. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. 22
WEBサーバー
アプリケーション
サーバー
HTTPS HTTP
これまでの環境
- 23. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. 23
HTTPS event
Amazon API Gateway AWS Lambda
Serverlessにすると
ロジック
ロジック
ロジック
- 24. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
動作の仕組み
24
f(x) = {…}
呼び出し元 処理対象
例・API呼び出し
・データ変更イベント
・ファイル配置イベント…
例・DBアクセス
・ファイル出力
・別の処理を呼び出し…
負荷に応じて
処理を多重化
リソース管理 リトライ ログ出力
g(x) = {…}
h(x) = {…}
- 25. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Sample Code
25
package example;
import example.Event;
import example.Response;
import com.amazonaws.services.lambda.runtime.Context;
public class Handler {
public Response myHandler(Event event, Context context) {
Response response = new Response();
String json = getJsonResponse(event);
response.setBody(json);
response.setStatusCode(200);
return response;
}
}
完全修飾クラス名::メソッド名
- 26. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Sample Code
26
この関数を呼び
出すサービス
Runtime言語の指定
Java 11 も選択可能
完全修飾クラス名::メソッド名
- 27. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
実はJava以外のRuntimeも利用できます。
27
- 28. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved. 28
ColdStart について
- 29. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. 29
Amazon API Gateway AWS Lambda
ColdStart と WarmStart
① ① ①
ColdStart
② ②
②
ColdStart
- 30. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. 30
Amazon API Gateway AWS Lambda
ColdStart と WarmStart
③ ③ ③
WarmStart
② ②
②
ColdStart
3 requests, 2 concurrency
- 31. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved. 31
でも、ColdStartってJavaだけ?
- 32. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
ColdStartを強制的に発生させる
32
for i in {1..100}
do
serverless deploy -f hello --force > /dev/null
curl -s -o /dev/null ¥
-w "%{time_starttransfer}¥n” <API URL>
done
- 33. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved. 33
Serverless Java チューニング
- 34. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Lambda の Life Cycle
34
Warm StartCold Start
コンテナ
生成
パッケージ
ロード
パッケージ
展開
ランタイム
起動
初期化
関数・メソッド
起動
デプロイ
- 35. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Lambda の Life Cycle
35
Warm StartCold Start
コンテナ
生成
パッケージ
ロード
パッケージ
展開
ランタイム
起動
初期化
関数・メソッド
起動
デプロイ
Zipサイズ最小化
遅延ロード
依存最小化
- 36. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
不要ライブラリを依存から排除
(Zipサイズ最小化+依存最小化)
36
dependencies {
runtimeOnly group: 'org.springframework’,
name: 'spring-core', version: '2.5'
runtimeOnly 'org.springframework:spring-core:2.5',
'org.springframework:spring-aop:2.5'
runtimeOnly(
[group: 'org.springframework', name: 'spring-core', version: '2.5'],
[group: 'org.springframework', name: 'spring-aop', version: '2.5']
)
runtimeOnly('org.hibernate:hibernate:3.0.5') {
transitive = true
}
runtimeOnly group: 'org.hibernate', name: 'hibernate', version: '3.0.5’,
transitive: true
runtimeOnly(group: 'org.hibernate', name: 'hibernate', version: '3.0.5') {
transitive = true
}
(snip)
少なくする
- 37. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
プレーンJavaで書く(さらに、Zipサイズ最小化)
37
public class Handler {
public Response myHandler(Map<String, Object> event,
Context context) {
Response response = new Response();
String json = getJsonResponse(event);
response.setBody(json);
response.setStatusCode(200);
return response;
}
}
- 38. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
AWS Lambdaのベストプラクティス
38
https://docs.aws.amazon.com/ja_jp/lambda/latest/dg/best-practices.html
- 39. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved. 39
プレーンJavaでは機能横断的な
要件を満たせないのでは?
- 40. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
機能横断的要件(あるいは非機能要件)
- 41. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
機能横断的要件(あるいは非機能要件) by Spring Boot
41
Spring Boot Actuator
ログ
Spring Security
認証認可
Layer7攻撃対策
パスワードポリシーメトリクス
トレース
FAT jar
- 42. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. 42
Amazon API Gateway AWS Lambda
FAT jarでLambdaを動かそうとすると
FAT jar
FAT jar
FAT jar
ColdStart Latencyが増大
- 43. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Jarを痩せ細らせたい
43
FAT jar
Skinny jar
アプリケーションロジック
依存ライブラリ
アプリケーションサーバー
- 44. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
機能横断的要件(あるいは非機能要件) by AWS Lambda
44
ログ 認証認可
Layer7攻撃対策
パスワードポリシーメトリクス
トレース
Skinny jar
AWS X-Ray AWS WAFAWS Lambda
Amazon CloudWatch Amazon Cognito
Amazon
API Gateway
ロジック
- 45. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved. 45
You aren't gonna need it
最初から機能追加すべきではない。
Martin Fowler
YAGNI
- 46. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
機能横断的要件(あるいは非機能要件) by AWS Lambda
46
ログ
メトリクス
AWS Lambda
Amazon CloudWatch
必要最低限から構築できる。
- 47. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. 47
Amazon API Gateway AWS Lambda
Skinny jarでLambdaを動かすと
ColdStartが抑制される
Skinny jar
Skinny ja
Skinny
- 48. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved. 48
それでもFrameworkを使いたい。
- 49. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
まずはプリミティブな構成から考えていく
49
AWS SDK for Java
最小構成
https://docs.aws.amazon.com/ja_jp/sdk-for-java/v2/developer-guide
- 50. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
アイデアとしては、
50
AWS SDK for Java
最小構成 + compile-time DI
https://dagger.dev/
Dagger は完全に静的な,
compile-time
dependency injection
framework
- 51. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
run-time dependency injection
51
Warm StartCold Start
コンテナ
生成
パッケージ
ロード
パッケージ
展開
ランタイム
起動
初期化
関数・メソッド
起動
デプロイ
run-time
通常のDI
o constructor
o static field
o handler
- 52. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
compile-time dependency injection
52
Warm StartCold Start
コンテナ
生成
パッケージ
ロード
パッケージ
展開
ランタイム
起動
初期化
関数・メソッド
起動
デプロイ
compile-time
- 53. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved. 53
これで満足?
- 54. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
最近のFrameworkの規模感
54
出典 https://dmitrykornilov.net/2018/09/07/helidon-takes-flight/
やっぱり手慣れた
Spring Bootで開発したい!
- 55. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved. 55
Spring Boot を Lambdaにのせる
- 56. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved. 56
FAT jar
Skinny jar
アプリケーションロジック
依存ライブラリ
アプリケーションサーバー
AWS SDK for Java
- 57. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved. 57
アプリケーションロジック
依存ライブラリ
アプリケーションサーバー
Thin jar
- 58. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved. 58
Thin jar = ロジック +
− アプリケーションサーバー
- 59. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved. 59
aws-serverless-java-container
- 60. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
aws-serverless-java-container
60
https://github.com/awslabs/aws-serverless-java-container
コ
ー
ドは
こ
ち
ら
- 61. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved. 61
https://aws.amazon.com/blogs/opensource/java-apis-aws-lambda
導
入
方
法
は
こ
ち
ら
- 62. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
アプリデプロイ(抜粋)
62
git clone
mvn package
sam package
sam deploy
AWS SAM
- 63. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
aws-serverless-java-container
63
Amazon
API Gateway
AWS Lambda
クライアント
aws-serverless-java-container
Request
Response
Event
HttpServletRequest
HttpServletResponse
Return Value
Thin jar No App Server
- 64. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
置き換え可能
対応Framework
64
AWS Lambda
aws-serverless-java-container
Thin jar No App Server
- 65. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved. 65
マイクロフレームワークで高速化
- 66. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
最近のFrameworkの規模感(再掲)
66
出典 https://dmitrykornilov.net/2018/09/07/helidon-takes-flight/
マイクロフレームワーク
- 67. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved. 67
compile-time
dependency injection
と AOP
GraalVM
に対応
宣言的
リアクティブ
- 68. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved. 68
GraalVMについて
- 69. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
GraalVMの簡単な紹介
69
• Oracle が開発している新たな JVM。JDK8 の代⽤として利⽤できる。
• Java 以外の⾔語も取り扱える Polyglot な JVM で、⾔語間通信ができる。
• ネイティブコードをバイナリとして⽣成できる。
-> Lambda で利⽤することで、Java のコールドスタートの軽減が⾒込まれる。
• ※AWS Lambdaのカスタムランタイムを利⽤したランタイム実装はAWSサ
ポート対象外
• マイクロサービス向けの Java フレームワーク (Micronaut, Quarkus 等) と相
性が良い。
-> メモリのフットプリントを抑えることができる。
• ※Micronaut, QuarkusはAWSサポート対象外
• 2019/05/09 に GA し Twitter が本番環境で採⽤。Community Edition と
Enterprise Edition がある。
• Community Edition のライセンスはクラスパス例外付きの GPL v2.0。
-> OpenJDK 等と同様に、利⽤のみであれば GPL は適⽤されない。
• ※GraalVMはOracle製品であり、AWSサポート対象外
- 70. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
GraalVMの簡単な紹介
70
実行可能な環境
提供する言語
https://www.graalvm.org/docs/
- 71. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
GraalVMの仕組み
71
LLVM言語
インタプリタ言語
- 72. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
HotSpot VM - これまでの標準的な JVM 実装
72
HotSpot VM
C1 C2
Java Source Code
AOT Compile
Java Byte Code
JIT Compile
(パフォーマンスに影響する部分を動的コンパイル)
Interpreter 実⾏
Native Code
javac
・C1 :
素早い起動が求められる GUI アプリ向けの
JIT コンパイラ (クライアントアプリ向け)
・C2 :
⻑時間実⾏されるサーバーサイド向けの
JIT コンパイラ
- 73. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
GraalVM 全体図
73
HotSpot VM
C1 JVMCI
Java Source Code
AOT Compile
Java Byte Code
Interpreter 実⾏
javac
Graal
Source Code
(インタプリタ⾔語)
Source Code
(LLVM 対応⾔語)
Sulong
AST Compile
LLVM Compile Truffle AST
Truffle 互換
Compiler
JIT Compile
Substrate VM
静的コンパイル
Native Code
- 74. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
GraalVM 全体図
74
HotSpot VM
C1 JVMCI
Java Source Code
AOT Compile
Java Byte Code
Interpreter 実⾏
javac
Graal
Source Code
(インタプリタ⾔語)
Source Code
(LLVM 対応⾔語)
Sulong
AST Compile
LLVM Compile Truffle AST
Truffle 互換
Compiler
JIT Compile
Substrate VM
静的コンパイル
Native Code
・Truffle :
Graal がインタプリタ⾔語を解釈するために満
たすべき要件をまとめたフレームワーク。
・Truffle AST :
Graal が解釈可能なインタプリタ⾔語の構造化
形式。
・Sulong :
LLVM の Bitcode を Truffle AST に変換する
プログラム。
- 75. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
GraalVM 全体図
75
HotSpot VM
C1 JVMCI
Java Source Code
AOT Compile
Java Byte Code
Interpreter 実⾏
javac
Graal
Source Code
(インタプリタ⾔語)
Source Code
(LLVM 対応⾔語)
Sulong
AST Compile
LLVM Compile Truffle AST
Truffle 互換
Compiler
JIT Compile
Substrate VM
静的コンパイル
Native Code
・Graal :
JVMCI 経由で JIT コンパイルを⾏う Java ベースの
プログラム。
・JVMCI (JVM Compiler Interface) :
HotSpot VM の JIT コンパイル関連リソースにアク
セスするためのインタフェース。
- 76. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
GraalVM 全体図
76
HotSpot VM
C1 JVMCI
Java Source Code
AOT Compile
Java Byte Code
Interpreter 実⾏
javac
Graal
Source Code
(インタプリタ⾔語)
Source Code
(LLVM 対応⾔語)
Sulong
AST Compile
LLVM Compile Truffle AST
Truffle 互換
Compiler
JIT Compile
Substrate VM
静的コンパイル
Native Code
・Substrate VM :
実⾏可能なネイティブコードにコンパイルすることがで
きる。そのため、事前に Java のバイトコードをバイナ
リ化することができる。
- 77. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
GraalVM ✖ Micronaut を取り込む
77
Warm StartCold Start
コンテナ
生成
パッケージ
ロード
パッケージ
展開
ランタイム
起動
初期化
関数・メソッド
起動
デプロイ
compile-time
- 78. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
JVM起動不要なNativeカスタムランタイム
78
AWS Lambda
aws-serverless-java-container
Native
Modules
ランタイムでは
や、ビジネス固有
のロジックは、
Nativeとして
実行される。
- 79. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
GraalVM ✖ Micronaut のデプロイ(抜粋)
79
git clone
gradle compile
docker build
sam package
sam deploy
AWS SAM
https://github.com/awslabs/aws-serverless-java-container/tree/master/samples/micronaut/pet-store
javac GraalVM
- 80. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
GraalVM ✖ SpringBoot はできないの?
80
Warm StartCold Start
コンテナ
生成
パッケージ
ロード
パッケージ
展開
ランタイム
起動
初期化
関数・メソッド
起動
デプロイ
compile-time
GraalVMの制約により現在は困難。
- 81. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
GraalVMの制約
81
項目 サポート
Dynamic Class Loading / Unloading Not supported
Reflection Requires Configuration
Dynamic Proxy Requires Configuration
InvokeDynamic Bytecode and Method
Handles
Not supported
Finalizers Not supported
Security Manager Not supported
https://github.com/oracle/graal/blob/master/substratevm/LIMITATIONS.md
- 82. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved. 82
まとめ
- 83. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Javaでサービスを作る場合
83
モノリス マイクロサービス
• 開発サイクルを高速にまわしたい
• バックエンドリソースを効率よく使いたい
- 84. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Javaでマイクロサービスを作る場合
84
SpringBoot FAT jar AWS Lambda
• サーバーを管理したくない。
• スケールを管理したくない。
- 85. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
AWS Lambda のランタイムをJavaにする場合
85
FAT jar Skinny jar
• ColdStartを抑制したい。
• ロジックにフォーカスしたい。
- 86. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
AWS Lambdaで フレームワークを使う場合
86
Docker Container
aws-serverless-
java-container
• 既存のSpringBoot資産を活かしたい。
• エンジニアはSpringBootでの開発に慣れている。
- 87. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
aws-serverless-java-containerを使う場合
87
javacで
バイトコードに
GraalVMで
Nativeに
• ColdStartの遅延を解消したい。
• 開発コードに手を入れたくない
- 88. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
AWS Lambdaで GraalVM を使う場合
88
SpringBoot Micronaut
• GraalVM対応フレームワークにしたい。
• DI / AOPも対応したい。
- 89. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
AWS Lambda + JAVA で Serverless開発しましょう!!
89
- 90. © 2019, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Thank you!
AWS Presents, Battle against Massive Load
using Your Super Sonic Lambda Function!
90
@_kensh