building a microservice federation with grails

94

Upload: spring-io

Post on 14-Jan-2017

1.413 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Building a Microservice Federation with Grails
Page 2: Building a Microservice Federation with Grails

Building aBuilding aMicroserviceMicroservice

Federation with GrailsFederation with Grails

Grails... microservices...you must be crazy man!

Page 3: Building a Microservice Federation with Grails

About meAbout me20 years Java

10+ years Spring

6+ years Groovy/Grails

PowerBuilder

C/C++

FORTRAN1st career in aviation/aerospace

twitter: @jackfrosch linkedin: ../in/jackfroschemail: [email protected] grails.slack.com: @jackfrosch

Page 4: Building a Microservice Federation with Grails

{ "about-me" :

{ "experience" : [ "20 years Java",

"6+ years Groovy/Grails",

"PowerBuilder, C/C++, FORTRAN"

"Aviation/Aerospace before Software Development"],

"community" : [ "Gateway JUG founder and leader",

"Past DFW GGUG Co-leader",

"Always cookin' up something ..."],

"contacts" : [ {"twitter" : "@jackfrosch"},

{"linkedin" : "../in/jackfrosch"},

{"email" : "[email protected]"},

{"grails.slack.com":"@jackfrosch"}]

}

}

Page 5: Building a Microservice Federation with Grails

The Story I'm About to TellThe Story I'm About to TellYou is TrueYou is True

Microservices are coming!Grails plays well in a microservice architectureBuilding a microservice federation with Grails

Page 6: Building a Microservice Federation with Grails

DemoDemo

Page 7: Building a Microservice Federation with Grails

Microservices are comingMicroservices are comingFoundational conceptsMicroservice advantagesMicroservice disadvantages

Page 8: Building a Microservice Federation with Grails

What are microservices?What are microservices?

“ MicroservicesMicroservicesare small,are small,

autonomousautonomousservices thatservices that

work together.work together.

Sam Newman Building Microservices, O'Reilly Media

Page 9: Building a Microservice Federation with Grails

Small is keySmall is key

Page 10: Building a Microservice Federation with Grails

Small in functional scope...Small in functional scope...

http://www.whattofix.com/images/ComplexERDExample.gif

Probably not small functional scope...

Page 11: Building a Microservice Federation with Grails

... not necesarily size... not necesarily size

Page 12: Building a Microservice Federation with Grails

Smaller is simplerSmaller is simpler

“ Everything shouldEverything shouldbe made as simple asbe made as simple as

possible, but notpossible, but notsimpler*simpler*

* though attributed to Einstein, this simple quote is actually from

Roger Sessions paraphrasing (and simplifying!) a statement by Albert Einstein

Page 13: Building a Microservice Federation with Grails

Simpler is betterSimpler is better

“ A system that is hard toA system that is hard tounderstand is hard to change.understand is hard to change.

— Eric Evans, Domain-Driven Design

Page 14: Building a Microservice Federation with Grails

Autonomy is keyAutonomy is key

Page 15: Building a Microservice Federation with Grails

Development autonomy*Development autonomy*

Page 16: Building a Microservice Federation with Grails

*Caveat: *Caveat: YouYou can't break can't break mymy stuff stuff

http://www.memes.com/meme/498049

Page 17: Building a Microservice Federation with Grails

Consumer Driven ContractsConsumer Driven Contracts

http://bit.ly/thoughtworks-consumer-driven-contracts

Page 18: Building a Microservice Federation with Grails

Testing: How much & what kind?Testing: How much & what kind?

http://famouswonders.com/wp-content/gallery/pyramids-of-egypt/pyramid-of-khafre.jpg

Page 19: Building a Microservice Federation with Grails

Data AutonomyData Autonomy

http://martinfowler.com/articles/microservices.html

Page 20: Building a Microservice Federation with Grails

Build/Deploy autonomyBuild/Deploy autonomy

http://www.openmakesoftware.com/images/ReleaseEngineer/CI-CD.png

Page 21: Building a Microservice Federation with Grails

Scale where the load is...Scale where the load is...

Page 22: Building a Microservice Federation with Grails

Operational AutonomyOperational Autonomy

https://media.licdn.com/mpr/mpr/p/8/005/083/1a8/257d716.jpg

Page 23: Building a Microservice Federation with Grails

All this sounds great, but...All this sounds great, but...

...what about our monolith?...what about our monolith?

Page 24: Building a Microservice Federation with Grails

Identify bounded contexts...Identify bounded contexts...

... and divide along the seams... and divide along the seams

