spring into the cloud - jdc2012 cairo, egypt

111
Spring into the Cloud Chris Richardson Author of POJOs in Action Founder of the original CloudFoundry.com @crichardson [email protected] 1

Upload: chris-richardson

Post on 17-May-2015

1.359 views

Category:

Technology


3 download

DESCRIPTION

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.

TRANSCRIPT

Page 1: Spring into the Cloud - JDC2012 Cairo, Egypt

Spring into the CloudChris Richardson

Author of POJOs in ActionFounder of the original CloudFoundry.com

@[email protected]

1

Page 2: Spring into the Cloud - JDC2012 Cairo, Egypt

Presentation goal

2

Cloud Foundry simplifies the development and

deployment of modern applications

Spring is the ideal cloud computing platform

Page 3: Spring into the Cloud - JDC2012 Cairo, Egypt

3

About Chris

Page 4: Spring into the Cloud - JDC2012 Cairo, Egypt

About Chris

4

Page 5: Spring into the Cloud - JDC2012 Cairo, Egypt

(About Chris)

5

Page 6: Spring into the Cloud - JDC2012 Cairo, Egypt

About Chris()

6

Page 7: Spring into the Cloud - JDC2012 Cairo, Egypt

About Chris

7

Page 8: Spring into the Cloud - JDC2012 Cairo, Egypt

About Chris

http://www.theregister.co.uk/2009/08/19/springsource_cloud_foundry/

8

Page 9: Spring into the Cloud - JDC2012 Cairo, Egypt

About Chris

9

Developer Advocate for CloudFoundry.com

Signup at http://cloudfoundry.com

Promo code: EgyptJUG

Page 10: Spring into the Cloud - JDC2012 Cairo, Egypt

Agenda

Why PaaS? Cloud Foundry basics

Using Cloud Foundry services

Spring and Cloud Foundry

Using NoSQL databases

Cloud Foundry and asynchronous architectures

Page 11: Spring into the Cloud - JDC2012 Cairo, Egypt

Tomcat

Traditional web application architecture

11

Desktop Browser

Apache

WAR

MySQL Database

developtestdeployscale Or is it?

ShippingService

AccountingService

InventoryService

StoreFront

Simple to

Page 12: Spring into the Cloud - JDC2012 Cairo, Egypt

?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?

Page 13: Spring into the Cloud - JDC2012 Cairo, Egypt

And things are changing:

This simple, monolithic architecture is inadequate

13

Page 14: Spring into the Cloud - JDC2012 Cairo, Egypt

14

Smart phones overtake PCs in Q4 2010

New kinds of clients

Page 15: Spring into the Cloud - JDC2012 Cairo, Egypt

Users expect a rich, dynamic and interactive experience on mobile devices and desktop

15

Java Web Application

Desktop Browser

HTTP Request

HTML/Javascript

Old style UI architecture isn’t good enough

Page 16: Spring into the Cloud - JDC2012 Cairo, Egypt

Need to handle massive loads and the data explosion

16

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

Page 17: Spring into the Cloud - JDC2012 Cairo, Egypt

Scaling development

17

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

!=Scalable development

WAR

ShippingService

AccountingService

InventoryService

StoreFront

Page 18: Spring into the Cloud - JDC2012 Cairo, Egypt

Desktop Browser

NodeJS front-end application

Native Mobile Application

HTML5 mobile application

RabbitMQ

NodeJS

MySQL

Mongo

Redis

ShippingService

AccountingService

billing.war

shipping.war

InventoryService

inventory.war

StoreUI

StoreUI

StoreUI

StoreUI

18

Modern application architecture

Page 19: Spring into the Cloud - JDC2012 Cairo, Egypt

Developing and testing these applications is even more challenging

19

Page 20: Spring into the Cloud - JDC2012 Cairo, Egypt

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, ....??

Page 21: Spring into the Cloud - JDC2012 Cairo, Egypt

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, ....??

Page 22: Spring into the Cloud - JDC2012 Cairo, Egypt

