LINE における Spring
Framework の活⽤用
Tokuhiro Matsuno
LINE Corp.
Service development 1
お前誰よ?
• LINE Corp. サービス開発1室
• LINE Bot SDK 開発まとめ
• Java版実装
• LINE Notify 開発
LINE って Java のイ
メージありますか??
結構使われてるんですよ。
↓
どのぐらい使われているのか?
↓
github enterprise を
検索してみた
CSS
4%
HTML
4%Clojure
5%Perl
5%
Objective-C
6%
Shell
9%
Python
10%
JavaScript
18%
Java
41%
"org.springframework spring-
webmvc" で検索: 547
"spring-boot-starter-web" で検索: 285
Why Spring?
• 開発のサイクルが(Java EE に⽐比べると)速い
• Java EE は B2C のスケールが必要な Web サー
ビスでの事例例をあまり聞かない
• 社内に Java EE の経験ある⼈人が少ない
Microservices
• 時代は Microservices
• ⼩小さな application server の組み合わせでおおき
なサービスを実現
→ ⾔言語/フレームワークはある程度柔軟に
⾔言語/フレームワークを
締め付けすぎない
• ⼀一つの技術にのみコミットしすぎると変化に弱
くなる
• spring, guice, Vert.x, Dagger2, avans,etc.
• groovy, scala, …
• golang, perl, python, …
There’s
more than one way
to do it
spring-boot は Microservice
で使うには良いパーツ
なぜ spring-boot か?
• uber jar でお⼿手軽に deploy できる
• アプリケーションの開発がわりと簡単に開始で
きる
• XML 書かなくていい
• Java のわりに 起動が速い
開発事例例: LINE Notify
LINE Notify
curl -X POST 
-H 'Authorization: Bearer <ACCESS_TOKEN>'
-F 'message=Hello'
https://notify-api.line.me/api/notify
LINE Notify サーバー構成
MySQL MySQL
API 1
API 2
Web 1
Web 2
Redis Redis
MySQL
• MySQL 5.6
• InnoDB
• MMM
• 速い
• 安い
• うまい
Redis
• Redis sentinel
• KVS は基本的に Redis が
⼈人気
• 柔軟性
• パフォーマンス
• 安定性
• 楽しい
• Cache
• Rate limit
nginx
• ACL 管理理
• rewrite
• Access log
nginx jetty
nginx jetty
nginx jetty
Jetty
• spring-boot-starter-jetty
• コード読みやすい
• 起動速い
• 開発時に便便利利
• 開発時と本番で違う app server 使うとかイヤ
• undertow よりも開発者多い
LINE ♥ Fluentd
• アクセスログの集計
• http status コードの統計監視
• イベントログ
• Fluency が最近は優勢
• 社内のおっきなストレージに保存
spring-boot 1.4.0.RELEASE
• spring-boot-starter-
web
• spring-boot-starter-
freemarker
• spring-boot-starter-
data-redis
• spring-boot-starter-
security
• spring-boot-starter-
mobile
• mybatis-spring-boot-
starter
spring-boot-starter-web
• 安定している
• @GetMapping, @PostMapping とか最近はいっ
て便便利利
• 特に語ることがない。
spring-boot-starter-
freemarker
• freemarker 便便利利
• 社内では thymeleaf と⼈人気を⼆二分している。気が
する。
• IDEA の補完が freemarker の⽅方が便便利利という説も?
• 最近 Apache Incubator ⼊入り。
• 最近、⾃自動エスケープ⼊入った。(spring.ftl issue)
spring-boot-starter-redis
• ⼊入れたら普通に使える
• 特に問題なし。
spring-boot-starter-mobile
• スマホかどうかを User-Agent で判定するのに利利
⽤用
• ちょっと前だとタブレットと判定するかどうか
みたいなのにうるさい⼈人がいたけど、最近はこ
れぐらい雑でも⼤大丈夫。
mybatis-spring-boot-starter
• 社内の O/R Mapper は mybatis 優勢
• JPA は少ない
• B2C でトラフィック予測難しい
• パフォーマンス問題起きたときの、MyBatis の安⼼心
感。
• MyBatis の XML をアップすると DBA がクエリ評価し
てくれるサービス
MyBatis + Kotlin
• XML ⾟辛い
• Java アノテーションでも書けるけど⾟辛い
• 複数⾏行行⽂文字列列書けない → 複雑なクエリが⾟辛い
• そこで Kotlin ですよ!
@Mapper
interface BlogDao {
@Select("""
SELECT * FROM blog
""")
fun findAll(): List<Blog>
@Select("""
SELECT * FROM blog WHERE id=#{id}
""")
fun findById(@Param("id") id: Long): Blog
なんで kotlin?
• ぶっちゃけ groovy でも良かった
• でも、時代の趨勢的に kotlin かなって
プロジェクト
モジュール構成
line-notify-common
• application.yml
• logback-spring.xml
line-notify-api
line-notify-admin
line-notify-webline-notify-test
line-notify-model
Deploy & CI
at LINE
Deploy
• PMC という内製ツールでデプロイ
• Shell script で rsync して再起動する Web UI
• (規模が⼤大きいのでオンプレです)
デプロイ⽅方法
github jenkins
nexus PMC
App
server
Build jar &
testing
upload jar
実⾏行行環境
• supervisord
• java
• nginx
⾃自動再起動
ブランチ構成は git flow?
master
beta
Topic
LINEにおける
アプリケーション
モニタリング
Prometheus
• クエリが使える TSDB
• 1サーバーで千台規模を監視可能
• Pull型
Prometheus アーキテクチャ
Prometheus
node_exporter
jmx_exporter
java
app
node_exporter
jmx_exporter
java
app
httpd
Pull
grafana
node_exporter
jmx_exporter
Dropwizard metrics
• アプリケーション固有のメトリクス値を取得す
るのに利利⽤用
• jmx_exporter 経由で prometheus へ
LINE Bot
+
Spring Boot
LINE Messaging API
• LINE のボットを簡単に作れる
• 最⼤大優勝賞⾦金金1000万円の LINE BOT AWARDS も
やってる
• Java + spring-boot なら簡単につくれる
LINE Messaging SDK for
Java
• JSON をマッピングするためのクラス
• HTTP Client library based on Retrofit
• Servlet support
• spring-boot support
なぜ spring-boot をサポート
しているのか
• 設定が簡単
• とりあえずすぐ起動できる
• 使ってる⼈人が多い
• AutoConfiguration まわりのサポートが簡単にで
きる
spring-boot supports
auto-configuration
META-INF/spring.factories
org.springframework.boot.autoconfigure.Enabl
eAutoConfiguration=
com.linecorp.bot.spring.boot.LineBotAutoConf
iguration,com.linecorp.bot.spring.boot.LineB
otWebMvcConfigurer
どのぐらい簡単に作れ
るのか?
依存を設定する
<dependency>
    <groupId>com.linecorp.bot</groupId>
    <artifactId>line-bot-spring-boot</artifactId>
    <version>1.3.0</version>
</dependency>
@SpringBootApplication
public class EchoApplication {
public static void main(String[] args) {
SpringApplication.run(
EchoApplication.class, args);
}
}
@LineMessageHandler
public class EchoHandler {
@Autowired
private LineMessagingService lineMessagingService;
@EventMapping
public void handle(MessageEvent<TextMessageContent> event) throws
Exception {
lineMessagingService.replyMessage(new ReplyMessage(
event.getReplyToken(), singletonList(
new TextMessage(event.getMessage().getText()))))
.execute();
}
}
jjug ccc 2016 fall で
LINE Bot の live coding やり
ます
ご清聴ありがとうございました。
懇親会までいるのでお気軽にお声が
けください。
Bot についてのご質問にもお答えで
きます。
We’re hiring.

Lineにおけるspring frameworkの活用