Copenhagen Denmark
Building microservices with Kotlin and gRPC Marharyta Nedzelska
@jMargaritaN
Building microservices with Kotlin and gRPC Marharyta Nedzelska - - PowerPoint PPT Presentation
Building microservices with Kotlin and gRPC Marharyta Nedzelska @jMargaritaN Copenhagen Denmark Building microservices with Kotlin and gRPC Who am I? Marharyta Nedzelska Software Engineer @ Wix KKUG & KNight Kyiv, Devoxx
Copenhagen Denmark
@jMargaritaN
Who am I?
Building microservices with Kotlin and gRPC
Marharyta Nedzelska
2
One more thing you should know
Building microservices with Kotlin and gRPC
3
I like boxing! So think twice before asking tricky questions!
Let’s talk about gRPC Implement Service gRPC and coroutines gRPC Kotlin libs
4
5
6
Microservices Rock!
▪ Independent ▪ Scalable ▪ Best suitable tool ▪ Parallel development ▪ etc...
Building microservices with Kotlin and gRPC
7
8
The biggest issue in changing a monolith into microservices lies in changing the communication pattern.
9
The biggest issue in changing a monolith into microservices lies in changing the communication pattern.
Martin Fowler
10
11
Clients Servers
12
13
14
Building microservices with Kotlin and gRPC
“gRPC” means gRPC Remote Procedure Call
15
Building microservices with Kotlin and gRPC
16
Building microservices with Kotlin and gRPC
17
Building microservices with Kotlin and gRPC
In distributed computing, a remote procedure call (RPC) is when a computer program causes a procedure to execute in a different address space, which is coded as if it was a normal (local) procedure call.
https://en.wikipedia.org/wiki/Remote_procedure_call
18
Building microservices with Kotlin and gRPC
gRPC Features
19
Building microservices with Kotlin and gRPC
gRPC Features
▪ Use HTTP/2 20
Building microservices with Kotlin and gRPC
1991
HTTP 0.9
HTTP protocol evolution
1996
HTTP 1.0
1997
HTTP 1.1
21
Building microservices with Kotlin and gRPC
HTTP protocol evolution
Building microservices with Kotlin and gRPC
No progress here
2000 1999 1998 22
HTTP protocol evolution
And here
2003 2002 2001 23
Building microservices with Kotlin and gRPC
HTTP protocol evolution
And still no progress...
2006 2005 2004 24
Building microservices with Kotlin and gRPC
HTTP protocol evolution
Are they alive?
2009 2008 2007 25
Building microservices with Kotlin and gRPC
HTTP protocol evolution
I guess no...
2012 2011 2010 26
Building microservices with Kotlin and gRPC
HTTP protocol evolution
And suddenly!
2015 2014 2013 27
Building microservices with Kotlin and gRPC
HTTP protocol evolution
2015
HTTP/2
28
Building microservices with Kotlin and gRPC
I am alive!
gRPC Features
▪ Use HTTP/2
○
Single TCP connection
29
Building microservices with Kotlin and gRPC
gRPC Features
▪ Use HTTP/2
○
Single TCP connection
○
Bidirectional streaming
30
Building microservices with Kotlin and gRPC
gRPC Features
▪ Use HTTP/2
○
Single TCP connection
○
Bidirectional streaming
○
Flow control
31
Building microservices with Kotlin and gRPC
gRPC Features
▪ Use HTTP/2
○
Single TCP connection
○
Bidirectional streaming
○
Flow control
▪ Supports multiple languages
Building microservices with Kotlin and gRPC
32
gRPC Features
▪ Use HTTP/2
○
Single TCP connection
○
Bidirectional streaming
○
Flow control
▪ Supports multiple languages ▪ Binary, uses protobuf
Building microservices with Kotlin and gRPC
33
34
35
Building microservices with Kotlin and gRPC
+ ScalaPB
36
Building microservices with Kotlin and gRPC
37
Building microservices with Kotlin and gRPC
gRPC Discovered!
▪ Use HTTP/2
○
Single TCP connection
○
Bidirectional streaming
○
Flow control
▪ Supports multiple languages ▪ Binary, uses protobuf
Building microservices with Kotlin and gRPC
38
39
Protocol buffers are Google’s language-neutral, platform-neutral, extensible mechanism for serializing structured data — think XML, but smaller, faster, and simpler.
https://developers.google.com/protocol-buffers/
40
Building microservices with Kotlin and gRPC
Sample .proto file
Building microservices with Kotlin and gRPC
syntax = "proto3"; package helloworld; service Greeter { rpc SayHello (HelloRequest) returns (HelloReply) {} } message HelloRequest { string name = 1; } message HelloReply { string message = 1; }
41
Building microservices with Kotlin and gRPC
42
compiling...
Protobuf summary
▪ Type Safety
Building microservices with Kotlin and gRPC
43
Protobuf summary
▪ Type Safety ▪ No schema violations
Building microservices with Kotlin and gRPC
44
Protobuf summary
▪ Type Safety ▪ No schema violations
▪ Fast serialization/deserialization
Building microservices with Kotlin and gRPC
45
Protobuf summary
▪ Type Safety ▪ No schema violations
▪ Fast serialization/deserialization ▪ Backward compatibility
Building microservices with Kotlin and gRPC
46
Protobuf summary
▪ Type Safety ▪ No schema violations
▪ Fast serialization/deserialization ▪ Backward compatibility ▪ Human readability
Building microservices with Kotlin and gRPC
47
48
49
50
Building microservices with Kotlin and gRPC
Task for this session
▪ Write a “Death Star” ▪ Destroy as much planets as you can ▪ Tweet with #kotlinconf #gRPC and #Kotlin tags ▪ Greet winners at the end of our session! ▪ Enjoy!
Building microservices with Kotlin and gRPC
51
52
Building microservices with Kotlin and gRPC
When user joins ...
53
l
s
Building microservices with Kotlin and gRPC
When user destroys planet ...
54
l
s
55
First look at proto
syntax = "proto3";
package deathstar; import "planet.proto"; service DeathStarService { rpc Destroy (stream DestroyPlanetRequest) returns (stream Planets) {} }
Building microservices with Kotlin and gRPC
56
First look at proto
message Planet { int64 planetId = 1; string name = 2; int64 weight = 3; int32 img = 4; } message Planets { repeated Planet planets = 1; } message DestroyPlanetRequest { string userName = 1; int64 planetId = 2; int64 weight = 3; }
Building microservices with Kotlin and gRPC
57
Building microservices with Kotlin and gRPC
58
And we have generated
@javax.annotation.Generated( value = "by gRPC proto compiler (version 1.16.1)", comments = "Source: death-star.proto") public final class DeathStarServiceGrpc {...} public final class DeathStarProto {...} public final class PlanetProto {...} ...
Building microservices with Kotlin and gRPC
59
Building microservices with Kotlin and gRPC
60
Server is
class DeathStarServer (private val port: Int = 50051, private val serverBuilder: ServerBuilder<*> = ServerBuilder.forPort(port)) { private lateinit var server: Server fun start() { server = serverBuilder .addService(DeathStarServiceImpl()) .build() .start() println("Server started!") } }
Building microservices with Kotlin and gRPC
61
Building microservices with Kotlin and gRPC
62
Building microservices with Kotlin and gRPC
63
64
The whole destroy method...
Building microservices with Kotlin and gRPC
65
The whole destroy method...
Building microservices with Kotlin and gRPC
66
The whole destroy method...
Building microservices with Kotlin and gRPC
67
68
Building microservices with Kotlin and gRPC
69
Destroy method is
Building microservices with Kotlin and gRPC
70
Destroy method is
Building microservices with Kotlin and gRPC
71
72
Building microservices with Kotlin and gRPC
73
We need a new hero!
74
Building microservices with Kotlin and gRPC
Building microservices with Kotlin and gRPC
75
Destroy method is
val channel = Channel<Planets>() listeners.add(channel) channel.send(populateWithCoordinnates(planetStub.getAllPlanets())) for (request in requests) { val wasRemoved = planetStub.removePlanet(RemovePlanetRequest { planetId = request.planetId }) if (wasRemoved.result) { scoreStub.addScore(AddScoreRequest { userName = request.userName toAdd = request.weight }) logStub.destroyedPlanet(request) val newPlanet = planetStub.generateNewPlanet() logStub.newPlanet(newPlanet) listeners.forEach { it.send(Planets { addPlanets(populateWithCoordinates(newPlanet, request.coordinates.x, request.coordinates.y)) }) } } } return channel } Building microservices with Kotlin and gRPC
76
Destroy method is
val channel = Channel<Planets>() listeners.add(channel) channel.send(populateWithCoordinnates(planetStub.getAllPlanets())) for (request in requests) { val wasRemoved = planetStub.removePlanet(RemovePlanetRequest { planetId = request.planetId }) if (wasRemoved.result) { scoreStub.addScore(AddScoreRequest { userName = request.userName toAdd = request.weight }) logStub.destroyedPlanet(request) val newPlanet = planetStub.generateNewPlanet() logStub.newPlanet(newPlanet) listeners.forEach { it.send(Planets { addPlanets(populateWithCoordinates(newPlanet, request.coordinates.x, request.coordinates.y)) }) } } } return channel } Building microservices with Kotlin and gRPC
77
78
Building microservices with Kotlin and gRPC
We have another hero!
79
Building microservices with Kotlin and gRPC
Building microservices with Kotlin and gRPC
80
Destroy method is
responseChannel: SendChannel<PlanetProto.Planets>) { listeners.add(responseChannel) responseChannel.send(populateWithCoordinnates(planetStub.getAllPlanets())) for (request in requestChannel) { val wasRemoved = planetStub.removePlanet(RemovePlanetRequest { planetId = request.planetId }) if (wasRemoved.result) { scoreStub.addScore(AddScoreRequest { userName = request.userName toAdd = request.weight }) logStub.destroyedPlanet(request) val newPlanet = planetStub.generateNewPlanet() logStub.newPlanet(newPlanet) listeners.forEach { it.send(Planets { addPlanets(populateWithCoordinates(newPlanet, request.coordinates.x, request.coordinates.y)) }) } } } } Building microservices with Kotlin and gRPC
81
Destroy method is
responseChannel: SendChannel<PlanetProto.Planets>) { listeners.add(responseChannel) responseChannel.send(populateWithCoordinnates(planetStub.getAllPlanets())) for (request in requestChannel) { val wasRemoved = planetStub.removePlanet(RemovePlanetRequest { planetId = request.planetId }) if (wasRemoved.result) { scoreStub.addScore(AddScoreRequest { userName = request.userName toAdd = request.weight }) logStub.destroyedPlanet(request) val newPlanet = planetStub.generateNewPlanet() logStub.newPlanet(newPlanet) listeners.forEach { it.send(Planets { addPlanets(populateWithCoordinates(newPlanet, request.coordinates.x, request.coordinates.y)) }) } } } } Building microservices with Kotlin and gRPC
82
83
Expectation
Building microservices with Kotlin and gRPC
84
Reality
Building microservices with Kotlin and gRPC
85
86
https://github.com/rouzwawi/grpc-kotlin.git
Building microservices with Kotlin and gRPC
87
https://github.com/rouzwawi/grpc-kotlin.git
Building microservices with Kotlin and gRPC
88
https://github.com/rouzwawi/grpc-kotlin.git
Building microservices with Kotlin and gRPC
89
https://github.com/rouzwawi/grpc-kotlin.git
Building microservices with Kotlin and gRPC
90
https://github.com/rouzwawi/grpc-kotlin.git
Building microservices with Kotlin and gRPC
91
https://github.com/rouzwawi/grpc-kotlin.git
Building microservices with Kotlin and gRPC
92
Building microservices with Kotlin and gRPC
https://github.com/marcoferrer/kroto-plus.git
A year ago
93
Building microservices with Kotlin and gRPC
https://github.com/marcoferrer/kroto-plus.git
94
Building microservices with Kotlin and gRPC
https://github.com/marcoferrer/kroto-plus.git
Useful links
https://github.com/salesforce/grpc-java-contrib/ https://github.com/salesforce/reactive-grpc https://github.com/cretz/pb-and-k https://github.com/leveretka/grpc-death-star https://medium.com/@bimeshde/grpc-vs-rest-performance-simplified-fd35d01bbd4
95
Building microservices with Kotlin and gRPC
One day we’ll have a stable kotlin support
96
We have a stable kotlin support!
97
98
Takeaways
▪ Use gRPC for effective communications 99
Building microservices with Kotlin and gRPC
Takeaways
▪ Use gRPC for effective communications ▪ gRPC + Kotlin can be used today 100
Building microservices with Kotlin and gRPC
Takeaways
▪ Use gRPC for effective communications ▪ gRPC + Kotlin can be used today ▪ gRPC + Kotlin make things easier 101
Building microservices with Kotlin and gRPC
Takeaways
▪ Use gRPC for effective communications ▪ gRPC + Kotlin can be used today ▪ gRPC + Kotlin make things easier ▪ Waiting for 1.0 release, flows... 102
Building microservices with Kotlin and gRPC
Takeaways
▪ Use gRPC for effective communications ▪ gRPC + Kotlin can be used today ▪ gRPC + Kotlin make things easier ▪ Waiting for 1.0 release, flows...
▪ Feel free to contribute :)
103
Building microservices with Kotlin and gRPC
Takeaways
▪ Use gRPC for effective communications ▪ gRPC + Kotlin can be used today ▪ gRPC + Kotlin make things easier ▪ Waiting for 1.0 release, flows...
▪ Feel free to contribute :) ▪ Don’t use grpc!
104
Building microservices with Kotlin and gRPC
Takeaways
▪ Use gRPC for effective communications ▪ gRPC + Kotlin can be used today ▪ gRPC + Kotlin make things easier ▪ Waiting for 1.0 release, flows...
▪ Feel free to contribute :) ▪ Don’t use grpc! (when don’t need it)
105
Building microservices with Kotlin and gRPC
106
107
108
Without them I wouldn’t have done it!
Building microservices with Kotlin and gRPC
Ray Tsang Rouzbeh Delavari Marco Ferrer
109
Without them I wouldn’t have done it!
Building microservices with Kotlin and gRPC
Alex Borysov Andrii Petryk George Lucas
#KotlinConf
Marharyta Nedzelska @jMargaritaN