A Blueprint for Scala Microservices

Federico Feroldi
Federico FeroldiDigital Transformation leadership & CTO
A BLUEPRINT FOR
SCALA
MICROSERVICES
FEDERICO FEROLDI
CTO / MEASURENCE.COM
@CLOUDIFY
OUR GOAL
Empower developers with tools
and process to own a service
from development to production.
Monolithic (1990s) SOA (2000s) Microservices (2010s)
RATIONALE
Small team: time is scarce
Heavy use of open source
Automate al the things
Sane standards, easy override
A Blueprint for Scala Microservices
MAIN TOPICS
Minimum Viable Service
Deployment & Discovery
Best practices
MINIMUM VIABLE SERVICE
Business Logic
REST API
Configuration
Health / Monitoring
Releasing / Versioning
Packaging
BUSINESS LOGIC = AKKA ACTORS
Sum

Service
HTTP
API
SumOp
SumRes
“ask” pattern
BUSINESS LOGIC = AKKA ACTORS
object SumService {
def props = Props(classOf[SumService])
!
case class SumOp(a: Int, b: Int)
case class SumRes(result: Int)
}
!
class SumService extends Actor {
import SumService._
!
def receive: Receive = {
case SumOp(a, b) => sender() ! SumRes(a + b)
}
}
REST API = SPRAY.IO
Sum

