@CesarHgt @tomitribejugbaq.org
César Hernández
Creando Microservicios con Java,
MicroProfile y Apache TomEE
@CesarHgt @tomitribejugbaq.org
SPEAKER
César Hernández
● Senior Software Engineer at Tomitribe
● Java Champion & Oracle Groundbreaker Ambassador
● Duke’s Choice Award 2016, 2017
● Oracle Certified Professional
● +14 experience with Java Enterprise
● Apache TomEE, Jakarta EE Committer, Contributor
for Microprofile.
● Open Source advocate, teacher and public speaker
@CesarHgt @tomitribejugbaq.org
@CesarHgt @tomitribejugbaq.org
Agenda
● Introduction and setup
● Microservices
● Eclipse MicroProfile
● MicroProfile Configuration
● MicroProfile Health
● MicroProfile Metrics
● MicroProfile JWT
● MicroProfile Fault Tolerance
●
@CesarHgt @tomitribejugbaq.org
Microservices
@CesarHgt @tomitribejugbaq.org
A microservice architectural style is an approach to
developing a single application as a suite of small services,
each running in its own process and communicating with
lightweight mechanisms, often an HTTP resource API.
— Martin Fowler
@CesarHgt @tomitribejugbaq.org
● Infraestructura
● Arquitectura
● Investigación
Perspectivas Micro Servicios
@CesarHgt @tomitribejugbaq.org
Microservices
Source: http://eventuate.io/exampleapps.html
@CesarHgt @tomitribejugbaq.org
● Alcance
● Distribuídos
● Orquestación
● Adopción
Retos Microservices
@CesarHgt @tomitribejugbaq.org
Microservices Patterns teaches enterprise developers and
architects how to build applications with the microservice
architecture.
— Chris Richardson
@CesarHgt @tomitribejugbaq.org
Patrones Microservices
Source: http://eventuate.io/exampleapps.html
@CesarHgt @tomitribejugbaq.org
● 39% plan to use Java to build microservices in the next year
● Using Java for cloud native applications
○ 44% Spring/Spring Boot ( - 13%)
○ 35% Jakarta EE
● Architectural Approach
○ 39% Microservices
○ 25% Monolithic (2x since 2019)
○ 23% Hybrid
Microservices and Cloud-native
Source: Jakarta EE Developer Survey 2020 - The Eclipse Foundation’s global survey of 2180 Java developers.
○ 29% MicroProfile
○ 16% Quarkus
@CesarHgt @tomitribejugbaq.org
Eclipse MicroProfile
@CesarHgt @tomitribejugbaq.org
Eclipse MicroProfile
● An open-source community specification
● Focus on Enterprise Java microservices
● Generates: SPEC, API, and TCK.
● https://microprofile.io
● Implemented by different vendors.
@CesarHgt @tomitribejugbaq.org
Eclipse MicroProfile
MicroProfile 3.3
JAX-RS 2.1JSON-P 1.1CDI 2.0
Config
Fault
Tolerance
JWT
Propagation
HealthMetrics
Open
Tracing
Open API Rest Client
JSON-B 1.0
@CesarHgt @tomitribejugbaq.org
Eclipse Foundation
https://www.eclipse.org/membership/documents/eclipse-foundation-overview.pdf
360+
Projects
275+
Members
1550+
Committers
195M+
Lines of code
15
Years
30
Members
@CesarHgt @tomitribejugbaq.org
Eclipse foundation, EE4J, JavaEE, and JakartaEE
@CesarHgt @tomitribejugbaq.org
MicroProfile Implementations
@CesarHgt @tomitribejugbaq.org
Apache TomEE
@CesarHgt @tomitribejugbaq.org
● Apache Tomcat + Java EE = Apache TomEE
● Built from Apache components
● MicroProfile compliant
● Footprint: 30MB zip, 100~MB memory
● TomEE 9.0.0-M2 (javax -> jakarta)
● tomee.apache.org
@CesarHgt @tomitribejugbaq.org
TomEE JAX-RS (~ Microprofile)
TomEE JAX-RS Microprofile
Java Server Pages
(JSP)
Java Server Faces
(JSF)
Java Transaction
API (JTA)
Bean Validation
Enterprise
JavaBeansJavaMail API
Java API for RESTful
Web Services
(JAX-RS)
Java Persistence
API (JPA)
Contexts and
Dependency
Injection (CDI)
Java Servlets
Java Authentication
and Authorization
Service (JAAS)
Java Authorization
Contract for
Containers (JACC)
@CesarHgt @tomitribejugbaq.org
New to TomEE?
https://www.katacoda.com/cesarhernandezgt/scenarios/1
@CesarHgt @tomitribejugbaq.org
https://start.microprofile.io
Hola mundo
1. unzip demo
2. cd demo
3. mvn package tomee:run
4. http://localhost:8080/data/hello
5. Revisa la estructura del proyecto en
tu IDE.
@CesarHgt @tomitribejugbaq.org
● Command-line Interface
● Visual Studio Code extension
start.microprofile.io
https://microprofile.io/2019/07/08/command-line-interface-for-microprofile-starter-is-available-now/
https://microprofile.io/2019/09/17/announcing-visual-studio-code-extension-for-microprofile-starter/
https://www.tomitribe.com/blog/how-to-create-a-microservice-with-eclipse-microprofile-and-apache-tomee/
@CesarHgt @tomitribejugbaq.org
MicroProfile Specifications
@CesarHgt @tomitribejugbaq.org
Eclipse MicroProfile
MicroProfile 3.3
JAX-RS 2.1JSON-P 1.1CDI 2.0
Config
Fault
Tolerance
JWT
Propagation
HealthMetrics
Open
Tracing
Open API Rest Client
JSON-B 1.0
● Why?
● How?
● Code
● Integrate
@CesarHgt @tomitribejugbaq.org
MicroProfile Health
@CesarHgt @tomitribejugbaq.org
Health Feature
● Probes state of services and resources.
● Infrastructure benefits
● It’s intended as a machine to machine (M2M) mechanism
● Examples: CloudFoundry Health Checks and Kubernetes Liveness and
Readiness Probes.
@CesarHgt @tomitribejugbaq.org
Familiarize yourself with the API
● Default endpoints:
○ http://host:port/health
○ http://host:port/<app-context>/health
● General output:
○ {"checks":[],"outcome":"UP","status":"UP"}
@CesarHgt @tomitribejugbaq.org
{
"status": "UP",
"checks": [
{
"name": "second-check",
"status": "UP"
}
]
}
Producer Consumer
Health
Check
Procedure
DB
@CesarHgt @tomitribejugbaq.org
{
"status": "UP",
"checks": [
{
"name": "spotify-service",
"status": "UP",
"data": {
"key": "foo",
"foo": "bar"
}
},
{
"name": "db-check",
"status": "UP"
}
]
}
Producer Consumer
DB
Service
@CesarHgt @tomitribejugbaq.org
¿Cómo puedo seguir aprendiendo?
@CesarHgt @tomitribejugbaq.org
Recursos de ayuda
● https://microprofile.io/
● https://tomee.apache.org/
● https://github.com/apache/tomee
● https://www.tomitribe.com/blog/category/microprofile/
● https://kodnito.com/posts/building-an-api-backend-with-microprofile-
ebook/
@CesarHgt @tomitribejugbaq.org
Descargar los Ejemplos
1. wget https://github.com/apache/tomee/archive/tomee-8.0.2.zip
2. unzip tomee-8.0.2.zip
3. cd tomee-tomee-8.0.2/examples/
4. cesar:examples$ ll -d mp-*
mp-config-example/ mp-faulttolerance-timeout/
mp-metrics-gauge/ mp-rest-client/
mp-config-source-database/ mp-jsonb-configuration/
mp-metrics-histogram/ mp-rest-jwt/
mp-custom-healthcheck/ mp-jwt-bean-validation/
mp-metrics-metered/ mp-rest-jwt-jwk/
mp-faulttolerance-fallback/ mp-jwt-bean-validation-strongly-typed/
mp-metrics-timed/ mp-rest-jwt-principal/
mp-faulttolerance-retry/ mp-metrics-counted/
mp-opentracing-traced/ mp-rest-jwt-public-key/
Descripciones de los ejemplos: https://tomee.apache.org/tomee-8.0/examples/
@CesarHgt @tomitribejugbaq.org
MicroProfile Configuration
@CesarHgt @tomitribejugbaq.org
● External configuration
● Cloud Native development lifecycle
● Influences and History
○ DeltaSpike Config (http://deltaspike.apache.org/documentation/configuration.html)
○ Extracted parts of DeltaSpike Config (https://github.com/struberg/javaConfig/)
○ Apache Tamaya (http://tamaya.incubator.apache.org/)
MicroProfile Configuration
@CesarHgt @tomitribejugbaq.org
MicroProfile config examples
import org.eclipse.microprofile.config.inject.ConfigProperty;
@Inject
@ConfigProperty(name = "injected.value")
private String value;
@Inject
@ConfigProperty(name = "app.year", defaultValue = "2020")
private int year;
@CesarHgt @tomitribejugbaq.org
● Where to store configurations?
○ The default config sources always available by default are:
■ System properties (ordinal=400)
■ Environment properties (ordinal=300)
■ /META-INF/microprofile-config.properties (ordinal=100)
○ The priority will be used to order the ConfigSource implementations to search for
the configuration key.
MicroProfile Configuration
@CesarHgt @tomitribejugbaq.org
MicroProfile Metrics
@CesarHgt @tomitribejugbaq.org
Telemetry
● Unified Monitoring data ("Telemetry")
● Unified Java API
● Polyglot environment
● Open standards API path
@CesarHgt @tomitribejugbaq.org
Difference to health checks
● Health checks
○ "Is my application still running ok?".
● Metrics:
○ Determine the overall the microservices health.
○ Provide long term trend data.
@CesarHgt @tomitribejugbaq.org
● Application Scope Rest endpoint
● Metrics aggregation services
● Metrics types:
○ @Counted
○ @Gauge
○ @Metered
Metrics API
○ @Timed
○ Histogram
@CesarHgt @tomitribejugbaq.org
Metrics aggregation services
@CesarHgt @tomitribejugbaq.org
Ejemplo de Metrics
@CesarHgt @tomitribejugbaq.org
MicroProfile JWT
@CesarHgt @tomitribejugbaq.org
What is JWT?
● Stands for JSON Web Token.
● It’s a JSON-based text format for exchanging information between
parties.
● It’s an open standard specified under RFC 7519.
● Standards such as OpenID Connect and OAuth 2 use JWT to represent
their own tokens.
@CesarHgt @tomitribejugbaq.org
https://tribestream.io/tools/jwt
@CesarHgt @tomitribejugbaq.org
● Standard
● Interoperability across the enterprise and vendors that favors
microservices development.
● JWT can be both verified and propagated by each microservice.
Why do we need MicroProfile JWT?
@CesarHgt @tomitribejugbaq.org
MicroProfile JWT big picture
@CesarHgt @tomitribejugbaq.org
JsonWebToken
import javax.ws.rs.Path;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import org.eclipse.microprofile.jwt.JsonWebToken;
@Path("/pizza")
@RequestScoped
public class PizzaEndpoint {
@Inject
private JsonWebToken callerPrincipal;
@Inject
@Claim("address") // Custom claim
private JsonObject address;
@CesarHgt @tomitribejugbaq.org
● Cons
○ Base64 encoding isn’t encryption
○ Larger Payload
○ Token Expiration
● Pros
○ JSON
○ Speed and Reliability
○ Secure
○ Claims
The Pros and Cons to using JWT
@CesarHgt @tomitribejugbaq.org
Example
@CesarHgt @tomitribejugbaq.org
MicroProfile Fault Tolerance
@CesarHgt @tomitribejugbaq.org
The Rise of Resilience and Eventual Consistency
● Distributed systems
● All-or-nothing approach.
● Resilience
● Design patterns under the Fault Tolerance umbrella:
○ Bulkhead – isolate failures in part of the system.
○ Circuit breaker – offer a way to fail fast.
○ Retry – define criteria on when to retry.
○ Fallback – provide an alternative solution for a failed execution.
○ Timeout
@CesarHgt @tomitribejugbaq.org
@Timeout @Retry
import org.eclipse.microprofile.faulttolerance.Timeout;
@Timeout(500)
public String callSomeServiceLogic() {...}
@Retry(retryOn = IOException.class)
public String callSomeServiceLogic() {...}
@Timeout(500)
@Retry(maxRetries = 1)
public String callSomeService() {...}
@CesarHgt @tomitribejugbaq.org
Creando Microservicios con
Java, MicroProfile y TomEE
Thank you

Creando microservicios con Java MicroProfile y TomEE - OGBT