building microservices with kotlin and grpc marharyta
play

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


  1. 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 }

  2. 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 }

  3. 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 }

  4. Why don’t you use futures? 68

  5. Building microservices with Kotlin and gRPC 69

  6. 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

  7. 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

  8. 05 gRPC and coroutines II 72

  9. Building microservices with Kotlin and gRPC 73

  10. Building microservices with Kotlin and gRPC We need a new hero! grpc-kotlin 74

  11. Building microservices with Kotlin and gRPC 75

  12. 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

  13. 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

  14. Building microservices with Kotlin and gRPC 78

  15. Building microservices with Kotlin and gRPC We have another hero! kroto+ 79

  16. Building microservices with Kotlin and gRPC 80

  17. 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

  18. 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

  19. 06 More libs for Kotlin 83

  20. Building microservices with Kotlin and gRPC Expectation 84

  21. Building microservices with Kotlin and gRPC Reality 85

  22. Building microservices with Kotlin and gRPC https://github.com/rouzwawi/grpc-kotlin.git 86

  23. Building microservices with Kotlin and gRPC https://github.com/rouzwawi/grpc-kotlin.git 87

  24. Building microservices with Kotlin and gRPC https://github.com/rouzwawi/grpc-kotlin.git 88

  25. Building microservices with Kotlin and gRPC https://github.com/rouzwawi/grpc-kotlin.git 89

  26. Building microservices with Kotlin and gRPC https://github.com/rouzwawi/grpc-kotlin.git 90

  27. Building microservices with Kotlin and gRPC https://github.com/rouzwawi/grpc-kotlin.git 91

  28. Building microservices with Kotlin and gRPC https://github.com/marcoferrer/kroto-plus.git A year ago 92

  29. Building microservices with Kotlin and gRPC https://github.com/marcoferrer/kroto-plus.git 93

  30. Building microservices with Kotlin and gRPC https://github.com/marcoferrer/kroto-plus.git 94

  31. 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

  32. One day we’ll have a stable kotlin support 96

  33. We have a stable kotlin support! 97

  34. Time to summarize 98

  35. Building microservices with Kotlin and gRPC Takeaways ▪ Use gRPC for effective communications 99

  36. Building microservices with Kotlin and gRPC Takeaways ▪ Use gRPC for effective communications ▪ gRPC + Kotlin can be used today 100

Recommend


More recommend