SlideShare a Scribd company logo
Development with PaaS
An introduction using Heroku and Spring
Roberto Casadei
Concurrent and Distributed Programming course
Department of Computer Science and Engineering (DISI)
Alma Mater Studiorum – Università of Bologna
June 16, 2018
PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 1/38
Outline
1 Introduction: PaaS
2 Google AppEngine: a Quick Outlook
3 Heroku
4 Spring Cloud
5 Example: Heroku+Spring
PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 2/38
Introduction: PaaS
Platform-as-a-Service (PaaS)
NIST definition: “The capability provided to the consumer is to deploy onto the cloud
infrastructure consumer-created or acquired applications created using
programming languages and tools supported by the provider. The consumer
does not manage or control the underlying cloud infrastructure including
network, servers, operating systems, or storage, but has control over the deployed
applications and possibly application hosting environment configurations.”
PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 3/38
PaaS is fuzzy
Beware: there is not only one PaaS model
https://blog.mccrory.me/2011/01/23/current-paas-patterns-types-of-paas/
1) Upload your environment as a VM/container (cf., IaaS)
2) Upload your compiled/packaged application (e.g., WAR)
3) Upload your code—compilation happens in the PaaS itself (cf., Heroku)
4) Upload platform-specific code (cf., Salesforce)
“Comparing PaaS Offerings in Light of SaaS Development”[4]
1) Unfocused PaaS platforms that mimic and match the APIs of popular
enterprise application servers and middleware platforms (e.g., Heroku, Cloud
Foundry, Azure, OpenShift)
2) Focused PaaS platforms—aim to optimally support specific types of cloud
applications, for better scalability (e.g., GAE)
3) Metadata-driven PaaS platforms (e.g., Salesforce)
https://www.youtube.com/watch?v=V_lM59cvGoI
1) Bring Your Own Code pattern
2) Bring Your Own Container pattern
PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 4/38
Outline
1 Introduction: PaaS
2 Google AppEngine: a Quick Outlook
3 Heroku
4 Spring Cloud
5 Example: Heroku+Spring
PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 5/38
GAE: Flexible vs. Standard Environment
PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 6/38
GAE: Flexible vs. Standard Environment
Standard Environment
Application instances run in a sandbox, using the runtime environment of a
supported language (Python 2.7, Java 7/8, PHP 5.5, Go 1.6/1.8/1.9)
Building an application is more constrained and involved, but your apps will have
faster scale up times, better reacting to sudden and extreme spikes of traffic.
Flexible Environment
Application instances run within Docker containers on GAE VMs
Use a predefined runtime (Python, Java, Node.js, Go, Ruby, PHP, .NET) or build
your custom runtime to support any PL/framework
Your apps can depend on other SW, including OS packages through apt-get
No sandbox limitations: you can create threads, processes, sockets.
No GAE APIs: apps are written like standard web apps that can run anywhere
Google Cloud client libraries to call Google Cloud Services
These client libraries work everywhere ⇒ your app is more portable
https://cloud.google.com/appengine/docs/the-appengine-environments
PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 7/38
Google Cloud SDK
Google Cloud SDK is a set of tools for accessing Google Cloud services via
command-line
1) Install it: you’ll get, among other things, a tool gcloud
2) Setup: $ gcloud init
Authorizes Cloud SDK tools to use your user account credentials to access Google
Cloud Platform
Sets up a Cloud SDK configuration (i.e., a set of properties)
$ gcloud config list
3) View/Install SDK components: $ gcloud components list
app-engine-java: gcloud App Java Extensions
gcd-emulator: emulator for Google Cloud Datastore
PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 8/38
GAE: example (i)
Java web apps for GAE use the standard Java Servlet interface1
An app is deployed as a Web Application Archive (WAR)
The Java GAE SDK is the bridge to GAE and supports simulating GAE locally
/src/main/java/it/unibo/MyServlet.java
public class MyServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
resp.setContentType("text/html");
resp.getWriter().println("Hello GAE");
} }
/src/main/webapp/WEB-INF/web.xml
<web-app ....>
<servlet>
<servlet-name>myservlet</servlet-name>
<servlet-class>clock.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myservlet</servlet-name> <url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
1https://guides.gradle.org/building-java-web-applications/
PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 9/38
GAE: example (ii)
/build.gradle
apply plugin: 'java'
apply plugin: 'war'
dependencies {
compile 'com.google.appengine:appengine-api-1.0-sdk:+'
providedCompile 'javax.servlet:javax.servlet-api:3.1.0'
} // providedCompile to prevent inclusion in the packaged .war
// those dependencies will be provided by the container
Gradle plugin for GAE is also available2
Build: $ gradle build
Running the GAE app locally: $ dev_appserver <path/to/war>
2https://cloud.google.com/appengine/docs/standard/java/tools/gradle
PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 10/38
GAE: example (iii)
Registring/uploading the app to the cloud
Access the Google Cloud Console:
https://console.developers.google.com
Create a new project with a name, e.g., myapp
Provide a deployment descriptor:
/src/main/webapp/WEB-INF/appengine-web.xml
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
<application>myapp</application>
<version>1</version>
<threadsafe>true</threadsafe> <!-- enables instance reuse for
multiple requests -->
</appengine-web-app>
Rebuild so that the WAR includes the deployment descriptor
Deploy: $ appcfg update <path/to/war>
You’re done at http://<projectID>.appspot.com/
The Console includes a Dashboard for viewing usage statistics
To use more resources, enable billing.
PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 11/38
GAE: example (iv)
Example of Cloud service (Users API): handling users via Google Accounts
import com.google.appengine.api.users.*;
//...
UserService userService = UserServiceFactory.getUserService();
User user = userService.getCurrentUser();
if(user == null){
String loginUrl = userService.createLoginURL("/login.jsp");
// ...
} else {
String logoutUrl = userService.createLogoutURL("/logout.jsp");
// ...
}
When the app is running in the development server, the login link goes to the dev
server’s simulated version of the Google Accounts sign-in screen.
PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 12/38
Outline
1 Introduction: PaaS
2 Google AppEngine: a Quick Outlook
3 Heroku
4 Spring Cloud
5 Example: Heroku+Spring
PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 13/38
Heroku: intro
Heroku
Heroku is a PaaS: it lets you deploy, run and manage applications
Heroku services are hosted on Amazon’s EC2
Pro: very easy to deploy and configure applications
Pro: does not force you to code against vendor-specific APIs
Con: can get expensive fast
Con: not as comprehensive as e.g. Google Cloud
Some history
Heroku was founded in 2007, initially targeting Ruby
Heroku is one of the first cloud platforms (PaaS)
In 2010, it has been acquired by Salesforce.com
In 2011, support for Node.js and Clojure was added
Now, it supports Java, Node.js, Scala, Clojure, Python, PHP, and Go
PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 14/38
Heroku: basic concepts I
Application = source code + dependencies + Procfile
Application source code and dependencies organised as per build definition
When using a well-known framework, Heroku can figure out how to run apps
Cf., main field in package.json for Node apps
To explicitly tell Heroku what to run, you can define a Procfile
web: java -jar lib/foobar.jar $PORT
queue: java -jar lib/queue-processor.jar
Format is <processType>: <commandToRun>
Only web processes can receive web traffic
Deployment
Heroku supports multiple mechanisms for deployment
Git: by pushing to your Heroku app remote
Github integration
REST API
PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 15/38
Heroku: basic concepts II
Building apps
When Heroku receives your app sources, it starts the building process
A slug is a bundle of your source, fetched dependencies, the language runtime,
the Procfile and compiled/generated output of the build system.
A buildpack is responsible of transforming the deployed code into a slug.
There is a set of officially supported buildpacks; these are open-source
There is also a huge num of 3rd-party buildpacks
Running apps on dynos
Dyno: a lightweight, isolated, secure, virtualized Unix container that provide the
environment required to run an app.
Apps are executed by running the commands specified in the Procfile on a dyno
that has been preloaded with a slug.
Dyno formation: the total number of currently-exeucting dynos
Each dyno gets its own ephemeral filesystem
PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 16/38
Heroku: basic concepts III
Release = slug + configuration + addons
Configuration variables contain customizable config data that can be changed
independently of your source code and exposed to a running app via
environment variables
Add-ons are resources attached to an app
Addons provide backing services (e.g., DBs, MOMs, Logging, E-mail..)
The interface to apps is often provided through configuration variables
A new release is generated any time you change sources, config vars, or add-ons
PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 17/38
How Heroku works: summary
PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 18/38
Heroku: free tier
Monthly pool of free dyno hours (450 hours/month) across all apps
Up to 5 free apps (unverified account) or 100 (verified account)
Max per free app: 1 web dyno, 1 worker dyno
Free apps sleep automatically after 30 mins of inactivity
Many add-ons (e.g., Heroku Postgres and Heroku Redis) do offer free plans
PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 19/38
Heroku Command-Line Interface (CLI)
Install: e.g., via package manager — brew install heroku (macOs)
Heroku CLI: main commands
Getting help: $ heroku help addons
Login: $ heroku auth:login
Create apps: $ heroku apps:create <appname>
Deploy apps: $ git push heroku master
Scale apps: $ heroku ps:scale web=1
Provision add-ons: $ heroku addons:create heroku-redis:hobby-dev
Set config vars: $ heroku config:set SOME_VAR=xxx
List dynos: $ heroku ps
See logs: $ heroku logs
Run a one-off process inside a dyno: $ heroku run <cmd>
Run Heroku locally: $ heroku local
Service-specific commands
$ heroku redis
$ heroku pg
PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 20/38
Heroku CLI: deployment commands
PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 21/38
12-factor apps
https://12factor.net
By Heroku co-founder Adam Wiggins
A methodology for building SaaS apps
1) Codebase: one codebase tracked in revision control, many deploys
2) Dependencies: explicitly declare and isolate dependencies
3) Config: store config in the environment
4) Backing services: treat backing services as attached resources
5) Build, release, run: strictly separate build and run stages
6) Processes: execute the app as one or more stateless processes
7) Port binding: export services via port binding
8) Concurrency: scale out via the process model
9) Disposability: maximize robustness with fast startup and graceful shutdown
10) Dev/Prod parity: keep development, staging, and production as similar as
possible
11) Logs: treat logs as event streams
12) Admin Processes: run admin/management tasks as one-off processes
PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 22/38
So, how do we develop microservice apps on Heroku?
Each µservice has a Git repo
You create a Heroku app for each µservice (repo)
Each Heroku app (i.e., µservice) has a public URI
For other services (e.g., Data, MOM, Logging), use add-ons
Add-ons will inject config vars to your apps to communicate the endpoints
PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 23/38
Outline
1 Introduction: PaaS
2 Google AppEngine: a Quick Outlook
3 Heroku
4 Spring Cloud
5 Example: Heroku+Spring
PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 24/38
Spring Cloud
Spring Cloud makes it easy to operationalize and deploy µservices to a private or
public cloud
Spring Cloud services
Spring Cloud Config: supports management of app config data through a
centralised service
Service Discovery: handles lookup and de/registration of service instances
E.g., with Eureka as service discovery engine
Hystrix integration: for resiliency patterns (circuit breaker, bulkhead..)
Ribbon integration: for service discovery and client-side load-balancing
PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 25/38
Spring Cloud Config I
Spring Cloud Config: general config management solution for many backends
It helps you build a configuration server as a Spring Boot service
How-to
Dependency: org.springframework.cloud:spring-cloud-config-server
Annotate the Spring Boot app class with @EnableConfigServer
Configure the server: src/main/resources/bootstrap.yml
server.port: ${PORT}
spring.profiles.active: git # use git as backend
spring.cloud.config.server.git:
uri: ${CONFIG_REPO_URI}
label: master
searchPaths: /some/path/
Within <REPO>/some/path/ there are files <appname>-<env>.yml
PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 26/38
Spring Cloud Config II
Apps will define in their bootstrap.yml:
spring.application.name: helloservice # appname
spring.profiles.active: default # profile maps to env
spring.cloud.config.uri: ${CONFIG_SERVER_URI}
and then bind config data to properties of beans
Component
public class MyConfig {
Value("${some.config.property}") private String prop;
public int getProp(){ return prop; }
//...
}
RestController
public class MyController {
Autowired MyConfig config;
//...
}
PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 27/38
Google Cloud service discovery I
Service discovery in Spring Cloud
Services register with the Eureka service registry by providing IP, port, and
service ID
Services use Ribbon for client-side load balancing
Ribbon will contact Eureka to retrieve service info and then cache it locally.
Periodically, Ribbon will ping Eureka to refresh the local cache.
Eureka service registry
Dependency: spring-cloud-starter-eureka-server
Configuration: application.yml
server.port: ${PORT}
eureka.client.registerWithEureka: false
eureka.client.fetchRegistry: false # dont't cache info
Finally, annotate the Spring Boot app with @EnableEurekaServer
Service registry URI: http://HOST:8761/eureka
PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 28/38
Google Cloud service discovery II
Registering services with Eureka
Dependency: spring-cloud-starter-eureka
Configuration: application.yml
spring.application.name: abc # logical name to register
eureka.instance.preferIpAddress: true # no server name
eureka.client:
registerWithEureka: true
fetchRegistry: true # pull down a local copy of registry
serviceUrl.defaultZone: ${SERVICE_REGISTRY_URI}
PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 29/38
Google Cloud service discovery III
Service discovery – Approach 1: Netflix Feign client
Dependency: spring-cloud-starter-feign
Annotate the Spring Boot app with @EnableFeignClients
Define a service proxy bean by simply annotating an interface:
FeignClient("service-ID") public interface MyProxy {
RequestMapping(method = RequestMethod.GET,
value = "/path/{arg}", consumes="text/plain")
String getSomething( PathVariable("arg") String arg);
}
Note: also provides load balancing
PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 30/38
Google Cloud service discovery IV
Service discovery – Approach 2: Ribbon-aware Spring RestTemplate
Create a RestTemplate bean annotated with @LoadBalanced:
SpringBootApplication public class MyApp {
LoadBalanced Bean public RestTemplate getRestTemplate(){
return new RestTemplate();
}
//...
}
Wire the RestTemplate bean in your components
Component
public class AnotherProxy {
Autowired RestTemplate rest;
public String getSomething(String arg){
return rest.exchange("http://SERVICE_ID/path/"+arg,
HttpMethod.GET, null, String.class).getBody();
} // note: SERVICE_ID is used (and not host:port!!!)
}
Note: also provides load balancing
PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 31/38
Client Resiliency Patterns with Hystrix I
Client resiliency patterns
These pattenrs focus on protecting a client from crashing when the remote resource
is throwing errors or performing poorly.
Client-side load balancing: the load balancer can remove unstable instances
Provided by Netflix Ribbon libs out of the box with no extra config.
Circuit breakers: ensure a client doesn’t repeatedly call a failing service
Fallbacks: when a call does fail, it looks if there’s an alternative to run
Bulkheads: segregates different service calls to ensure a poor-behaving service
does not use all resources on the client.
Using Hystrix
Dependency: spring-cloud-starter-netflix-hystrix
Activate Hystrix by annotating the Spring app with @EnableCircuitBreaker
Annotate service component methods with @HystrixCommand to automatically
generate a proxy wrapping method calls over a thread pool.
PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 32/38
Client Resiliency Patterns with Hystrix II
Implementing client resiliency patterns
Circuit Breaker
Component public class MyProxy {
Autowired RestTemplate rest;
HystrixCommand(commandProperties = {
HystrixProperty(name="execution.isolation.thread.
timeoutInMilliseconds",
value="5000")
}) public String getSomething(String arg){ /* as before */ };
}
Fallback pattern
HystrixCommand(fallbackMethod="fallbackGet", ...)
public String getSomething(String arg){ /* as before */ };
public String fallbackGet(String arg){ /* fallback logic */ };
Bulkhead pattern
HystrixCommand(threadPoolKey="myThreadPool", ...)
public String getSomething(String arg){ /* as before */ };
PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 33/38
Outline
1 Introduction: PaaS
2 Google AppEngine: a Quick Outlook
3 Heroku
4 Spring Cloud
5 Example: Heroku+Spring
PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 34/38
Application structure
Repositories = heroku apps = µservices
Repos: http://bitbucket.org/unibo_disi/<appname>
Public URLs: http://<appname>.herokuapp.com/
pcd1718-paas-service-registry: Spring/Eureka service registry
pcd1718-paas-service-config: Spring Config server
The repo of the service is also the configuration data store
pcd1718-paas-helloservice: µservice with simple API
It is meant to be scaled
It uses a backing Redis data store
pcd1718-paas-serviceconsumer: µservice that depends on helloservice
Uses Spring/Ribbon/Hystrix for load balancing and client resiliency patterns
Other services
Redis: NoSQL Key/Value store
Heroku Redis addon on Cloud / local Redis server for dev
PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 35/38
From Spring to Heroku apps
Tuning Spring µservices for Heroku
Create an app: $ heroku apps:create <appname>
Define a Procfile: since gradle bootJar packages the µservice into a JAR
within build/libs, the Procfile for <APPNAME> will be something as:
web: java -jar build/libs/<APPNAME>-<VERS>.jar
Adjust configuration to use environment variables
server.port: ${PORT} # PORT is set by Heroku!!!
spring.cloud.config.uri: ${CONFIG_SERVICE_URI}
Heroku Local development
Command e.g. heroku local web=2 starts the app locally (2 instances)
Define environment vars in .env
CONFIG_SERVICE_URI=http://127.0.0.1:8888
SERVICE_REGISTRY_URI=http://127.0.0.1:8761/eureka
REDIS_URL=redis://127.0.0.1:6379
PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 36/38
Exercise: inspect the application
Steps
Take your time to understand all the pieces involved:
1) Clone the repositories, open a terminal tab on each
2) Try to start the system using heroku locala
You need to run redis-server to start Redis locally
You may run heroku local in background; then, keep track of the PIDs to kill by
looking at the logs.
Use -e to provide a custom xxx.env file
Use -p to specify the deployment port
3) Study the source code
4) Try to deploy the app on Heroku
You need to properly set the config vars to provide the correct URLs for each
deployed app
Attach Redis Heroku add-on to helloservice app; it will result in the injection of a
config var REDIS_URL
You won’t be able to scale it in the free tier, though
ahttps://devcenter.heroku.com/articles/heroku-local
PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 37/38
References I
J. Carnell. Spring Microservices in Action. Manning Publications Company, 2017. ISBN:
9781617293986. URL: https://books.google.it/books?id=xDH9MAAACAAJ.
Heroku DevCenter. https://devcenter.heroku.com/. [Online; accessed 2018-06].
Spring Docs. https://spring.io/docs. [Online; accessed 2018-06].
Stefan Walraven, Eddy Truyen, and Wouter Joosen. “Comparing PaaS offerings in light of SaaS
development”. In: Computing 96.8 (2014), pp. 669–724.
PCD1718 Appendix References 38/38

