Experiences using grails in a Microservice Architecture SpringOne2gx 2014

52
Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Experiences using Grails in a Microservice Architecture Jeff Beck @beckje01

description

We deployed five new Grails micro services into production roughly a year ago. I will describe how Grails fits into our larger polyglot architecture. And go through our experiences building and maintaining these micro services. Showing what items we reused and standardized across the application, and the pitfalls we have encountered. I will also work through some of the supporting technologies including Puppet, Vagrant, OpenStack, Spring Integration, and ActiveMQ and how we are using them with Grails. By the end of the talk you should have the knowledge to evaluate if Grails micro services are good for your team and what support you will need to put in place before you can take the micro service approach.

Transcript of Experiences using grails in a Microservice Architecture SpringOne2gx 2014

Page 1: Experiences using grails in a Microservice Architecture SpringOne2gx 2014

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Experiences using Grails in a Microservice Architecture

Jeff Beck @beckje01

Page 2: Experiences using grails in a Microservice Architecture SpringOne2gx 2014

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Who Am I

• Jeff Beck • TechLead at ReachLocal • @beckje01 everywhere

2

Page 3: Experiences using grails in a Microservice Architecture SpringOne2gx 2014

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

What I mean by Microservice

3

• Single concern • Deployable in isolation

Page 4: Experiences using grails in a Microservice Architecture SpringOne2gx 2014

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Our Architecture

4

Over the course of acquisitions and expanding the products we have come out with a polyglot architecture including Java, Groovy,

Ruby, PHP, Node, and PERL. In order to take advantage of our existing talent and software, we have started down the micro-

service path.

Page 5: Experiences using grails in a Microservice Architecture SpringOne2gx 2014

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Our Architecture

5

Page 6: Experiences using grails in a Microservice Architecture SpringOne2gx 2014

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Our Architecture

• Layered Microservices • Asynchronous Broadcast Event System • REST calls can be either Synchronous or

Asynchronous

6

Page 7: Experiences using grails in a Microservice Architecture SpringOne2gx 2014

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Where Grails Fits

7

• REST Resources • GORM

Page 8: Experiences using grails in a Microservice Architecture SpringOne2gx 2014

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Where Grails Doesn't Fit

8

• Batch Jobs • Lacking any web endpoint

Page 9: Experiences using grails in a Microservice Architecture SpringOne2gx 2014

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Example Service

9

Page 10: Experiences using grails in a Microservice Architecture SpringOne2gx 2014

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Subscription Service

• Exposes the Subscription REST Resource • Emanates Events

10

Acts as the source of truth about subscriptions.

Page 11: Experiences using grails in a Microservice Architecture SpringOne2gx 2014

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Requirements

• Application Monitoring • Security • Server Setup • Builds • Deployments

11

Page 12: Experiences using grails in a Microservice Architecture SpringOne2gx 2014

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Building Our Service

12

All our Grails micro services start out the same way.

Page 13: Experiences using grails in a Microservice Architecture SpringOne2gx 2014

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Project Setup

• Grails create-app • Change Java Version to 1.7 • Add Internal Nexus

13

Page 14: Experiences using grails in a Microservice Architecture SpringOne2gx 2014

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Project Setup - Plugins

• Add our normal plugins • code-coverage • console • cucumber

• Remove the extra standard plugins • scaffolding • asset-pipeline • jquery

14

Page 15: Experiences using grails in a Microservice Architecture SpringOne2gx 2014

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Project Setup - Plugins Config

• Set up cucumber plugin • Configure GORM

15

Page 16: Experiences using grails in a Microservice Architecture SpringOne2gx 2014

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Build and Repo Setup

• Commit everything as the starting point • Add the new CI job • Bring up a dev server

16

Page 17: Experiences using grails in a Microservice Architecture SpringOne2gx 2014

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Customizing Our Service

17

We add scenarios, using cucumber allows us to keep our testing more DRY.

Page 18: Experiences using grails in a Microservice Architecture SpringOne2gx 2014

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

18

Scenario: Get nonexistent subscription by ID! Given I am a valid api client! And A valid subscription ID which does not match any subscription! When I request a subscription by ID! Then I get a 404 response!

Page 19: Experiences using grails in a Microservice Architecture SpringOne2gx 2014

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

19

