SlideShare a Scribd company logo
objectcomputing.com
© 2022, Object Computing, Inc. (OCI). All rights reserved. No part of these notes may be reproduced, stored in a retrieval system, or transmitted, in any
form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior, written permission of Object Computing, Inc. (OCI)
Groovy-Powered Microservices
with Micronaut
Zachary Klein, Principal Software Engineer, 2GM Team
© 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com
❖ Zachary Klein - Developer & Architect
❖ Principal Software Engineer at Object
Computing, Inc
❖ 12+ years of software development experience
❖ OSS contributor
❖ Training instructor
2
About me
Copyright © 2022 Object Computing, Inc. (OCI) All rights reserved.
A modern, JVM-based, full-stack framework for building modular,
easily testable microservice and serverless applications.
Copyright © 2022 Object Computing, Inc. (OCI) All rights reserved.
https://micronaut.io/launch
Copyright © 2022 Object Computing, Inc. (OCI) All rights reserved.
❖ Controller with
Dependency Injection
© 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com
❖ Micronaut is language-agnostic
❖ 1st Class support for:
❖ Java
❖ Kotlin
❖ Groovy
6
Micronaut and Groovy
© 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com
❖ Micronaut implements core framework
components using Groovy language features
❖ E.g, Groovy AST transformations are used
for AOT compilation support (vs Annotation
Processors in Java/Kotlin)
❖ Support for Groovy configuration files,
serverless functions, Spock/Geb tests, and
GORM!
7
Micronaut and Groovy
Copyright © 2022 Object Computing, Inc. (OCI) All rights reserved.
❖ Controller with
Dependency Injection
❖ New class files generated
at compilation time (via
AST Transformations) to
implement the controller,
instantiate & supply
dependencies, etc
Copyright © 2022 Object Computing, Inc. (OCI) All rights reserved.
❖ Controller with
Dependency Injection
❖ New class files generated
at compilation time (via
AST Transformations) to
implement the controller,
instantiate & supply
dependencies, etc
© 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com
❖ Micronaut Launch is the recommended
method for creating Micronaut apps
❖ Choose Groovy as the Language (and
Spock as testing framework, if desired)
❖ Using the CLI:
10
Getting Started
© 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 11
Getting Started
© 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 12
Getting Started
© 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 13
Getting Started
Groovy auto-imports the
groovy.lang.singleton
annotation - make sure you
import from jakarta.inject
!
Copyright © 2022 Object Computing, Inc. (OCI) All rights reserved.
❖ Dependency Injection via
@Inject
Copyright © 2022 Object Computing, Inc. (OCI) All rights reserved.
❖ Micronaut’s HTTP Client
(implemented through
Groovy AST
Transformations)
Copyright © 2022 Object Computing, Inc. (OCI) All rights reserved.
❖ Dependency Injection via
Constructor Injection
© 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com
❖ YAML is the default, but Groovy
is supported
❖ Groovy allows for powerful,
expressive, programmatic
configuration
❖ Can mix and match!
17
Configuration with Groovy
© 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 18
Configuration with Groovy
❖ Micronaut accepts
configuration in JSON,
properties, YAML, and
Groovy ConfigSlurper (also
env variables, system
properties)
❖ Note that properties cannot
contain dashes - use
camelCase or snake_case
© 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com
❖ Micronaut Controllers (and Clients) express routes
declaratively via annotation arguments:
❖ @Controller("/hello")
❖ @Get("/profile “)
❖ Optionally, routes can be expressed
programmatically using the RouteBuilder interface
❖ GroovyRouteBuilder provides an expressive DSL
for routes (similar to Grails UrlMappings)
19
Controller Routes
© 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 20
Controller Routes
❖ Annotation-based routes
© 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 21
Controller Routes
❖ GroovyRouteBuilder
© 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 22
@MicronautTest & Spock
❖ Micronaut is test-framework agnostic - no special
tooling required
❖ E.g, JUnit, Spock
❖ Because of Micronaut’s fast startup time, many
developers prefer integration tests
❖ @MicronautTest automatically starts up the
application context for the test run, and shuts it
down cleanly
© 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 23
@MicronautTest & Spock
© 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 24
@MicronautTest & Spock
© 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 25
@MicronautTest & Spock
© 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 26
@MicronautTest & Spock
© 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 27
@MicronautTest & Spock
© 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 28
Micronaut and GORM
❖ GORM is the persistence framework pioneered by
the Grails framework - https://gorm.grails.org
❖ GORM provides expressive querying, persistence
and domain modeling features, including
validation, multi-tenancy, and more
❖ When configured with Groovy support, Micronaut
apps can leverage GORM’s powerful features in a
comparatively lightweight manner
© 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 29
Micronaut and GORM
Dependency Description
micronaut-
hibernate-gorm
Configures GORM for Hibernate for Groovy
applications
micronaut-mongo-
gorm
Configures GORM for Mongo DB for Groovy
applications
micronaut-neo4j-
gorm
Configures GORM for Mongo DB for Groovy
applications
© 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 30
Adding GORM to Micronaut
❖ Using Micronaut Launch
or the CLI command
feature-diff, you can
generate a delta showing
how to add a feature, like
hibernate-gorm
© 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 31
Adding GORM to Micronaut
❖ Add GORM and JDBC
dependencies - also a
database driver (H2 in this
demo)
© 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com
Configuring Datasources
❖ Datasource configuration
❖ Standard Hibernate/JDBC
configuration properties -
database credentials,
dialect, etc
❖ Can configure multiple
datasources
© 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com
Enabling Entity-scanning
❖ Application class needs to
be modified
❖ Application context is
started up using the
builder pattern - the
packages() method
species the package under
which entities are defined
© 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 34
Creating Domain Classes (aka Entities)
❖ A Domain Class / Entity can
be a simple POGO
annotated with @Entity
❖ Class name will be mapped
as database table name (by
convention)
❖ Properties of the class will be
mapped to columns in the
database table
© 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 35
Creating Domain Classes (aka Entities)
❖ Additional data-mapping
features (like validation)
can be added by
implementing the
GormEntity<> trait
© 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 36
GORM Data Services in Micronaut
© 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 37
GORM Data Services in Micronaut
© 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 38
GORM Multi-tenancy
https://gorm.grails.org/latest/hibernate/manual/index.html#multiTenancy
© 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 39
GORM Multi-tenancy
https://gorm.grails.org/latest/hibernate/manual/index.html#multiTenancy
© 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 40
GORM Multi-tenancy
https://gorm.grails.org/latest/hibernate/manual/index.html#multiTenancy
© 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 41
GORM Multi-tenancy
https://gorm.grails.org/latest/hibernate/manual/index.html#multiTenancy
© 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 42
GORM Multi-tenancy Modes
https://gorm.grails.org/latest/hibernate/manual/index.html#multiTenancy
Modes Description Isolation
DATABASE
Separate database with a
separate connection pool is used
to store each tenants data.
HIGHEST
SCHEMA
The same database, but different
schemas are used to store each
tenants’ data.
HIGH
DISCRIMINATOR
The same database is used with
a discriminator used to partition
and isolate data.
LOW
© 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 43
GORM Multi-tenancy Tenant Resolvers
https://gorm.grails.org/latest/hibernate/manual/index.html#multiTenancy
Name Description
CookieTenantResolver Removes the current tenant from an HTTP cookie
FixedTenantResolver Resolves against a fixed tenant id
HttpHeaderTenantResolver Resolves the current tenant from the request HTTP Header
PrincipalTenantResolver Resolves the current tenant from the authenticated username
SessionTenantResolver Resolves the current tenant from the HTTP Session
SubdomainTenantResolver Resolves the tenant id from the subdomain
SystemPropertyTenantResolver Resolves the tenant id from a system property
© 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 44
GORM Multi-tenancy: Specify tenantId
© 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 45
GORM Multi-tenancy Transformations (Annotations)
Transformation Description
@CurrentTenant Resolve the current tenant for the context of a class or method
@Tenant Use a specifc tenant for the context of a class or method
@WithoutTenant Execute logic without a specific tentnat (using the default connection)
© 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 46
© 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 47
Micronaut and Grails
❖ Grails is a rapid-application Java web-framework, based on
Groovy and Spring Boot
❖ Since Grails 4, Grails applications include a Micronaut
application context, allowing Micronaut features and libraries
to be integrated with Grails apps
❖ Micronaut HTTP Client & Grails: https://guides.grails.org/
grails-micronaut-http/guide/index.html
❖ Micronaut Kafka & Grails: https://guides.grails.org/grails-
micronaut-kafka/guide/index.html
© 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com
❖ Micronaut supports Groovy for writing serverless
functions in environments like AWS Lambda, Oracle
Cloud, Microsoft Azure, & Google Cloud Platform.
❖ You can deploy “pure” functions (invoked by events in
the platform) or lightweight “HTTP functions” (with
controllers, REST endpoints, etc)
48
Micronaut and Groovy: Serverless Functions
© 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 49
Micronaut and Groovy: Serverless Functions
https://micronaut-projects.github.io/
micronaut-gcp/latest/guide/
© 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 50
Micronaut and Groovy: Serverless Functions
https://micronaut-projects.github.io/
micronaut-aws/latest/guide/
© 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 51
Micronaut and Groovy: Serverless Functions
https://micronaut-projects.github.io/
micronaut-azure/latest/guide/
© 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 52
Micronaut and Groovy: Serverless Functions
https://micronaut-projects.github.io/
micronaut-oracle-cloud/latest/guide/
© 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 53
Micronaut and Groovy: CLI Apps
❖ Micronaut supports creation of Command Line
Applications using the picocli library (https://picocli.info/)
❖ CLI apps can be created using Groovy as well!
❖ Apps can utilize dependency injection, HTTP clients, &
more
❖ Docs: https://micronaut-projects.github.io/micronaut-
picocli/latest/guide/
© 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 54
Micronaut and Groovy: CLI Apps
objectcomputing.com
© 2022, Object Computing, Inc. (OCI). All rights reserved. No part of these notes may be reproduced, stored in a retrieval system, or transmitted, in any
form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior, written permission of Object Computing, Inc. (OCI)
Zachary Klein, Principal Software Engineer, 2GM Team

More Related Content

What's hot (20)

TXT
OPTEE on QEMU - Build Tutorial
Dalton Valadares
 
PPTX
JSP Directives
ShahDhruv21
 
PDF
Docker fundamentals
Alper Unal
 
PPTX
Operating Systems: Virtual Memory
Damian T. Gordon
 
PPTX
Linux Boot Process
darshhingu
 
PPT
Ios development
Shakil Ahmed
 
PDF
Configuring the Apache Web Server
webhostingguy
 
PPTX
Session: A Reference Architecture for Running Modern APIs with NGINX Unit and...
NGINX, Inc.
 
PDF
Alarms
maamir farooq
 
PPTX
AEM (CQ) Dispatcher Security and CDN+Browser Caching
Andrew Khoury
 
PDF
SEH on mingw32
kikairoya
 
PDF
ABS 2013: Dive into Android Networking - Adding Ethernet Connectivity
Benjamin Zores
 
PPTX
Dead Lock in operating system
Ali Haider
 
PPTX
Processors with Advanced Technologies
CherukuriGopikrishna
 
PDF
BPF - in-kernel virtual machine
Alexei Starovoitov
 
PDF
CNIT 126: Ch 6: Recognizing C Constructs in Assembly
Sam Bowne
 
PDF
Reverse Engineering a (M)MORPG
Antonin Beaujeant
 
PPTX
Memory management ppt
ManishaJha43
 
PPTX
File System Reliability & Virtual File in Operating System
Meghaj Mallick
 
OPTEE on QEMU - Build Tutorial
Dalton Valadares
 
JSP Directives
ShahDhruv21
 
Docker fundamentals
Alper Unal
 
Operating Systems: Virtual Memory
Damian T. Gordon
 
Linux Boot Process
darshhingu
 
Ios development
Shakil Ahmed
 
Configuring the Apache Web Server
webhostingguy
 
Session: A Reference Architecture for Running Modern APIs with NGINX Unit and...
NGINX, Inc.
 
AEM (CQ) Dispatcher Security and CDN+Browser Caching
Andrew Khoury
 
SEH on mingw32
kikairoya
 
ABS 2013: Dive into Android Networking - Adding Ethernet Connectivity
Benjamin Zores
 
Dead Lock in operating system
Ali Haider
 
Processors with Advanced Technologies
CherukuriGopikrishna
 
BPF - in-kernel virtual machine
Alexei Starovoitov
 
CNIT 126: Ch 6: Recognizing C Constructs in Assembly
Sam Bowne
 
Reverse Engineering a (M)MORPG
Antonin Beaujeant
 
Memory management ppt
ManishaJha43
 
File System Reliability & Virtual File in Operating System
Meghaj Mallick
 

Similar to Groovy-Powered Microservices with Micronaut (20)

PDF
Native Cloud-Native: Building Agile Microservices with the Micronaut Framework
Zachary Klein
 
PDF
Introduction to Micronaut at Oracle CodeOne 2018
graemerocher
 
PDF
Micronaut: Changing the Micro Future
Zachary Klein
 
PDF
Getting Groovy with JHipster and Micronaut
Zachary Klein
 
PDF
Micronaut Launchpad
Zachary Klein
 
PDF
Micronaut Deep Dive - Codeone 2019
graemerocher
 
PDF
SevillaJUG - Unleash the power of your applications with Micronaut® ,GraalVM...
Juarez Junior
 
PDF
GeeCon Prague 2023 - Unleash the power of your applications with Micronaut®, ...
Juarez Junior
 
PPTX
Discover Micronaut
Riadh MNASRI
 
PDF
Building Microservices with Micronaut: A Full-Stack JVM-Based Framework
Michael Redlich
 
PDF
Developing Micronaut Applications With IntelliJ IDEA
Iván López Martín
 
PDF
Micronaut Deep Dive - Devoxx Belgium 2019
graemerocher
 
PDF
Java Cloud Native Hack Nights GDL
Domingo Suarez Torres
 
PDF
Introduction to Micronaut - JBCNConf 2019
graemerocher
 
PDF
Grails 4 and Micronaut at Devnexus 2019
graemerocher
 
PDF
Micronaut Deep Dive - Devnexus 2019
graemerocher
 
PDF
Reactive microservices with Micronaut - Greach 2018
Alvaro Sanchez-Mariscal
 
PPTX
Micronaut brainbit
Michel Schudel
 
PDF
Microservices with Micronaut
QAware GmbH
 
PDF
Micronaut Framework Guide Framework Basics and Fundamentals.pdf
Inexture Solutions
 
Native Cloud-Native: Building Agile Microservices with the Micronaut Framework
Zachary Klein
 
Introduction to Micronaut at Oracle CodeOne 2018
graemerocher
 
Micronaut: Changing the Micro Future
Zachary Klein
 
Getting Groovy with JHipster and Micronaut
Zachary Klein
 
Micronaut Launchpad
Zachary Klein
 
Micronaut Deep Dive - Codeone 2019
graemerocher
 
SevillaJUG - Unleash the power of your applications with Micronaut® ,GraalVM...
Juarez Junior
 
GeeCon Prague 2023 - Unleash the power of your applications with Micronaut®, ...
Juarez Junior
 
Discover Micronaut
Riadh MNASRI
 
Building Microservices with Micronaut: A Full-Stack JVM-Based Framework
Michael Redlich
 
Developing Micronaut Applications With IntelliJ IDEA
Iván López Martín
 
Micronaut Deep Dive - Devoxx Belgium 2019
graemerocher
 
Java Cloud Native Hack Nights GDL
Domingo Suarez Torres
 
Introduction to Micronaut - JBCNConf 2019
graemerocher
 
Grails 4 and Micronaut at Devnexus 2019
graemerocher
 
Micronaut Deep Dive - Devnexus 2019
graemerocher
 
Reactive microservices with Micronaut - Greach 2018
Alvaro Sanchez-Mariscal
 
Micronaut brainbit
Michel Schudel
 
Microservices with Micronaut
QAware GmbH
 
Micronaut Framework Guide Framework Basics and Fundamentals.pdf
Inexture Solutions
 
Ad

More from Zachary Klein (7)

PDF
Grails 4: Upgrade your Game!
Zachary Klein
 
PDF
Groovy for Java Devs
Zachary Klein
 
PDF
Micronaut For Single Page Apps
Zachary Klein
 
PDF
Grails Launchpad - From Ground Zero to Orbit
Zachary Klein
 
PDF
Room with a Vue - Introduction to Vue.js
Zachary Klein
 
PDF
Shields Up! Securing React Apps
Zachary Klein
 
PDF
Using React with Grails 3
Zachary Klein
 
Grails 4: Upgrade your Game!
Zachary Klein
 
Groovy for Java Devs
Zachary Klein
 
Micronaut For Single Page Apps
Zachary Klein
 
Grails Launchpad - From Ground Zero to Orbit
Zachary Klein
 
Room with a Vue - Introduction to Vue.js
Zachary Klein
 
Shields Up! Securing React Apps
Zachary Klein
 
Using React with Grails 3
Zachary Klein
 
Ad

Recently uploaded (20)

PDF
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
PPTX
Farrell_Programming Logic and Design slides_10e_ch02_PowerPoint.pptx
bashnahara11
 
PPTX
Agile Chennai 18-19 July 2025 | Workshop - Enhancing Agile Collaboration with...
AgileNetwork
 
PPTX
Agile Chennai 18-19 July 2025 | Emerging patterns in Agentic AI by Bharani Su...
AgileNetwork
 
PDF
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
PDF
TrustArc Webinar - Navigating Data Privacy in LATAM: Laws, Trends, and Compli...
TrustArc
 
PDF
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
PPTX
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
PDF
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
PDF
GDG Cloud Munich - Intro - Luiz Carneiro - #BuildWithAI - July - Abdel.pdf
Luiz Carneiro
 
PPTX
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
PPTX
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
PPTX
The Future of AI & Machine Learning.pptx
pritsen4700
 
PPTX
Agentic AI in Healthcare Driving the Next Wave of Digital Transformation
danielle hunter
 
PDF
RAT Builders - How to Catch Them All [DeepSec 2024]
malmoeb
 
PDF
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
PPTX
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
PDF
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
PDF
How ETL Control Logic Keeps Your Pipelines Safe and Reliable.pdf
Stryv Solutions Pvt. Ltd.
 
PPTX
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
Farrell_Programming Logic and Design slides_10e_ch02_PowerPoint.pptx
bashnahara11
 
Agile Chennai 18-19 July 2025 | Workshop - Enhancing Agile Collaboration with...
AgileNetwork
 
Agile Chennai 18-19 July 2025 | Emerging patterns in Agentic AI by Bharani Su...
AgileNetwork
 
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
TrustArc Webinar - Navigating Data Privacy in LATAM: Laws, Trends, and Compli...
TrustArc
 
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
GDG Cloud Munich - Intro - Luiz Carneiro - #BuildWithAI - July - Abdel.pdf
Luiz Carneiro
 
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
The Future of AI & Machine Learning.pptx
pritsen4700
 
Agentic AI in Healthcare Driving the Next Wave of Digital Transformation
danielle hunter
 
RAT Builders - How to Catch Them All [DeepSec 2024]
malmoeb
 
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
How ETL Control Logic Keeps Your Pipelines Safe and Reliable.pdf
Stryv Solutions Pvt. Ltd.
 
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 

Groovy-Powered Microservices with Micronaut

  • 1. objectcomputing.com © 2022, Object Computing, Inc. (OCI). All rights reserved. No part of these notes may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior, written permission of Object Computing, Inc. (OCI) Groovy-Powered Microservices with Micronaut Zachary Klein, Principal Software Engineer, 2GM Team
  • 2. © 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com ❖ Zachary Klein - Developer & Architect ❖ Principal Software Engineer at Object Computing, Inc ❖ 12+ years of software development experience ❖ OSS contributor ❖ Training instructor 2 About me
  • 3. Copyright © 2022 Object Computing, Inc. (OCI) All rights reserved. A modern, JVM-based, full-stack framework for building modular, easily testable microservice and serverless applications.
  • 4. Copyright © 2022 Object Computing, Inc. (OCI) All rights reserved. https://micronaut.io/launch
  • 5. Copyright © 2022 Object Computing, Inc. (OCI) All rights reserved. ❖ Controller with Dependency Injection
  • 6. © 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com ❖ Micronaut is language-agnostic ❖ 1st Class support for: ❖ Java ❖ Kotlin ❖ Groovy 6 Micronaut and Groovy
  • 7. © 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com ❖ Micronaut implements core framework components using Groovy language features ❖ E.g, Groovy AST transformations are used for AOT compilation support (vs Annotation Processors in Java/Kotlin) ❖ Support for Groovy configuration files, serverless functions, Spock/Geb tests, and GORM! 7 Micronaut and Groovy
  • 8. Copyright © 2022 Object Computing, Inc. (OCI) All rights reserved. ❖ Controller with Dependency Injection ❖ New class files generated at compilation time (via AST Transformations) to implement the controller, instantiate & supply dependencies, etc
  • 9. Copyright © 2022 Object Computing, Inc. (OCI) All rights reserved. ❖ Controller with Dependency Injection ❖ New class files generated at compilation time (via AST Transformations) to implement the controller, instantiate & supply dependencies, etc
  • 10. © 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com ❖ Micronaut Launch is the recommended method for creating Micronaut apps ❖ Choose Groovy as the Language (and Spock as testing framework, if desired) ❖ Using the CLI: 10 Getting Started
  • 11. © 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 11 Getting Started
  • 12. © 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 12 Getting Started
  • 13. © 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 13 Getting Started Groovy auto-imports the groovy.lang.singleton annotation - make sure you import from jakarta.inject !
  • 14. Copyright © 2022 Object Computing, Inc. (OCI) All rights reserved. ❖ Dependency Injection via @Inject
  • 15. Copyright © 2022 Object Computing, Inc. (OCI) All rights reserved. ❖ Micronaut’s HTTP Client (implemented through Groovy AST Transformations)
  • 16. Copyright © 2022 Object Computing, Inc. (OCI) All rights reserved. ❖ Dependency Injection via Constructor Injection
  • 17. © 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com ❖ YAML is the default, but Groovy is supported ❖ Groovy allows for powerful, expressive, programmatic configuration ❖ Can mix and match! 17 Configuration with Groovy
  • 18. © 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 18 Configuration with Groovy ❖ Micronaut accepts configuration in JSON, properties, YAML, and Groovy ConfigSlurper (also env variables, system properties) ❖ Note that properties cannot contain dashes - use camelCase or snake_case
  • 19. © 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com ❖ Micronaut Controllers (and Clients) express routes declaratively via annotation arguments: ❖ @Controller("/hello") ❖ @Get("/profile “) ❖ Optionally, routes can be expressed programmatically using the RouteBuilder interface ❖ GroovyRouteBuilder provides an expressive DSL for routes (similar to Grails UrlMappings) 19 Controller Routes
  • 20. © 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 20 Controller Routes ❖ Annotation-based routes
  • 21. © 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 21 Controller Routes ❖ GroovyRouteBuilder
  • 22. © 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 22 @MicronautTest & Spock ❖ Micronaut is test-framework agnostic - no special tooling required ❖ E.g, JUnit, Spock ❖ Because of Micronaut’s fast startup time, many developers prefer integration tests ❖ @MicronautTest automatically starts up the application context for the test run, and shuts it down cleanly
  • 23. © 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 23 @MicronautTest & Spock
  • 24. © 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 24 @MicronautTest & Spock
  • 25. © 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 25 @MicronautTest & Spock
  • 26. © 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 26 @MicronautTest & Spock
  • 27. © 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 27 @MicronautTest & Spock
  • 28. © 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 28 Micronaut and GORM ❖ GORM is the persistence framework pioneered by the Grails framework - https://gorm.grails.org ❖ GORM provides expressive querying, persistence and domain modeling features, including validation, multi-tenancy, and more ❖ When configured with Groovy support, Micronaut apps can leverage GORM’s powerful features in a comparatively lightweight manner
  • 29. © 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 29 Micronaut and GORM Dependency Description micronaut- hibernate-gorm Configures GORM for Hibernate for Groovy applications micronaut-mongo- gorm Configures GORM for Mongo DB for Groovy applications micronaut-neo4j- gorm Configures GORM for Mongo DB for Groovy applications
  • 30. © 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 30 Adding GORM to Micronaut ❖ Using Micronaut Launch or the CLI command feature-diff, you can generate a delta showing how to add a feature, like hibernate-gorm
  • 31. © 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 31 Adding GORM to Micronaut ❖ Add GORM and JDBC dependencies - also a database driver (H2 in this demo)
  • 32. © 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com Configuring Datasources ❖ Datasource configuration ❖ Standard Hibernate/JDBC configuration properties - database credentials, dialect, etc ❖ Can configure multiple datasources
  • 33. © 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com Enabling Entity-scanning ❖ Application class needs to be modified ❖ Application context is started up using the builder pattern - the packages() method species the package under which entities are defined
  • 34. © 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 34 Creating Domain Classes (aka Entities) ❖ A Domain Class / Entity can be a simple POGO annotated with @Entity ❖ Class name will be mapped as database table name (by convention) ❖ Properties of the class will be mapped to columns in the database table
  • 35. © 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 35 Creating Domain Classes (aka Entities) ❖ Additional data-mapping features (like validation) can be added by implementing the GormEntity<> trait
  • 36. © 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 36 GORM Data Services in Micronaut
  • 37. © 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 37 GORM Data Services in Micronaut
  • 38. © 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 38 GORM Multi-tenancy https://gorm.grails.org/latest/hibernate/manual/index.html#multiTenancy
  • 39. © 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 39 GORM Multi-tenancy https://gorm.grails.org/latest/hibernate/manual/index.html#multiTenancy
  • 40. © 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 40 GORM Multi-tenancy https://gorm.grails.org/latest/hibernate/manual/index.html#multiTenancy
  • 41. © 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 41 GORM Multi-tenancy https://gorm.grails.org/latest/hibernate/manual/index.html#multiTenancy
  • 42. © 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 42 GORM Multi-tenancy Modes https://gorm.grails.org/latest/hibernate/manual/index.html#multiTenancy Modes Description Isolation DATABASE Separate database with a separate connection pool is used to store each tenants data. HIGHEST SCHEMA The same database, but different schemas are used to store each tenants’ data. HIGH DISCRIMINATOR The same database is used with a discriminator used to partition and isolate data. LOW
  • 43. © 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 43 GORM Multi-tenancy Tenant Resolvers https://gorm.grails.org/latest/hibernate/manual/index.html#multiTenancy Name Description CookieTenantResolver Removes the current tenant from an HTTP cookie FixedTenantResolver Resolves against a fixed tenant id HttpHeaderTenantResolver Resolves the current tenant from the request HTTP Header PrincipalTenantResolver Resolves the current tenant from the authenticated username SessionTenantResolver Resolves the current tenant from the HTTP Session SubdomainTenantResolver Resolves the tenant id from the subdomain SystemPropertyTenantResolver Resolves the tenant id from a system property
  • 44. © 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 44 GORM Multi-tenancy: Specify tenantId
  • 45. © 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 45 GORM Multi-tenancy Transformations (Annotations) Transformation Description @CurrentTenant Resolve the current tenant for the context of a class or method @Tenant Use a specifc tenant for the context of a class or method @WithoutTenant Execute logic without a specific tentnat (using the default connection)
  • 46. © 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 46
  • 47. © 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 47 Micronaut and Grails ❖ Grails is a rapid-application Java web-framework, based on Groovy and Spring Boot ❖ Since Grails 4, Grails applications include a Micronaut application context, allowing Micronaut features and libraries to be integrated with Grails apps ❖ Micronaut HTTP Client & Grails: https://guides.grails.org/ grails-micronaut-http/guide/index.html ❖ Micronaut Kafka & Grails: https://guides.grails.org/grails- micronaut-kafka/guide/index.html
  • 48. © 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com ❖ Micronaut supports Groovy for writing serverless functions in environments like AWS Lambda, Oracle Cloud, Microsoft Azure, & Google Cloud Platform. ❖ You can deploy “pure” functions (invoked by events in the platform) or lightweight “HTTP functions” (with controllers, REST endpoints, etc) 48 Micronaut and Groovy: Serverless Functions
  • 49. © 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 49 Micronaut and Groovy: Serverless Functions https://micronaut-projects.github.io/ micronaut-gcp/latest/guide/
  • 50. © 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 50 Micronaut and Groovy: Serverless Functions https://micronaut-projects.github.io/ micronaut-aws/latest/guide/
  • 51. © 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 51 Micronaut and Groovy: Serverless Functions https://micronaut-projects.github.io/ micronaut-azure/latest/guide/
  • 52. © 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 52 Micronaut and Groovy: Serverless Functions https://micronaut-projects.github.io/ micronaut-oracle-cloud/latest/guide/
  • 53. © 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 53 Micronaut and Groovy: CLI Apps ❖ Micronaut supports creation of Command Line Applications using the picocli library (https://picocli.info/) ❖ CLI apps can be created using Groovy as well! ❖ Apps can utilize dependency injection, HTTP clients, & more ❖ Docs: https://micronaut-projects.github.io/micronaut- picocli/latest/guide/
  • 54. © 2022, Object Computing, Inc. (OCI). All rights reserved. objectcomputing.com 54 Micronaut and Groovy: CLI Apps
  • 55. objectcomputing.com © 2022, Object Computing, Inc. (OCI). All rights reserved. No part of these notes may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior, written permission of Object Computing, Inc. (OCI) Zachary Klein, Principal Software Engineer, 2GM Team