Building microservices with Kotlin and gRPC The whole destroy method... override fun destroy(responseObserver: StreamObserver<Planets>): StreamObserver<DestroyPlanetRequest> { listeners .add(responseObserver) planetStub .getAllPlanets(Empty.getDefaultInstance(), object : StreamObserver<Planets> by DefaultStreamObserver() { override fun onNext(planets: Planets) { responseObserver.onNext( populateWithCoordinnates (planets)) } }) return object : StreamObserver<DestroyPlanetRequest> by DefaultStreamObserver() { override fun onNext(destroyPlanetRequest: DestroyPlanetRequest) { planetStub .removePlanet( RemovePlanetRequest { planetId = destroyPlanetRequest. planetId } , object : StreamObserver<PlanetServiceProto.RemovePlanetResponse> by DefaultStreamObserver() { override fun onNext(removePlanetResponse: PlanetServiceProto.RemovePlanetResponse) { if (removePlanetResponse. result ) { scoreStub .addScore( AddScoreRequest { userName = destroyPlanetRequest. userName toAdd = destroyPlanetRequest. weight } , object : StreamObserver<Empty> by DefaultStreamObserver() {}) logStub .destroyedPlanet(destroyPlanetRequest, object : StreamObserver<Empty> by DefaultStreamObserver() {}) planetStub .generateNewPlanet(Empty.getDefaultInstance(), object : StreamObserver<PlanetProto.Planet> by DefaultStreamObserver() { override fun onNext(planet: PlanetProto.Planet) { logStub .newPlanet(planet, object : StreamObserver<Empty> by DefaultStreamObserver() {}) listeners . forEach { it .onNext( Planets { addPlanets( populateWithCoordinates (planet, destroyPlanetRequest. coordinates . x , destroyPlanetRequest. coordinates . y )) } ) } } }) } } }) } } 65 }
Building microservices with Kotlin and gRPC The whole destroy method... override fun destroy(responseObserver: StreamObserver<Planets>): StreamObserver<DestroyPlanetRequest> { listeners .add(responseObserver) planetStub .getAllPlanets(Empty.getDefaultInstance(), object : StreamObserver<Planets> by DefaultStreamObserver() { override fun onNext(planets: Planets) { responseObserver.onNext( populateWithCoordinnates (planets)) } }) return object : StreamObserver<DestroyPlanetRequest> by DefaultStreamObserver() { override fun onNext(destroyPlanetRequest: DestroyPlanetRequest) { planetStub .removePlanet( RemovePlanetRequest { planetId = destroyPlanetRequest. planetId } , object : StreamObserver<PlanetServiceProto.RemovePlanetResponse> by DefaultStreamObserver() { override fun onNext(removePlanetResponse: PlanetServiceProto.RemovePlanetResponse) { if (removePlanetResponse. result ) { scoreStub .addScore( AddScoreRequest { userName = destroyPlanetRequest. userName toAdd = destroyPlanetRequest. weight } , object : StreamObserver<Empty> by DefaultStreamObserver() {}) logStub .destroyedPlanet(destroyPlanetRequest, object : StreamObserver<Empty> by DefaultStreamObserver() {}) planetStub .generateNewPlanet(Empty.getDefaultInstance(), object : StreamObserver<PlanetProto.Planet> by DefaultStreamObserver() { override fun onNext(planet: PlanetProto.Planet) { logStub .newPlanet(planet, object : StreamObserver<Empty> by DefaultStreamObserver() {}) listeners . forEach { it .onNext( Planets { addPlanets( populateWithCoordinates (planet, destroyPlanetRequest. coordinates . x , destroyPlanetRequest. coordinates . y )) } ) } } }) } } }) } } 66 }
Building microservices with Kotlin and gRPC The whole destroy method... override fun destroy(responseObserver: StreamObserver<Planets>): StreamObserver<DestroyPlanetRequest> { listeners .add(responseObserver) planetStub .getAllPlanets(Empty.getDefaultInstance(), object : StreamObserver<Planets> by DefaultStreamObserver() { override fun onNext(planets: Planets) { responseObserver.onNext( populateWithCoordinnates (planets)) } }) return object : StreamObserver<DestroyPlanetRequest> by DefaultStreamObserver() { override fun onNext(destroyPlanetRequest: DestroyPlanetRequest) { planetStub .removePlanet( RemovePlanetRequest { planetId = destroyPlanetRequest. planetId } , object : StreamObserver<PlanetServiceProto.RemovePlanetResponse> by DefaultStreamObserver() { override fun onNext(removePlanetResponse: PlanetServiceProto.RemovePlanetResponse) { if (removePlanetResponse. result ) { scoreStub .addScore( AddScoreRequest { userName = destroyPlanetRequest. userName toAdd = destroyPlanetRequest. weight } , object : StreamObserver<Empty> by DefaultStreamObserver() {}) logStub .destroyedPlanet(destroyPlanetRequest, object : StreamObserver<Empty> by DefaultStreamObserver() {}) planetStub .generateNewPlanet(Empty.getDefaultInstance(), object : StreamObserver<PlanetProto.Planet> by DefaultStreamObserver() { override fun onNext(planet: PlanetProto.Planet) { logStub .newPlanet(planet, object : StreamObserver<Empty> by DefaultStreamObserver() {}) listeners . forEach { callback hell it .onNext( Planets { addPlanets( populateWithCoordinates (planet, destroyPlanetRequest. coordinates . x , destroyPlanetRequest. coordinates . y )) } ) } } }) } } }) } } 67 }
Why don’t you use futures? 68
Building microservices with Kotlin and gRPC 69
Building microservices with Kotlin and gRPC Destroy method is override fun destroy(responseObserver: StreamObserver<PlanetProto.Planets>): StreamObserver<DestroyPlanetRequest> { listeners .add(responseObserver) GlobalScope. launch { val allPlanets = planetStub .getAllPlanets(Empty.getDefaultInstance()). await () responseObserver.onNext( populateWithCoordinnates (allPlanets)) } return object : StreamObserver<DestroyPlanetRequest> by DefaultStreamObserver() { override fun onNext(destroyPlanetRequest: DestroyPlanetRequest) { GlobalScope. launch { val removePlanet = planetStub .removePlanet( RemovePlanetRequest { planetId = destroyPlanetRequest. planetId } ). await () if (removePlanet. result ) { scoreStub .addScore( AddScoreRequest { userName = destroyPlanetRequest. userName toAdd = destroyPlanetRequest. weight } ) logStub .destroyedPlanet(destroyPlanetRequest) val newPlanet = planetStub .generateNewPlanet(Empty.getDefaultInstance()). await () logStub .newPlanet(newPlanet) listeners . forEach { it .onNext( Planets { addPlanets( populateWithCoordinates (newPlanet, destroyPlanetRequest. coordinates . x , destroyPlanetRequest. coordinates . y )) } ) } } } } } } 70
Building microservices with Kotlin and gRPC Destroy method is override fun destroy(responseObserver: StreamObserver<PlanetProto.Planets>): StreamObserver<DestroyPlanetRequest> { listeners .add(responseObserver) GlobalScope. launch { val allPlanets = planetStub .getAllPlanets(Empty.getDefaultInstance()). await () responseObserver.onNext( populateWithCoordinnates (allPlanets)) } return object : StreamObserver<DestroyPlanetRequest> by DefaultStreamObserver() { override fun onNext(destroyPlanetRequest: DestroyPlanetRequest) { GlobalScope. launch { val removePlanet = planetStub .removePlanet( RemovePlanetRequest { planetId = destroyPlanetRequest. planetId } ). await () if (removePlanet. result ) { scoreStub .addScore( AddScoreRequest { userName = destroyPlanetRequest. userName toAdd = destroyPlanetRequest. weight } ) logStub .destroyedPlanet(destroyPlanetRequest) val newPlanet = planetStub .generateNewPlanet(Empty.getDefaultInstance()). await () logStub .newPlanet(newPlanet) listeners . forEach { it .onNext( Planets { addPlanets( populateWithCoordinates (newPlanet, destroyPlanetRequest. coordinates . x , destroyPlanetRequest. coordinates . y )) } ) } } } } } } 71
05 gRPC and coroutines II 72
Building microservices with Kotlin and gRPC 73
Building microservices with Kotlin and gRPC We need a new hero! grpc-kotlin 74
Building microservices with Kotlin and gRPC 75
Building microservices with Kotlin and gRPC Destroy method is override suspend fun destroy(requests: ReceiveChannel<DestroyPlanetRequest>): ReceiveChannel<Planets> { 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 } 76
Building microservices with Kotlin and gRPC Destroy method is override fun destroy(requests: ReceiveChannel<DestroyPlanetRequest>): ReceiveChannel<Planets> { 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 } 77
Building microservices with Kotlin and gRPC 78
Building microservices with Kotlin and gRPC We have another hero! kroto+ 79
Building microservices with Kotlin and gRPC 80
Building microservices with Kotlin and gRPC Destroy method is override suspend fun destroy(requestChannel: ReceiveChannel<PlanetProto.DestroyPlanetRequest>, 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 )) } ) } } } } 81
Building microservices with Kotlin and gRPC Destroy method is override suspend fun destroy(requestChannel: ReceiveChannel<PlanetProto.DestroyPlanetRequest>, 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 )) } ) } } } } 82
06 More libs for Kotlin 83
Building microservices with Kotlin and gRPC Expectation 84
Building microservices with Kotlin and gRPC Reality 85
Building microservices with Kotlin and gRPC https://github.com/rouzwawi/grpc-kotlin.git 86
Building microservices with Kotlin and gRPC https://github.com/rouzwawi/grpc-kotlin.git 87
Building microservices with Kotlin and gRPC https://github.com/rouzwawi/grpc-kotlin.git 88
Building microservices with Kotlin and gRPC https://github.com/rouzwawi/grpc-kotlin.git 89
Building microservices with Kotlin and gRPC https://github.com/rouzwawi/grpc-kotlin.git 90
Building microservices with Kotlin and gRPC https://github.com/rouzwawi/grpc-kotlin.git 91
Building microservices with Kotlin and gRPC https://github.com/marcoferrer/kroto-plus.git A year ago 92
Building microservices with Kotlin and gRPC https://github.com/marcoferrer/kroto-plus.git 93
Building microservices with Kotlin and gRPC https://github.com/marcoferrer/kroto-plus.git 94
Building microservices with Kotlin and gRPC 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
One day we’ll have a stable kotlin support 96
We have a stable kotlin support! 97
Time to summarize 98
Building microservices with Kotlin and gRPC 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
Recommend
More recommend