I laugh at people that tell me that Java is slow, heavyweight and cumbersome. Maybe it was true when we had EJB2. I will prove to you that we can develop Enterprise Applications with just a few lines of code that can run in a Raspberry PI. If it runs in a PI, I think we can safely say it would run anywhere! To be able to do it, I'm going to use a new platform called Microprofile. Microprofile optimizes Enterprise Java for a Microservices Architecture and delivers application portability across multiple runtimes. You can use a subset of the Java EE specifications to develop Microprofile applications, with JAX-RS, CDI and JSON-P. Join me for this live coding session and help me spread the word that Java is actually great for the Enterprise.
6. Microservices
• Have you heard about Microservices?
• How big is a micro?
• Who develops Microservices?
• Who deploys Microservices?
• Who likes Microservices?
@radcortez | #TomEE
13. Why Microservices?
• Deliver new features quicker
• Smaller, agile teams
• Scale services independently
• Cloud
@radcortez | #TomEE
14. What is MicroProfile?
• http://microprofile.io/
• Enterprise Java for Microservices
• Open Source (Eclipse)
@radcortez | #TomEE
15. What is MicroProfile?
• Initial version 1.0 with CDI, JAX-RS, JSON-P
• Application portability across runtimes
@radcortez | #TomEE
16. What is MicroProfile?
• Currently at version 1.3
• Configuration, Fault Tolerance, JWT Propagation,
Health Check, Metrics, Open Tracing, Open API and
REST Client
• A version 2.0 is planned
@radcortez | #TomEE
18. Why MicroProfile?
@radcortez | #TomEE
0
10
20
30
40
EE 5 Specs (2006) EE 6 Specs (2009) EE 7 Specs (2013) EE 8 Specs (2017)
39
37
30
24
19. Why MicroProfile?
• A set of “standards” were required to offer guidance in
building MicroServices in Java
• These need to evolve very quickly to adjust to the fast
moving MicroServices world
@radcortez | #TomEE
21. What is TomEE?
• Tomcat + Java EE = TomEE
• Built within the OpenEJB community to offer a lightweight
alternative with Tomcat being the top dog.
• TomEE 7 is a MicroProfile implementation, including JAX-RS, CDI,
JSON-P and other specifications from Java EE.
• Currently working on MicroProfile 1.3.
@radcortez | #TomEE
24. JAX-RS
• RESTful Web Services
• Annotation based
• HTTP Centric
@radcortez | #TomEE
25. JSON-P
• Parse, Generate Transform and Query JSON
• Streaming API
• Object Model API
@radcortez | #TomEE
26. Putting it all together
@ApplicationScoped
@Path("books")
public class BookResource {
@Inject
private BookBean bookBean;
@GET
@Path("{id}")
public Response find(@PathParam("id") final Long id) {
final Book book = bookBean.find(id);
final JsonObjectBuilder builder = Json.createObjectBuilder();
builder.add("id", book.getId())
.add("name", book.getTitle());
return Response.ok(builder.build().toString()).build();
}
}
@radcortez | #TomEE
27. Show me the code!
@radcortez | #TomEE
https://github.com/radcortez/microprofile-samples
28. Deploy
• Raspberry PI running Hypriot OS.
• Package everything with Docker
• Local Docker Registry
• Push Docker Images with Ansible
@radcortez | #TomEE
29. Raspberry PI
• Raspberry V3
• HypriotOS is a minimal Debian dist
• Optimized to run Docker on a Raspberry PI
@radcortez | #TomEE
30. Docker
• Use Maven Fabric8 Plugin
• Docker Hub too slow
• Use a Local Docker Registry
• Speed up Deployment
@radcortez | #TomEE
31. Ansible
• Automate Tasks
• Ping PIs
• Deploy Docker Images, Start, Stop
• Ansible Playbooks
• List of tasks to perform
@radcortez | #TomEE
33. Configuration
• Applications need configuration based on their running
environment
• It must be possible to change configuration without
repacking the application
• Based on DeltaSpike Config, Apache Tamaya and Sabot
@radcortez | #TomEE
36. Metrics
• Monitor essential System Parameters
• Ensure reliable operation of software
• Monitoring endpoints to collect data
@radcortez | #TomEE
37. Metrics
• Accessible via REST interface
• /metrics/base for MP compliant servers
• /metrics/application for Application specific Metrics
• /metrics/vendor for Server specific metrics
• OPTIONS provides metadata, such as Unit of measure
@radcortez | #TomEE
39. {
"fooVal": {
"unit": "milliseconds",
"type": "gauge",
"description": "The average duration of foo
requests during last 5 minutes",
"displayName": "Duration of foo",
"tags": "app=webshop"
},
"barVal": {
"unit": "megabytes",
"type": "gauge",
"tags": "component=backend,app=webshop"
}
}
OPTIONS /metrics/base
@radcortez | #TomEE
40. Healthchecks
• Probe the state of a computer node
• Primary target cloud infrastructure
• Automated processes to maintain the state of nodes
@radcortez | #TomEE
41. Healthchecks
• Simple API to specify health status
• /health JAX-RS endpoint reporting server health
• Response status indicates if the health check passed
• Payload can include more detail
@radcortez | #TomEE
44. Fault Tolerance
• Different strategies to guide the execution and result
of some logic
• TimeOut, RetryPolicy, Fallback, Bulkhead and Circuit
Breaker
• Inspired by Hystrix and Failsafe
@radcortez | #TomEE
45. @CircuitBreaker
@Fallback(NumberFallbackHandler.class)
public String getNumber() {
final Response response = numberApi.request().get();
if (OK.getStatusCode() == response.getStatus()) {
return response.readEntity(String.class);
}
throw new WebApplicationException(INTERNAL_SERVER_ERROR);
}
public class NumberFallbackHandler implements FallbackHandler<String> {
@Override
public String handle(final ExecutionContext context) {
return "FALLBACK_NUMBER";
}
}
@radcortez | #TomEE
46. JWT Propagation
• Security Tokens
• Most common: OAuth2, OpenID Connect JWT
• Lightweight way to propagate identities across
different services
@radcortez | #TomEE
47. OpenTracing
• Trace the flow of a request across services
• OpenTracing is a distributed tracing standard for
applications
• Java Binding to OpenTracing Implementation
@radcortez | #TomEE
49. Open API
• Java API for the OpenAPI v3 specification
• OpenAPI v3 is based on Swagger v2
• Annotations should be similar
@radcortez | #TomEE
50. @GET
@Path(“/findByStatus")
@Operation(
summary = "Finds Pets by status",
description = "Multiple status values can be
provided with comma separated strings")
public Response findPetsByStatus() { }
GET /openapi
/pet/findByStatus:
get:
summary: Finds Pets by status
description: Multiple status values can be provided with comma separated
strings
operationId: findPetsByStatus
@radcortez | #TomEE
51. REST Client
• Microservices typically talk REST to other services
• Several JAX-RS implementations already support
interface definition
• Consistent and easy to reuse
@radcortez | #TomEE
52. REST Client
• Type safe REST Service over HTTP
• Extends JAX-RS 2.0 API’s
• More natural code style
• Similar to Feign
@radcortez | #TomEE
53. @RegisterRestClient
public interface MyServiceClient {
@GET
@Path("/greet")
Response greet();
}
@ApplicationScoped
public class MyService {
@Inject
@RestClient
private MyServiceClient client;
}
@radcortez | #TomEE