Service
SPRAY
API
SumOp
SumRes
GET /v1/sum
JSON
REST API = SPRAY.IO
case class SumApiResponse(result: Int)
!
object SumProtocol extends DefaultJsonProtocol {
implicit val sumApiResponseFormat = jsonFormat1(SumApiResponse)
}
!
class SumHttpServiceV1(services: Services) {
import SumProtocol._
!
def route = path("v1" / "sum") {
get {
parameters('a.as[Int], 'b.as[Int]) { (a, b) =>
def future = (services.sumService ? SumOp(a, b)).mapTo[SumRes]
onSuccess(future) { response =>
complete(SumApiResponse(response.result))
}
}
}
}
}
API DOCS = SWAGGER
SPRAY-
SWAGGER
GET /
HTML/JSON
docs
API DOCS = SWAGGER
@Api(value = “/v1/sum", description = "Sum numbers.")
class SumHttpServiceV1(services: Services) {
…
}
API DOCS = SWAGGER
!
@ApiOperation(value = "Returns the sum”, httpMethod = “GET")
!
@ApiImplicitParams(Array(
new ApiImplicitParam(
name="a", required=true, dataType="integer", paramType=“query"
),
new ApiImplicitParam(
name="b", required=true, dataType="integer", paramType=“query"
)
))
!
@ApiResponses(Array(
new ApiResponse(
code=200, message="The sum", response=classOf[SumApiResponse]
)
))
!
def route = path("v1" / "sum") { … }
API DOCS = SWAGGER
@ApiModel(description = "Result of the sum")
case class SumApiResponse(
@(ApiModelProperty @field)(value = "The sum of a and b")
result: Int
)
REST API = SPRAY.IO + SWAGGER
CONFIG = TYPESAFE CONFIG
application.conf
development.conf
testing.conf staging.conf
production.conf
CONFIG = TYPESAFE CONFIG
akka.loglevel = "INFO"
!
metrics.carbon = "graphite.local:2003"
!
acme {
environment = "production"
!
svc-calculator {
hostname = “0.0.0.0”
port = "9999"
}
}
application.conf
CONFIG = TYPESAFE CONFIG
include "application"
production.conf
CONFIG = TYPESAFE CONFIG
include "application"

!
akka.loglevel = "DEBUG"
metrics.carbon = "localhost:2003"
!
acme {
environment = "development"
!
svc-calculator {
hostname = "127.0.0.1"
}
}
development.conf
CONFIG = TYPESAFE CONFIG
val config = ConfigFactory.defaultApplication()
$ sbt -Dconfig.resource=development.conf run
Loads application.conf by default
Loads development.conf
CONFIG = TYPESAFE CONFIG
{
mysql_username=${MYSQL_USERNAME}
mysql_password=${MYSQL_PASSWORD}
}
PRO-TIP

Don’t store passwords with source code!
Instead make use ENV vars substitution and
keep passwords in a safe place.
HEALTH = SPRAY + HAPROXY
SPRAY
GET /ping
Load
Balancer
Are you ok?
HEALTH = SPRAY + HAPROXY
def route = get {
path("ping") {
complete("pong")
}
}
PERFMON = METRICS + GRAPHITE
CODAHALE
METRICS
data points
GRAPHITE
CARBON
PERF.MON. = METRICS + GRAPHITE
object MetricsInstrumentation {
val metricRegistry = new MetricRegistry()
}
!
trait Instrumented extends InstrumentedBuilder {
val metricRegistry = MetricsInstrumentation.metricRegistry
}
!
class Actor extends Actor with Instrumented {
val meter = metrics.meter("requests")
!
def receive: Receive = {
!
case Request =>
meter.mark()
!
}
}
PERFMON = METRICS + GRAPHITE
RELEASING = SBT-RELEASE
SNAPSHOT/RELEASE artifacts
Semantic versioning
version.sbt + Git tags
publishing of artifacts
100% customizable process
interactive OR automated
PACKAGING = ASSEMBLY + DOCKER
JVM
SERVICE
(fat jar)
HTTP Requests
METRICS
“Immutable” container
Other Svc HTTP Reqs
PACKAGING = SBT-ASSEMBLY + SBT-DOCKER
docker <<= (docker dependsOn assembly)
!
dockerfile in docker := {
val artifact = (outputPath in assembly).value
val artifactTargetPath = s"/app/${artifact.name}"
new Dockerfile {
from(“acme/javabase")
expose(9999)
add(artifact, artifactTargetPath)
entryPoint("java", "-jar", artifactTargetPath)
}
}
!
imageNames in docker := Seq(
ImageName(
namespace = Some(organization.value),
repository = name.value,
tag = Some("v" + version.value)
)
)
MINIMUM VIABLE SERVICE
Business Logic = Akka
REST API = Spray + Swagger
Configuration = Typesafe Config
Health / Monitoring = Metrics
Packaging = Assembly & Docker
DEPLOYMENT & DISCOVERY
Continuous Integration
Deployment
Service Discovery
CI/DEPLOY = JENKINS + SBT + ANSIBLE
Git repo
Jenkins Jobs + sbt
Test Release Dockerize Deploy
Commit
Staging / Production
Ansible
HOSTING = MESOS/MARATHON
{
"id": "/svc-calculator",
"container": {
"type": "DOCKER",
"docker": {
"image": "docker.com/svc-calculator:1.0.0",
"network": "BRIDGE",
"portMappings": [{
"containerPort": 9999, "protocol": "tcp"
}]
}
},
"env": {"JAVA_ARGS": “-Dconfig.resource=production"},
"cpus": 1,
"mem": 1024,
"instances": 2
}
HOSTING = MESOS/MARATHON
HOSTING = MESOS/MARATHON
Dynamic port mapping
DISCOVERY = ?
mesos1
/svc-calculator /svc-calculator
mesos2
/web-app
mesos3
HOST:PORT?
DISCOVERY = BAMBOO + HAPROXY
mesos1
/svc-calculator /svc-calculator
mesos2
/web-app
mesos3
BAMBOO +
HAPROXY
BAMBOO +
HAPROXY
BAMBOO +
HAPROXY
MARATHON
http://localhost/svc-calculator
:80
:30001:30002
DEPLOYMENT & DISCOVERY
CI = Jenkins + sbt
Deploy = Ansible + Marathon
Discovery = Bamboo + HAProxy
FINAL TIPS
DRY + keep team aligned
Zero friction on new services
DRY: CUSTOM SBT PLUGIN
package com.acme.sbt.plugin
import sbt._
import Keys._
object AcmePlugin extends AutoPlugin {
lazy val acmeBuildSettings = Seq(
scalacOptions ++= Seq(
"-unchecked",
"-deprecation",
"-feature",
"-Xlint",
"-Ywarn-dead-code",
"-target:jvm-1.7"
)
)
} AcmePlugin.scala
FINAL TIP: CUSTOM SBT PLUGIN
import com.acme.sbt.plugin.AcmePlugin._
!
Seq(acmeBuildSettings:_*)
!
// custom project settings
build.sbt
ZERO FRICTION: GITER8 TEMPLATES
% g8 file://g8-svc-spray/ --name=svc-calculator 
-—servicePort=9999
!
Template applied in ./svc-calculator
!
% ls svc-calculator
build.sbt project src version.sbt
Recap
A Blueprint for Scala Microservices
Thank you!
1 of 46

Recommended

API first with Swagger and Scala by Slava Schmidt by
API first with Swagger and Scala by  Slava SchmidtAPI first with Swagger and Scala by  Slava Schmidt
API first with Swagger and Scala by Slava SchmidtJavaDayUA
2.3K views60 slides
Federico Feroldi - Scala microservices by
Federico Feroldi - Scala microservicesFederico Feroldi - Scala microservices
Federico Feroldi - Scala microservicesScala Italy
769 views46 slides
Why You Should Use TAPIs by
Why You Should Use TAPIsWhy You Should Use TAPIs
Why You Should Use TAPIsJeffrey Kemp
4K views62 slides
A Lifecycle Of Code Under Test by Robert Fornal by
A Lifecycle Of Code Under Test by Robert FornalA Lifecycle Of Code Under Test by Robert Fornal
A Lifecycle Of Code Under Test by Robert FornalQA or the Highway
444 views19 slides
Julio Capote, Twitter by
Julio Capote, TwitterJulio Capote, Twitter
Julio Capote, TwitterOntico
1.9K views125 slides
Intro to GraphQL on Android with Apollo DroidconNYC 2017 by
Intro to GraphQL on Android with Apollo DroidconNYC 2017Intro to GraphQL on Android with Apollo DroidconNYC 2017
Intro to GraphQL on Android with Apollo DroidconNYC 2017Mike Nakhimovich
2.1K views71 slides

More Related Content

What's hot

Testing Java Code Effectively - BaselOne17 by
Testing Java Code Effectively - BaselOne17Testing Java Code Effectively - BaselOne17
Testing Java Code Effectively - BaselOne17Andres Almiray
669 views59 slides
PHP 8.1 - What's new and changed by
PHP 8.1 - What's new and changedPHP 8.1 - What's new and changed
PHP 8.1 - What's new and changedAyesh Karunaratne
144 views128 slides
RESTful API using scalaz (3) by
RESTful API using scalaz (3)RESTful API using scalaz (3)
RESTful API using scalaz (3)Yeshwanth Kumar
1.6K views23 slides
QA Lab: тестирование ПО. Яков Крамаренко: "KISS Automation" by
QA Lab: тестирование ПО. Яков Крамаренко: "KISS Automation"QA Lab: тестирование ПО. Яков Крамаренко: "KISS Automation"
QA Lab: тестирование ПО. Яков Крамаренко: "KISS Automation"GeeksLab Odessa
673 views104 slides
QA Lab: тестирование ПО. Станислав Шмидт: "Self-testing REST APIs with API Fi... by
QA Lab: тестирование ПО. Станислав Шмидт: "Self-testing REST APIs with API Fi...QA Lab: тестирование ПО. Станислав Шмидт: "Self-testing REST APIs with API Fi...
QA Lab: тестирование ПО. Станислав Шмидт: "Self-testing REST APIs with API Fi...GeeksLab Odessa
497 views55 slides
Refactoring by
RefactoringRefactoring
RefactoringAmir Barylko
909 views113 slides

What's hot(20)

Testing Java Code Effectively - BaselOne17 by Andres Almiray
Testing Java Code Effectively - BaselOne17Testing Java Code Effectively - BaselOne17
Testing Java Code Effectively - BaselOne17
Andres Almiray669 views
QA Lab: тестирование ПО. Яков Крамаренко: "KISS Automation" by GeeksLab Odessa
QA Lab: тестирование ПО. Яков Крамаренко: "KISS Automation"QA Lab: тестирование ПО. Яков Крамаренко: "KISS Automation"
QA Lab: тестирование ПО. Яков Крамаренко: "KISS Automation"
GeeksLab Odessa673 views
QA Lab: тестирование ПО. Станислав Шмидт: "Self-testing REST APIs with API Fi... by GeeksLab Odessa
QA Lab: тестирование ПО. Станислав Шмидт: "Self-testing REST APIs with API Fi...QA Lab: тестирование ПО. Станислав Шмидт: "Self-testing REST APIs with API Fi...
QA Lab: тестирование ПО. Станислав Шмидт: "Self-testing REST APIs with API Fi...
GeeksLab Odessa497 views
RIAs Done Right: Grails, Flex, and EXT GWT by Michael Galpin
RIAs Done Right: Grails, Flex, and EXT GWTRIAs Done Right: Grails, Flex, and EXT GWT
RIAs Done Right: Grails, Flex, and EXT GWT
Michael Galpin870 views
How Testability Inspires AngularJS Design / Ran Mizrahi by Ran Mizrahi
How Testability Inspires AngularJS Design / Ran MizrahiHow Testability Inspires AngularJS Design / Ran Mizrahi
How Testability Inspires AngularJS Design / Ran Mizrahi
Ran Mizrahi716 views
Reactive Programming - ReactFoo 2020 - Aziz Khambati by Aziz Khambati
Reactive Programming - ReactFoo 2020 - Aziz KhambatiReactive Programming - ReactFoo 2020 - Aziz Khambati
Reactive Programming - ReactFoo 2020 - Aziz Khambati
Aziz Khambati21 views
Exploring Angular 2 - Episode 2 by Ahmed Moawad
Exploring Angular 2 - Episode 2Exploring Angular 2 - Episode 2
Exploring Angular 2 - Episode 2
Ahmed Moawad386 views
JAX-RS and CDI Bike the (Reactive) Bridge by José Paumard
JAX-RS and CDI Bike the (Reactive) BridgeJAX-RS and CDI Bike the (Reactive) Bridge
JAX-RS and CDI Bike the (Reactive) Bridge
José Paumard1.2K views
Test-Driven Development of AngularJS Applications by FITC
Test-Driven Development of AngularJS ApplicationsTest-Driven Development of AngularJS Applications
Test-Driven Development of AngularJS Applications
FITC9.3K views
Moderne backends mit dem aktor programmiermodell by Damir Dobric
Moderne backends mit dem aktor programmiermodellModerne backends mit dem aktor programmiermodell
Moderne backends mit dem aktor programmiermodell
Damir Dobric395 views
Diving into HHVM Extensions (PHPNW Conference 2015) by James Titcumb
Diving into HHVM Extensions (PHPNW Conference 2015)Diving into HHVM Extensions (PHPNW Conference 2015)
Diving into HHVM Extensions (PHPNW Conference 2015)
James Titcumb1K views
Typed? Dynamic? Both! Cross-platform DSLs in C# by Vagif Abilov
Typed? Dynamic? Both! Cross-platform DSLs in C#Typed? Dynamic? Both! Cross-platform DSLs in C#
Typed? Dynamic? Both! Cross-platform DSLs in C#
Vagif Abilov388 views
RESTful API Design & Implementation with CodeIgniter PHP Framework by Bo-Yi Wu
RESTful API Design & Implementation with CodeIgniter PHP FrameworkRESTful API Design & Implementation with CodeIgniter PHP Framework
RESTful API Design & Implementation with CodeIgniter PHP Framework
Bo-Yi Wu59.7K views
Arrays &amp; functions in php by Ashish Chamoli
Arrays &amp; functions in phpArrays &amp; functions in php
Arrays &amp; functions in php
Ashish Chamoli558 views

Similar to A Blueprint for Scala Microservices

Let's play with adf 3.0 by
Let's play with adf 3.0Let's play with adf 3.0
Let's play with adf 3.0Eugenio Romano
1.5K views63 slides
Diseño y Desarrollo de APIs by
Diseño y Desarrollo de APIsDiseño y Desarrollo de APIs
Diseño y Desarrollo de APIsRaúl Neis
153 views141 slides
Crossing the Bridge: Connecting Rails and your Front-end Framework by
Crossing the Bridge: Connecting Rails and your Front-end FrameworkCrossing the Bridge: Connecting Rails and your Front-end Framework
Crossing the Bridge: Connecting Rails and your Front-end FrameworkDaniel Spector
1.3K views65 slides
IBM Cloud University: Build, Deploy and Scale Node.js Microservices by
IBM Cloud University: Build, Deploy and Scale Node.js MicroservicesIBM Cloud University: Build, Deploy and Scale Node.js Microservices
IBM Cloud University: Build, Deploy and Scale Node.js MicroservicesChris Bailey
543 views88 slides
Quick and Easy Development with Node.js and Couchbase Server by
Quick and Easy Development with Node.js and Couchbase ServerQuick and Easy Development with Node.js and Couchbase Server
Quick and Easy Development with Node.js and Couchbase ServerNic Raboy
756 views45 slides
Node Interactive: Node.js Performance and Highly Scalable Micro-Services by
Node Interactive: Node.js Performance and Highly Scalable Micro-ServicesNode Interactive: Node.js Performance and Highly Scalable Micro-Services
Node Interactive: Node.js Performance and Highly Scalable Micro-ServicesChris Bailey
914 views70 slides

Similar to A Blueprint for Scala Microservices(20)

Diseño y Desarrollo de APIs by Raúl Neis
Diseño y Desarrollo de APIsDiseño y Desarrollo de APIs
Diseño y Desarrollo de APIs
Raúl Neis153 views
Crossing the Bridge: Connecting Rails and your Front-end Framework by Daniel Spector
Crossing the Bridge: Connecting Rails and your Front-end FrameworkCrossing the Bridge: Connecting Rails and your Front-end Framework
Crossing the Bridge: Connecting Rails and your Front-end Framework
Daniel Spector1.3K views
IBM Cloud University: Build, Deploy and Scale Node.js Microservices by Chris Bailey
IBM Cloud University: Build, Deploy and Scale Node.js MicroservicesIBM Cloud University: Build, Deploy and Scale Node.js Microservices
IBM Cloud University: Build, Deploy and Scale Node.js Microservices
Chris Bailey543 views
Quick and Easy Development with Node.js and Couchbase Server by Nic Raboy
Quick and Easy Development with Node.js and Couchbase ServerQuick and Easy Development with Node.js and Couchbase Server
Quick and Easy Development with Node.js and Couchbase Server
Nic Raboy756 views
Node Interactive: Node.js Performance and Highly Scalable Micro-Services by Chris Bailey
Node Interactive: Node.js Performance and Highly Scalable Micro-ServicesNode Interactive: Node.js Performance and Highly Scalable Micro-Services
Node Interactive: Node.js Performance and Highly Scalable Micro-Services
Chris Bailey914 views
Angular for Java Enterprise Developers: Oracle Code One 2018 by Loiane Groner
Angular for Java Enterprise Developers: Oracle Code One 2018Angular for Java Enterprise Developers: Oracle Code One 2018
Angular for Java Enterprise Developers: Oracle Code One 2018
Loiane Groner726 views
Serverless in-action by Assaf Gannon
Serverless in-actionServerless in-action
Serverless in-action
Assaf Gannon318 views
Continuous Integration and Deployment Best Practices on AWS by Danilo Poccia
Continuous Integration and Deployment Best Practices on AWSContinuous Integration and Deployment Best Practices on AWS
Continuous Integration and Deployment Best Practices on AWS
Danilo Poccia1.6K views
AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308) by Amazon Web Services
AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)
AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)
Amazon Web Services1.7K views
Scala & sling by michid
Scala & slingScala & sling
Scala & sling
michid1.5K views
Cutting Edge Data Processing with PHP & XQuery by William Candillon
Cutting Edge Data Processing with PHP & XQueryCutting Edge Data Processing with PHP & XQuery
Cutting Edge Data Processing with PHP & XQuery
William Candillon6.4K views
Analytics Metrics delivery and ML Feature visualization: Evolution of Data Pl... by Chester Chen
Analytics Metrics delivery and ML Feature visualization: Evolution of Data Pl...Analytics Metrics delivery and ML Feature visualization: Evolution of Data Pl...
Analytics Metrics delivery and ML Feature visualization: Evolution of Data Pl...
Chester Chen339 views
Programming IoT Gateways in JavaScript with macchina.io by Günter Obiltschnig
Programming IoT Gateways in JavaScript with macchina.ioProgramming IoT Gateways in JavaScript with macchina.io
Programming IoT Gateways in JavaScript with macchina.io
Günter Obiltschnig2.2K views

