JCON 2018, Düsseldorf: Talk by Mario-Leander Reimer (@LeanderReimer, Principal Software Architect at QAware)
Abstract:
With Java EE 8 finally released and the latest additions to MicroProfile it is about time we have a closer look at how to develop modern and lightweight microservices with the latest API improvements. This session is a comprehensive guide showing how to develop state-of-the-art microservices with the latest Java EE 8 APIs. We will start by giving an overview of Java EE 8 and the latest API additions and improvements. Then we will implement, build and package our first working Java EE 8 microservice. We will then dive into the details of implementing synchronous RESTful web services, and learn about the specifics of data binding and content marshalling using the JSON-B and JSON-P APIs. We will see how to leverage the power of using the asynchronous APIs on the server as well as the client-side, and how Server-Sent-Events (SSE) can be used for push communication. Finally, we will cover some advanced topics such as validation, security, resiliency, health checks and diagnosability.
Building Microservivces with Java EE 8 and Microprofile
1. Mario-Leander Reimer, QAware GmbH
mario-leander.reimer@qaware.de
Building Microservices
with Java EE 8 and Microprofile
Düsseldorf, 09. Oktober 2018
2. Mario-Leander Reimer
Principal Software Architect, QAware GmbH
Contact Details
Mail: mario-leander.reimer@qaware.de
Twitter: @LeanderReimer
Github: https://github.com/lreimer/
09.10.2018
2
Developer && Architect
20+ years of experience
#CloudNativeNerd
Open Source Enthusiast
Speaker && Author
3. Fork me on Github.
https://github.com/lreimer/building-javaee-microservices
5. 5
Components All Along the Software Lifecycle.
DESIGN
Complexity unit
Data integrity unit
Coherent and cohesive
features unit
Decoupled unit
RUN
Release unit
Deployment unit
Runtime unit
(crash, slow-down, access)
Scaling unit
n:1
BUILD
Planning unit
Team assignment unit
Knowledge unit
Development unit
Integration unit
1:1
6. Essential Design Principles.
6
Design for Distribution: Containers; microservices; API driven development.
Design for Configuration: One image, multiple environments.
Design for Resiliency: Fault-tolerant and self-healing.
Design for Elasticity: Scales dynamically and reacts to stimuli.
Design for Delivery: Short roundtrips and automated provisioning.
Design for Performance: Responsive; concurrent; resource efficient.
Design for Automation: Automated Dev & Ops tasks.
Design for Diagnosability: Cluster-wide logs, metrics and traces.
Design for Security: Secure Endpoints, API-Gateways, E2E-Encryption
11. @ApplicationPath("api")
public class JAXRSConfiguration extends Application {
}
@Path("hello")
public class HelloWorldResource {
@GET
@Produces(MediaType.APPLICATION_JSON)
public JsonObject helloWorld() {
String hostname = ofNullable(getenv("HOSTNAME")).orElse("localhost");
return Json.createObjectBuilder()
.add("message", "Cloud Native Application Development with Java EE.")
.add("hostname", hostname)
.build();
}
}
Our first JAX-RS application and REST endpoint.
11
12. // JSON Patch is a web standard format for describing changes in a JSON document.
// The JSON Patch media type is application/json-patch+json.
[ { "op": "replace", "path": "/0/aString", "value": "Patched JSON-P." } ]
JsonArray jsonArray = Json.createArrayBuilder()
.add(Json.createObjectBuilder()
.add("aString", "Hello Json-P.")
.add("aInteger", 42)
.add("aBoolean", false)
.add("aNullValue", JsonValue.NULL))
.build();
JsonPatch jsonPatch = Json.createPatchBuilder(jsonPatchArray).build();
jsonPatch.apply(jsonArray)
Use JSON-P for flexible parsing and processing.
12
Add: add a value into an object or array.
Remove: remove a value from an object or array.
Replace: replaces a value. Logically identical to
using remove and then add.
Copy: copy a value from one path to another by
adding the value at a specified to another location.
Move: moves a value from one place to another
by removing from one location and adding to
another.
Test: tests for equality at a certain path for a
certain value.
13. JsonbConfig jsonbConfig = new JsonbConfig()
.withDateFormat("dd.MM.yyyy", Locale.GERMANY)
.withFormatting(true)
.withPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CASE_WITH_DASHES)
.withNullValues(true)
.withPropertyOrderStrategy(PropertyOrderStrategy.REVERSE);
Jsonb jsonb = JsonbBuilder.newBuilder().withConfig(jsonbConfig).build();
String jsonBody = jsonb.toJson(jsonPojo);
JsonbPojo jsonbPojo = jsonb.fromJson(jsonBody, JsonbPojo.class);
@JsonbNillable
public class JsonbPojo {
@JsonbProperty(value = "greeting", nillable = true) private String message;
@JsonbNumberFormat("#,##0.00") private Integer answerToEverything;
@JsonbTransient private Object ignored;
}
Use JSON-B for easy data binding.
13
14. Building RESTful Web Services with Java EE 8
M.-Leander Reimer, Packt Publishing
eBook or Video Discount Code: BRWSWJ50
Print Discount Code: BRWSWJ15