More Related Content

PDF
Hands-on GitOps Patterns for Helm Users
PDF
PuppetConf 2016: Using Puppet with Kubernetes and OpenShift – Diane Mueller, ...
PDF
DEEP: a user success story
PDF
Leveraging Gradle @ Netflix (Madrid GUG Feb 2, 2021)
PDF
Peering Inside the Black Box: A Case for Observability
PDF
Cloud Native Engineering with SRE and GitOps
PDF
Observe and command your fleets across any kubernetes with weave git ops
PDF
Kubernetes One-Click Deployment: Hands-on Workshop (Mainz)
Hands-on GitOps Patterns for Helm Users
PuppetConf 2016: Using Puppet with Kubernetes and OpenShift – Diane Mueller, ...
DEEP: a user success story
Leveraging Gradle @ Netflix (Madrid GUG Feb 2, 2021)
Peering Inside the Black Box: A Case for Observability
Cloud Native Engineering with SRE and GitOps
Observe and command your fleets across any kubernetes with weave git ops
Kubernetes One-Click Deployment: Hands-on Workshop (Mainz)

What's hot (20)

PDF
Secure GitOps pipelines for Kubernetes with Snyk & Weaveworks
PDF
Delivering Quality at Speed with GitOps
PDF
Red Hat OpenShift Operators - Operators ABC
PDF
Hardening Your CI/CD Pipelines with GitOps and Continuous Security
PDF
Pivotal Cloud Foundry 2.0: First Look
PPTX
もう知らずにはいられないGitOpsをArgoCDで学ぶ【WESEEK Tech Conf #3】
PDF
Open shift 4-update
PDF
Full Steam Ahead, R2DBC!
PDF
Weave GitOps Core Overview (Free GitOps Workshop)
PDF
Exploring the GitHub Service Universe
PDF
OpenShift Taiwan Vol.1 Technology Overview
PDF
Kubecon US 2019: Kubernetes Multitenancy WG Deep Dive
PDF
Automated Virtualized Testing (AVT) with Docker, Kubernetes, WireMock and Gat...
PDF
Pycon9 - Paas per tutti i gusti con Dokku and Kubernetes
PDF
Gitops Hands On
PDF
Introduction to Reactive Streams and Reactor 2.5
PDF
Container Technologies and Transformational value
PDF
You Want to Kubernetes? You MUST Know Containers!
PDF
GitOps - Operation By Pull Request
PPTX
[JOI] TOTVS Developers Joinville - Java #1
Secure GitOps pipelines for Kubernetes with Snyk & Weaveworks
Delivering Quality at Speed with GitOps
Red Hat OpenShift Operators - Operators ABC
Hardening Your CI/CD Pipelines with GitOps and Continuous Security
Pivotal Cloud Foundry 2.0: First Look
もう知らずにはいられないGitOpsをArgoCDで学ぶ【WESEEK Tech Conf #3】
Open shift 4-update
Full Steam Ahead, R2DBC!
Weave GitOps Core Overview (Free GitOps Workshop)
Exploring the GitHub Service Universe
OpenShift Taiwan Vol.1 Technology Overview
Kubecon US 2019: Kubernetes Multitenancy WG Deep Dive
Automated Virtualized Testing (AVT) with Docker, Kubernetes, WireMock and Gat...
Pycon9 - Paas per tutti i gusti con Dokku and Kubernetes
Gitops Hands On
Introduction to Reactive Streams and Reactor 2.5
Container Technologies and Transformational value
You Want to Kubernetes? You MUST Know Containers!
GitOps - Operation By Pull Request
[JOI] TOTVS Developers Joinville - Java #1
Ad

