Let's face it, the cloud's here to stay. Cloud Foundry, introduced to rave reviews in the NoSQL, Node.js, Ruby, Scala and Java communities, represents the most promising, most open cloud platform for Java and Spring applications today, and tomorrow. In this talk, we introduce Cloud Foundry and describe it's architecture.
You will learn about why Spring is the ideal cloud computing platform. We describe how Cloud Foundry can be used with both existing Spring applications and new ones leveraging Spring 3.1. You will learn how to use Spring Data to develop NoSQL applications on Cloud Foundry, and how to integrate applications with RabbitMQ and Spring AMQP.
8. About Chris
http://www.theregister.co.uk/2009/08/19/springsource_cloud_foundry/
8
9. About Chris
Developer Advocate for
CloudFoundry.com
Signup at http://cloudfoundry.com
Promo code: EgyptJUG
9
10. Agenda
Why PaaS?
Cloud Foundry basics
Using Cloud Foundry services
Spring and Cloud Foundry
Using NoSQL databases
Cloud Foundry and asynchronous architectures
11. Traditional web application architecture
WAR
StoreFront
Accounting
Service
MySQL
Desktop Database
Apache
Browser InventoryService
Shipping
Service
Simple to Tomcat
develop
test
deploy
scale Or is it? 11
12. Let’s imagine you want to run a
performance test...
?
How long to get the servers?
Who is going to set up the servers?
Who is going to set up Apache,
Tomcat, and MySQL?
13. And things are changing:
This simple, monolithic architecture is
inadequate
13
14. New kinds of clients
Smart phones overtake PCs in Q4 2010
14
15. Users expect a rich, dynamic and interactive experience on mobile
devices and desktop
h
oug
d en
oo
’t g
HTTP Request
e isn
r
ctu
Desktop Java Web
e
hit
Browser HTML/Javascript Application
c
I ar
ty le U
s
Old
15
16. Need to handle massive loads and the data explosion
Need scalable architectures
Application tier:
• Replicated/clustered servers
• Modular so that components can be scaled differently
• Asynchronous architecture - communication via a message broker
Database tier:
• Replication
• Sharding
• Polyglot persistence: Relational, NoSQL, NewSQL databases
16
17. Scaling development
WAR
StoreFront
Accounting
Service Scalable
!=
InventoryService development
Shipping
Service
Forces multiple developers/teams to synchronize development efforts
Obstacle to frequent, independent deployments
Increases risk of failure - need to redeploy everything to change one thing
17
18. Modern application architecture
billing.war
Desktop Accounting
Browser Service
StoreUI
Native Mobile NodeJS
NodeJS inventory.war
Application front-end
MySQL
application
StoreUI
StoreUI InventoryService
HTML5 mobile
application
StoreUI Redis shipping.war
RabbitMQ
Mongo
ShippingService
18
20. Let’s imagine you are fixing a bug
and want to run some JUnit
integration tests...
?
Who is going to install and configure
your sandbox: MySQL, RabbitMQ,
MongoDB, ....?
21. Let’s imagine you have fixed a bug
and want to run some functional
tests...
?
How long to purchase the servers?
Who is going to set up the servers?
Who is going to install and configure
MySQL, RabbitMQ, MongoDB, ....?
22. Platform-as-a-Service is the solution
Easy deployment Services:
Database
Application management + Blob storage
Messaging
Easy scaling up and down ...
22
23. Agenda
Why PaaS?
Cloud Foundry basics
Using Cloud Foundry services
Spring and Cloud Foundry
Using NoSQL databases
Cloud Foundry and asynchronous architectures
24. About Cloud Foundry
.js
Ap
p
lica
Private
7o
Clouds
n
S
Data Services
erv
…
i
ce
In
ter
e
fac
fac
ter
Public
e
r
In Clouds
ide
Msg Services
ov
Pr
ud
Cl o
Micro .COM
Clouds
Other Services
24
28. Vibrant open-source ecosystem
AppFog.com
• Community lead for PHP
• PaaS for PHP
Joyent
• Community lead for Node.js
ActiveState
• Community lead for Python
• Stackato private PaaS
29. Cloud Foundry - Architecture
Web
Web
Web
100’s of DEAs
Load Balancers
Load Balancers
App Load Balancers
App
App
Message Bus
Message Bus
Message Bus
(NATS)
(NATS)
(NATS)
DEAs
DEAs
DEAs
Routers
Routers
Routers
DB
DB
DB Cloud
Cloud
Controllers
Cloud
Controllers
Controllers
Services Health
Health
Services Health
Manager
Services Manager
Manager
Web Service API
33. Using the Cloud Foundry CLI
CLI Application name Dir containing WAR
vmc push cf1 --path target
--url cer-cf1.cloudfoundry.com
Application URL
34. Deploy the application
$ vmc push cf1 --path target
--url cer-cf1.cloudfoundry.com
Detected a Java Web Application, is this correct?
[Yn]:
Memory Reservation [Default:512M] (64M, 128M, 256M,
512M, 1G or 2G)
Creating Application: OK
Would you like to bind any services to 'cf1'? [yN]:
Uploading Application:
Checking for available resources: OK
Packing application: OK
Uploading (2K): OK
Push Status: OK
Starting Application: OK
37. Update the running application
$ vmc update cf1 --path target
Uploading Application:
Checking for available resources:
OK
Packing application: OK
Uploading (2K): OK
Push Status: OK
Successfully updated Application:
'cf1'
Stopping Application: OK
Starting Application: OK
38. Access it again
$ curl cer-cf1.cloudfoundry.com
<html>
<body>
<h2>This is Cloud Foundry</h2>
</body>
</html>
44. Deploying it
$ vmc push cer-spring --path web/target/
Application Deployed URL [cer-spring.cloudfoundry.com]:
Detected a Java SpringSource Spring Application, is this correct? [Yn]:
Memory Reservation (64M, 128M, 256M, 512M, 1G) [512M]:
Creating Application: OK
Would you like to bind any services to 'cer-spring'? [yN]: y
Would you like to bind any services to 'cer-spring'? [yN]: y
Would you like to use anan existing provisioned service? [yN]: y
Would you like to use existing provisioned service? [yN]: y
The The following provisioned servicesavailable
following provisioned services are are available
1: mysql1
1: mysql1
2: mysql-135e0
2: mysql-135e0
Please select one you wish to use:use: 1
Please select one
you wish to 1
Binding Service [mysql1]: OK OK
Binding Service
[mysql1]:
Uploading Application:
Checking for available resources: OK
Processing resources: OK
Packing application: OK
Uploading (12K): OK
45. DataSource bean is reconfigured automatically
<beans>
<bean id="dataSource" class="…">
… DataSource for MySQL instance …
</bean>
</beans>
class SomeClass {
@Autowired
private DataSource dataSource;
}
47. Using Caldecott…
$ vmc tunnel
1: mysql-135e0
2: mysql1
Which service to tunnel to?: 2
Password: ********
Stopping Application: OK
Redeploying tunnel application 'caldecott'.
Uploading Application:
Checking for available resources: OK
Packing application: OK
Uploading (1K): OK
Push Status: OK
Binding Service [mysql1]: OK
Staging Application: OK
Starting Application: OK
Getting tunnel connection info: OK
Service connection info:
username : uMe6Apgw00AhS
password : pKcD76PcZR7GZ
name : d7cb8afb52f084f3d9bdc269e7d99ab50
Starting tunnel to mysql1 on port 10000.
1: none
2: mysql
Which client would you like to start?: 2
48. …Using Caldecott
Launching 'mysql --protocol=TCP --host=localhost --port=10000 --
user=uMe6Apgw00AhS --password=pKcD76PcZR7GZ
d7cb8afb52f084f3d9bdc269e7d99ab50'
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 10944342
Server version: 5.1.54-rel12.5 Percona Server with XtraDB (GPL), Release
12.5, Revision 188
Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights
reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or 'h' for help. Type 'c' to clear the current input
statement.
mysql>
49. Testing scenario
Create MySQL service instance
Use Caldecott to tunnel database
Populate DB using mysql+sql scripts
Deploy application bound to database
Test the application
…
Delete application and database
50. Agenda
Why PaaS?
Cloud Foundry basics
Using Cloud Foundry services
Spring and Cloud Foundry
Using NoSQL databases
Cloud Foundry and asynchronous architectures
51. Spring programming model
Benefits:
•Improved developer
productivity
•Higher quality code
•Portability across
Dependency Injection application servers
POJO
Portable Service Aspect-Oriented
Abstractions Programming
(Transactions, data
access, …)
51
52. Spring is ideal for the cloud environment
Yet another environment for your application to run in
• Different Java runtime
• Different databases – sometimes not relational!
Dynamic environment:
• Servers come and go
• IP addresses change
Must integrate with in-house applications:
• RPC
• Asynchronous messaging
To leverage elasticity your application must be architected to scale up and down
• Asynchronous, loose coupling
= Problems already solved by the Spring framework
62. <cloud:properties>
Exposes basic information about services that can be
consumed with Spring’s property placeholder support
Properties automatically available when deploying Spring
3.1 applications
<cloud:properties id="cloudProperties" />
<context:property-placeholder
properties-ref="cloudProperties"/>
class SomeClass {
@Value("${cloud.services.mysql1.connection.hostname}")
private String mysqlHost;
62
64. Isolating Cloud Foundry configuration
Use Spring 3.1 profiles
Beans belong to one or more profiles
At runtime, one or active profiles active beans
By default, default profile is active
Activate profiles using
• system property: -Dspring.active.profiles=profile1,profile2
• programmatically
65. Using <beans profile=""/>
<bean class="org.sf.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
</bean>
Outside of Cloud Foundry
<beans profile="default">
<bean class="org.a.commons.dbcp.BasicDataSource" id="dataSource">
<property name="url" value="jdbc:mysql://localhost/my_db" />
</bean>
</beans>
Inside Cloud Foundry
<beans profile="cloud">
<cloud:data-source id="dataSource" />
</beans>
Profiles available in Spring 3.1
65
66. Using @Profile
@Configuration
@Profile("default")
public class LocalDataSourceConfiguration {
@Configuration
@Bean
@Profile("cloud")
public DataSource dataSource() {
public class CloudDataSourceConfiguration {
}
@Bean
public DataSource dataSource() {
}
! ! …
}
}
66
67. Application configuration via environment variables
$ vmc env-add cf1
PAYMENT_SVC=http://...
Adding Environment Variable
[PAYMENT_SVC=http://...]: OK
Stopping Application: OK
Staging Application: OK
String value = System.getenv("PAYMENT_SVC")
@Value("#{systemEnvironment['PAYMENT_SVC']}")
private String envVariable;
68. Agenda
Why PaaS?
Cloud Foundry basics
Using Cloud Foundry services
Spring and Cloud Foundry
Using NoSQL databases
• Why NoSQL?
• Using MongoDB
• Cloud Foundry and MongoDB
Cloud Foundry and asynchronous architectures
69. Cloud Foundry provides NoSQL-aaS
But what’s a NoSQL database?
Why would you want to use it?
How do you use it?
69
71. Future = multi-paradigm data storage for enterprise applications
e.g. Netflix
• RDBMS
• SimpleDB
• Cassandra
• Hadoop/Hbase
IEEE Software Sept/October 2010 - Debasish Ghosh / Twitter @debasishg
71
72. Spring Data is here to help
For
NoSQL databases
http://www.springsource.org/spring-data
72
73. What you get
Template classes that hide the boilerplate code, similar to JdbcTemplate
Auto-generated (generic) repositories
Java NoSQL mapping
Cross Store Persistence
Support in Roo and Grails
73
74. Agenda
Why PaaS?
Cloud Foundry basics
Using Cloud Foundry services
Spring and Cloud Foundry
Using NoSQL databases
• Why NoSQL?
• Using MongoDB
• Cloud Foundry and MongoDB
Cloud Foundry and asynchronous architectures
75. MongoDB
Document-oriented database
• JSON-style documents: Lists, Maps, primitives
• Schema-less
Transaction = update of a single document
Rich query language for dynamic queries
Very fast
Writes are asynchronous!
Highly scalable and available
75
76. Data model = Binary JSON documents
Server
Database: Food To Go
Collection: Restaurants
{
"name" : "Sahn Maru",
"type" : ”Korean",
"serviceArea" : [
"94619",
"94618"
],
"openingHours" : [
{
"dayOfWeek" : "Wednesday",
Sequence of
"open" : 1730, bytes on disk
"close" : 2230
} fast i/o
],
"_id" : ObjectId("4bddc2f49d1505567c6220a0")
}
76
77. MongoDB use cases
Use cases
• High volume writes
• Complex data
• Semi-structured data
Who is using it?
• Shutterfly, Foursquare
• Bit.ly Intuit
• SourceForge, NY Times
• GILT Groupe, Evite,
• SugarCRM
77
83. Agenda
Why PaaS?
Cloud Foundry basics
Using Cloud Foundry services
Spring and Cloud Foundry
Using NoSQL databases
• Why NoSQL?
• Using MongoDB
• Cloud Foundry and MongoDB
Cloud Foundry and asynchronous architectures
84. Using Mongo and Redis with Cloud Foundry
Create a service - Mongo or Redis
Bind it to your application
Use <cloud:*/> namespace to access the bound service
• when cloud profile is active
84
87. NoSQL and Caldecott
Caldecott let’s you tunnel to a NoSQL service
Use Redis CLI
• redis-cli
• Explore database, adhoc operations
• ...
Use Mongo CLI etc
• Explore database, adhoc operations
• Mongo dump/restore
• ...
87
88. Agenda
Why PaaS?
Cloud Foundry basics
Using Cloud Foundry services
Spring and Cloud Foundry
Using NoSQL databases
Cloud Foundry and asynchronous architectures
• Why use messages?
• RabbitMQ and Spring AMQP
• Using Spring Integration
• Using RabbitMQ on Cloud Foundry
89. Cloud Foundry provides
RabbitMQ - aaS
But what’s RabbitMQ?
Why would you want to use it?
How do you use it?
89
90. But why messaging? Why RabbitMQ?
Application A Application B
RabbitMQ
Traditional application integration
90
91. Messaging within applications wgrus-billing.war
Accounting
Service
wgrus-inventory.war
wgrus-store.war Widget
Message InventoryService
StoreFront Broker
MySQL
wgrus-inventory.war
Gadget
InventoryService
Decouples front-end from
back-end
Front-end continues to work wgrus-shipping.war
when back-end is down Shipping
Broker smoothes out traffic Service
spikes 91
92. Agenda
Why PaaS?
Cloud Foundry basics
Using Cloud Foundry services
Spring and Cloud Foundry
Using NoSQL databases
Cloud Foundry and asynchronous architectures
• Why use messages?
• RabbitMQ and Spring AMQP
• Using Spring Integration
• Using RabbitMQ on Cloud Foundry
93. RabbitMQ – Messaging that Just Works
Robust
High-performance
Easy to use
AMQP LEADER
94. Why AMQP?
A
Protocol,
not
an
API
•A defined set of
messaging capabilities
called the AMQ model
•A network wire-level
protocol, AMQP
On
commodity
hardware
•10-‐25
thousand
messages
per
second
is
rou7ne
*
•The
NIC
is
usually
the
boIleneck
*
Non-‐persistent
messages
17
95. Spring AMQP
Encapsulates low-level details
Producer Consumer
Simplifies sending and receiving of
messages
Amqp Listener
Template Container
Spring AMQP
AMQP
96. Sending AMQP messages
@Component public class MessageSender {
@Autowired
private volatile AmqpTemplate amqpTemplate;
public void send(String message) {
this.amqpTemplate.convertAndSend(
"myExchange", "some.routing.key", message);
}
public void read() throws Exception {
...
String value =
amqpTemplate.receiveAndConvert("myQueueName");
...
} 96
97. Spring AMQP is flexible and dynamic
BUT
It’s very low level
97
98. Agenda
Why PaaS?
Cloud Foundry basics
Using Cloud Foundry services
Spring and Cloud Foundry
Using NoSQL databases
Cloud Foundry and asynchronous architectures
• Why use messages?
• RabbitMQ and Spring AMQP
• Using Spring Integration
• Using RabbitMQ on Cloud Foundry
99. Spring Integration
Builds on Spring framework
High-level of abstraction for building message
based applications
Implements EAI patterns
Provides plumbing for exchanging messages
between application components
Promotes loosely coupled components
Integrates with external messaging infrastructure:
JMS, AMQP, HTTP, Email, File transfer
99
100. Spring Integration concepts
Message channel
• Virtual pipe connecting producer and consumer
Message endpoints
• The filter of a pipes-and-filter architecture
• Read from and/or write to channel
Endpoint types:
• Transformer
• Filter
• Router
• Splitter
• Aggregator
• ServiceActivator
• Inbound channel adapter - read from external source, writes to channel
• Outbound channel adapter - read from channel write to external destination
100
102. wgrus-store.war wgrus-shipping.war
StoreFront Shipping
Service
Spring Integration RabbitMQ Spring Integration
wgrus-billing.war wgrus-inventory.war
Credit Service Spring Integration
Widget
InventoryService
Gadget
InventoryService
102
104. Inventory flow
Json to credit check inventory inventory Object to
AMQP inventory AMQP
Object enricher encricher AMQP Out JSON
inventory
router
credit check
credit check Content Based
service Router
activator
Credit
Service
widget gadget
inventory inventory
service service
104
105. Shipping flow
shipping
service
Json to
AMQP shipping Service
Object
order Activator
channel
105
106. Agenda
Why PaaS?
Cloud Foundry basics
Using Cloud Foundry services
Spring and Cloud Foundry
Using NoSQL databases
Cloud Foundry and asynchronous architectures
• Why use messages?
• RabbitMQ and Spring AMQP
• Using Spring Integration
• Using RabbitMQ on Cloud Foundry