22

Platform-as-a-Service is the solution

Easy deployment

Application management

Easy scaling up and down

Services:

Database

Blob storage

Messaging

...

+

Page 23: Spring into the Cloud - JDC2012 Cairo, Egypt

Agenda

Why PaaS?

Cloud Foundry basics

Using Cloud Foundry services

Spring and Cloud Foundry

Using NoSQL databases

Cloud Foundry and asynchronous architectures

Page 24: Spring into the Cloud - JDC2012 Cairo, Egypt

24

About Cloud Foundry

Cloud  Provider  Interface

Private  Clouds  

PublicClouds

MicroClouds

Applica7on  Service  Interface

Data Services

Other Services

Msg Services

.COM

.js

Page 25: Spring into the Cloud - JDC2012 Cairo, Egypt

Cloud Foundry.COM

Runtimes & Frameworks

Services

vCenter / vSphere

Cloud Foundry

Infrastructure

Page 26: Spring into the Cloud - JDC2012 Cairo, Egypt

What is a Micro Cloud?

26

Entire Cloud Running inside of a single VM

Or

Signup @ http://cloudfoundry.com/micro

Page 27: Spring into the Cloud - JDC2012 Cairo, Egypt

27

Cloud Foundry.ORG

The Cloud Foundry Source Code

Cloud Foundry

Download

Code

Setup Environment

Deploy Behind Firewall

Setup Scripts

Page 28: Spring into the Cloud - JDC2012 Cairo, Egypt

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

Page 29: Spring into the Cloud - JDC2012 Cairo, Egypt

Cloud Foundry - Architecture

DEAs

Web

App

Services

DB

HealthManager

Routers

CloudControllers

Load Balancers

Message Bus(NATS)

DEAs

Web

App

DEAs

Web

App

100’

s of

DEA

s

CloudControllersCloud

Controllers

Services

DB

Services

DB

Load BalancersLoad Balancers

Routers

HealthManagerHealth

Manager

Routers

Message Bus(NATS)

Message Bus(NATS)

Web Service API

Page 30: Spring into the Cloud - JDC2012 Cairo, Egypt

Let's deploy an application

Page 31: Spring into the Cloud - JDC2012 Cairo, Egypt

Create an application

$ mvn archetype:generate…18: internal -> maven-archetype-webapp

(A simple Java web application)…Choose a number:

(1/2/3/4/5/6/7/8/9/10/11/12/13/14/15/16/17/18/19/20/21/22/23/24/25/26/27/28/29/30/31/32/33/34/35/36/37/38/39/40/41) 15: : 18

…$ mvn package…

Page 32: Spring into the Cloud - JDC2012 Cairo, Egypt

Options for using Cloud Foundry

32

Page 33: Spring into the Cloud - JDC2012 Cairo, Egypt

Using the Cloud Foundry CLI

vmc push cf1 --path target \ --url cer-cf1.cloudfoundry.com

CLI Application name Dir containing WAR

Application URL

Page 34: Spring into the Cloud - JDC2012 Cairo, Egypt

Deploy the application

Memory Reservation [Default:512M] (64M, 128M, 256M, 512M, 1G or 2G)

Creating Application: OKWould you like to bind any services to 'cf1'? [yN]:

Uploading Application: Checking for available resources: OK Packing application: OK Uploading (2K): OK Push Status: OKStarting Application: OK

$ vmc push cf1 --path target \ --url cer-cf1.cloudfoundry.comDetected a Java Web Application, is this correct?

[Yn]:

Page 35: Spring into the Cloud - JDC2012 Cairo, Egypt

Access the application

$ curl cer-cf1.cloudfoundry.com<html><body><h2>Hello World!</h2></body></html>

Page 36: Spring into the Cloud - JDC2012 Cairo, Egypt

Change the application

$ vi src/main/webapp/index.jsp $ mvn package

Page 37: Spring into the Cloud - JDC2012 Cairo, Egypt

Update the running application