More from Federico Feroldi

Project IO - TS-Conf 2019 by
Project IO - TS-Conf 2019Project IO - TS-Conf 2019
Project IO - TS-Conf 2019Federico Feroldi
436 views43 slides
Una Pubblica Amministrazione Agile, Funzionale e Serverless: si può fare! - C... by
Una Pubblica Amministrazione Agile, Funzionale e Serverless: si può fare! - C...Una Pubblica Amministrazione Agile, Funzionale e Serverless: si può fare! - C...
Una Pubblica Amministrazione Agile, Funzionale e Serverless: si può fare! - C...Federico Feroldi
494 views33 slides
From 1 to infinity: how to scale your tech organization, build a great cultur... by
From 1 to infinity: how to scale your tech organization, build a great cultur...From 1 to infinity: how to scale your tech organization, build a great cultur...
From 1 to infinity: how to scale your tech organization, build a great cultur...Federico Feroldi
573 views27 slides
From Startup to Exit in 18 months by
From Startup to Exit in 18 monthsFrom Startup to Exit in 18 months
From Startup to Exit in 18 monthsFederico Feroldi
1.3K views9 slides
Design and development of an Online Social Network crawler by
Design and development of an Online Social Network crawlerDesign and development of an Online Social Network crawler
Design and development of an Online Social Network crawlerFederico Feroldi
1.2K views86 slides
Scaling web application in the Cloud by
Scaling web application in the CloudScaling web application in the Cloud
Scaling web application in the CloudFederico Feroldi
2.8K views34 slides

