Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
サーバサイドKotlinでgRPCをやってみよう
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Takehata Naoto
September 09, 2019
Programming
8k
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
サーバサイドKotlinでgRPCをやってみよう
2019年9月9日 「Kotlin Fest Reject Conference 2019」の発表資料です。
Takehata Naoto
September 09, 2019
More Decks by Takehata Naoto
See All by Takehata Naoto
AI時代でも変わらない技術コミュニティの力~10年続く“ゆるい”つながりが生み出す価値
n_takehata
2
1.4k
KotlinConf 2025で発表された言語のアップデートと現地参加レポート
n_takehata
2
410
KotlinConf 2025 現地で感じたServer-Side Kotlin
n_takehata
2
520
KotlinConf 2025 現地参加の土産話
n_takehata
0
230
組織貢献をするフリーランスエンジニアという生き方
n_takehata
1
6.8k
「2024年版 Kotlin サーバーサイドプログラミング実践開発」の補講 〜O/Rマッパー編〜
n_takehata
2
850
2024年版 Kotlin サーバーサイドプログラミング実践開発
n_takehata
9
9.2k
Server-Side目線で見る、Kotlin Festの楽しみ方
n_takehata
0
670
KotlinとCloud Vision APIで領収書の電子帳簿保存法対応をする
n_takehata
1
2.1k
Other Decks in Programming
See All in Programming
The Good Stuff, Not the Slop: Engineering High-Quality Android Apps with Modern AI Tooling
danybony
1
260
Family mrubyの進捗
kishima
1
130
thread_parallel_with_free-threaded_Python_and_NumPy.pdf
riku_sakamoto
0
350
20260914 AIエージェント時代のPlatform Engineering LLM基盤とプロダクトの責務境界線
kanfab1
7
2.2k
C#の現在地 進化の歴史と、AI時代の.NET Everywhere
neuecc
4
3.3k
すこし踏み込む CancellationToken
htkym
2
720
技術的負債を組織課題として解く-増えすぎたマイクロサービスとの戦い-
reimaru
1
2.2k
iOSDC2026登壇資料.pdf
riofujimon
0
170
Webの地図
yosuke_furukawa
PRO
6
4.7k
Omarchy Tokyo やると聞いて UMPC 買ってセットアップしてきた
mtsmfm
0
150
更なる可用性を求めて、5年間運用したKotlinのアプリケーションをGoでリプレイスする話
ken_tunc
0
330
AGENTS.md Is Not Enough:Build Skills, Don't Download Them
lx_t
0
130
Featured
See All Featured
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
Future Trends and Review - Lecture 12 - Web Technologies (1019888BNR)
signer
PRO
0
3.8k
Rails Girls Zürich Keynote
gr2m
96
14k
Building a Modern Day E-commerce SEO Strategy
aleyda
45
9.2k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
23k
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
290
How to build a perfect <img>
jonoalderson
1
6k
How People are Using Generative and Agentic AI to Supercharge Their Products, Projects, Services and Value Streams Today
helenjbeal
1
330
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
162
16k
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
35
2.8k
Accessibility Awareness
sabderemane
1
220
Transcript
サーバーサイドKotlinでgRPCをやってみよう ⽵端 尚⼈ @n_takehata 2019/9/9 Kotlin Fest Reject Conference 2019
⾃⼰紹介
概要 ⽵端 尚⼈ 所属:株式会社アプリボット 職種:バックエンドエンジニア 好きな⾔語:Kotlin Twitter:@n_takehata 2006.04 公務員 2007.12
SES 2011.01 サイバーエージェント 2014.04 アプリボット
登壇、執筆など • CEDEC 2018、2019登壇 https://speakerdeck.com/n_takehata/cedec- 2018 • Kotlin Fest 2018登壇(LT⼤会)
• 雑誌Software Design 2019年2〜4⽉号に てサーバーサイドKotlinについての短期連 載執筆
Kotlin Fest 2019に申し込んだテーマ
Kotlin × gRPCを使⽤した次世代のサーバーサイド開発
Rejected…
セッション⼀覧をよく⾒ると
gRPCよりGraphQLなのか︕︕
サーバーサイドKotlinでgRPCをやってみよう ⽵端 尚⼈ @n_takehata 2019/9/9 Kotlin Fest Reject Conference 2019
タイトルパクりました ※ @shiraj_iさんごめんなさい
今⽇はgRPCもいいなって 思ってもらいたいです︕
アジェンダ 1.gRPCとはなにか? 2.Protocol Buffersについて 3.サーバーサイドKotlinでgRPCを使うには
2019/9/9 gRPCとはなにか︖ 1
gRPCとは︖ • Google製のRPCフレームワーク • HTTP/2、Protocol Bufferesを標準でサポートし、ハイパ フォーマンスな通信を実現 • RESTに代わる通信⽅式の⼀つとしても期待されている
特徴
• Protocol Bufferesによるインターフェース定義共通化 • Java、C#、Goなど様々な⾔語に対応 • 各⾔語のコード⾃動⽣成が可能 • HTTP/2による⾼速で柔軟な通信
パフォーマンス(RESTとの⽐較)
平均リクエスト時間 (ms) 平均レスポンス時間 (ms) gRPC 2.92 3.6 REST 7.16 12.78
※2018年3⽉当時の検証結果です リクエストが約2倍、レスポンスが約4倍速かった (条件付き)
2019/9/9 Protocol Buffersについて 2
Protocol Buffersとは︖ • 通信のインターフェースを定義できるIDLの⼀種 • protocというコマンドで、定義ファイルからインターフェース に合わせた様々な⾔語のコードを⽣成できる (Java、C#、C++、Python、Go、Ruby、PHP、Dart) • 定義した構造のバイナリデータで通信する
サンプル
①リクエスト、レスポンスのパラメータを定義 ②APIのインターフェースを定義 .protoという拡張⼦で定義 service Greeter { rpc SayHello (HelloRequest) returns
(HelloReply); } message HelloRequest { string name = 1; } message HelloReply { string message = 1; } ① ②
使いたい⾔語を指定してコンパイル
protocコマンドの例(Golang) protoc --go_out=plugins=grpc:../pb hello.proto
⽣成されるコード • Messageのデータ構造に合わせたModelクラス • データのシリアライズ、デシリアライズ • gRPCのサーバー実装のBaseクラス(プラグイン必要) • gRPCのクライアントStub (プラグイン必要)
• etc…
コンパイルイメージ
⽣成したファイルの実⾏イメージ ⾃動⽣成のServer、Stubのプログラムを介して 通信を実装できる
通信部分の実装が容易に • 定義ファイルさえ書けば、パラメータのクラスなどを作成する ⼿間が不要 • サーバー、クライアント間の実装の齟齬も防げる • GraphQLやSwaggerとも通づる機構
2019/9/9 サーバーサイドKotlinでgRPCを使うには 2
KotlinでのgRPCを使うには • ⾃動⽣成のコードはJavaで作成する(Kotlinには⾮対応) • Spring Bootを使っている場合はgrpc-spring-boot-starterを使う のが⼿軽 • コード⽣成はprotocを直接使わず、Gradleプラグインで実⾏で きる
⼿順
Gradleにプラグイン、依存関係を追加 compile("io.github.lognet:grpc-spring-boot-starter:3.3.0") id("com.google.protobuf") version "0.8.9" ①Protocol Buffersをコンパイルするためのプラグイン ②Spring BootでgRPCを扱うためのStarter
コードジェネレータの設定 ①使⽤するprotocのライブラリ、バージョンを指定 ②gRPCのコードを⽣成するプラグインを指定 protobuf { protoc { artifact = "com.google.protobuf:protoc:3.5.1-1"
} plugins { grpc { artifact = "io.grpc:protoc-gen-grpc-java:${grpcVersion}" } } generateProtoTasks { all().each { task -> task.plugins { grpc } } } generatedFilesBaseDir = "$projectDir/src/" } ① ②
コンパイルタスクの実⾏ generateProtoタスクがprotocを実⾏ ./gradlew generateProto
ファイルが⽣成される • xxxOuterClass.java • service、messageで定義したクラスを含むクラス • シリアライズ、デシリアライズなどをやってくれる • xxxGrpc.java •
xxxOuterClassを使⽤してgRPCで通信するサーバー処理やクライアン トStubなどを含んだクラス
サーバー側のコードの実装 ①⾃動⽣成のBaseクラスを継承 ②gRPCのサービスとして起動時に読み込ませるアノテーション @GRpcService class GreeterService: GreeterGrpc.GreeterImplBase() { override fun
sayHello(request: HelloRequest, responseObserver: StreamObserver<HelloReply>) { val replyBuilder = HelloReply.newBuilder().setMessage("Hello " + request.name) responseObserver.onNext(replyBuilder.build()) responseObserver.onCompleted() } } ① ②
クライアント側のコードの実装 ①接続の設定 ②Stubのインスタンス⽣成 ③Stubのメソッド実⾏ fun execSayHello(name: String) { val request
= HelloRequest.newBuilder().setName(name).build() val channel = ManagedChannelBuilder.forAddress("localhost", 6565).build() val stub = GreeterGrpc.newBlockingStub(channel) stub.sayHello(request) } ① ② ③
まとめ
サーバーサイドKotlinでgRPCを使うには • Gradleにprotobufのプラグイン、grpc-spring-boot-starterの依存関係 を追加 • protoファイルを定義しコンパイル • ⽣成されたコードを継承し、アノテーションを付けて実装 参考: https://blog.takehata-engineer.com/entry/server-side-kotlin-grpc-project-
creation-to-start-confirmation
ご清聴ありがとうございました