CON2306_Ashmore Writing Microservices in Java JavaOne 2015-10-28

download CON2306_Ashmore Writing Microservices in Java JavaOne 2015-10-28

of 42

Transcript of CON2306_Ashmore Writing Microservices in Java JavaOne 2015-10-28

  • 8/17/2019 CON2306_Ashmore Writing Microservices in Java JavaOne 2015-10-28

    1/42

    Writing Microservices in Java

    Given by Derek C. Ashmore

    October 28, 2015

    ©2015 Derek C. Ashmore, All Rights Reserved 1

  • 8/17/2019 CON2306_Ashmore Writing Microservices in Java JavaOne 2015-10-28

    2/42

    Who am I?• Professional Geek

    since 1987

    • Java/J2EE/Java EE

    since 1999

    • Roles include:• Developer

    • Architect

    • Project Manager

    •DBA

    • System Admin

    ©2015 Derek C. Ashmore, All Rights Reserved 2

    http://www.amazon.com/gp/product/0972954880/ref=as_li_ss_tl?ie=UTF8&camp=1789&creative=390957&creativeASIN=0972954880&linkCode=as2&tag=dvtpresscom-20

  • 8/17/2019 CON2306_Ashmore Writing Microservices in Java JavaOne 2015-10-28

    3/42

    Discussion Resources

    • This slide deck – http://www.slideshare.net/derekashmore 

    • Sample code on my Github –

    https://github.com/Derek-Ashmore/ 

    • Sample Java Microservice (Moneta) – https://github.com/Derek-Ashmore/moneta 

    • Slide deck has hyper-links!

     – Don’t bother writing down URLs 

    ©2015 Derek C. Ashmore, All Rights Reserved 3

    http://www.slideshare.net/derekashmorehttps://github.com/Derek-Ashmore/https://github.com/Derek-Ashmore/monetahttps://github.com/Derek-Ashmore/monetahttps://github.com/Derek-Ashmore/monetahttps://github.com/Derek-Ashmore/monetahttps://github.com/Derek-Ashmore/monetahttps://github.com/Derek-Ashmore/https://github.com/Derek-Ashmore/https://github.com/Derek-Ashmore/https://github.com/Derek-Ashmore/http://www.slideshare.net/derekashmorehttp://www.slideshare.net/derekashmore

  • 8/17/2019 CON2306_Ashmore Writing Microservices in Java JavaOne 2015-10-28

    4/42

    Agenda

    The “What”

    and “Why” of

    microservices

    DesignConsiderations

    and Patterns

    Cross-cuttingconcerns

    When to usemicroservices

    Summary /Q&A

    ©2015 Derek C. Ashmore, All Rights Reserved 4

  • 8/17/2019 CON2306_Ashmore Writing Microservices in Java JavaOne 2015-10-28

    5/42

    What are Microservices?

    • No concrete definition

    • Common microservice traits

     – Single functional purpose

    • Most/all changes only impact one service

    • Not dependent on execution context

     – “loosely coupled” 

     –Independent process/jvm

     – Standard Interface (typically Web Service/REST)

     – Analogy: Stereo system, Linux utilities

    ©2015 Derek C. Ashmore, All Rights Reserved 5

  • 8/17/2019 CON2306_Ashmore Writing Microservices in Java JavaOne 2015-10-28

    6/42

    Refactoring into Microservices

    • Databasesphysically

    separated

    • What to do with

    common dataneeds?

    • Service call or

    • Data copy

    ©2015 Derek C. Ashmore, All Rights Reserved 6

  • 8/17/2019 CON2306_Ashmore Writing Microservices in Java JavaOne 2015-10-28

    7/42

    No Lock-in• Platform agnostic

    • Fewer dependency

    conflicts

    • Still have cross-cutting

    concerns

    • “Toll” for first app 

    • Still have support

    concerns

    • Others need to be

    able to support your

    work

    7©2015 Derek C. Ashmore, All Rights Reserved

  • 8/17/2019 CON2306_Ashmore Writing Microservices in Java JavaOne 2015-10-28

    8/42

    Easier Management /

    Higher Throughput

    • Easier to manage largenumbers of developers

     – Concentrate onintelligently drawing

    service boundaries – Manage/enforce service

    contracts

    • Each service team worksindependently

    • Team independence leadsto higher developmentthroughput

    ©2015 Derek C. Ashmore, All Rights Reserved 8

  • 8/17/2019 CON2306_Ashmore Writing Microservices in Java JavaOne 2015-10-28

    9/42

    Agenda

    The “What”

    and “Why” of

    microservices

    DesignConsiderations

    and Patterns

    Cross-cuttingconcerns

    When to usemicroservices

    Summary /Q&A

    ©2015 Derek C. Ashmore, All Rights Reserved 9

  • 8/17/2019 CON2306_Ashmore Writing Microservices in Java JavaOne 2015-10-28

    10/42

    Design considerations

    • Service Boundaries (gerrymandering)

    • Service call Failure / Unavailability

    Data Integrity• Performance

    ©2015 Derek C. Ashmore, All Rights Reserved 10

  • 8/17/2019 CON2306_Ashmore Writing Microservices in Java JavaOne 2015-10-28

    11/42

    Service Boundaries

    • Core Services – Services responsible for maintaining a specific business area data

     – Usually named after Nouns• Service is a system of record for a

     – Student, Course, Classroom, etc.

    •Process Services – Services responsible for performing single complex tasks

     – Usually represents an Action or Process• Service is responsible for processing

     – Student applications, Debt collection, etc.

     – These services rely on core services

    • Partitioning is an art – Too few same drawbacks as traditional architecture

     – Too many excessive network hops

    ©2015 Derek C. Ashmore, All Rights Reserved 11

  • 8/17/2019 CON2306_Ashmore Writing Microservices in Java JavaOne 2015-10-28

    12/42

    Boundary Sanity Check

    • Verbalize a mission statement in one sentence

    in business terms

     – Examples

    • This service is the system of record for Student

    information

    • This service registers students for classes

    • This service suspends students

    • This service records student payments

    • This service produces official transcripts

    ©2015 Derek C. Ashmore, All Rights Reserved 12

  • 8/17/2019 CON2306_Ashmore Writing Microservices in Java JavaOne 2015-10-28

    13/42

    Context Independence Check

    • Does your service have multiple consumers? – Could it?

    • Could your service execute as easily in batch asonline? – If ‘No’, then you’re making context assumptions

    • Warning Signs – Spending time analyzing service call flow

    • Your services likely make context assumptions

     – Agonizing over which service should do a givenactivity• Maybe you need a new service

    ©2015 Derek C. Ashmore, All Rights Reserved 13

  • 8/17/2019 CON2306_Ashmore Writing Microservices in Java JavaOne 2015-10-28

    14/42

    Microservices are not about size

    ©2015 Derek C. Ashmore, All Rights Reserved 14

    ….. Microservices are about having a single business purpose!

  • 8/17/2019 CON2306_Ashmore Writing Microservices in Java JavaOne 2015-10-28

    15/42

    Designing for Failure

    • Dependent services could be down

     – Minimize human intervention

     – Fail sooner rather than later

     – Horizontal scaling / clustering is your first line of defense

     – Coding patterns can help as a backup

    • Common Patterns:

     – Retry

     –

    Circuit Breaker – Dispatch via Messaging

     – Service Call Mediator

    ©2015 Derek C. Ashmore, All Rights Reserved 15

  • 8/17/2019 CON2306_Ashmore Writing Microservices in Java JavaOne 2015-10-28

    16/42

    Retry Pattern

    ©2015 Derek C. Ashmore, All Rights Reserved 16

    • Best for asynchronous tasks• Limit the number of tries

    • Use sleep interval between tries

    • Only addresses temporary outages

    • Sample Retry Pattern implementation here.

    • Tooling Support: – Apache CXF supports Retry 

     – Spring Batch RetryTemplate 

     – Apache HttpClient (Example here)

    https://github.com/Derek-Ashmore/Insanityhttp://cxf.apache.org/docs/failoverfeature.htmlhttp://docs.spring.io/spring-batch/trunk/reference/html/retry.htmlhttp://hc.apache.org/httpcomponents-client-ga/tutorial/html/fundamentals.html#d5e303http://hc.apache.org/httpcomponents-client-ga/tutorial/html/fundamentals.html#d5e303http://docs.spring.io/spring-batch/trunk/reference/html/retry.htmlhttp://cxf.apache.org/docs/failoverfeature.htmlhttps://github.com/Derek-Ashmore/Insanity

  • 8/17/2019 CON2306_Ashmore Writing Microservices in Java JavaOne 2015-10-28

    17/42

    Circuit Breaker

    ©2015 Derek C. Ashmore, All Rights Reserved 17

  • 8/17/2019 CON2306_Ashmore Writing Microservices in Java JavaOne 2015-10-28

    18/42

    Circuit Breaker (continued)

    • Objective: Error out sooner – Conserves resources

     – Automatically “recovers” after a time period 

    • Modeled after home circuit

    • Works on thresholds – Number of errors required to trip circuit

     – Amount of time required to attempt retry

    • Has Hysterix support

    • Best embedded in interface clients / delegates• More information here.

    • Sample Circuit implementation here.

    ©2015 Derek C. Ashmore, All Rights Reserved 18

    https://github.com/Netflix/Hystrix/wiki/How-it-Works#circuit-breakerhttp://martinfowler.com/bliki/CircuitBreaker.htmlhttps://github.com/Derek-Ashmore/CircuitBreakerhttps://github.com/Derek-Ashmore/CircuitBreakerhttp://martinfowler.com/bliki/CircuitBreaker.htmlhttps://github.com/Netflix/Hystrix/wiki/How-it-Works#circuit-breakerhttps://github.com/Netflix/Hystrix/wiki/How-it-Works#circuit-breaker

  • 8/17/2019 CON2306_Ashmore Writing Microservices in Java JavaOne 2015-10-28

    19/42

    Dispatch via Messaging

    ©2015 Derek C. Ashmore, All Rights Reserved 19

    • Place work instruction on persistent queue• If receivers are down, work stacks in queue

    • Work throttled by number of receivers

    • Queue can be JMS or AMQP

    • Tooling Support: – JMS Api (easy API – many use natively)

     – Spring JMSTemplate  or RabbitTemplate (producer)

    http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/jms.htmlhttp://docs.spring.io/spring-amqp/api/org/springframework/amqp/rabbit/core/RabbitTemplate.htmlhttp://docs.spring.io/spring-amqp/api/org/springframework/amqp/rabbit/core/RabbitTemplate.htmlhttp://docs.spring.io/spring-amqp/api/org/springframework/amqp/rabbit/core/RabbitTemplate.htmlhttp://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/jms.html

  • 8/17/2019 CON2306_Ashmore Writing Microservices in Java JavaOne 2015-10-28

    20/42

    Service Call Mediator

    ©2015 Derek C. Ashmore, All Rights Reserved 20

    • Provide “Partial” functionality when dependentservices are down

    • Providing partial functionality better userexperience than complete outage – Airline Wifi provider providing service even if payment

    processing is down

    • Sample implementation here 

    https://github.com/Derek-Ashmore/TaskMediatorhttps://github.com/Derek-Ashmore/TaskMediator

  • 8/17/2019 CON2306_Ashmore Writing Microservices in Java JavaOne 2015-10-28

    21/42

    Designing for Performance

    • More network traffic

     – Make services course-grained

     – User Interfaces need a general manager

     –Horizontal or Vertical Scaling helps

    • Common Patterns:

     – Back-ends for Front-ends (a.k.a. API Gateway)

     – Dispatch via Messaging

     – Expiring Cache

    ©2015 Derek C. Ashmore, All Rights Reserved 21

  • 8/17/2019 CON2306_Ashmore Writing Microservices in Java JavaOne 2015-10-28

    22/42

    Back-ends for Front-ends

    ©2015 Derek C. Ashmore, All Rights Reserved 22

  • 8/17/2019 CON2306_Ashmore Writing Microservices in Java JavaOne 2015-10-28

    23/42

    Back-ends for Front-ends(continued)

    • Consolidates service calls for the browser

     – Enhances performance

    • Open web often not as performant as local LAN

    • Also known as “API Gateway” 

    • Implications

     – Don’t expose microservices directly to the

    browser

    ©2015 Derek C. Ashmore, All Rights Reserved 23

  • 8/17/2019 CON2306_Ashmore Writing Microservices in Java JavaOne 2015-10-28

    24/42

    Expiring Cache

    ©2015 Derek C. Ashmore, All Rights Reserved 24

  • 8/17/2019 CON2306_Ashmore Writing Microservices in Java JavaOne 2015-10-28

    25/42

    Expiring Cache (continued)

    • Look up data once and cache it – Evict data from the cache after a defined time period

     – Sometimes known as “Cache Aside” 

     – Reduces network calls for data

     –

    Trades memory for speed – More information here 

    • When to use – Only use with static data

     – Different clustered nodes “could” have different data for a short

    time• Tooling Support:

     – I recommend Google Guava 

     – EHCache, Gemfire, and other tools available

    ©2015 Derek C. Ashmore, All Rights Reserved 25

    https://msdn.microsoft.com/en-us/library/dn589799.aspxhttps://code.google.com/p/guava-libraries/wiki/CachesExplainedhttps://code.google.com/p/guava-libraries/wiki/CachesExplainedhttps://msdn.microsoft.com/en-us/library/dn589799.aspx

  • 8/17/2019 CON2306_Ashmore Writing Microservices in Java JavaOne 2015-10-28

    26/42

    Designing for Integrity

    • Services are context independent

     – Have no knowledge of how/when they are executed

    • One service == One Transaction

     – Two-phase commits/rollbacks are a much larger problem

    • Common Patterns:

     – Custom Rollback

    • Write your own reversing transaction

    ©2015 Derek C. Ashmore, All Rights Reserved 26

  • 8/17/2019 CON2306_Ashmore Writing Microservices in Java JavaOne 2015-10-28

    27/42

    Custom Rollback

    ©2015 Derek C. Ashmore, All Rights Reserved 27

  • 8/17/2019 CON2306_Ashmore Writing Microservices in Java JavaOne 2015-10-28

    28/42

    Custom Rollback (continued)

    • Reverses a transaction previously posted

    • Only use this for multi-service transactions

     – Keeping the transaction within one service is

    preferred

    • This pattern is completely custom

     – No special product support available

    • More information here 

    ©2015 Derek C. Ashmore, All Rights Reserved 28

    https://msdn.microsoft.com/en-us/library/dn589799.aspxhttps://msdn.microsoft.com/en-us/library/dn589799.aspx

  • 8/17/2019 CON2306_Ashmore Writing Microservices in Java JavaOne 2015-10-28

    29/42

    Common code between services?

    • Yes, but…. 

     – Version it; services make decision as to when to

    upgrade

     – Changes to common code can’t require thedeployment of multiple services

    • That ‘common code’ needs to be its own separate

    service

    • Tends *not* to have business logic as that can change

    and impact multiple services

    ©2015 Derek C. Ashmore, All Rights Reserved 29

  • 8/17/2019 CON2306_Ashmore Writing Microservices in Java JavaOne 2015-10-28

    30/42

    Agenda

    The “What”

    and “Why” of

    microservices

    DesignConsiderations

    and Patterns

    Cross-cuttingconcerns

    When to usemicroservices

    Summary /Q&A

    ©2015 Derek C. Ashmore, All Rights Reserved 30

  • 8/17/2019 CON2306_Ashmore Writing Microservices in Java JavaOne 2015-10-28

    31/42

    Cross-cutting Concerns

    • Deployment

    • Transaction tracking

    Security• Contract Testing

    • Same as traditional applications

     –

    Health checks – Logging consolidation

     – Performance measurement

    ©2015 Derek C. Ashmore, All Rights Reserved 31

  • 8/17/2019 CON2306_Ashmore Writing Microservices in Java JavaOne 2015-10-28

    32/42

    Deployment

    • Microservices are deployed as a process

     – For Java, embedded containers are easy

     – Spring Boot 

     – Dropwizard 

    • Docker – standardizes the process deployment

    and environment

    • Sample here.

    ©2015 Derek C. Ashmore, All Rights Reserved 32

    http://projects.spring.io/spring-boot/http://www.dropwizard.io/https://www.docker.com/https://github.com/Derek-Ashmore/monetahttps://github.com/Derek-Ashmore/monetahttps://www.docker.com/http://www.dropwizard.io/http://projects.spring.io/spring-boot/

  • 8/17/2019 CON2306_Ashmore Writing Microservices in Java JavaOne 2015-10-28

    33/42

    Correlation IDs

    •Provides context forservice calls or user

    actions

    • Track using HTTP

    Header

    • Log it on all messages /error reports

    • Include it on all service

    calls or message

    dispatches

    • Code sample here 

    • Spring Boot support has

    been requested 

    33©2015 Derek C. Ashmore, All Rights Reserved

    https://github.com/Derek-Ashmore/RequestCorrelationSlf4Jhttps://github.com/spring-projects/spring-boot/issues/2343https://github.com/spring-projects/spring-boot/issues/2343https://github.com/Derek-Ashmore/RequestCorrelationSlf4J

  • 8/17/2019 CON2306_Ashmore Writing Microservices in Java JavaOne 2015-10-28

    34/42

    Security

    ©2015 Derek C. Ashmore, All Rights Reserved 34

  • 8/17/2019 CON2306_Ashmore Writing Microservices in Java JavaOne 2015-10-28

    35/42

    Security (continued)

    • Keep User-level security to the UI

    • Microservice security in layers

     – Layer 1 – Network routing enforcement

    • Limit access only to within the firewall

    • Limit access to specific hosts or subnets

     – Layer 2 – Use Service Accounts

    • Similar to database access

    ©2015 Derek C. Ashmore, All Rights Reserved 35

  • 8/17/2019 CON2306_Ashmore Writing Microservices in Java JavaOne 2015-10-28

    36/42

    Contract Testing

    • Critical for MS architectures

     – Contract changes can break other services

     – Bulkhead for rogue developers

     – Makes individual services more disposable• Consumer-based testing

    • Tooling support

     –Apache HttpClient 

     – SoapUI 

     – ActiveMQ for JMS (embedded broker)

    ©2015 Derek C. Ashmore, All Rights Reserved 36

    http://hc.apache.org/httpcomponents-client-4.4.x/index.htmlhttp://www.soapui.org/http://activemq.apache.org/how-to-unit-test-jms-code.htmlhttp://activemq.apache.org/how-to-unit-test-jms-code.htmlhttp://activemq.apache.org/how-to-unit-test-jms-code.htmlhttp://www.soapui.org/http://hc.apache.org/httpcomponents-client-4.4.x/index.html

  • 8/17/2019 CON2306_Ashmore Writing Microservices in Java JavaOne 2015-10-28

    37/42

    Agenda

    The “What”

    and “Why” of

    microservices

    DesignConsiderations

    and Patterns

    Cross-cuttingconcerns

    When to usemicroservices

    Summary /Q&A

    ©2015 Derek C. Ashmore, All Rights Reserved 37

  • 8/17/2019 CON2306_Ashmore Writing Microservices in Java JavaOne 2015-10-28

    38/42

    When to consider MS

    • Starting out with MS isn’t recommended unless 

     – Parts of the application will have extremely high volume

    • Need to scale a portion of the application differently

    • Note: MS isn’t all or nothing!  

    • Warning signs for app that’s too large  – Unintended consequences after release

     – High technical debt / design rot

     – Release testing cycles abnormally large

     – Need to coordinate large numbers of developers for asingle code base

    • Large number == takes more than two pizzas to feed

    ©2015 Derek C. Ashmore, All Rights Reserved 38

  • 8/17/2019 CON2306_Ashmore Writing Microservices in Java JavaOne 2015-10-28

    39/42

    Common Mistakes

    • Inappropriate Service Boundries

     – Services that are not truly loosely coupled

    • One change Multiple services deployed

     – Services that make ‘assumptions’ about executioncontext

    • Deployments cause unintended consequences

    • Not recording all requests/responses

     –

    Support developers need to localize problems – Include request/response data in exceptions

    • Contexted Exceptions in Commons Lang

    ©2015 Derek C. Ashmore, All Rights Reserved 39

    https://commons.apache.org/proper/commons-lang/javadocs/api-3.1/org/apache/commons/lang3/exception/ContextedRuntimeException.htmlhttps://commons.apache.org/proper/commons-lang/javadocs/api-3.1/org/apache/commons/lang3/exception/ContextedRuntimeException.htmlhttps://commons.apache.org/proper/commons-lang/javadocs/api-3.1/org/apache/commons/lang3/exception/ContextedRuntimeException.htmlhttps://commons.apache.org/proper/commons-lang/javadocs/api-3.1/org/apache/commons/lang3/exception/ContextedRuntimeException.html

  • 8/17/2019 CON2306_Ashmore Writing Microservices in Java JavaOne 2015-10-28

    40/42

    Common Mistakes (continued)

    • Not checking arguments up front

     – Derivative exceptions take longer to debug/fix

     –   NullPointerException == Argument not checked!

    •No Change in Governance – Easier / quicker path to production

     – Automated Deployments/Backouts

    • Less manual intervention

    • Less manual testing (quick reaction vs prevention) – Continuous Delivery / DevOps / Microservices go

    hand-in-hand

    ©2015 Derek C. Ashmore, All Rights Reserved 40

  • 8/17/2019 CON2306_Ashmore Writing Microservices in Java JavaOne 2015-10-28

    41/42

    Further Reading

    • Microservices reading list – http://www.mattstine.com/microservices 

    • Microsoft’s Cloud Design Patterns  – https://msdn.microsoft.com/en-us/library/dn600223.aspx 

    • Moneta Java microservice example – https://github.com/Derek-Ashmore/moneta 

    • This slide deck –

    http://www.slideshare.net/derekashmore 

    ©2015 Derek C. Ashmore, All Rights Reserved 41

    http://www.mattstine.com/microserviceshttps://msdn.microsoft.com/en-us/library/dn600223.aspxhttps://github.com/Derek-Ashmore/monetahttp://www.slideshare.net/derekashmorehttp://www.slideshare.net/derekashmorehttp://www.slideshare.net/derekashmorehttps://github.com/Derek-Ashmore/monetahttps://github.com/Derek-Ashmore/monetahttps://github.com/Derek-Ashmore/monetahttps://github.com/Derek-Ashmore/monetahttps://msdn.microsoft.com/en-us/library/dn600223.aspxhttps://msdn.microsoft.com/en-us/library/dn600223.aspxhttps://msdn.microsoft.com/en-us/library/dn600223.aspxhttps://msdn.microsoft.com/en-us/library/dn600223.aspxhttp://www.mattstine.com/microserviceshttp://www.mattstine.com/microservices

  • 8/17/2019 CON2306_Ashmore Writing Microservices in Java JavaOne 2015-10-28

    42/42

    Questions?

    • Derek Ashmore:

     – Blog: www.derekashmore.com 

     – LinkedIn: www.linkedin.com/in/derekashmore 

     – Twitter: https://twitter.com/Derek_Ashmore 

     – GitHub: https://github.com/Derek-Ashmore  

     – Book: http://dvtpress.com/ 

    http://www.derekashmore.com/http://www.linkedin.com/in/derekashmorehttps://twitter.com/Derek_Ashmorehttps://github.com/Derek-Ashmorehttp://dvtpress.com/http://www.amazon.com/gp/product/0972954880/ref=as_li_ss_tl?ie=UTF8&camp=1789&creative=390957&creativeASIN=0972954880&linkCode=as2&tag=dvtpresscom-20http://dvtpress.com/http://dvtpress.com/http://dvtpress.com/https://github.com/Derek-Ashmorehttps://github.com/Derek-Ashmorehttps://github.com/Derek-Ashmorehttps://github.com/Derek-Ashmorehttps://github.com/Derek-Ashmorehttps://twitter.com/Derek_Ashmorehttps://twitter.com/Derek_Ashmorehttp://www.linkedin.com/in/derekashmorehttp://www.derekashmore.com/