More from Federico Feroldi(12)

Una Pubblica Amministrazione Agile, Funzionale e Serverless: si può fare! - C... by Federico Feroldi
Una Pubblica Amministrazione Agile, Funzionale e Serverless: si può fare! - C...Una Pubblica Amministrazione Agile, Funzionale e Serverless: si può fare! - C...
Una Pubblica Amministrazione Agile, Funzionale e Serverless: si può fare! - C...
Federico Feroldi494 views
From 1 to infinity: how to scale your tech organization, build a great cultur... by Federico Feroldi
From 1 to infinity: how to scale your tech organization, build a great cultur...From 1 to infinity: how to scale your tech organization, build a great cultur...
From 1 to infinity: how to scale your tech organization, build a great cultur...
Federico Feroldi573 views
From Startup to Exit in 18 months by Federico Feroldi
From Startup to Exit in 18 monthsFrom Startup to Exit in 18 months
From Startup to Exit in 18 months
Federico Feroldi1.3K views
Design and development of an Online Social Network crawler by Federico Feroldi
Design and development of an Online Social Network crawlerDesign and development of an Online Social Network crawler
Design and development of an Online Social Network crawler
Federico Feroldi1.2K views
Scaling web application in the Cloud by Federico Feroldi
Scaling web application in the CloudScaling web application in the Cloud
Scaling web application in the Cloud
Federico Feroldi2.8K views
Innovate, optimize and profit with cloud computing by Federico Feroldi
Innovate, optimize and profit with cloud computingInnovate, optimize and profit with cloud computing
Innovate, optimize and profit with cloud computing
Federico Feroldi521 views
Crawling the web for fun and profit by Federico Feroldi
Crawling the web for fun and profitCrawling the web for fun and profit
Crawling the web for fun and profit
Federico Feroldi8.5K views
Cloudify your applications with Amazon Web Services by Federico Feroldi
Cloudify your applications with Amazon Web ServicesCloudify your applications with Amazon Web Services
Cloudify your applications with Amazon Web Services
Federico Feroldi2.3K views