Page 25: Building a Microservice Federation with Grails

Favor choreography over orchestrationFavor choreography over orchestration

http://kennysilva.net/wp-content/uploads/2010/12/orchestra-conductor.jpg

Page 26: Building a Microservice Federation with Grails

MonolithMonolith MicroserviceMicroserviceclass Passenger { String accountNo String firstName String lastName Address billingAddress Payment paymentPreference List<Payment> paymentHistory Phone home Phone mobile Phone work BloodType bloodType ...}

What about shared domain?What about shared domain?

// For Trip Managementclass Passenger { String accountNo String firstName String lastName Phone mobile ...}

// For Billingclass Passenger { String accountNo String firstName String lastName Address billingAddress List<Payment> paymentHistory Phone home Phone work ...}

“ The evils of too much coupling between

services are far worse than the problems

caused by code duplication.- Sam Newman, Building Microservices

Page 27: Building a Microservice Federation with Grails

Microservice FederationMicroservice Federation

LoggingSecurityMetrics and monitoring

Page 28: Building a Microservice Federation with Grails

First, what's a federation?First, what's a federation?

“ an organization that is made by looselyjoining together smaller organizations

http://www.merriam-webster.com/dictionary/federation

“ the formation of a political unity, with a central government, by a numberof separate states, each of which retains control of its own internal affairs.

http://dictionary.reference.com/browse/federation

In our terms: The formation of an application, with central governance ofcommon infrastructure concerns, by a number of separate microservices,

each of which retains control of its own internal design and implementation.

Page 29: Building a Microservice Federation with Grails

Real world examplesReal world examples

“ The advancement and diffusion of knowledgeis the only guardian of true liberty.

- James Madison, father of the US ConstitutionKnowledge is power

Page 30: Building a Microservice Federation with Grails

Logging TipsLogging TipsUse a Correlation IDUse a consistent log message formatUse log aggregation

Page 31: Building a Microservice Federation with Grails

Logging AggregatorsLogging Aggregators

Page 32: Building a Microservice Federation with Grails

"ELK Stack"

Page 33: Building a Microservice Federation with Grails

SecuritySecurityNoneAt the gateway onlyAt every microservice

Page 34: Building a Microservice Federation with Grails

Security at the gateway onlySecurity at the gateway only

"They're inside the room!" "Yikes!"

"Mmm, you look tasty."

Perimeter security reminds me of the movie, Aliens

Page 35: Building a Microservice Federation with Grails

Security at every microserviceSecurity at every microservice

Page 36: Building a Microservice Federation with Grails

Metrics, Monitoring & MoreMetrics, Monitoring & MoreMicroservices need to report metricsResiliency requires monitoring & circuit breakersAutomatic service discoveryGateway / reverse proxyLoad balancing

Spring Boot & Spring Cloud to the rescue!

Page 37: Building a Microservice Federation with Grails

Spring Boot includes metrics endpointSpring Boot includes metrics endpoint

There are manymetrics out of the box,

but you can createyour own.

http://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-endpoints.html

Page 38: Building a Microservice Federation with Grails

Spring Boot includes Spring Boot includes manymany endpoints endpoints

http://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-endpoints.html

Page 39: Building a Microservice Federation with Grails

Spring Cloud provides monitoringSpring Cloud provides monitoring

... and much more... and much more

Page 40: Building a Microservice Federation with Grails

Service discovery with EurekaService discovery with Eureka

Page 41: Building a Microservice Federation with Grails

Gateway /Rev Proxy with ZuulGateway /Rev Proxy with Zuul

http://bit.ly/vignette2_wikia_nocookie_net_ghostbusters

Page 42: Building a Microservice Federation with Grails

Gateway /Rev Proxy with ZuulGateway /Rev Proxy with Zuul

Page 43: Building a Microservice Federation with Grails

Monitoring & Circuit BreakersMonitoring & Circuit Breakers w/ Hystrixw/ Hystrix

http://martinfowler.com/bliki/CircuitBreaker.html

Page 44: Building a Microservice Federation with Grails

Microservice ArchitectureMicroservice ArchitectureAdvantagesAdvantages

ScalableAdaptableResilientTestableMore future-proofMore greenfield

Page 45: Building a Microservice Federation with Grails

Microservice ArchitectureMicroservice ArchitectureDisadvantagesDisadvantages

http://martinfowler.com/bliki/images/microservicePrerequisites/sketch.png

Page 46: Building a Microservice Federation with Grails

Microservice ArchitectureMicroservice ArchitectureDisadvantagesDisadvantages

