Upload
magnolia
View
2.759
Download
0
Embed Size (px)
Citation preview
Using Magnolia in a Microservices Architecture Presented by Nicolas Barbé Broadcast on 2015-09-10
Nicolas Barbé
Software engineer and technology enthusiast working for Magnolia
MICROSERVICES
“Loosely coupled service oriented architecture
with bounded contexts” Adrian Cockcroft
Your organisation can scale
WHY DOES IT MATTER?
NOT A FREE LUNCHDistributed systems are complex ๏ Testability ๏ Eventual consistency ๏ Operational complexity
http://highscalability.com/blog/2014/4/8/microservices-not-a-free-lunch.html
COMMON PATTERNS
REQUEST - RESPONSErequest
response
BLOCKED
one to one and synchronous
client server
This is a service
PUBLISH - SUBSCRIBE
publish
one to many and asynchronous
subscribetopic
producer
consumers
FACADE
C
Facade B
A
A facade provides an uniformed API abstracting several underlying services
Request
PROXY
C
proxy B
A
A proxy delegates a request to one of the underlying services
Request
CRUD
Create / Read / Update / Delete
Straightforward mapping to DB operators but does not perform well with task based UI
CQRS
Different models for queries and commands
Commands Queries
ROLE OF MAGNOLIA๏ Editorial Content - Magnolia
๏ Dynamic or User Generated Content ๏ Content & Business Logic - Microservices ๏ Frontend - Magnolia ๏ Backend - Magnolia
MAGNOLIA ASKA proof of concept of a Q&A website like stack-overflow
WHICH SERVICES?References must be checked - Question / Answer - Question / User - Answer / User - Rating / Answer - Rating / Question
Users have only one vote Answers are single threaded
USERS
QUESTIONS ANSWERS
RATINGS
ARCHITECTURE
USERS-COMMANDS
users
RATINGS-COMMANDS
ANSWERS-COMMANDS
QUESTIONS-COMMANDS
USERS-QUERIES
votes
questions
answers
QUESTIONS-QUERIES
ANSWERS-QUERIESpub
pub
pub
pubsub
sub
sub
sub
sub
sub
sub
sub
sub
req/res
req/res
sub
Magn
olia
req/res
req/res
req/res
ANATOMY OF A MICROSERVICE
Commands Queries
QUESTIONS
‣ AskQuestion ‣ CloseQuestion
{
title: Eget lacinia odio sem nec elit?
description: Cum sociis natoque penatibus et magnis dis parturient ontes, nascetur ridiculus mus.
initiator: nbarbe
createdAt: 2015-08-14T14:45:43
closed: false
}
‣ ListQuestions ‣ ShowQuestion
{ title: Eget lacinia odio sem nec elit? description: Cum sociis natoque penatibus et magnis dis parturient ontes, nascetur ridiculus mus. initiator: Nicolas Barbé createdAt: 2015-08-14T14:45:43 closed: false votes: 12 answers: 3 answersBucketId: fh34t738
}
DEEP DIVE IN THE IMPLEMENTATION
CONTAINERS
Additional layer of abstraction and automation of an OS
CONTAINERS OR VM?
Actually both !
Virtual machine for the hosts Containers for the microservices
CONTAINER = MICROSERVICE๏ Clear Boundaries
๏ Reproducible Builds
๏ Network Plumbing
๏ From Dev to Prod to Dev
๏ Uniform Operations - Packaging - Distribution - Deployment / Upgrade - Backup / Restore - Monitoring - Logging - Snaphots
DOCKERIZE MAGNOLIA$ echo \ 'FROM nicolasbarbe/magnolia-base MAINTAINER Nicolas Barbé "https://github.com/nicolasbarbe" RUN wget -nv http://sourceforge.net/projects/magnolia/files/magnolia/Magnolia%20CE%205.4.2/magnolia-bundled-webapp-5.4.2.war/download?use_mirror=autoselect -O $CATALINA_BASE/webapps/ROOT.war' > Dockerfile
$ docker build -t magnolia .
$ docker run -p 3000:8080 magnolia
TIPS & TRICKS๏ Follows the recommendations of
the Filesystem Hierarchy Standard
๏ Configuration through environment variables
- INSTANCE_TYPE - DB_TYPE - DB_SCHEMA - DB_USERNAME - DB_PASSWORD - DEVELOP_MODE - CLUSTER_ID
๏ Tomcat & JVM settings can be customised COPY setenv.sh $CATALINA_BASE/bin/setenv.sh
๏ Map a volume from the host to the container to persist the JCR repository -v /var/lib/magnolia/repositories:/var/lib/magnolia/repositories
๏ Map a volume from the host to the container for the resources -v $MAGNOLIA_WEB_RESOURCES:/var/opt/magnolia
MORE INFORMATIONhttp://nicolasbarbe.com/2015/01/02/a-docker-image-for-magnolia/
FROM DEV …Build a clone of the production environment locally with Docker Compose db-author: image: postgres environment: - POSTGRES_USER=magnolia - POSTGRES_PASSWORD=mysecretpassword author: image: nicolasbarbe/ms-frontend:1.0-SNAPSHOT command: run ports: - "3000:8080" links: - db-author:db environment: - INSTANCE_TYPE=author - DB_TYPE=postgresql - DB_ADDRESS=db - DB_PORT=5432 - DB_SCHEMA=magnolia - DB_USERNAME=magnolia - DB_PASSWORD=mysecretpassword
Create a new container for the database
Create a network link between the database and magnolia
Create another container for Magnolia
Configure the instance as an author and to use the postgresql driver
Thanks to the link, we don’t have to specify the database IP address
… TO PROD๏ Use a full Docker stack
- Reuse the same Docker Compose definition - If the architectures are slightly different use extends keyword - Use Docker Swarm to deploy on multiple hosts - Use Docker Machine to provision the hosts
๏ But, wait, Docker Compose/Swarm are still in Beta - Use Kubernetes if you want a cluster with dynamic provisioning and hosts allocation - Use Ansible if you want static hosts provisioning and allocation (see my blog post*)
๏ In all cases use the same Docker Images !
* http://nicolasbarbe.com/2015/07/13/magnolia-devops-automate-deployment/
SERVICE DISCOVERYBuild a Service Registry to let Magnolia discovers the services automatically
The Service Registry
- Defines microservices in a YAML file
- Discovers the services through environment variables, Java system properties, from the JCR or by value
- Injects services in the templates
- Provides a generic connector to build dedicated content apps in YAML
services: questions: connection: host: QUESTIONS_QUERIES_HOST port: QUESTIONS_QUERIES_PORT type: env apiVersion: v1 resource: name: question properties: - name: id type: Integer
- name: title type: String
- name: description
https://github.com/nicolasbarbe/magnolia-http-utils
DISPLAYING CONTENT
[#assign id=ctx.getParameter("id")] [#assign question=httpfn.service("question").GET(id)]
<div class="row"> <h2>${question.title}</h2> <h4>${question.initiator}</h4> <p class="lead"> ${question.description} </p> <div class="pull-right">Asked ${question.createdAt?datetime?date}</div> </div>
MANAGING CONTENTPresenters uses
Query sideActions connect to the
Command side
HTTPS://GITHUB.COM/NICOLASBARBE/MAGNOLIA-ASK
QUESTIONS?