Recently uploaded

SAP FOR CONTRACT MANUFACTURING.pdf by
SAP FOR CONTRACT MANUFACTURING.pdfSAP FOR CONTRACT MANUFACTURING.pdf
SAP FOR CONTRACT MANUFACTURING.pdfVirendra Rai, PMP
11 views2 slides
Agile 101 by
Agile 101Agile 101
Agile 101John Valentino
7 views20 slides
Programming Field by
Programming FieldProgramming Field
Programming Fieldthehardtechnology
5 views9 slides
DSD-INT 2023 3D hydrodynamic modelling of microplastic transport in lakes - J... by
DSD-INT 2023 3D hydrodynamic modelling of microplastic transport in lakes - J...DSD-INT 2023 3D hydrodynamic modelling of microplastic transport in lakes - J...
DSD-INT 2023 3D hydrodynamic modelling of microplastic transport in lakes - J...Deltares
9 views24 slides
2023-November-Schneider Electric-Meetup-BCN Admin Group.pptx by
2023-November-Schneider Electric-Meetup-BCN Admin Group.pptx2023-November-Schneider Electric-Meetup-BCN Admin Group.pptx
2023-November-Schneider Electric-Meetup-BCN Admin Group.pptxanimuscrm
14 views19 slides
Short_Story_PPT.pdf by
Short_Story_PPT.pdfShort_Story_PPT.pdf
Short_Story_PPT.pdfutkarshsatishkumarsh
5 views16 slides

