Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Microservicios con Kotlin y MicroProfile

Microservicios con Kotlin y MicroProfile

En esta presentación exploramos el uso de Kotlin en el contexto de Java EE y MicroProfile, mezclando la estabilidad de estandares de 20 años ampliamente aceptados por la industria, con la innovación de un lenguaje JVM de popularidad reciente.

Víctor Orozco

May 30, 2019
Tweet

Other Decks in Technology

Transcript

  1. Ense˜ nando trucos de 20 a˜ nos a Kotlin V´

    ıctor Orozco 30 de mayo de 2019 @tuxtor 1
  2. Kotlin • Tipado est´ atico y compilado • Java inter-op

    • OO + FP • Null safety • Extensions • Operator overloading • Data classes • One line methods 2
  3. Kotlin - Datos interesantes • Effective Java - Inmutabilidad, builder,

    singleton, override, final by default, variance by generics • Elvis - Groovy • Inferencia de tipos - Scala • Inmutabilidad - Scala • Declaraci´ on de variables - Scala • Manejo de Null - Groovy • Closures y funciones - Groovy • Google 3
  4. Java - Muriendo desde 1995 • Sistemas legados (IBM) •

    Retrocompatibilidad • Release cadence (6 meses) • Innovaci´ on constante en el ecosistema (Spring Boot, Micronaut, MicroProfile, GraalVM) • Raw performance (Beam, Spark, Hadoop) • Tooling - IDE, Maven, Drivers RDBMS • JVM - (Twitter, Alibaba) • OpenJDK 4
  5. Microservicios - Java • DIY - Jooby, Javalin, Micronaut, Spark,

    Vert.x, Helidon SE • Enterprise - Spring Boot, Microprofile (implementaciones) 9
  6. Microservicios - Kotlin • DIY - Jooby, Javalin, Micronaut, Spark,

    Vert.x, Helidon SE, Ktor • Enterprise - Spring Boot, Microprofile (implementaciones) 10
  7. 11

  8. Eclipse MicroProfile - Implementaciones Bibliotecas • SmallRye (Red Hat) •

    Hammock • Apache Geronimo • Fujitsu Launcher JEAS - Fat Jar, Uber Jar • Dropwizard • KumuluzEE • Helidon (Oracle) • Open Liberty (IBM) • Apache Meecrowave • Thorntail (Red Hat) • Quarkus (Red Hat) • Payara Micro 17
  9. Eclipse MicroProfile - Implementaciones Micro server - Thin War •

    Payara Micro • TomEE JAX-RS Full server • Payara Application Server • JBoss Application Server / Wildfly Application Server • WebSphere Liberty (IBM) https://wiki.eclipse.org/MicroProfile/Implementation 18
  10. Eclipse MicroProfile en Payara 5 <dependency > <groupId >org.eclipse.microprofile </

    groupId > <artifactId >microprofile </ artifactId > <type >pom </type > <version >2.0.1 </ version > <scope >provided </scope > </dependency > 19
  11. Kotlin en Maven - Dependencias <dependency > <groupId >org.jetbrains.kotlin </groupId

    > <artifactId >kotlin -stdlib -jdk8 </artifactId > <version >${ kotlin.version}</version > </dependency > 20
  12. Kotlin en Maven - maven-compiler-plugin <execution > <id >default -compile

    </id > <phase >none </phase > </execution > <execution > <id >default -testCompile </id > <phase >none </phase > </execution > <execution > <id >java -compile </id > <phase >compile </phase > <goals > <goal >compile </goal > </goals > </execution > <execution > <id >java -test -compile </id > <phase >test -compile </phase > <goals > <goal >testCompile </goal > </goals > </execution > 21
  13. Kotlin en Maven - kotlin-maven-plugin <c o m p i

    l e r P l u g i n s> <plugin>a l l −open</plugin> </c o m p i l e r P l u g i n s> . . . <option>a l l −open : annotation=j av a x . ws . r s . Path</option> <option>a l l −open : annotation=j av a x . e n t e r p r i s e . context . RequestScoped</option> <option>a l l −open : annotation=j av a x . e n t e r p r i s e . context . SessionScoped </option> <option>a l l −open : annotation=j av a x . e n t e r p r i s e . context . ApplicationScoped </option> <option>a l l −open : annotation=j av a x . e n t e r p r i s e . context . Dependent</option> <option>a l l −open : annotation=j av a x . e j b . Singleton </option> <option>a l l −open : annotation=j av a x . e j b . S t a t e f u l </option> <option>a l l −open : annotation=j av a x . e j b . S t a t e l e s s </option> 22
  14. Kotlin + Jakarta EE + MicroProfile - Demo • Kotlin

    1.3 • Bibliotecas externas - SL4J, Flyway, PostgreSQL • Jakarta EE 8 - EJB, JPA • MicroProfile - CDI, JAX-RS, MicroProfile config • Testing - Arquillian, JUnit, Payara Embedded https://dzone.com/articles/ the-state-of-kotlin-for-jakarta-eemicroprofile-tra https://github.com/tuxtor/integrum-ee 23
  15. Kotlin - Entidad JPA @Entity @Table(name = "adm_phrase") @TableGenerator (...)

    data class AdmPhrase( @Id @GeneratedValue (strategy = GenerationType .TABLE , generator = " admPhraseIdGenerator ") @Column(name = "phrase_id") var phraseId:Long? = null , var author:String = "", var phrase:String = "" ) Data Clases, Nullable Types 26
  16. Kotlin - Repositorio CDI @RequestScoped class AdmPhraseRepository { @Inject private

    lateinit var em:EntityManager ... } Lateinit (nullable type) 27
  17. Kotlin - Repositorio CDI fun create(admPhrase:AdmPhrase) = em.persist(admPhrase) fun update(admPhrase:AdmPhrase)

    = em.merge(admPhrase) fun findById(phraseId: Long) = em.find(AdmPhrase :: class.java , phraseId) fun delete(admPhrase: AdmPhrase) = em.remove(admPhrase) . . . Single expression functions (One line methods) 28
  18. Kotlin - Repositorio CDI fun listAll(author: String , phrase: String

    ): List <AdmPhrase > { val query = """SELECT p FROM AdmPhrase p where p.author LIKE :author and p.phrase LIKE :phrase """ return em.createQuery(query , AdmPhrase :: class.java) .setParameter("author", " %$author %") .setParameter("phrase", " %$phrase %") .resultList } Multiline String, mutable declaration 29
  19. Kotlin - Controlador JAX-RS @Path ( "/phrases" ) @Produces (

    MediaType . APPLICATION JSON) @Consumes ( MediaType . APPLICATION JSON) c l a s s AdmPhraseController { @Inject p r i v a t e l a t e i n i t var admPhraseRepository : AdmPhraseReposit @Inject p r i v a t e l a t e i n i t var l o g g e r : Logger . . . } 30
  20. Kotlin - Controlador JAX-RS @GET fun f i n d

    A l l ( @QueryParam ( "author" ) @DefaultValue ( " %" ) author : String , @QueryParam ( "phrase" ) @DefaultValue ( " %" ) phrase : S t r i n g ) = admPhraseRepository . l i s t A l l ( author , phrase ) @GET @Path ( "/{id :[0 -9][0 -9]*}" ) fun f i n d B y I d ( @PathParam ( "id" ) i d : Long ) = admPhraseRepository . f i n d B y I d ( i d ) @PUT fun c r e a t e ( phrase : AdmPhrase ) : Response { admPhraseRepository . c r e a t e ( phrase ) return Response . ok ( ) . b u i l d () } 31
  21. Kotlin - Controlador JAX-RS @POST @Path ( "/{id :[0 -9][0

    -9]*}" ) fun update ( @PathParam ( "id" ) i d : Long ? , phrase : AdmPhrase ) : Response { i f ( i d != phrase . p h r a s e I d ) return Response . s t a t u s ( Response . Status .NOT FOUND) . v a l updatedEntity = admPhraseRepository . update ( phrase ) return Response . ok ( updatedEntity ) . b u i l d ( ) } @DELETE @Path ( "/{id :[0 -9][0 -9]*}" ) fun d e l e t e ( @PathParam ( "id" ) i d : Long ) : Response { v a l updatedEntity = admPhraseRepository . f i n d B y I d ( i d ) ? : return Response . s t a t u s ( Response . Status .NOT FOUND) . b u i l d () admPhraseRepository . d e l e t e ( updatedEntity ) return Response . ok ( ) . b u i l d () } Elvis operator as expression 32
  22. 12 factores cloud native (Heroku) Microprofile • Config • Backing

    service • Disposability Cloud • Codebase (Git-Flow) • Dependencies (Maven) • Build, Release, Run • Processes (Pipelines) • Port binding • Concurrency (Docker - k8s) • Dev / Prod parity • Logs • Admin process 33
  23. Oracle Cloud <groupId>i o . f a b r i

    c 8</ groupId> <a r t i f a c t I d>docker−maven−p l u g i n</ a r t i f a c t I d> <version>0 . 3 0 . 0</version> . . . <image> <name>i a d . o c i r . i o / t u x t o r / m i c r o p r o f i l e / integrum−ee</name> <b u i l d> <d o c k e r F i l e>${ p r o j e c t . b a s e d i r }/ D o c k e r f i l e</ d o c k e r F </ b u i l d> </image> 34
  24. Kotlin Ventajas • C´ odigo m´ as conciso • Soporte

    real Java inter-op • Aprovechar a personal Android para backend • Un lenguaje para dominar todo Desventajas • IntelliJ IDEA Ultimate (monolitos) • Requiere mejores programadores (m´ as convenciones) • Tiempo de compilaci´ on • No es una buena idea utilizar corutinas en entornos con managed threads 39
  25. V´ ıctor Orozco • [email protected] • @tuxtor • http://www.nabenik.com This

    work is licensed under a Creative Commons Attribution-ShareAlike 3.0. 40