$ vmc update cf1 --path target Uploading Application: Checking for available resources:

OK Packing application: OK Uploading (2K): OK Push Status: OKSuccessfully updated Application:

'cf1'Stopping Application: OKStarting Application: OK

Page 38: Spring into the Cloud - JDC2012 Cairo, Egypt

Access it again

$ curl cer-cf1.cloudfoundry.com<html><body><h2>This is Cloud Foundry</h2></body></html>

Page 39: Spring into the Cloud - JDC2012 Cairo, Egypt

Scale the application

$ vmc instances cf1 +2Scaling Application instances up to 3: OK

$ vmc stats cf1

+----------+-------------+----------------+--------------+--------------+| Instance | CPU (Cores) | Memory (limit) | Disk (limit) | Uptime |

+----------+-------------+----------------+--------------+--------------+| 0 | 1.2% (4) | 90.6M (512M) | 8.5M (2G) | 0d:0h:3m:3s || 1 | 9.5% (4) | 60.9M (512M) | 8.4M (2G) | 0d:0h:0m:17s |

| 2 | 9.4% (4) | 55.5M (512M) | 8.4M (2G) | 0d:0h:0m:17s |+----------+-------------+----------------+--------------+--------------+

Page 40: Spring into the Cloud - JDC2012 Cairo, Egypt

Agenda

Why PaaS?

Cloud Foundry basics

Using Cloud Foundry services Spring and Cloud Foundry

Using NoSQL databases

Cloud Foundry and asynchronous architectures

Page 41: Spring into the Cloud - JDC2012 Cairo, Egypt

Available services

41

Page 42: Spring into the Cloud - JDC2012 Cairo, Egypt

Creating a service instance

$ vmc create-service mysql --name mysql1Creating Service: OK

$ vmc services

============== System Services ==============+------------+---------+---------------------------------------+

| Service | Version | Description |+------------+---------+---------------------------------------+| mongodb | 1.8 | MongoDB NoSQL store |

| mysql | 5.1 | MySQL database service || postgresql | 9.0 | PostgreSQL database service (vFabric) |

| rabbitmq | 2.4 | RabbitMQ messaging service || redis | 2.2 | Redis key-value store service |+------------+---------+---------------------------------------+

=========== Provisioned Services ============

+-------------+---------+| Name | Service |

+-------------+---------+| mysql1 | mysql |

+-------------+---------+

Page 43: Spring into the Cloud - JDC2012 Cairo, Egypt

Typical Spring application is cloud ready

<beans>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">

<property name="driverClassName" ! ! ! value="com.mysql.jdbc.Driver" /> <property name="url" ! value="jdbc:mysql://localhost:3306/cf" /> <property name="username" value="cfuser" /> <property name="password" value="cfpassword" /> … </bean> class SomeClass {

@Autowired private DataSource dataSource;}

Page 44: Spring into the Cloud - JDC2012 Cairo, Egypt

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: OKWould you like to bind any services to 'cer-spring'? [yN]: y

Would you like to use an existing provisioned service? [yN]: yThe following provisioned services are available

1: mysql1

2: mysql-135e0Please select one you wish to use: 1

Binding Service [mysql1]: OKUploading Application:

Checking for available resources: OK

Processing resources: OK Packing application: OK

Uploading (12K): OK

Would you like to bind any services to 'cer-spring'? [yN]: yWould you like to use an existing provisioned service? [yN]: yThe following provisioned services are available1: mysql12: mysql-135e0Please select one you wish to use: 1Binding Service [mysql1]: OK

Page 45: Spring into the Cloud - JDC2012 Cairo, Egypt

DataSource bean is reconfigured automatically

<beans>

<bean id="dataSource" class="…"> … DataSource for MySQL instance … </bean>

</beans>

class SomeClass { @Autowired private DataSource dataSource;}

Page 46: Spring into the Cloud - JDC2012 Cairo, Egypt

Using database tools

Page 47: Spring into the Cloud - JDC2012 Cairo, Egypt

Using Caldecott…

