31
Using Magnolia in a Microservices Architecture Presented by Nicolas Barbé Broadcast on 2015-09-10

Using Magnolia in a Microservices Architecture

Embed Size (px)

Citation preview

Page 1: Using Magnolia in a Microservices Architecture

Using Magnolia in a Microservices Architecture Presented by Nicolas Barbé Broadcast on 2015-09-10

Page 2: Using Magnolia in a Microservices Architecture

Nicolas Barbé

Software engineer and technology enthusiast working for Magnolia

Page 3: Using Magnolia in a Microservices Architecture

MICROSERVICES

“Loosely coupled service oriented architecture

with bounded contexts” Adrian Cockcroft

Page 4: Using Magnolia in a Microservices Architecture

Your organisation can scale

WHY DOES IT MATTER?

Page 5: Using Magnolia in a Microservices Architecture

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

Page 6: Using Magnolia in a Microservices Architecture

COMMON PATTERNS

Page 7: Using Magnolia in a Microservices Architecture

REQUEST - RESPONSErequest

response

BLOCKED

one to one and synchronous

client server

This is a service

Page 8: Using Magnolia in a Microservices Architecture

PUBLISH - SUBSCRIBE

publish

one to many and asynchronous

subscribetopic

producer

consumers

Page 9: Using Magnolia in a Microservices Architecture

FACADE

C

Facade B

A

A facade provides an uniformed API abstracting several underlying services

Request

Page 10: Using Magnolia in a Microservices Architecture

PROXY

C

proxy B

A

A proxy delegates a request to one of the underlying services

Request

Page 11: Using Magnolia in a Microservices Architecture

CRUD

Create / Read / Update / Delete

Straightforward mapping to DB operators but does not perform well with task based UI

Page 12: Using Magnolia in a Microservices Architecture

CQRS

Different models for queries and commands

Commands Queries

Page 13: Using Magnolia in a Microservices Architecture

ROLE OF MAGNOLIA๏ Editorial Content - Magnolia

๏ Dynamic or User Generated Content ๏ Content & Business Logic - Microservices ๏ Frontend - Magnolia ๏ Backend - Magnolia

Page 14: Using Magnolia in a Microservices Architecture

MAGNOLIA ASKA proof of concept of a Q&A website like stack-overflow

Page 15: Using Magnolia in a Microservices Architecture

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

Page 16: Using Magnolia in a Microservices Architecture

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

Page 17: Using Magnolia in a Microservices Architecture

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

}

Page 18: Using Magnolia in a Microservices Architecture

DEEP DIVE IN THE IMPLEMENTATION

Page 19: Using Magnolia in a Microservices Architecture

CONTAINERS

Additional layer of abstraction and automation of an OS

Page 20: Using Magnolia in a Microservices Architecture

CONTAINERS OR VM?

Actually both !

Virtual machine for the hosts Containers for the microservices

Page 21: Using Magnolia in a Microservices Architecture

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

Page 22: Using Magnolia in a Microservices Architecture

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

Page 23: Using Magnolia in a Microservices Architecture

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

Page 24: Using Magnolia in a Microservices Architecture

MORE INFORMATIONhttp://nicolasbarbe.com/2015/01/02/a-docker-image-for-magnolia/

Page 25: Using Magnolia in a Microservices Architecture

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

Page 26: Using Magnolia in a Microservices Architecture

… 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/

Page 27: Using Magnolia in a Microservices Architecture

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

Page 28: Using Magnolia in a Microservices Architecture

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>

Page 29: Using Magnolia in a Microservices Architecture

MANAGING CONTENTPresenters uses

Query sideActions connect to the

Command side

Page 30: Using Magnolia in a Microservices Architecture

HTTPS://GITHUB.COM/NICOLASBARBE/MAGNOLIA-ASK

Page 31: Using Magnolia in a Microservices Architecture

QUESTIONS?