Solid domain understandingDevelopment more complicatedMore buildsMore databasesLog aggregation is essentialMetrics, monitoring & more essential

Page 47: Building a Microservice Federation with Grails

Grails plays wellGrails plays wellGrails full stack developmentGrails RESTGrails plugin architecture

Full stack != monolith

Page 48: Building a Microservice Federation with Grails

Grails full stack developmentGrails full stack development

Web UI, SQL / NOSQL Database Support, Spring, Hibernate,Security, REST, Java, Groovy...

Page 49: Building a Microservice Federation with Grails

Create AppCreate App$grails create-app HelloWorld

Page 50: Building a Microservice Federation with Grails

Hello World app... easy as 1 - 2 - 3Hello World app... easy as 1 - 2 - 3

grails> create-controller demo.Hello

package demo

class HelloController {

def index() { render 'Hello SpringOne2GX 2015!' }}

Full stack CRUD apps are about as easy...

1. Create controller

2. Make it do something

3. See it to believe it!

Page 51: Building a Microservice Federation with Grails

Create a Domain ClassCreate a Domain Class

Page 52: Building a Microservice Federation with Grails

Define demo.PersonDefine demo.Person

package demo

class Person { String firstName String lastName String email String twitterHandle

static constraints = { firstName blank:false, maxSize: 32 lastName blank: false, maxSize: 64 email blank: false, maxSize: 128, email:true twitterHandle nullable:true, maxSize:64 }}

Page 53: Building a Microservice Federation with Grails

Generate Scaffolding...Generate Scaffolding...

Page 54: Building a Microservice Federation with Grails

.. to bootstrap CRUD app.. to bootstrap CRUD app

Page 55: Building a Microservice Federation with Grails

After generationAfter generation

Page 56: Building a Microservice Federation with Grails

Run AppRun Appgrails> run-app

Page 57: Building a Microservice Federation with Grails

Retrieve Persons ListRetrieve Persons List

Page 58: Building a Microservice Federation with Grails

Create Person formCreate Person form

Page 59: Building a Microservice Federation with Grails

Create a Person recordCreate a Person record

Page 60: Building a Microservice Federation with Grails

Show a Person recordShow a Person record

Page 61: Building a Microservice Federation with Grails

Retrieve Persons ListRetrieve Persons List

Page 62: Building a Microservice Federation with Grails

Delete a Person recordDelete a Person record

Page 63: Building a Microservice Federation with Grails

Test RestTest Rest

$ curl -i -X GET http://localhost:8080/persons

[jfrosch@localhost demo]$ HTTP/1.1 200 OKServer: Apache-Coyote/1.1X-Application-Context: application:developmentContent-Type: text/xml;charset=UTF-8Transfer-Encoding: chunkedDate: Mon, 07 Sep 2015 21:26:16 GMT

<?xml version="1.0" encoding="UTF-8"?><list />

$

Hmm... 200 OK good ... XML bad.

Page 64: Building a Microservice Federation with Grails

With an IDE...With an IDE...

Page 65: Building a Microservice Federation with Grails

Besides UI CRUD, what about REST?Besides UI CRUD, what about REST?

package demo

import grails.rest.Resource

@Resource(uri='/persons')class Person { String firstName String lastName String email String twitterHandle

static constraints = { firstName blank:false, maxSize: 32 lastName blank: false, maxSize: 64 email blank: false, maxSize: 128, email:true twitterHandle nullable:true, maxSize:64 }}

grails create-app RestfulCrud

Page 66: Building a Microservice Federation with Grails

Grails content negotiationGrails content negotiation

$ curl -i -X GET http://localhost:8080/persons.json

HTTP/1.1 200 OKServer: Apache-Coyote/1.1X-Application-Context: application:developmentContent-Type: text/xml;charset=UTF-8Transfer-Encoding: chunkedDate: Mon, 07 Sep 2015 21:26:16 GMT

[]

$

200 OK good ... JSON good.

Page 67: Building a Microservice Federation with Grails

Grails content negotiationGrails content negotiation

$ curl -i -X GET --header "Accept:application/json" http://localhost:8080/persons

HTTP/1.1 200 OKServer: Apache-Coyote/1.1X-Application-Context: application:developmentContent-Type: text/xml;charset=UTF-8Transfer-Encoding: chunkedDate: Mon, 07 Sep 2015 21:26:16 GMT

[]

$

200 OK good ... JSON good.

Page 68: Building a Microservice Federation with Grails

Change @Resource to prefer JSONChange @Resource to prefer JSON

package demo

import grails.rest.Resource