Recently uploaded(20)

DSD-INT 2023 3D hydrodynamic modelling of microplastic transport in lakes - J... by Deltares
DSD-INT 2023 3D hydrodynamic modelling of microplastic transport in lakes - J...DSD-INT 2023 3D hydrodynamic modelling of microplastic transport in lakes - J...
DSD-INT 2023 3D hydrodynamic modelling of microplastic transport in lakes - J...
Deltares9 views
2023-November-Schneider Electric-Meetup-BCN Admin Group.pptx by animuscrm
2023-November-Schneider Electric-Meetup-BCN Admin Group.pptx2023-November-Schneider Electric-Meetup-BCN Admin Group.pptx
2023-November-Schneider Electric-Meetup-BCN Admin Group.pptx
animuscrm14 views
.NET Developer Conference 2023 - .NET Microservices mit Dapr – zu viel Abstra... by Marc Müller
.NET Developer Conference 2023 - .NET Microservices mit Dapr – zu viel Abstra....NET Developer Conference 2023 - .NET Microservices mit Dapr – zu viel Abstra...
.NET Developer Conference 2023 - .NET Microservices mit Dapr – zu viel Abstra...
Marc Müller38 views
Gen Apps on Google Cloud PaLM2 and Codey APIs in Action by Márton Kodok
Gen Apps on Google Cloud PaLM2 and Codey APIs in ActionGen Apps on Google Cloud PaLM2 and Codey APIs in Action
Gen Apps on Google Cloud PaLM2 and Codey APIs in Action
Márton Kodok5 views
FIMA 2023 Neo4j & FS - Entity Resolution.pptx by Neo4j
FIMA 2023 Neo4j & FS - Entity Resolution.pptxFIMA 2023 Neo4j & FS - Entity Resolution.pptx
FIMA 2023 Neo4j & FS - Entity Resolution.pptx
Neo4j6 views
Myths and Facts About Hospice Care: Busting Common Misconceptions by Care Coordinations
Myths and Facts About Hospice Care: Busting Common MisconceptionsMyths and Facts About Hospice Care: Busting Common Misconceptions
Myths and Facts About Hospice Care: Busting Common Misconceptions
DSD-INT 2023 Leveraging the results of a 3D hydrodynamic model to improve the... by Deltares
DSD-INT 2023 Leveraging the results of a 3D hydrodynamic model to improve the...DSD-INT 2023 Leveraging the results of a 3D hydrodynamic model to improve the...
DSD-INT 2023 Leveraging the results of a 3D hydrodynamic model to improve the...
Deltares6 views
DSD-INT 2023 European Digital Twin Ocean and Delft3D FM - Dols by Deltares
DSD-INT 2023 European Digital Twin Ocean and Delft3D FM - DolsDSD-INT 2023 European Digital Twin Ocean and Delft3D FM - Dols
DSD-INT 2023 European Digital Twin Ocean and Delft3D FM - Dols
Deltares7 views
DSD-INT 2023 Delft3D FM Suite 2024.01 2D3D - New features + Improvements - Ge... by Deltares
DSD-INT 2023 Delft3D FM Suite 2024.01 2D3D - New features + Improvements - Ge...DSD-INT 2023 Delft3D FM Suite 2024.01 2D3D - New features + Improvements - Ge...
DSD-INT 2023 Delft3D FM Suite 2024.01 2D3D - New features + Improvements - Ge...
Deltares17 views
Navigating container technology for enhanced security by Niklas Saari by Metosin Oy
Navigating container technology for enhanced security by Niklas SaariNavigating container technology for enhanced security by Niklas Saari
Navigating container technology for enhanced security by Niklas Saari
Metosin Oy13 views
Quality Engineer: A Day in the Life by John Valentino
Quality Engineer: A Day in the LifeQuality Engineer: A Day in the Life
Quality Engineer: A Day in the Life
John Valentino5 views
DSD-INT 2023 Wave-Current Interaction at Montrose Tidal Inlet System and Its ... by Deltares
DSD-INT 2023 Wave-Current Interaction at Montrose Tidal Inlet System and Its ...DSD-INT 2023 Wave-Current Interaction at Montrose Tidal Inlet System and Its ...
DSD-INT 2023 Wave-Current Interaction at Montrose Tidal Inlet System and Its ...
Deltares10 views
DSD-INT 2023 The Danube Hazardous Substances Model - Kovacs by Deltares
DSD-INT 2023 The Danube Hazardous Substances Model - KovacsDSD-INT 2023 The Danube Hazardous Substances Model - Kovacs
DSD-INT 2023 The Danube Hazardous Substances Model - Kovacs
Deltares8 views
Sprint 226 by ManageIQ
Sprint 226Sprint 226
Sprint 226
ManageIQ5 views