$ vmc tunnel1: mysql-135e02: mysql1

Which service to tunnel to?: 2Password: ********

Stopping Application: OKRedeploying tunnel application 'caldecott'.Uploading Application:

Checking for available resources: OK Packing application: OK

Uploading (1K): OK Push Status: OKBinding 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: none2: mysql

Which client would you like to start?: 2

Page 48: Spring into the Cloud - JDC2012 Cairo, Egypt

…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 respectiveowners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

Page 49: Spring into the Cloud - JDC2012 Cairo, Egypt

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

Page 50: Spring into the Cloud - JDC2012 Cairo, Egypt

Agenda

Why PaaS?

Cloud Foundry basics

Using Cloud Foundry services

Spring and Cloud Foundry Using NoSQL databases

Cloud Foundry and asynchronous architectures

Page 51: Spring into the Cloud - JDC2012 Cairo, Egypt

51

Spring programming model

POJO

Dependency Injection

Aspect-Oriented Programming

Portable ServiceAbstractions(Transactions, data access, …)

Benefits:•Improved developer productivity•Higher quality code•Portability across application servers

Page 52: Spring into the Cloud - JDC2012 Cairo, Egypt

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

Page 53: Spring into the Cloud - JDC2012 Cairo, Egypt

Spring 3.1 features for cloud applications

53

Page 54: Spring into the Cloud - JDC2012 Cairo, Egypt

Cloud Foundry Runtime

Spring configuration options

Auto-reconfiguration

<cloud:*/> namespace

CloudEnvironment API

VCAP_APPLICATION, VCAP_SERVICES env vars

Page 55: Spring into the Cloud - JDC2012 Cairo, Egypt

VCAP_APPLICATION