Similar to Introduction to cloud-native application development: with Heroku and Spring Boot/Cloud (20)

PPT
Heroku for-team-collaboration
PPT
Heroku for team collaboration
PPTX
what is Heroku , where, when and how it is used
PDF
Powerful Google Cloud tools for your hack
PDF
Introduction to PaaS and Heroku
PDF
Java Tech & Tools | Deploying Java & Play Framework Apps to the Cloud | Sande...
PDF
Google App Engine
PDF
[B6]heroku postgres-hgmnz
PPTX
Introduction to Heroku - CCT London 2013
PDF
Serverless computing with Google Cloud
PDF
Java Web Programming Using Cloud Platform: Module 10
PDF
Heroku
PPTX
Social ent. with java on heroku
PDF
Harper Reed: Cloud Contraints
PDF
Cloudcamp Athens 2011 Presenting Heroku
PDF
Powerful Google Cloud tools for your hack (2020)
PPTX
Google Cloud Platform
PDF
Platform as a service google app engine
PDF
Java Web Programming on Google Cloud Platform [1/3] : Google App Engine
PDF
Introduction to Cloud Computing (New)
Heroku for-team-collaboration
Heroku for team collaboration
what is Heroku , where, when and how it is used
Powerful Google Cloud tools for your hack
Introduction to PaaS and Heroku
Java Tech & Tools | Deploying Java & Play Framework Apps to the Cloud | Sande...
Google App Engine
[B6]heroku postgres-hgmnz
Introduction to Heroku - CCT London 2013
Serverless computing with Google Cloud
Java Web Programming Using Cloud Platform: Module 10
Heroku
Social ent. with java on heroku
Harper Reed: Cloud Contraints
Cloudcamp Athens 2011 Presenting Heroku
Powerful Google Cloud tools for your hack (2020)
Google Cloud Platform
Platform as a service google app engine
Java Web Programming on Google Cloud Platform [1/3] : Google App Engine
Introduction to Cloud Computing (New)
Ad