A Blueprint for Scala Microservices

  • 1. A BLUEPRINT FOR SCALA MICROSERVICES FEDERICO FEROLDI CTO / MEASURENCE.COM @CLOUDIFY
  • 2. OUR GOAL Empower developers with tools and process to own a service from development to production.
  • 3. Monolithic (1990s) SOA (2000s) Microservices (2010s)
  • 4. RATIONALE Small team: time is scarce Heavy use of open source Automate al the things Sane standards, easy override
  • 6. MAIN TOPICS Minimum Viable Service Deployment & Discovery Best practices
  • 7. MINIMUM VIABLE SERVICE Business Logic REST API Configuration Health / Monitoring Releasing / Versioning Packaging
  • 8. BUSINESS LOGIC = AKKA ACTORS Sum
 Service HTTP API SumOp SumRes “ask” pattern
  • 9. BUSINESS LOGIC = AKKA ACTORS object SumService { def props = Props(classOf[SumService]) ! case class SumOp(a: Int, b: Int) case class SumRes(result: Int) } ! class SumService extends Actor { import SumService._ ! def receive: Receive = { case SumOp(a, b) => sender() ! SumRes(a + b) } }
  • 10. REST API = SPRAY.IO Sum
 Service SPRAY API SumOp SumRes GET /v1/sum JSON
  • 11. REST API = SPRAY.IO case class SumApiResponse(result: Int) ! object SumProtocol extends DefaultJsonProtocol { implicit val sumApiResponseFormat = jsonFormat1(SumApiResponse) } ! class SumHttpServiceV1(services: Services) { import SumProtocol._ ! def route = path("v1" / "sum") { get { parameters('a.as[Int], 'b.as[Int]) { (a, b) => def future = (services.sumService ? SumOp(a, b)).mapTo[SumRes] onSuccess(future) { response => complete(SumApiResponse(response.result)) } } } } }
  • 12. API DOCS = SWAGGER SPRAY- SWAGGER GET / HTML/JSON docs
  • 13. API DOCS = SWAGGER @Api(value = “/v1/sum", description = "Sum numbers.") class SumHttpServiceV1(services: Services) { … }
  • 14. API DOCS = SWAGGER ! @ApiOperation(value = "Returns the sum”, httpMethod = “GET") ! @ApiImplicitParams(Array( new ApiImplicitParam( name="a", required=true, dataType="integer", paramType=“query" ), new ApiImplicitParam( name="b", required=true, dataType="integer", paramType=“query" ) )) ! @ApiResponses(Array( new ApiResponse( code=200, message="The sum", response=classOf[SumApiResponse] ) )) ! def route = path("v1" / "sum") { … }
  • 15. API DOCS = SWAGGER @ApiModel(description = "Result of the sum") case class SumApiResponse( @(ApiModelProperty @field)(value = "The sum of a and b") result: Int )
  • 16. REST API = SPRAY.IO + SWAGGER
  • 17. CONFIG = TYPESAFE CONFIG application.conf development.conf testing.conf staging.conf production.conf
  • 18. CONFIG = TYPESAFE CONFIG akka.loglevel = "INFO" ! metrics.carbon = "graphite.local:2003" ! acme { environment = "production" ! svc-calculator { hostname = “0.0.0.0” port = "9999" } } application.conf
  • 19. CONFIG = TYPESAFE CONFIG include "application" production.conf
  • 20. CONFIG = TYPESAFE CONFIG include "application"
 ! akka.loglevel = "DEBUG" metrics.carbon = "localhost:2003" ! acme { environment = "development" ! svc-calculator { hostname = "127.0.0.1" } } development.conf
  • 21. CONFIG = TYPESAFE CONFIG val config = ConfigFactory.defaultApplication() $ sbt -Dconfig.resource=development.conf run Loads application.conf by default Loads development.conf
  • 22. CONFIG = TYPESAFE CONFIG { mysql_username=${MYSQL_USERNAME} mysql_password=${MYSQL_PASSWORD} } PRO-TIP
 Don’t store passwords with source code! Instead make use ENV vars substitution and keep passwords in a safe place.
  • 23. HEALTH = SPRAY + HAPROXY SPRAY GET /ping Load Balancer Are you ok?
  • 24. HEALTH = SPRAY + HAPROXY def route = get { path("ping") { complete("pong") } }
  • 25. PERFMON = METRICS + GRAPHITE CODAHALE METRICS data points GRAPHITE CARBON
  • 26. PERF.MON. = METRICS + GRAPHITE object MetricsInstrumentation { val metricRegistry = new MetricRegistry() } ! trait Instrumented extends InstrumentedBuilder { val metricRegistry = MetricsInstrumentation.metricRegistry } ! class Actor extends Actor with Instrumented { val meter = metrics.meter("requests") ! def receive: Receive = { ! case Request => meter.mark() ! } }
  • 27. PERFMON = METRICS + GRAPHITE
  • 28. RELEASING = SBT-RELEASE SNAPSHOT/RELEASE artifacts Semantic versioning version.sbt + Git tags publishing of artifacts 100% customizable process interactive OR automated
  • 29. PACKAGING = ASSEMBLY + DOCKER JVM SERVICE (fat jar) HTTP Requests METRICS “Immutable” container Other Svc HTTP Reqs
  • 30. PACKAGING = SBT-ASSEMBLY + SBT-DOCKER docker <<= (docker dependsOn assembly) ! dockerfile in docker := { val artifact = (outputPath in assembly).value val artifactTargetPath = s"/app/${artifact.name}" new Dockerfile { from(“acme/javabase") expose(9999) add(artifact, artifactTargetPath) entryPoint("java", "-jar", artifactTargetPath) } } ! imageNames in docker := Seq( ImageName( namespace = Some(organization.value), repository = name.value, tag = Some("v" + version.value) ) )
  • 31. MINIMUM VIABLE SERVICE Business Logic = Akka REST API = Spray + Swagger Configuration = Typesafe Config Health / Monitoring = Metrics Packaging = Assembly & Docker
  • 32. DEPLOYMENT & DISCOVERY Continuous Integration Deployment Service Discovery
  • 33. CI/DEPLOY = JENKINS + SBT + ANSIBLE Git repo Jenkins Jobs + sbt Test Release Dockerize Deploy Commit Staging / Production Ansible
  • 35. { "id": "/svc-calculator", "container": { "type": "DOCKER", "docker": { "image": "docker.com/svc-calculator:1.0.0", "network": "BRIDGE", "portMappings": [{ "containerPort": 9999, "protocol": "tcp" }] } }, "env": {"JAVA_ARGS": “-Dconfig.resource=production"}, "cpus": 1, "mem": 1024, "instances": 2 } HOSTING = MESOS/MARATHON
  • 37. DISCOVERY = ? mesos1 /svc-calculator /svc-calculator mesos2 /web-app mesos3 HOST:PORT?
  • 38. DISCOVERY = BAMBOO + HAPROXY mesos1 /svc-calculator /svc-calculator mesos2 /web-app mesos3 BAMBOO + HAPROXY BAMBOO + HAPROXY BAMBOO + HAPROXY MARATHON http://localhost/svc-calculator :80 :30001:30002
  • 39. DEPLOYMENT & DISCOVERY CI = Jenkins + sbt Deploy = Ansible + Marathon Discovery = Bamboo + HAProxy
  • 40. FINAL TIPS DRY + keep team aligned Zero friction on new services
  • 41. DRY: CUSTOM SBT PLUGIN package com.acme.sbt.plugin import sbt._ import Keys._ object AcmePlugin extends AutoPlugin { lazy val acmeBuildSettings = Seq( scalacOptions ++= Seq( "-unchecked", "-deprecation", "-feature", "-Xlint", "-Ywarn-dead-code", "-target:jvm-1.7" ) ) } AcmePlugin.scala
  • 42. FINAL TIP: CUSTOM SBT PLUGIN import com.acme.sbt.plugin.AcmePlugin._ ! Seq(acmeBuildSettings:_*) ! // custom project settings build.sbt
  • 43. ZERO FRICTION: GITER8 TEMPLATES % g8 file://g8-svc-spray/ --name=svc-calculator -—servicePort=9999 ! Template applied in ./svc-calculator ! % ls svc-calculator build.sbt project src version.sbt
  • 44. Recap