{ "instance_id": "e53853fe21d5d740aadf7ddcc0670b4e", "instance_index": 0, "name": "cer-spring", "uris": ["cer-spring.cloudfoundry.com"], "users": ["[email protected]"], "version": "56892923b974db90a8f6c7120b665fb17cff7edb-1", "start": "2011-12-02 23:06:01 +0000", "runtime": "java", "state_timestamp": 1322867161, "port": 40198, "limits": { "fds": 256, "mem": 536870912, "disk": 2147483648 }, "host": "172.30.50.9"

Page 56: Spring into the Cloud - JDC2012 Cairo, Egypt

VCAP_SERVICES

{ "mysql-5.1": [{ "name": "mysql1", "label": "mysql-5.1", "plan": "free", "tags": ["mysql", "mysql-5.1", "relational"], "credentials": { "name": "d7cb8afb52f084f3d9bdc269e7d99ab50", "hostname": "172.30.48.20", "host": "172.30.48.20", "port": 3306, "user": "usmMRfwzyYG53", "username": "usmMRfwzyYG53", "password": "pxWSBVeu80GDp" } }]

Page 57: Spring into the Cloud - JDC2012 Cairo, Egypt

CloudEnvironment: parses JSON so you don't have to

CloudEnvironment env = new CloudEnvironment();Properties props = env.getCloudProperties();String prop = props.getProperty( "cloud.services.mysql1.connection.hostname" );

ApplicationInstanceInfo instanceInfo = env.getInstanceInfo();

int port = instanceInfo.getPort();List<String> urls = instanceInfo.getUris();…

Page 58: Spring into the Cloud - JDC2012 Cairo, Egypt

Provides access to ServiceInfo…

RdbmsServiceInfo mis = env.getServiceInfo(

! ! "mysql1", ! ! RdbmsServiceInfo.class);String mySqlUrl = mis.getUrl();

Page 59: Spring into the Cloud - JDC2012 Cairo, Egypt

59

… which can create DataSources etc

@Configurationpublic class CloudDataSourceConfiguration {

@Bean public DataSource dataSource() { CloudEnvironment environment = new CloudEnvironment();

RdbmsServiceInfo mysqlSvc = ! ! environment.getServiceInfo("mysql1",

RdbmsServiceInfo.class);

! RdbmsServiceCreator dataSourceCreator = new RdbmsServiceCreator();

DataSource dataSource = dataSourceCreator.createService(mysqlSvc);

return dataSource; }

}

Page 60: Spring into the Cloud - JDC2012 Cairo, Egypt

<cloud:*/> namespace

Spring XML namespace Defines beans from CloudEnvironment• Connection factories, e.g. DataSource

• Properties

Page 61: Spring into the Cloud - JDC2012 Cairo, Egypt

61

<cloud:data-source/> Configures a JDBC DataSource

<cloud:data-source id="dataSource" service-name="mysql1"> <cloud:pool pool-size="1-5"/> <cloud:connection properties="charset=utf-8"/></cloud:data-source>...

@Autowired private DataSource dataSource ;

Page 62: Spring into the Cloud - JDC2012 Cairo, Egypt

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;

Page 63: Spring into the Cloud - JDC2012 Cairo, Egypt

Configuring Cloud Foundry apps is easy

BUT

How to run on the desktop too?

Page 64: Spring into the Cloud - JDC2012 Cairo, Egypt

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

Page 65: Spring into the Cloud - JDC2012 Cairo, Egypt

65

Using <beans profile=""/>

<bean class="org.sf.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource"/></bean>

<beans profile="default"> <bean class="org.a.commons.dbcp.BasicDataSource" id="dataSource"> <property name="url" value="jdbc:mysql://localhost/my_db" /> </bean></beans>

<beans profile="cloud"> <cloud:data-source id="dataSource" /></beans>

Outside of Cloud Foundry

Inside Cloud Foundry

Profiles available in Spring 3.1

Page 66: Spring into the Cloud - JDC2012 Cairo, Egypt

66

Using @Profile

@Configuration@Profile("default")public class LocalDataSourceConfiguration {

@Bean public DataSource dataSource() {

}

}

@Configuration@Profile("cloud")public class CloudDataSourceConfiguration {

@Bean public DataSource dataSource() {! ! … }

}

Page 67: Spring into the Cloud - JDC2012 Cairo, Egypt

Application configuration via environment variables

String value = System.getenv("PAYMENT_SVC")

@Value("#{systemEnvironment['PAYMENT_SVC']}")private String envVariable;

$ vmc env-add cf1 PAYMENT_SVC=http://...Adding Environment Variable

[PAYMENT_SVC=http://...]: OKStopping Application: OKStaging Application: OK

Page 68: Spring into the Cloud - JDC2012 Cairo, Egypt

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

Page 69: Spring into the Cloud - JDC2012 Cairo, Egypt

Cloud Foundry provides NoSQL-aaS

69

But what’s a NoSQL database?

Why would you want to use it?

How do you use it?

Page 70: Spring into the Cloud - JDC2012 Cairo, Egypt

Solution: Use NoSQL

Benefits• Higher performance• Higher scalability• Richer data-model• Schema-less

Drawbacks• Limited transactions• Relaxed consistency• Unconstrained data

70

Page 71: Spring into the Cloud - JDC2012 Cairo, Egypt

Future = multi-paradigm data storage for enterprise applications

IEEE Software Sept/October 2010 - Debasish Ghosh / Twitter @debasishg

e.g. Netflix• RDBMS• SimpleDB• Cassandra• Hadoop/Hbase

71

Page 72: Spring into the Cloud - JDC2012 Cairo, Egypt

Spring Data is here to help

http://www.springsource.org/spring-data

NoSQL databases

For

72

Page 73: Spring into the Cloud - JDC2012 Cairo, Egypt

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

Page 74: Spring into the Cloud - JDC2012 Cairo, Egypt

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

Page 75: Spring into the Cloud - JDC2012 Cairo, Egypt

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

Page 76: Spring into the Cloud - JDC2012 Cairo, Egypt

Server

Database: Food To Go

Collection: Restaurants

Data model = Binary JSON documents

{ "name" : "Sahn Maru", "type" : ”Korean", "serviceArea" : [ "94619", "94618" ], "openingHours" : [ { "dayOfWeek" : "Wednesday", "open" : 1730, "close" : 2230 } ], "_id" : ObjectId("4bddc2f49d1505567c6220a0")}

Sequence of bytes on disk fast i/o

76

Page 77: Spring into the Cloud - JDC2012 Cairo, Egypt

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

Page 78: Spring into the Cloud - JDC2012 Cairo, Egypt

MongoTemplate

databaseNameuserIdPassworddefaultCollectionName

writeConcernwriteResultChecking

save()insert()remove()updateFirst()findOne()find()…

MongoTemplate

Mongo(Java Driver class)

<<interface>>MongoConvertor

write(Object, DBObject)read(Class, DBObject)

uses

POJO DBObjectmapping

Simplifies data accessTranslates exceptions

MongoMappingConverter

78

Page 79: Spring into the Cloud - JDC2012 Cairo, Egypt

Example entity

public class Restaurant { private String id; private String name; private List<MenuItem> menuItems;

public Restaurant() { }

public Restaurant(String name) { this.name = name; … }

public String getName() { return name; }

public void setName(String name) { this.name = name; }

…getters and setters…

public class MenuItem { private String name; private double price;

public MenuItem() { }

public MenuItem(String name, double price) { this.name = name; this.price = price; }

…getters and setters…79

Page 80: Spring into the Cloud - JDC2012 Cairo, Egypt

Example data access code

@Repositorypublic class RestaurantRepository {

@Autowired private MongoTemplate mongoTemplate; public static final String RESTAURANTS_COLLECTION = "restaurants";

public void add(Restaurant restaurant) { mongoTemplate.save(RESTAURANTS_COLLECTION, restaurant); }

public List<Restaurant> findRestaurantsByName(String restaurantName) { return mongoTemplate.find(RESTAURANTS_COLLECTION, new Query(where("name").is(restaurantName)), Restaurant.class); }

80

Page 81: Spring into the Cloud - JDC2012 Cairo, Egypt

Mongo document

81

{ "_id" : ObjectId("4d977f55d3fe3119c904e026"), "menuItems" : [ { "name" : "Tandoori Portobello Mushrooms", "price" : 5.5 }, { "name" : "Duck Curry Kerala", "price" : 15 } ], "name" : "Ajanta" }

Page 82: Spring into the Cloud - JDC2012 Cairo, Egypt

MongoDB bean definitions

82

Page 83: Spring into the Cloud - JDC2012 Cairo, Egypt

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

Page 84: Spring into the Cloud - JDC2012 Cairo, Egypt

Using Mongo and Redis with Cloud Foundry

84

Create a service - Mongo or Redis

Bind it to your application

Use <cloud:*/> namespace to access the bound service

• when cloud profile is active

Page 85: Spring into the Cloud - JDC2012 Cairo, Egypt

Deploying a Mongo application

85

Page 86: Spring into the Cloud - JDC2012 Cairo, Egypt

Using the Mongo Application

86

Page 87: Spring into the Cloud - JDC2012 Cairo, Egypt

NoSQL and Caldecott

87

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

• ...

Page 88: Spring into the Cloud - JDC2012 Cairo, Egypt

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

Page 89: Spring into the Cloud - JDC2012 Cairo, Egypt

Cloud Foundry provides RabbitMQ - aaS

89

But what’s RabbitMQ?

Why would you want to use it?

How do you use it?

Page 90: Spring into the Cloud - JDC2012 Cairo, Egypt

But why messaging? Why RabbitMQ?

90

Application A Application B

RabbitMQ

Traditional application integration

Page 91: Spring into the Cloud - JDC2012 Cairo, Egypt

ShippingService

StoreFront

wgrus-store.war

AccountingService

wgrus-billing.war

wgrus-shipping.war

WidgetInventoryService

wgrus-inventory.war

GadgetInventoryService

wgrus-inventory.war

MySQL

91

Message Broker

Decouples front-end from back-endFront-end continues to work when back-end is downBroker smoothes out traffic spikes

Messaging within applications

Page 92: Spring into the Cloud - JDC2012 Cairo, Egypt

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

Page 93: Spring into the Cloud - JDC2012 Cairo, Egypt

RabbitMQ – Messaging that Just Works

RobustHigh-performance

Easy to useAMQP LEADER

Page 94: Spring into the Cloud - JDC2012 Cairo, Egypt

Why AMQP?

17

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

Page 95: Spring into the Cloud - JDC2012 Cairo, Egypt

Spring AMQP

Encapsulates low-level details

Simplifies sending and receiving of messages

Producer

Spring AMQP

AMQP

AmqpTemplate

Consumer

ListenerContainer

Page 96: Spring into the Cloud - JDC2012 Cairo, Egypt

@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

Sending AMQP messages

Page 97: Spring into the Cloud - JDC2012 Cairo, Egypt

Spring AMQP is flexible and dynamic

BUT

It’s very low level

97

Page 98: Spring into the Cloud - JDC2012 Cairo, Egypt

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

Page 99: Spring into the Cloud - JDC2012 Cairo, Egypt

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

Page 100: Spring into the Cloud - JDC2012 Cairo, Egypt

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

Page 101: Spring into the Cloud - JDC2012 Cairo, Egypt

Using Spring Integration with the web store application

101

Page 102: Spring into the Cloud - JDC2012 Cairo, Egypt

ShippingService

StoreFront

wgrus-store.war

Credit Service

wgrus-billing.war

wgrus-shipping.war

WidgetInventoryService

wgrus-inventory.war

GadgetInventoryService

102

Spring Integration

Spring IntegrationSpring Integration RabbitMQ

Page 103: Spring into the Cloud - JDC2012 Cairo, Egypt

orderChannel object to JSON amqpOut

StoreUI

Message Endpoint AMQP

Store front flow

103

Page 104: Spring into the Cloud - JDC2012 Cairo, Egypt

AMQP inventory Json to Object

credit checkenricher

inventory encricher

inventoryAMQP Out AMQP

CreditService

credit check

credit checkservice

activator

widgetinventoryservice

inventoryrouter

gadgetinventoryservice

Content Based Router

Object to JSON

Inventory flow

104

Page 105: Spring into the Cloud - JDC2012 Cairo, Egypt

AMQP shipping order

channel

Json to Object

Service Activator

shipping service

Shipping flow

105

Page 106: Spring into the Cloud - JDC2012 Cairo, Egypt

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

Page 107: Spring into the Cloud - JDC2012 Cairo, Egypt

Rabbit on Cloud Foundry

107

Page 108: Spring into the Cloud - JDC2012 Cairo, Egypt

Configuring a ConnectionFactory

108

<rabbit:template id="rabbitTemplate" connection-factory="rabbitConnectionFactory"/>

<beans profile="default"> ... <rabbit:connection-factory id="rabbitConnectionFactory"/></beans>

<beans profile="cloud"> ... <cloud:rabbit-connection-factory id="rabbitConnectionFactory"/></beans>

Page 109: Spring into the Cloud - JDC2012 Cairo, Egypt

Using Caldecott with RabbitMQ

Use for JUnit/Integration tests

Run RabbitMQ tools

109

xxxxx

xxxxx

xxxxx

Page 110: Spring into the Cloud - JDC2012 Cairo, Egypt

Summary

PaaS? Good.

Cloud Foundry? Good.

Spring? Good.

Cloud Foundry and Spring? Really good!

Home work:• Learn Spring: http://www.springframework.org• Learn Spring Data http://www.springframework.org/spring-data• sign up for (free) Cloud Foundry at http://www.cloudfoundry.com

• Download the Cloud Foundry Micro Cloud

110

Page 111: Spring into the Cloud - JDC2012 Cairo, Egypt

Questions?

@cloudfoundry @crichardson [email protected]

www.cloudfoundry.com promo code EgyptJUG