Scenario: Get existing subscription by ID! Given I am a valid api client! And A valid subscription ID which matches a subscription! When I request a subscription by ID! Then I get a 200 response!

Page 20: Experiences using grails in a Microservice Architecture SpringOne2gx 2014

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

20

Given(~'^A valid subscription ID which matches a subscription$') { ->! //Have a valid subscription which exposes an ID!}!Given(~'^A valid subscription ID which does not match any subscription$') { ->! //Have an ID which is valid but no matching subscription!}!When(~'^I request a subscription by ID$') { ->! //Do actual request!}!Then(~'^I get a (\\d+) response$') { int statusCode ->! if (response.statusCode != statusCode) {! println response.asString()! }!! assert response.statusCode == statusCode!}!

Page 21: Experiences using grails in a Microservice Architecture SpringOne2gx 2014

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

What We Reused

• Health Checks • Security • Server Setup • Deployments • SI Components

21

Page 22: Experiences using grails in a Microservice Architecture SpringOne2gx 2014

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Health Checks

22

To support reusable monitoring we expose a health check in a known way that attempts to be both human readable and

programmatically useful.

Page 23: Experiences using grails in a Microservice Architecture SpringOne2gx 2014

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Example 200 Status

23

{! "dependencies": {! "database":"OK",! "file-access":"WARN"! }! }!

Page 24: Experiences using grails in a Microservice Architecture SpringOne2gx 2014

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

What Was Exposed

• Common Grails focused health check implementations • Controller that supports the expected output

24

Via a Grails plugin we share

Page 25: Experiences using grails in a Microservice Architecture SpringOne2gx 2014

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Sharing Outside Grails

• Common health check implementations • A registry of health checks

25

Via a JAR

Page 26: Experiences using grails in a Microservice Architecture SpringOne2gx 2014

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Security

26

We do server to server authentication with a token. So checking the Authorization header the plugin authenticates a

client.

Page 27: Experiences using grails in a Microservice Architecture SpringOne2gx 2014

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Our Plugin

• Expects a known GORM object that has an ID which is the token.

• Uses a static list of resource names to secure • Intended to be as light weight as possible

27

Page 28: Experiences using grails in a Microservice Architecture SpringOne2gx 2014

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Experiences With Our Plugin

• Moving away from our custom implementation towards a SpringSecurity based Grails plugin

• Was opt in security which was easy to miss a controller • Intently lacked roles which we have found a use-case

for now

28

Page 29: Experiences using grails in a Microservice Architecture SpringOne2gx 2014

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

How We Shared The Plugins

29

We use an internal Nexus repo, and release plugins to that.

Page 30: Experiences using grails in a Microservice Architecture SpringOne2gx 2014

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Experiences

• Supports Versioning • Dependency resolution works the way the rest of Grails

does • Changes don't reload

30

Page 31: Experiences using grails in a Microservice Architecture SpringOne2gx 2014

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Guide for Internal Plugins

• Tend to adding features allowing customization. • Each plugin is a project it needs CI, CodeNarc, etc • Use Versioning • CI pushing out SNAPSHOT versions is very helpful

31

Page 32: Experiences using grails in a Microservice Architecture SpringOne2gx 2014

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Server Setup

32

We use puppet to automate our server setup. Using classes we share default setup for a Tomcat server.

Page 33: Experiences using grails in a Microservice Architecture SpringOne2gx 2014

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

init.pp

33

class apps_subscription_api (! $heap_min = '256m',! $heap_max = '1024m',! $permgen_size = '1024m'! ){! class { 'standard_tomcat7_web_server':! minimum_heap => $heap_min,! maximum_heap => $heap_max,! permgen_size => $permgen_size,! }!! include apps_subscription_api::config!! base::nagios::hostgroup { "rsubscription_api_servers": }! base::nagios::hostparam { "_healthuri": value => '/health' }! }!

Page 34: Experiences using grails in a Microservice Architecture SpringOne2gx 2014

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

config.pp

34

class apps_subscription_api::config {!! file { '/rl/path/configs/subscription-config.groovy':! ensure => present,! owner => tomcat7,! group => tomcat7,! mode => '0400',! content => template('apps_subscription_api/subscription-config.groovy.erb'),! require => File['/rl/path/configs']! }! }!

Page 35: Experiences using grails in a Microservice Architecture SpringOne2gx 2014

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Deployments

35

We automate our deployments via custom bash scripts kicked off by bamboo deployments.

Page 36: Experiences using grails in a Microservice Architecture SpringOne2gx 2014

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Changes per Service

• Server List • Artifact Name • Deployment permissions

36

Page 37: Experiences using grails in a Microservice Architecture SpringOne2gx 2014

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Spring Integration Components

37

Our spring integration apps tend to need the same filters and transformers for our Events. Grails apps are not the only users

of these components.

Page 38: Experiences using grails in a Microservice Architecture SpringOne2gx 2014

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Shared via a Jar

• Exposes a Pojo that represents our internal idea of an Event

• De-duplication filter • An Event deserializer that supports our Builder • Built to include the least dependencies

38

Page 39: Experiences using grails in a Microservice Architecture SpringOne2gx 2014

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Logging

39

As a single request can spread out across the graph of microservices it is helpful to have some way to correlating all

the work back together.

Page 40: Experiences using grails in a Microservice Architecture SpringOne2gx 2014

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Correlation ID

40

We use Log4J's MDC to log a correlation ID throughout the requests life in Grails.MDC.put('correlationId', ", CorrelationId=${correlationId}")!

The Grails Filternew EnhancedPatternLayout(conversionPattern: ' %d{ISO8601}%d{ z}{GMT+0} %-5p [%t] %c{2}(:%L) - %m%X{correlationId}%n')!

Page 41: Experiences using grails in a Microservice Architecture SpringOne2gx 2014

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Correlation ID Pitfalls

• LB calls without Correlation ID generating noisy logs • Passing further on to other systems transparently is

tricky without some standards for http clients etc • Passing the correlation id around explicitly is messy

41

Page 42: Experiences using grails in a Microservice Architecture SpringOne2gx 2014

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Log Aggregation

42

We use Splunk for log aggregation for all applications. Allowing a query across many apps for a single correlation id.

Page 43: Experiences using grails in a Microservice Architecture SpringOne2gx 2014

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Splunk Correlation ID and Transactions

43

With Splunk we can use our correlationId as a transaction key, so we see log messages grouped together in spunk

index=my_api | transaction correlationId keepevicted=true!

Page 44: Experiences using grails in a Microservice Architecture SpringOne2gx 2014

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Centralized Monitoring

• Nagios • NewRelic • Starting to use dashboards of health checks

44

Page 45: Experiences using grails in a Microservice Architecture SpringOne2gx 2014

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

NewRelic Map

45

Page 46: Experiences using grails in a Microservice Architecture SpringOne2gx 2014

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

NewRelic + Grails

46

import com.newrelic.api.agent.Trace!!class SubscriptionService {!! @Trace! def save(Subscription subscription) {! //Work Here! }!

#enable_custom_tracing is used to allow @Trace on methods! enable_custom_tracing: true!

newrelic.yml

Grails Service

Blog Post

Page 47: Experiences using grails in a Microservice Architecture SpringOne2gx 2014

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Compensation

47

Account for failures across the system building a convergent system.

Page 48: Experiences using grails in a Microservice Architecture SpringOne2gx 2014

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Compensation

• Spring Batch for jobs and batching up the work • POST to a REST(ful) endpoint that will do the work • Reuse the same code/libraries to do the compensation

work that would have done the work in real time

48

Page 49: Experiences using grails in a Microservice Architecture SpringOne2gx 2014

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Pitfalls

• Poor communications between teams gets worse • Coordinating Releases • Development to spec vs finished service • Leaving in -SNAPSHOT dependencies • Poor monitoring in lower than production environments • Lacking log aggregation in lower environments

49

Page 50: Experiences using grails in a Microservice Architecture SpringOne2gx 2014

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Grails Microservice Checklist

Low overhead to start new projects Internal maven repo Automated deployments Server configuration management Good team to team communication

50

Page 51: Experiences using grails in a Microservice Architecture SpringOne2gx 2014

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/

Open Questions

• Correct size of Microservices • Pure REST vs RESTful • Security: Centralized vs Decentralized

51

Page 52: Experiences using grails in a Microservice Architecture SpringOne2gx 2014

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 52

Learn More. Stay Connected

Thinking about Microservices? Make sure to complete the checklist first!

@springcentral | spring.io/video

@beckje01