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

Migrating Spring Boot to Quarkus, Challenge Accepted!

Migrating Spring Boot to Quarkus, Challenge Accepted!

Many development teams using Spring Boot are evaluating Quarkus as an alternative. They come for the speed and they stay for the productivity. We will show an example of how to migrate the Spring Boot sample app, "Pet-Clinic" (REST), to Quarkus and how the lessons learned landed in the Migration Toolkit for Applications to make life easier for other developers doing this migration.

Red Hat Livestreaming

December 15, 2020
Tweet

More Decks by Red Hat Livestreaming

Other Decks in Technology

Transcript

  1. Migrating
    QUARKUS

    View Slide

  2. Contents
    1. who are we?
    2. what can you expect?
    3. what is quarkus?
    4. why migrate?
    5. migration
    a. application description
    b. steps
    6. performance comparative
    7. references

    View Slide

  3. $ whoami
    @vilojona
    [email protected]
    aytartana.wordpress.com
    github.com/jonathanvila
    Java Champion
    JUG co-leader at BarcelonaJUG
    Co-founder of JBCNConf
    Software Engineer at Red Hat
    App. Modernisation and Migration team

    View Slide

  4. About:
    4
    Miguel Pérez Colino
    ● Product Manager for MTA
    ● ~9 years in Red Hat
    ○ Consultant (~3)
    ○ Solution Architect (~2)
    ● Technical Project Leader
    ● IT Architect
    ● Systems Engineer
    ● SysAdmin
    @mmmmmmpc
    ?

    View Slide

  5. What can you expect ?
    My experience
    v 2.2.5
    v 1.7.0
    opinionated option
    magistral lecture
    something that works
    THE way
    QUARKUS

    View Slide

  6. Quarkus brief description

    View Slide

  7. What is Quarkus ?
    A Kubernetes Native, Java, Scala, Kotlin stack
    tailored for GraalVM and OpenJDK HotSpot
    crafted from the best of breed Java libraries and standards.

    View Slide

  8. 300x faster 10x smaller focused on easy to code
    opensource fast paced releases tons of extensions
    Quarkus key facts

    View Slide

  9. Quarkus extensions ( > 90 )
    Kafka
    Camel
    Keycloak
    Spring
    Vert.x
    RestEasy
    ….
    Microprofile
    Openshift
    Jaegger
    Flyway
    Hibernate
    Infinispan
    ...

    View Slide

  10. Quarkus evolution
    2018-12 v 0.0.1
    2019-06 v 0.15.0
    2019-11 v 1.0.0 Release
    2020-01 v 1.2.0
    1 minor version every 2 weeks !!! (+/-)

    View Slide

  11. Quarkus performance

    View Slide

  12. Quarkus OSS status

    View Slide

  13. Migration

    View Slide

  14. Why to migrate
    My experience
    easy to develop with an easy CDI, REST, …
    long startup times, eternal tests
    heavyweight
    too many things happen under the hood
    easy to develop applications
    fast
    lightweight
    GraalVM compatible
    QUARKUS
    ✅ FaaS compatible ( lambdas )
    ✅ less money on public cloud

    View Slide

  15. Application migrated
    Spring Data ( repositories, jdbc , ... )
    Spring Web ( REST )
    Spring Security
    Spring documentation ( Swagger )
    Spring actuators
    Spring micrometer
    Spring CDI
    Spring AOP
    Spring cache
    Hibernate Panache
    JaxRS
    Quarkus Security
    OpenAPI
    SmallRye Health
    Microprofile Metrics
    CDI Spec ( Arc )
    ---
    Quarkus cache ( Caffeine )
    QUARKUS

    View Slide

  16. Elements not migrated
    Spring JDBC querying
    * no helper methods to work with Inserts, Updates….
    * no equivalent to org.springframework.jdbc.core classes
    * we need to reimplement everything using AGROAL
    JMX
    * not supported by GraalVM definition

    View Slide

  17. Migration Steps

    View Slide

  18. CDI
    ● Replace Autowired by Inject
    ● Beans declaration using @ApplicationScoped
    ● Auto injection on Constructors
    ● Lazy by default

    View Slide

  19. CDI
    PROs
    ● straight forward migration for Beans
    ● no feature missing
    ● we also can annotate the beans to be injected depending on
    the profile or a config property
    CONs
    ● no private members on injection

    View Slide

  20. JPA Repositories
    ● Repository classes to implement PanacheRepositoryBase
    Quarkus will generate the implementation of the usual methods :
    list , find, persist, delete

    View Slide

  21. JPA Repositories
    PROs
    ● straightforward migration
    CONs
    ● no Query DSL methods

    View Slide

  22. Spring REST to JAX-RS
    ● Move from Spring REST to the standard JAX-RS
    @RestController
    @RequestMapping, @GetMapping,
    @PostMapping, @PutMapping,
    @DeleteMapping
    @PathVariable
    @Path
    @GET,
    @POST, @PUT
    @DELETE
    @PathParam
    QUARKUS

    View Slide

  23. Spring REST to JAX-RS
    PROs
    ● straightforward migration
    INFO
    ● Spring also supports JAX-RS as 3rd party lib

    View Slide

  24. Spring REST Security to Quarkus Security
    ● replaced Spring REST Security

    View Slide

  25. Spring REST JDBC Security to Elytron JDBC realm
    Add extension dependency : elytron-security-jdbc
    Configure on properties file and remove configureGlobal method

    View Slide

  26. Spring REST Security to Quarkus security
    PROs
    ● Almost the same result
    ● By configuration rather than code
    CONs
    ● Quarkus doesn’t have an expression language

    View Slide

  27. CORS
    ● configured CORS in the application.properties file

    View Slide

  28. CORS
    PROs
    ● easy to enable , no code involved
    CONs
    ● at global level, not per Controller

    View Slide

  29. Metrics
    ● Add smallRye metrics extension
    ● Annotate each method ( for custom metrics )
    Micrometer compatibility
    quarkus.smallrye-metrics.micrometer.compatibility=true

    View Slide

  30. Metrics
    PROs
    ● OpenMetrics standard naming or Micrometer,
    you choose
    ● You can even use Micrometer extension
    CONs
    ● no AOP so you need to annotate every
    method

    View Slide

  31. Validation
    ● Spring Validation to Hibernate Validator
    ● Move from @ControllerAdvice to JAX-RS ExceptionMapper

    View Slide

  32. Validation
    PROs
    ● standard
    ● easy
    CONs
    ● none

    View Slide

  33. Swagger ( OpenAPI v 3.0 )
    ● Add extension “openapi”
    ● extend Application

    View Slide

  34. Swagger ( OpenAPI v 3.0 )
    PROs
    ● straightforward migration
    ● standard and easy way to configure
    ● Swagger-UI OOTB
    CONs
    ● not REST paths scan configuration

    View Slide

  35. AOP for app metrics
    ● use of microprofile metrics annotations

    View Slide

  36. AOP for repository metrics
    ● using Hibernate built in metrics enabled in properties

    View Slide

  37. AOP for app metrics
    PROs
    ● standard
    ● easy to customise
    CONs
    ● need to annotate ALL methods one by one for
    custom app metrics
    ● no expression language to define methods affected

    View Slide

  38. Local caching
    ● Spring uses a default ConcurrentHashMap
    ● Caffeine uses a ConcurrentLinkedHashMap
    ● Add “cache” extension ( Caffeine ) and annotate the method

    View Slide

  39. Local Caching
    PROs
    ● easy to implement and configure
    ● straightforward migration
    CONs
    ● none

    View Slide

  40. Test : REST
    MockMVC -> RestAssured

    View Slide

  41. Test REST
    PROs
    ● RESTassured is easy to handle and very intuitive
    CONs
    ● No way to simulate Roles without creating an user for each role to test
    INFO
    ● Changed JSonPath to GPath

    View Slide

  42. Test - resources
    ● Annotate test suites with @QuarkusTest to boot the app
    ● Annotate test suites with @QuarkusTestResource to load an embedded
    resource

    View Slide

  43. Test - Mock
    ● Use the quarkus-mockito dependency : quarkus-junit5-mockito
    ● Annotate mocks with @InjectMock

    View Slide

  44. Spring API in Quarkus

    View Slide

  45. Spring WEB ( REST) : spring-web extension
    Using same Spring API for REST Supported Annotations
    @RestController
    @RequestMapping
    @GetMapping
    @PostMapping
    @PutMapping
    @DeleteMapping
    @PatchMapping
    @RequestParam
    @RequestHeader
    @MatrixVariable
    @PathVariable
    @CookieValue
    @RequestBody
    @ResponseStatus
    @ExceptionHandler

    View Slide

  46. Spring DI : spring-di extension
    Using same API as Spring DI
    Annotations Supported
    @Autowired @Repository
    @Qualifier @Scope
    @Value @Component
    * doesn’t support an expression language
    @Configuration @Service
    @Bean

    View Slide

  47. Spring Data : spring-data-jpa
    Using same API for Spring Data Supported
    Any interface of : Repository,
    CrudRepository, PagingAndSortingRepository,
    JpaRepository
    Derived Query Methods
    User defined Queries
    Not Supported
    Invocations to any method of
    QueryByExampleExecutor
    QueryDSL

    View Slide

  48. Spring Security: spring-security extension
    Using same API as Spring Security
    Annotations Supported
    @Secured
    @Preauthorized
    Expressions allowed
    hasAnyRole
    permitAll
    denyAll
    isAnonymous
    isAuthenticated
    #paramName ==

    View Slide

  49. Performance comparative (shallow)
    SPRING BOOT
    2.2.5
    QUARKUS
    Hotspot 1.8
    QUARKUS
    GraalVM 20.1
    build
    ( uber-jar )
    4.3 s
    48 Mb
    13 s
    42 Mb
    185 s
    97 Mb
    boot 6.8 s
    630 Mb
    2.6 s ( 3x )
    251 Mb ( 2.5x )
    0.462 s ( 15x )
    21 Mb ( 30x )

    View Slide

  50. References

    View Slide

  51. Repositories
    ● Spring REST PetClinic
    https://github.com/spring-petclinic/spring-petclinic-rest
    ● Quarkus REST PetClinic
    https://github.com/jonathanvila/spring-petclinic-rest/tree/quarkus
    Documentation/News/Help
    ● https://quarkus.io
    ● @QuarkusIO
    ● https://developers.redhat.com/search/?t=quarkus
    [email protected]

    View Slide

  52. Interactive Tutorials ( Katacoda )
    ● https://developers.redhat.com/courses/quarkus/
    List of Tutorials
    ● Getting Started
    ● For Spring Devs
    ● Streaming with
    Kafka
    ● Hibernate and
    Panache
    ● Prometeus &
    Grafana

    View Slide

  53. Cheat Sheet
    ● https://lordofthejars.github.io/quarkus-cheat-sheet/
    by Alex Soto

    View Slide

  54. Do you want to start coding with Quarkus ?
    https://code.quarkus.io/

    View Slide

  55. Migration Toolkit for Applications by Red Hat (MTA)
    New Web Console UI with Patternfly 4 (v 5.1.0)

    View Slide

  56. Migration Toolkit for Applications by Red Hat (MTA)
    Review rules new interface in 5.1.0

    View Slide

  57. ● Rules to help to migrate from SB to Quarkus ( MTA 5.1.0 )
    ○ Open Source tool to help on applications migrations
    ■ https://red.ht/mta
    ■ https://github.com/windup
    ○ 24 rules , covering ...
    ■ DI
    ■ Integration
    ■ Metrics
    ■ JPA
    ■ Security
    ■ Web
    ■ Shell
    ■ ….
    Migration Toolkit for Applications by Red Hat (MTA)

    View Slide

  58. Migration Toolkit for Applications by Red Hat (MTA)
    Spring Boot to Quarkus output example

    View Slide

  59. Migration Toolkit for Applications by Red Hat (MTA)
    IDE Integration
    Eclipse / VS Code / Eclipse Che

    View Slide

  60. Migration Toolkit for Applications by Red Hat (MTA)
    OpenShift Operator (v 5.1.0)

    View Slide

  61. Real use cases
    Lufthansa Technik AVIATAR experiences significant cloud resources
    savings by moving to Kubernetes-native Quarkus (link)
    Suomen Asiakastieto Oy chooses Quarkus for their microservices
    development (link)
    Vodafone Greece replaces Spring Boot with Quarkus (link)
    ● “Quarkus offers by default (without trying to optimize it) is 50%-60% more
    lightweight (in JVM mode) that what Spring offers”
    ● “30 to 40% better developer productivity vis-a-vis Spring Boot,”
    Public Clouds exp.
    -- Internal Labs ---
    Save up to :
    ● 37 % on Cloud ( Quarkus JVM )
    ● 71 % on Cloud ( Quarkus + GraalVM )

    View Slide

  62. @vilojona
    [email protected]
    aytartana.wordpress.com
    github.com/jonathanvila
    Thank you :)
    @mmmmmmpc
    [email protected]
    https://developers.redhat.com/blog/
    author/miguel/
    github.com/mpereco

    View Slide

  63. Questions received on meetups ( 1/3 )
    1. What do the spring extensions do internally ?
    2. Those quarkus extensions for libraries are like a fork of the general library ?
    3. General library and quarkus extension will be always on sync on versions ?
    4. What about the patches on the general library and the impact on quarkus extension ?
    5. Have you tried compiling the bytecode with Graal ? [I assume this means the bytecode generated by the Spring version]
    a. Yes, but I didn’t succeed . I assume there are a lot of things to do and elements to take into consideration (libraries not supported)
    6. Do you recommend to always migrate from Spring ?
    a. NO
    b. migration means investing money and time, so the goal has to be clear. Benefit is great when you have microservices with scale to
    zero as common, or when you can consider moving to Serverless/FaaS (lambdas)
    7. Using the spring extensions would have same performace as pure quarkus ?
    8. How many redhatters in the project ?
    a. Currently there are more than 360 contributors, and lot’s of redhatters but also non redhatters.
    9. Is there an Elytron LDAP extension for security ?
    a. YES : https://quarkus.io/guides/security-ldap

    View Slide

  64. Questions received on meetups ( 2/3 )
    10. How would the Quarkus support work ?
    a. https://quarkus.io/support/
    11. AOP on JVM ?
    a. NO
    b. because AspectJ needs to add the plugin to wave the classes on the build phase, and this conflicts with the same
    Quarkus plugin
    12. Company using Quarkus in native mode in production ?
    13. Porting websockets to Quarkus ?
    14. Quarkus native throughput degradation ?
    a. basically related to the GraalVM way of working with AOT compilation
    b. switch to g1gc in native images, profile guided optimizations ( GraalVM Enterprise )
    15. Hibernate metrics affect performance ?
    16. Is it possible to run Quarkus inside EAP ?
    17. Does Quarkus have enterprise support ?
    a. YES
    b. https://quarkus.io/support/
    18. Difference between Hotspot and GraalVM
    a. hotspot mean the JVM, so you need a Virtual Machine installed to run the app
    b. graalvm will produce native code applications that don’t need any vm installed

    View Slide

  65. Questions received on meetups ( 3/3 )
    19. When to migrate ?
    a. see question #6
    20. Benefits of Quarkus vs Micronaut
    a. both are good technologies, with different flavour, covering a space of the optimized native applications
    b. you need to read and try both and decide based on your own criteria
    21. Can SB compile to native with GraalVM ?
    a. they are working on that
    b. they have projects being able to go native in the labs/experimental repos
    22. Does MTA/Windup use AI ?
    a. NO
    b. it statically analyses applications based on rules , mainly defined on xml files that you can create yours rules
    23. How to migrate from EAP to Quarkus
    24. In a migration what do you recommend , going to the standard libraries or using the spring extensions ?
    25. Is Spring Kafka supported ?
    26. Is there a way to debug a GraalVM compiled application ?
    27. Are there many companies adopting Quarkus ?
    28. How easy is to produce the native image ?
    29. How easy is to deploy to cloud ?

    View Slide