Más contenido relacionado Similar a Microservices for Java Architects (Chicago, April 21, 2015) (20) Microservices for Java Architects (Chicago, April 21, 2015)1. Microservices for Java Architects
Given by Derek C. Ashmore
April 21, 2015
©2015 Derek C. Ashmore, All Rights Reserved 1
2. 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
3. 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
4. Agenda
The “What”
and “Why” of
microservices
Design
Considerations
and Patterns
Packaging
Options
Cross-cutting
concerns
When to use
microservices
Summary /
Q&A
©2015 Derek C. Ashmore, All Rights Reserved 4
5. 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
6. Traditional Java EE Application Architecture
• Like a layer cake
• Highly cohesive
• Defined
dependencies
• Deployed as one
unit (war/ear)
• Limited Scalability
• Code Size
©2015 Derek C. Ashmore, All Rights Reserved 6
7. What is a Monolith?
• Hard to change
– QA test cycles are long
– Change causes unintended
consequences
• Hard to onboard new
developers
• Married to your technical
stack
• Harder to diagnose
bottlenecks and memory
issues
©2015 Derek C. Ashmore, All Rights Reserved 7
8. Microservices organize by domain
• Silo by business
competency
• Divide and Conquer
• How to divide is the
hard part
©2015 Derek C. Ashmore, All Rights Reserved 8
9. Microservices follow the Org Chart
• Conway’s Law
– Systems copy the
structure of the
organizations that build
them
• Why?
– Corps need one manager
in charge
– Reduces inter-group
conflict
©2015 Derek C. Ashmore, All Rights Reserved 9
10. Refactoring into Microservices
• Large benefits to
unified user
interface
• Databases
introduce
unwanted coupling
between services
©2015 Derek C. Ashmore, All Rights Reserved 10
12. Alternate Database Strategy
• Centralized databases are hurdles for MS
– i.e. multiple apps use the same db
• MS-Central DB compromise
– Data items only maintained by *one* service
• All other services/apps consider that data item read-
only
• Helps data corruption investigations
©2015 Derek C. Ashmore, All Rights Reserved 12
13. 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
13©2015 Derek C. Ashmore, All Rights Reserved
14. Easier Management /
Higher Throughput
• Easier to manage large
numbers of developers
– Concentrate on
intelligently drawing
service boundaries
– Manage/enforce service
contracts
• Each service team works
independently
• Team independence leads
to higher development
throughput
©2015 Derek C. Ashmore, All Rights Reserved 14
15. Isn’t this SOA?
• Yes – More or less
• No concrete definitions
• SOA == dumb endpoints and smart routes
– ESB routes using Mule, Camel, etc.
– Transformations en route
• MS == dumb routes and smart end-points
– Simple routes
• Usually REST or Soap calls via http(s)
• Persistent queue route at it’s most complex
• Analogy: Electrical supply for Stereo
©2015 Derek C. Ashmore, All Rights Reserved 15
16. Agenda
The “What”
and “Why” of
microservices
Design
Considerations
and Patterns
Packaging
Options
Cross-cutting
concerns
When to use
microservices
Summary /
Q&A
©2015 Derek C. Ashmore, All Rights Reserved 16
17. Design considerations
• Service Boundaries (gerrymandering)
• Service call Failure / Unavailability
• Data Integrity
• Performance
©2015 Derek C. Ashmore, All Rights Reserved 17
18. 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 <blank>
– Student, Course, Classroom, etc.
• Process Services
– Services responsible for performing single complex tasks
– Usually represents an Action or Process
• Service is responsible for processing <blank>
– 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 18
19. 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 19
20. Microservices are not about size
©2015 Derek C. Ashmore, All Rights Reserved 20
….. Microservices are about having a single business purpose!
21. 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
©2015 Derek C. Ashmore, All Rights Reserved 21
22. Retry Pattern
©2015 Derek C. Ashmore, All Rights Reserved 22
• 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)
24. 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
• No formal tooling support for this pattern
• Best embedded in interface clients / delegates
• More information here.
• Sample Circuit implementation here.
©2015 Derek C. Ashmore, All Rights Reserved 24
25. Dispatch via Messaging
©2015 Derek C. Ashmore, All Rights Reserved 25
• 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)
26. 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 26
28. 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 28
30. 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 30
31. 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 31
32. Context Independence
• No context assumptions
– Think in terms of a stereo system
• Warning Signs
– Spending time analyzing service call flow
• Your services likely make context assumptions
– Agonizing over which service should do a given
activity
• Maybe you need a new service
©2015 Derek C. Ashmore, All Rights Reserved 32
34. 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 34
35. Special Problems
• Versioning
– Issues same as more traditional applications
– “Breaking” vs. “Non-Breaking” changes
• Feature “additions” usually are “Non-Breaking”
• Trend away from breaking changes
– Versioning is a last result
• It greatly increases number of contract tests
• Backward compatibility not always obvious to developers
• Prefer header versioning to URL versioning
• Explicitly error out for versions no longer supported
• More info here
©2015 Derek C. Ashmore, All Rights Reserved 35
36. Special Problems (continued)
• External Calls to 3rd Party Products
– Wrap external interfaces with a separate service
– Product upgrades or changes should only affect
the interface service
– Log all requests/responses
©2015 Derek C. Ashmore, All Rights Reserved 36
37. Common code between services?
• Yes, but….
– Version it; services make decision as to when to
upgrade
– Changes to common code can’t require the
deployment 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 37
38. Agenda
The “What”
and “Why” of
microservices
Design
Considerations
and Patterns
Packaging
Options
Cross-cutting
concerns
When to use
microservices
Summary /
Q&A
©2015 Derek C. Ashmore, All Rights Reserved 38
39. Packaging Support
• Microservices are deployed as a process
– For Java, embedded containers are easy
– Spring Boot
– Dropwizard
• Docker – standardizes the process deployment
and environment
©2015 Derek C. Ashmore, All Rights Reserved 39
40. Spring Boot
• Packages Java EE application into *one* deployment jar
– java –jar myApp.jar
• Support for health checks and other admin add ons via ‘Actuator’
add-on
• Supports either Jetty or Tomcat
• Best for ‘Spring-mvc’ apps
– For non-spring apps, it’s swimming upstream
• Required artifacts
– Maven
• spring-boot
• spring-boot-starter-jetty (tomcat is available)
• spring-boot-starter-actuator (optional – health checks, etc.)
– Application class with public static void main()
• Configuration coded (usually into the application class)
• Will read application.properties (app-specific properties)
©2015 Derek C. Ashmore, All Rights Reserved 40
41. Spring Boot Actuator
• Actuator automatically adds endpoints
– Health (needs configuration)
– Metrics
– Mappings
– Trace
– Env
©2015 Derek C. Ashmore, All Rights Reserved 41
42. Dropwizard
• Packages Java EE application into *one* deployment jar
– java –jar myApp.jar server myConfig.yaml
• Provides file configuration options (yaml format)
– Database connection pools
– Logging config
– Port and other container specs
• Provides easy metrics/healthcheck support
• Uses Jetty
• Required artifacts
– Application class (with main())
– Maven: dropwizard-core, maven-shade-plugin
©2015 Derek C. Ashmore, All Rights Reserved 42
43. Docker
• Is a “mini VM”
• runs a linux kernal
• Compare to
shipping container
• Standard
“connections” to
outside world
• Supported formally
by Oracle, Tomcat,
Jboss, and many
more
43©2015 Derek C. Ashmore, All Rights Reserved
Package Once, Run Anywhere!
44. Why Docker?
• Docker is Win-Win
– Easier for OPS and system administrators
• All software looks the same
• Standard interface for disk and network resources
– Containers can be “linked”
• Inherently automated
– Easier for developers
• Fewer environment difference issues
• Less to communicate to OPS / system administrators
• Easy to leverage work of others (docker-hub)
– If you haven’t tried Docker yet – you should!
©2015 Derek C. Ashmore, All Rights Reserved 44
45. Sample Java/EE Microservice
• Moneta – Greek goddess of ‘memory’
– Open source: https://github.com/Derek-Ashmore/moneta
• Objective:
– Provide a RESTful Web Service interface to a relational database
• Feature set:
– Provides generic ‘core’ services
– Returns Json-formatted data
– Supports startRow and maxRows query options
– Supports a security call-out
– Built-in Dropwizard, Spring Boot, and War-file deployments
• Sample contract spec – currently read-only (writes in progress)
– /moneta/topics – lists ‘topics’ of information
• E.g. – Topic Customer configured
– /moneta/topic/customers?startRow=5&maxRows=25
– /moneta/topic/customer/111-222-333
©2015 Derek C. Ashmore, All Rights Reserved 45
46. Agenda
The “What”
and “Why” of
microservices
Design
Considerations
and Patterns
Packaging
Options
Cross-cutting
concerns
When to use
microservices
Summary /
Q&A
©2015 Derek C. Ashmore, All Rights Reserved 46
47. Cross-cutting Concerns
• Transaction tracking
• Security
• Contract Testing
• Same as traditional applications
– Health checks
– Logging consolidation
– Performance measurement
©2015 Derek C. Ashmore, All Rights Reserved 47
48. Correlation IDs
• Provides context for
service 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
48©2015 Derek C. Ashmore, All Rights Reserved
50. 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 50
51. 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 51
52. Agenda
The “What”
and “Why” of
microservices
Design
Considerations
and Patterns
Packaging
Options
Cross-cutting
concerns
When to use
microservices
Summary /
Q&A
©2015 Derek C. Ashmore, All Rights Reserved 52
53. 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 a
single code base
• Large number == takes more than two pizzas to feed
©2015 Derek C. Ashmore, All Rights Reserved 53
54. Where’s the marketing fluff?
• Easier to manage
– Maybe
• You *must* be good at contract management
• You *must* be good at specifying precisely what a microservice
needs to do
• You *must* ensure that services make no assumptions on how
they get invoked
• Easier for developers to “understand” applications
– No – sorry
• It is easier to understand a particular ‘cog’ in the machine (e.g. the
function of one service
• It is *not* easier to understand how microservices fit together to
provide a particular piece of business functionality
©2015 Derek C. Ashmore, All Rights Reserved 54
55. Where’s the marketing fluff?
(continued)
• Increased Development Throughput
– Maybe
• Harder for business to ‘test’ a business function for a specific combination of
microservices
• Developers work on *one* service at a time.
• You *must* be good at error detection (unintended consequences)
• The more assumptions a service makes about its execution context, the more
unintended consequences (e.g. errors) you are likely to have on deployment
• Services become disposable and can be “replaced” instead of
“maintained / fixed”.
– Maybe
• It’s more easily replaced than when using traditional architectures
• Requires rigorous contract testing
– Can’t have the “replacement” act any differently than the original (except for the bug
being fixed, of course)
• Requires architecture support for cross-cutting concerns
– Can’t take a lot of time to implement / test
©2015 Derek C. Ashmore, All Rights Reserved 55
56. Common Mistakes
• Inappropriate Service Boundries
– Services that are not truly loosely coupled
• One change Multiple services deployed
– Services that make ‘assumptions’ about execution
context
• Deployments cause unintended consequences
• Exposing MS to the browser
– Causes performance issues
– Use Back-ends for Front-ends
– Keep MS traffic behind the firewall and on a fast
network
©2015 Derek C. Ashmore, All Rights Reserved 56
57. Why Now?
• SOA an old idea
• Virtualization and Cloud advances (e.g. Docker, AWS,
etc.)
– Less dependent on server set-up
• Hardware cheaper than it’s ever been
– We’re not as concerned with the extra process overhead
for MS
• Infrastructure Automation advances
– Additional tooling support(e.g. Chef or Puppet)
– Easier to manage larger numbers of deployments
• Can you think of other possible reasons?
©2015 Derek C. Ashmore, All Rights Reserved 57
58. Future Directions
• What about Jigsaw?
– Java platform support for modularity
– Coming with Java9 (rumors are 2016)
• Early release is available
– Compared to Microservices
• More limited scaling flexibility
• More limited technical stack
• Chief benefit over MS is reduction in process memory
– Less important as memory is cheaper these days
©2015 Derek C. Ashmore, All Rights Reserved 58
59. Future Directions
• What about OSGi?
– OSGi supplies modularity to JVM
– Compared to Microservices
• More limited scaling flexibility
• More limited technical stack
• Chief benefit over MS is reduction in process memory
– Less important as memory is cheaper these days
– Low adoption to date
• Jobs oscillate around 0.01% (indeed.com)
• Google Trends shows steady decline
©2015 Derek C. Ashmore, All Rights Reserved 59
60. Future Directions (continued)
• Trend toward product releases designed to be
deployed as microservices
– Already seeing increase on GitHub
– Takes PAAS to a new level
• Will Microservices become main stream?
– Too early to tell
©2015 Derek C. Ashmore, All Rights Reserved 60
61. 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 61
62. 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/
©2015 Derek C. Ashmore, All Rights Reserved 62
May 14-15, 2015
http://www.chicagocoderconference.com/