@Resource(uri='/persons', formats=['json', 'xml'])class Person { String firstName String lastName String email String twitterHandle

static constraints = { firstName blank:false, maxSize: 32 lastName blank: false, maxSize: 64 email blank: false, maxSize: 128, email:true twitterHandle nullable:true, maxSize:64 }}

Page 69: Building a Microservice Federation with Grails

Test RestTest Rest

$ curl -i -X GET http://localhost:8080/persons

HTTP/1.1 200 OKServer: Apache-Coyote/1.1X-Application-Context: application:developmentContent-Type: text/xml;charset=UTF-8Transfer-Encoding: chunkedDate: Mon, 07 Sep 2015 21:26:16 GMT

[]

$

200 OK good ... JSON good.

Page 70: Building a Microservice Federation with Grails

Test RestTest Rest

$ curl -i -X GET http://localhost:8080/persons.xml

HTTP/1.1 200 OKServer: Apache-Coyote/1.1X-Application-Context: application:developmentContent-Type: text/xml;charset=UTF-8Transfer-Encoding: chunkedDate: Mon, 07 Sep 2015 21:26:16 GMT

<?xml version="1.0" encoding="UTF-8"?><list />

$

200 OK good ... XML weird, but you wanted weird

Page 71: Building a Microservice Federation with Grails

Restfully create a Person...Restfully create a Person...

Page 72: Building a Microservice Federation with Grails

... yields a 201 created response... yields a 201 created response

Page 73: Building a Microservice Federation with Grails

Grails REST URI patternsGrails REST URI patterns

Page 74: Building a Microservice Federation with Grails

What if I want to do more than CRUD?What if I want to do more than CRUD?

package demo

import grails.rest.RestfulController

class PersonController extends RestfulController<Person> { static responseFormats =['json', 'xml']

PersonController() { super(Person) }

def findPersonsWithLastNameLike(String likeness) { respond Person.findByLastNameLike("${likeness}%") }}

class UrlMappings {

static mappings = { "/persons"(resources:'person')

"/persons/search/withLastNameLike/$likeness"(controller: 'person', action: 'findPersonsWithLastNameLike') }}

Page 75: Building a Microservice Federation with Grails

Building a GrailsBuilding a Grailsmicroservice federationmicroservice federation

Page 76: Building a Microservice Federation with Grails

Build a Eureka ServerBuild a Eureka Server

Page 77: Building a Microservice Federation with Grails

Build a Eureka ServerBuild a Eureka Server

Page 78: Building a Microservice Federation with Grails

Build a Zuul ServerBuild a Zuul Server

/catalog acts as the root of the uri

Page 79: Building a Microservice Federation with Grails

Build a Zuul ServerBuild a Zuul Server

Page 80: Building a Microservice Federation with Grails

Zuul MappingsZuul Mappings

Page 81: Building a Microservice Federation with Grails

Finding Products Using ZuulFinding Products Using Zuul

Page 82: Building a Microservice Federation with Grails

Finding Product 1 Using ZuulFinding Product 1 Using Zuul

Page 83: Building a Microservice Federation with Grails

Finding Product 3 Using ZuulFinding Product 3 Using Zuul

This is the default after error in recommendation engine

Page 84: Building a Microservice Federation with Grails

Hystrix DashboardHystrix Dashboard

Page 85: Building a Microservice Federation with Grails

Hystrix DashboardHystrix Dashboard

Page 86: Building a Microservice Federation with Grails

Hystrix DashboardHystrix Dashboard

Page 87: Building a Microservice Federation with Grails

Hystrix DashboardHystrix Dashboard

Page 88: Building a Microservice Federation with Grails

Hystrix DashboardHystrix Dashboard

... After killing recommendation engine ...

Page 89: Building a Microservice Federation with Grails

@HystrixCommand@HystrixCommand

Page 90: Building a Microservice Federation with Grails

Microservice SetupMicroservice Setup

Page 91: Building a Microservice Federation with Grails

Microservice SetupMicroservice Setup

Page 92: Building a Microservice Federation with Grails

SummarySummary

AdaptableScalableResilientMaintainableMonitorableCloud ready

You'd be crazy to adopt amicroservice architecture using

Grails...

unless your applicationneeds to be ...

(even if your company isn't ready for the cloud yet)

Page 93: Building a Microservice Federation with Grails

ResourcesResourceshttp://projects.spring.io/spring-cloud/

http://cloud.spring.io/spring-cloud-netflix/

http://martinfowler.com/articles/microservices.html

Page 94: Building a Microservice Federation with Grails

Questions?Questions?