:sectanchors: :toc: macro :toclevels: 2 :toc-title: Table of Content :numbered:
= Hands On Lab with Wildfly Swarm, Forge & Keycloak
toc::[]
== Goals
microservices
on Wildfly-Swarm
with Forge
.Keycloak
Hawkular
polyglot microservices
by replacing some of the services with their equivalent in Node.js
and Vert.X
microservices
with Docker
and Openshift
== Workflow Overview
While a microservices architecture doesn't mandate using a specific language for implementation we decided here to take a pragmatic approach, we will choose the Java EE
path with Wildfly-Swarm
, then later in our demo, once we completed our basic app, we will explore how these services can interact with other services that may be implemented using a different technology such as Spring Boot
, Vert.X
, Node.JS
etc...
But let's keep that for the last section.
=== Workflow As explained earlier this path will be implemented using Wildfly-Swarm. If you want to see how to work with other languages, skip to the last section.
image::images/msademo-greenfield-workflow.png[]
== The Use case for this Lab
=== Entity Model
image::images/hol/Entities.png[]
=== Workflow
image::images/hol/Workflow.png[]
=== Architecture Pattern
image::images/hol/Architecture%20Pattern.png[]
== Configure your environment and core services
In this section we will be following the steps as defined in the Overview section. The steps can be followed an executed as individual pieces or you can run the full Forge
script script available link:wsfk-hol.fsh[here].
=== Setup Core Services
==== Security - Keycloak
In this session we will be using the purposely build Keycloak Wildfly-Swarm uberjar
.
==== Application Performance Management - Hawkular
We will be using the application performance management, distributed tracing and Business Transaction Management capabilities that are available with https://github.com/hawkular/hawkular-apm[Hawkulat APM]
To install, download and unpack the Hawkular APM distribution. The distribution includes both the server and the agent.
Create an application user with "read-write,read-only" roles.
Once user is created, start the server with a port offset to avoid conflict with the other services:
Then before running any of the services, in the command shell run:
Once you have build the services later in this lab, run the service with $JAVA_OPTS
supplied:
=== Tools, SDK and IDEs you will need to install the following on your machine:
=== Start Forge
image::images/forge-start.png[]
== Let's build our services
=== Create The BookService Project in Forge
project-new --named bookservice --stack JAVA_EE_7
jpa-new-entity --named Author jpa-new-field --named name
jpa-new-entity --named Book jpa-new-field --named title jpa-new-field --named isbn jpa-new-field --named author --type org.bookservice.model.Author --relationship-type Many-to-One
jpa-new-entity --named SellingPoint jpa-new-field --named name jpa-new-field --named latitude --type Double jpa-new-field --named longitude --type Double
scaffold-generate --provider AngularJS --generate-rest-resources --targets org.bookservice.model.*
wildfly-swarm-setup wildfly-swarm-detect-fractions --depend --build
=== Create The BookService Frontend Project in Forge
project-new --named bookstorefrontend --stack JAVA_EE_7 --type wildfly-swarm --http-port 8081 wildfly-swarm-add-fraction --fractions undertow mv ../bookservice/src/main/webapp/ src/main/
mkdir ../bookservice/src/main/webapp mkdir ../bookservice/src/main/webapp/WEB-INF
=== Create The SellingPoint service Project in Forge
project-new --named sellingPoint --stack JAVA_EE_7 --type wildfly-swarm --http-port 8082 wildfly-swarm-add-fraction --fractions hibernate-search
jpa-new-entity --named Book jpa-new-field --named isbn java-add-annotation --annotation org.hibernate.search.annotations.Field --on-property isbn
jpa-new-entity --named SellingPoint jpa-new-field --named name java-add-annotation --annotation org.hibernate.search.annotations.Indexed java-add-annotation --annotation org.hibernate.search.annotations.Spatial jpa-new-field --named latitude --type Double jpa-new-field --named longitude --type Double java-add-annotation --annotation org.hibernate.search.annotations.Longitude --on-property longitude java-add-annotation --annotation org.hibernate.search.annotations.Latitude --on-property latitude jpa-new-field --named books --type org.sellingPoint.model.Book --relationship-type Many-to-Many --fetch-type EAGER java-add-annotation --annotation org.hibernate.search.annotations.IndexedEmbedded --on-property books
scaffold-generate --provider AngularJS --generate-rest-resources --targets org.sellingPoint.model.* wildfly-swarm-detect-fractions --depend --build
=== Add geospatial endpoint in SellingPoint
In src/main/java/org/sellingPoint/rest/SellingPointEndpoint.java
add this method :
=== Update Frontend App to consume the new SellingPoint Service
Now, from the http://localhost:8081/app.html#/SellingPoints
you can search a specific Book by its isbn
in a radius of 5 Km around you (your geolocation is automatically retrieved but you can override it on the search form).
To get back at least one result, make sure that your boostrap data contains at least a SellingPoint that is your area or use the SellingPoint embedded frontend http://localhost:8083/app.html
.
=== Secure the endpoints with KeyCloak
==== Create Swarm Keycloak Server
==== Configure Keycloak Server
localhost:8083/auth
, you will have to create initially an Admin userlink:scripts/holrealm.json[the demo realm]
sebi
/ password : password
==== Secure the services
(Be sure to start from the link:scripts/[scripts]
folder)
cp bookservice_assets/keycloak.json bookservice/src/main/webapp/WEB-INF cd bookservice
wildfly-swarm-add-fraction --fractions keycloak security-add-login-config --auth-method KEYCLOAK --security-realm master security-add-constraint --web-resource-name Book --url-patterns /rest/* --security-roles user
rm src/main/java/org/bookservice/rest/NewCrossOriginResourceSharingFilter.java
cd ~~ cd ..
cp frontend_assets/keycloak.json bookstorefrontend/src/main/webapp cp frontend_assets/keycloak.js bookstorefrontend/src/main/webapp/scripts/vendor cp frontend_assets/app.js bookstorefrontend/src/main/webapp/scripts cp frontend_assets/app.html bookstorefrontend/src/main/webapp cd bookstorefrontend
cd ~~ cd ..
cp sellingpoint_assets/keycloak.json sellingPoint/src/main/webapp/WEB-INF cd sellingPoint wildfly-swarm-add-fraction --fractions keycloak security-add-login-config --auth-method KEYCLOAK --security-realm master security-add-constraint --web-resource-name SellingPoint --url-patterns /rest/* --security-roles user rm src/main/java/org/sellingPoint/rest/NewCrossOriginResourceSharingFilter.java
=== Enable Application Performance Monitoring
=== Putting it all together
You can run all the above commands from a single script. link:scripts/wsfk-hol.fsh[wsfk-hol.fsh]
Let's generate the uberjar
and run, using either ways:
or via your IDE with the Main
class.
=== Bootstrap data
IMPORTANT: For both of these scripts, make sure they have the name import.sql
and put them in src/main/resources
for each of the projects, they will be run at startup.
insert into Author (id, name, version) values (1000,'Seb',0); insert into Author (id, name, version) values (1001,'George',0);
insert into Book (id, isbn, version) values (1000, '1000',0); insert into Book (id, isbn, version) values (1001, '1001',0); insert into Book (id, isbn, version) values (1002, '1002',0);
insert into SellingPoint (id, latitude, longitude, name, version) values (2000, 43.5780, 7.0545, 'bob', 0); insert into SellingPoint (id, latitude, longitude, name, version) values (2001, 43.574357, 7.1142449, 'chris',0);
== Extend the Demo with more capabilities Now we have seen how to implement basic set of services, secure and monitor them, we would like to go one step further with moving those from traditional bare-metal environment to leverage new deployment model and targets. In the table below we explain briefly what capabilities are available in the different target environment to help you understand what you get for free or what you will to bring yourself depending on where you choose to deploy your services.
In this section we will demonstrate how to move the you build in the previous steps to Openshift and how you can benefit from its built-in capabilities.
[cols="1,1,1,1", options="header"] .capabilities per target platforms :=== Capabilities:Bare Metal:Container:Openshift
Management:yes [Hawkular Fraction] : yes : yes Security:yes [KeyCloak Fraction] : yes : yes Logging:::
:===
=== Polyglot microservices
In this section we will be taking some of our services and implementing them using Node.js, Vert.X and Spring Boot. We want them to be able to communicate with others services, remain secured and being monitored.
=== Integration
Explore Camel here
=== Deploying on Openshift
While this Lab has been focused on single machine/single instance development, there are critical capabilities that needs to be addressed if we want to deploy our solutions in productions. This section aims at listing those capabilities that comes for free in a PaaS environment like Openshift. We will demonstrate how our application can benefit from them.
==== Load balancing and fail-over ==== Discovery ==== API Mgt ==== Logging