More from Roberto Casadei (20)

PDF
Integrating Collective Computing and the Social Internet of Things for Smart ...
PDF
Software Engineering Methods for Artificial Collective Intelligence
PDF
Declarative Macro-Programming of Collective Systems with Aggregate Computing:...
PDF
Programming (and Learning) Self-Adaptive & Self-Organising Behaviour with Sca...
PDF
A Presentation of My Research Activity
PDF
Self-Organisation Programming: a Functional Reactive Macro Approach (FRASP) [...
PDF
Programming Distributed Collective Processes for Dynamic Ensembles and Collec...
PDF
Towards Automated Engineering for Collective Adaptive Systems: Vision and Res...
PDF
Aggregate Computing Research: an Overview
PDF
Introduction to the 1st DISCOLI workshop on distributed collective intelligence
PDF
Digital Twins, Virtual Devices, and Augmentations for Self-Organising Cyber-P...
PDF
FScaFi: A Core Calculus for Collective Adaptive Systems Programming
PDF
6th eCAS workshop on Engineering Collective Adaptive Systems
PDF
Augmented Collective Digital Twins for Self-Organising Cyber-Physical Systems
PDF
Tuple-Based Coordination in Large-Scale Situated Systems
PDF
Pulverisation in Cyber-Physical Systems: Engineering the Self-Organising Logi...
PDF
Collective Adaptive Systems as Coordination Media: The Case of Tuples in Spac...
PDF
Testing: an Introduction and Panorama
PDF
On Context-Orientation in Aggregate Programming
PDF
Engineering Resilient Collaborative Edge-enabled IoT
Integrating Collective Computing and the Social Internet of Things for Smart ...
Software Engineering Methods for Artificial Collective Intelligence
Declarative Macro-Programming of Collective Systems with Aggregate Computing:...
Programming (and Learning) Self-Adaptive & Self-Organising Behaviour with Sca...
A Presentation of My Research Activity
Self-Organisation Programming: a Functional Reactive Macro Approach (FRASP) [...
Programming Distributed Collective Processes for Dynamic Ensembles and Collec...
Towards Automated Engineering for Collective Adaptive Systems: Vision and Res...
Aggregate Computing Research: an Overview
Introduction to the 1st DISCOLI workshop on distributed collective intelligence
Digital Twins, Virtual Devices, and Augmentations for Self-Organising Cyber-P...
FScaFi: A Core Calculus for Collective Adaptive Systems Programming
6th eCAS workshop on Engineering Collective Adaptive Systems
Augmented Collective Digital Twins for Self-Organising Cyber-Physical Systems
Tuple-Based Coordination in Large-Scale Situated Systems
Pulverisation in Cyber-Physical Systems: Engineering the Self-Organising Logi...
Collective Adaptive Systems as Coordination Media: The Case of Tuples in Spac...
Testing: an Introduction and Panorama
On Context-Orientation in Aggregate Programming
Engineering Resilient Collaborative Edge-enabled IoT

Recently uploaded (20)

PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PPT
Mechanical Engineering MATERIALS Selection
PDF
composite construction of structures.pdf
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
web development for engineering and engineering
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
additive manufacturing of ss316l using mig welding
PPT
Project quality management in manufacturing
PPTX
Lecture Notes Electrical Wiring System Components
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
Foundation to blockchain - A guide to Blockchain Tech
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
Mechanical Engineering MATERIALS Selection
composite construction of structures.pdf
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
web development for engineering and engineering
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
additive manufacturing of ss316l using mig welding
Project quality management in manufacturing
Lecture Notes Electrical Wiring System Components
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
Internet of Things (IOT) - A guide to understanding
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS

Introduction to cloud-native application development: with Heroku and Spring Boot/Cloud

  • 1. Development with PaaS An introduction using Heroku and Spring Roberto Casadei Concurrent and Distributed Programming course Department of Computer Science and Engineering (DISI) Alma Mater Studiorum – Università of Bologna June 16, 2018 PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 1/38
  • 2. Outline 1 Introduction: PaaS 2 Google AppEngine: a Quick Outlook 3 Heroku 4 Spring Cloud 5 Example: Heroku+Spring PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 2/38
  • 3. Introduction: PaaS Platform-as-a-Service (PaaS) NIST definition: “The capability provided to the consumer is to deploy onto the cloud infrastructure consumer-created or acquired applications created using programming languages and tools supported by the provider. The consumer does not manage or control the underlying cloud infrastructure including network, servers, operating systems, or storage, but has control over the deployed applications and possibly application hosting environment configurations.” PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 3/38
  • 4. PaaS is fuzzy Beware: there is not only one PaaS model https://blog.mccrory.me/2011/01/23/current-paas-patterns-types-of-paas/ 1) Upload your environment as a VM/container (cf., IaaS) 2) Upload your compiled/packaged application (e.g., WAR) 3) Upload your code—compilation happens in the PaaS itself (cf., Heroku) 4) Upload platform-specific code (cf., Salesforce) “Comparing PaaS Offerings in Light of SaaS Development”[4] 1) Unfocused PaaS platforms that mimic and match the APIs of popular enterprise application servers and middleware platforms (e.g., Heroku, Cloud Foundry, Azure, OpenShift) 2) Focused PaaS platforms—aim to optimally support specific types of cloud applications, for better scalability (e.g., GAE) 3) Metadata-driven PaaS platforms (e.g., Salesforce) https://www.youtube.com/watch?v=V_lM59cvGoI 1) Bring Your Own Code pattern 2) Bring Your Own Container pattern PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 4/38
  • 5. Outline 1 Introduction: PaaS 2 Google AppEngine: a Quick Outlook 3 Heroku 4 Spring Cloud 5 Example: Heroku+Spring PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 5/38
  • 6. GAE: Flexible vs. Standard Environment PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 6/38
  • 7. GAE: Flexible vs. Standard Environment Standard Environment Application instances run in a sandbox, using the runtime environment of a supported language (Python 2.7, Java 7/8, PHP 5.5, Go 1.6/1.8/1.9) Building an application is more constrained and involved, but your apps will have faster scale up times, better reacting to sudden and extreme spikes of traffic. Flexible Environment Application instances run within Docker containers on GAE VMs Use a predefined runtime (Python, Java, Node.js, Go, Ruby, PHP, .NET) or build your custom runtime to support any PL/framework Your apps can depend on other SW, including OS packages through apt-get No sandbox limitations: you can create threads, processes, sockets. No GAE APIs: apps are written like standard web apps that can run anywhere Google Cloud client libraries to call Google Cloud Services These client libraries work everywhere ⇒ your app is more portable https://cloud.google.com/appengine/docs/the-appengine-environments PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 7/38
  • 8. Google Cloud SDK Google Cloud SDK is a set of tools for accessing Google Cloud services via command-line 1) Install it: you’ll get, among other things, a tool gcloud 2) Setup: $ gcloud init Authorizes Cloud SDK tools to use your user account credentials to access Google Cloud Platform Sets up a Cloud SDK configuration (i.e., a set of properties) $ gcloud config list 3) View/Install SDK components: $ gcloud components list app-engine-java: gcloud App Java Extensions gcd-emulator: emulator for Google Cloud Datastore PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 8/38
  • 9. GAE: example (i) Java web apps for GAE use the standard Java Servlet interface1 An app is deployed as a Web Application Archive (WAR) The Java GAE SDK is the bridge to GAE and supports simulating GAE locally /src/main/java/it/unibo/MyServlet.java public class MyServlet extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { resp.setContentType("text/html"); resp.getWriter().println("Hello GAE"); } } /src/main/webapp/WEB-INF/web.xml <web-app ....> <servlet> <servlet-name>myservlet</servlet-name> <servlet-class>clock.MyServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>myservlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app> 1https://guides.gradle.org/building-java-web-applications/ PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 9/38
  • 10. GAE: example (ii) /build.gradle apply plugin: 'java' apply plugin: 'war' dependencies { compile 'com.google.appengine:appengine-api-1.0-sdk:+' providedCompile 'javax.servlet:javax.servlet-api:3.1.0' } // providedCompile to prevent inclusion in the packaged .war // those dependencies will be provided by the container Gradle plugin for GAE is also available2 Build: $ gradle build Running the GAE app locally: $ dev_appserver <path/to/war> 2https://cloud.google.com/appengine/docs/standard/java/tools/gradle PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 10/38
  • 11. GAE: example (iii) Registring/uploading the app to the cloud Access the Google Cloud Console: https://console.developers.google.com Create a new project with a name, e.g., myapp Provide a deployment descriptor: /src/main/webapp/WEB-INF/appengine-web.xml <appengine-web-app xmlns="http://appengine.google.com/ns/1.0"> <application>myapp</application> <version>1</version> <threadsafe>true</threadsafe> <!-- enables instance reuse for multiple requests --> </appengine-web-app> Rebuild so that the WAR includes the deployment descriptor Deploy: $ appcfg update <path/to/war> You’re done at http://<projectID>.appspot.com/ The Console includes a Dashboard for viewing usage statistics To use more resources, enable billing. PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 11/38
  • 12. GAE: example (iv) Example of Cloud service (Users API): handling users via Google Accounts import com.google.appengine.api.users.*; //... UserService userService = UserServiceFactory.getUserService(); User user = userService.getCurrentUser(); if(user == null){ String loginUrl = userService.createLoginURL("/login.jsp"); // ... } else { String logoutUrl = userService.createLogoutURL("/logout.jsp"); // ... } When the app is running in the development server, the login link goes to the dev server’s simulated version of the Google Accounts sign-in screen. PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 12/38
  • 13. Outline 1 Introduction: PaaS 2 Google AppEngine: a Quick Outlook 3 Heroku 4 Spring Cloud 5 Example: Heroku+Spring PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 13/38
  • 14. Heroku: intro Heroku Heroku is a PaaS: it lets you deploy, run and manage applications Heroku services are hosted on Amazon’s EC2 Pro: very easy to deploy and configure applications Pro: does not force you to code against vendor-specific APIs Con: can get expensive fast Con: not as comprehensive as e.g. Google Cloud Some history Heroku was founded in 2007, initially targeting Ruby Heroku is one of the first cloud platforms (PaaS) In 2010, it has been acquired by Salesforce.com In 2011, support for Node.js and Clojure was added Now, it supports Java, Node.js, Scala, Clojure, Python, PHP, and Go PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 14/38
  • 15. Heroku: basic concepts I Application = source code + dependencies + Procfile Application source code and dependencies organised as per build definition When using a well-known framework, Heroku can figure out how to run apps Cf., main field in package.json for Node apps To explicitly tell Heroku what to run, you can define a Procfile web: java -jar lib/foobar.jar $PORT queue: java -jar lib/queue-processor.jar Format is <processType>: <commandToRun> Only web processes can receive web traffic Deployment Heroku supports multiple mechanisms for deployment Git: by pushing to your Heroku app remote Github integration REST API PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 15/38
  • 16. Heroku: basic concepts II Building apps When Heroku receives your app sources, it starts the building process A slug is a bundle of your source, fetched dependencies, the language runtime, the Procfile and compiled/generated output of the build system. A buildpack is responsible of transforming the deployed code into a slug. There is a set of officially supported buildpacks; these are open-source There is also a huge num of 3rd-party buildpacks Running apps on dynos Dyno: a lightweight, isolated, secure, virtualized Unix container that provide the environment required to run an app. Apps are executed by running the commands specified in the Procfile on a dyno that has been preloaded with a slug. Dyno formation: the total number of currently-exeucting dynos Each dyno gets its own ephemeral filesystem PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 16/38
  • 17. Heroku: basic concepts III Release = slug + configuration + addons Configuration variables contain customizable config data that can be changed independently of your source code and exposed to a running app via environment variables Add-ons are resources attached to an app Addons provide backing services (e.g., DBs, MOMs, Logging, E-mail..) The interface to apps is often provided through configuration variables A new release is generated any time you change sources, config vars, or add-ons PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 17/38
  • 18. How Heroku works: summary PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 18/38
  • 19. Heroku: free tier Monthly pool of free dyno hours (450 hours/month) across all apps Up to 5 free apps (unverified account) or 100 (verified account) Max per free app: 1 web dyno, 1 worker dyno Free apps sleep automatically after 30 mins of inactivity Many add-ons (e.g., Heroku Postgres and Heroku Redis) do offer free plans PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 19/38
  • 20. Heroku Command-Line Interface (CLI) Install: e.g., via package manager — brew install heroku (macOs) Heroku CLI: main commands Getting help: $ heroku help addons Login: $ heroku auth:login Create apps: $ heroku apps:create <appname> Deploy apps: $ git push heroku master Scale apps: $ heroku ps:scale web=1 Provision add-ons: $ heroku addons:create heroku-redis:hobby-dev Set config vars: $ heroku config:set SOME_VAR=xxx List dynos: $ heroku ps See logs: $ heroku logs Run a one-off process inside a dyno: $ heroku run <cmd> Run Heroku locally: $ heroku local Service-specific commands $ heroku redis $ heroku pg PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 20/38
  • 21. Heroku CLI: deployment commands PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 21/38
  • 22. 12-factor apps https://12factor.net By Heroku co-founder Adam Wiggins A methodology for building SaaS apps 1) Codebase: one codebase tracked in revision control, many deploys 2) Dependencies: explicitly declare and isolate dependencies 3) Config: store config in the environment 4) Backing services: treat backing services as attached resources 5) Build, release, run: strictly separate build and run stages 6) Processes: execute the app as one or more stateless processes 7) Port binding: export services via port binding 8) Concurrency: scale out via the process model 9) Disposability: maximize robustness with fast startup and graceful shutdown 10) Dev/Prod parity: keep development, staging, and production as similar as possible 11) Logs: treat logs as event streams 12) Admin Processes: run admin/management tasks as one-off processes PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 22/38
  • 23. So, how do we develop microservice apps on Heroku? Each µservice has a Git repo You create a Heroku app for each µservice (repo) Each Heroku app (i.e., µservice) has a public URI For other services (e.g., Data, MOM, Logging), use add-ons Add-ons will inject config vars to your apps to communicate the endpoints PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 23/38
  • 24. Outline 1 Introduction: PaaS 2 Google AppEngine: a Quick Outlook 3 Heroku 4 Spring Cloud 5 Example: Heroku+Spring PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 24/38
  • 25. Spring Cloud Spring Cloud makes it easy to operationalize and deploy µservices to a private or public cloud Spring Cloud services Spring Cloud Config: supports management of app config data through a centralised service Service Discovery: handles lookup and de/registration of service instances E.g., with Eureka as service discovery engine Hystrix integration: for resiliency patterns (circuit breaker, bulkhead..) Ribbon integration: for service discovery and client-side load-balancing PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 25/38
  • 26. Spring Cloud Config I Spring Cloud Config: general config management solution for many backends It helps you build a configuration server as a Spring Boot service How-to Dependency: org.springframework.cloud:spring-cloud-config-server Annotate the Spring Boot app class with @EnableConfigServer Configure the server: src/main/resources/bootstrap.yml server.port: ${PORT} spring.profiles.active: git # use git as backend spring.cloud.config.server.git: uri: ${CONFIG_REPO_URI} label: master searchPaths: /some/path/ Within <REPO>/some/path/ there are files <appname>-<env>.yml PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 26/38
  • 27. Spring Cloud Config II Apps will define in their bootstrap.yml: spring.application.name: helloservice # appname spring.profiles.active: default # profile maps to env spring.cloud.config.uri: ${CONFIG_SERVER_URI} and then bind config data to properties of beans Component public class MyConfig { Value("${some.config.property}") private String prop; public int getProp(){ return prop; } //... } RestController public class MyController { Autowired MyConfig config; //... } PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 27/38
  • 28. Google Cloud service discovery I Service discovery in Spring Cloud Services register with the Eureka service registry by providing IP, port, and service ID Services use Ribbon for client-side load balancing Ribbon will contact Eureka to retrieve service info and then cache it locally. Periodically, Ribbon will ping Eureka to refresh the local cache. Eureka service registry Dependency: spring-cloud-starter-eureka-server Configuration: application.yml server.port: ${PORT} eureka.client.registerWithEureka: false eureka.client.fetchRegistry: false # dont't cache info Finally, annotate the Spring Boot app with @EnableEurekaServer Service registry URI: http://HOST:8761/eureka PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 28/38
  • 29. Google Cloud service discovery II Registering services with Eureka Dependency: spring-cloud-starter-eureka Configuration: application.yml spring.application.name: abc # logical name to register eureka.instance.preferIpAddress: true # no server name eureka.client: registerWithEureka: true fetchRegistry: true # pull down a local copy of registry serviceUrl.defaultZone: ${SERVICE_REGISTRY_URI} PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 29/38
  • 30. Google Cloud service discovery III Service discovery – Approach 1: Netflix Feign client Dependency: spring-cloud-starter-feign Annotate the Spring Boot app with @EnableFeignClients Define a service proxy bean by simply annotating an interface: FeignClient("service-ID") public interface MyProxy { RequestMapping(method = RequestMethod.GET, value = "/path/{arg}", consumes="text/plain") String getSomething( PathVariable("arg") String arg); } Note: also provides load balancing PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 30/38
  • 31. Google Cloud service discovery IV Service discovery – Approach 2: Ribbon-aware Spring RestTemplate Create a RestTemplate bean annotated with @LoadBalanced: SpringBootApplication public class MyApp { LoadBalanced Bean public RestTemplate getRestTemplate(){ return new RestTemplate(); } //... } Wire the RestTemplate bean in your components Component public class AnotherProxy { Autowired RestTemplate rest; public String getSomething(String arg){ return rest.exchange("http://SERVICE_ID/path/"+arg, HttpMethod.GET, null, String.class).getBody(); } // note: SERVICE_ID is used (and not host:port!!!) } Note: also provides load balancing PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 31/38
  • 32. Client Resiliency Patterns with Hystrix I Client resiliency patterns These pattenrs focus on protecting a client from crashing when the remote resource is throwing errors or performing poorly. Client-side load balancing: the load balancer can remove unstable instances Provided by Netflix Ribbon libs out of the box with no extra config. Circuit breakers: ensure a client doesn’t repeatedly call a failing service Fallbacks: when a call does fail, it looks if there’s an alternative to run Bulkheads: segregates different service calls to ensure a poor-behaving service does not use all resources on the client. Using Hystrix Dependency: spring-cloud-starter-netflix-hystrix Activate Hystrix by annotating the Spring app with @EnableCircuitBreaker Annotate service component methods with @HystrixCommand to automatically generate a proxy wrapping method calls over a thread pool. PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 32/38
  • 33. Client Resiliency Patterns with Hystrix II Implementing client resiliency patterns Circuit Breaker Component public class MyProxy { Autowired RestTemplate rest; HystrixCommand(commandProperties = { HystrixProperty(name="execution.isolation.thread. timeoutInMilliseconds", value="5000") }) public String getSomething(String arg){ /* as before */ }; } Fallback pattern HystrixCommand(fallbackMethod="fallbackGet", ...) public String getSomething(String arg){ /* as before */ }; public String fallbackGet(String arg){ /* fallback logic */ }; Bulkhead pattern HystrixCommand(threadPoolKey="myThreadPool", ...) public String getSomething(String arg){ /* as before */ }; PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 33/38
  • 34. Outline 1 Introduction: PaaS 2 Google AppEngine: a Quick Outlook 3 Heroku 4 Spring Cloud 5 Example: Heroku+Spring PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 34/38
  • 35. Application structure Repositories = heroku apps = µservices Repos: http://bitbucket.org/unibo_disi/<appname> Public URLs: http://<appname>.herokuapp.com/ pcd1718-paas-service-registry: Spring/Eureka service registry pcd1718-paas-service-config: Spring Config server The repo of the service is also the configuration data store pcd1718-paas-helloservice: µservice with simple API It is meant to be scaled It uses a backing Redis data store pcd1718-paas-serviceconsumer: µservice that depends on helloservice Uses Spring/Ribbon/Hystrix for load balancing and client resiliency patterns Other services Redis: NoSQL Key/Value store Heroku Redis addon on Cloud / local Redis server for dev PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 35/38
  • 36. From Spring to Heroku apps Tuning Spring µservices for Heroku Create an app: $ heroku apps:create <appname> Define a Procfile: since gradle bootJar packages the µservice into a JAR within build/libs, the Procfile for <APPNAME> will be something as: web: java -jar build/libs/<APPNAME>-<VERS>.jar Adjust configuration to use environment variables server.port: ${PORT} # PORT is set by Heroku!!! spring.cloud.config.uri: ${CONFIG_SERVICE_URI} Heroku Local development Command e.g. heroku local web=2 starts the app locally (2 instances) Define environment vars in .env CONFIG_SERVICE_URI=http://127.0.0.1:8888 SERVICE_REGISTRY_URI=http://127.0.0.1:8761/eureka REDIS_URL=redis://127.0.0.1:6379 PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 36/38
  • 37. Exercise: inspect the application Steps Take your time to understand all the pieces involved: 1) Clone the repositories, open a terminal tab on each 2) Try to start the system using heroku locala You need to run redis-server to start Redis locally You may run heroku local in background; then, keep track of the PIDs to kill by looking at the logs. Use -e to provide a custom xxx.env file Use -p to specify the deployment port 3) Study the source code 4) Try to deploy the app on Heroku You need to properly set the config vars to provide the correct URLs for each deployed app Attach Redis Heroku add-on to helloservice app; it will result in the injection of a config var REDIS_URL You won’t be able to scale it in the free tier, though ahttps://devcenter.heroku.com/articles/heroku-local PCD1718 PaaS-Intro GAE Heroku Spring Cloud Example: Heroku+Spring 37/38
  • 38. References I J. Carnell. Spring Microservices in Action. Manning Publications Company, 2017. ISBN: 9781617293986. URL: https://books.google.it/books?id=xDH9MAAACAAJ. Heroku DevCenter. https://devcenter.heroku.com/. [Online; accessed 2018-06]. Spring Docs. https://spring.io/docs. [Online; accessed 2018-06]. Stefan Walraven, Eddy Truyen, and Wouter Joosen. “Comparing PaaS offerings in light of SaaS development”. In: Computing 96.8 (2014), pp. 669–724. PCD1718 Appendix References 38/38