Spring Into the Cloud

Preview:

DESCRIPTION

Spring is the most popular and productive enterprise Java development framework in the world, and has always provided developers with portability and choice. The cloud should be no different. Spring applications work flawlessly on all the major platform-as-a-service clouds including Heroku, Google App Engine, and Cloud Foundry. This session will focus on how to design, and create, modern enterprise applications using Spring 3 that are portable across cloud environments.

Citation preview

Spring into the Cloud

Jennifer Hickey@jencompgeekjennifer.hickey@springsource.com

About this session

Spring and the Cloud: a match

made in heaven

About Jennifer

3

•Long time SpringSource-er•Contributed to many Spring and SpringSource projects•Cloud Foundry Engineer•Focused on framework support

Agenda

•Why Cloud? Why PaaS?•Spring Framework for the Cloud•Developing NoSQL applications for the Cloud with Spring Data•Cloud application integration with RabbitMQ and Spring Integration•Wrap up

4

Tomcat

Traditional web application architecture

5

Desktop Browser Apache WAR MySQL

Database

Simple to developSimple to testSimple to deploySimple to scale: just add Apache + more Tomcats

•Browser client•Monolithic WAR•RDBMS

6

Smart phones overtake PCs in Q4 2010

New kinds of clients

Users expect a rich, dynamic and interactive experience

7

Java Web Application

Desktop Browser

HTTP Request

HTML/Javascript

Old style UI architecture isn’t good enough

on mobile devices and desktop

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

8

Web Services

Mobile and Desktop Browser

WS Request

XML/JSON response

HTML5Javascript

Application

Web Socket/Eventing

Popular social networks

•Applications need to integrate with them•Application integration problem

•Scaling graphs is challenging

•Application go viral through social networks•Very rapid growth

•Capacity planning nightmare

9

Need scalable architectures to handle massive loads

10

•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

Data Explosion: Data Volumes increasing at 60% per year

1120Horizontally scalable, distributed NoSQL Databases

Eventual consistency rather than ACID

Scaling development

12

•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

WAR

User Management

Search

Ordering

Front End

!= Scalable development

Scaling development

13

•Need “SOA” approach•Partition application into set of services

•Partition by noun or by verb

•Each team is responsible for a service and manages their own release schedule. •New code updates frequently

•Mature services upgrade infrequently

User Management

Search

User Management

Front-end

Ordering

SearchFront-End

OrderingFront-End

Modern application architecture

14

TomcatDesktop Browser

User Management

NodeJS front-end application

Native Mobile Application

HTML5 mobile application

RabbitMQ

NodeJS front-end application

Tomcat

Ordering

MySQL

Mongo

Redis

Tomcat

Search

SaaS

PaaS

IaaS

Three layers of cloud computing

15

Need to be here

16

What you need is PaaS =

Easy deployment

Application management

Easy scaling up and down

Services:DatabaseBlob storage Messaging...

+

Cloud Foundry: Services, Frameworks and Clouds

17

Agenda

•Why Cloud? Why PaaS?•Spring Framework for the Cloud•Developing NoSQL applications for the Cloud with Spring Data•Cloud application integration with RabbitMQ and Spring Integration•Wrap up

18

Spring’s aim:

bring simplicity to java development

19

web tier &

RIAservice tier batch

processingintegration & messaging

data access

/ NoSQL / Big Data

mobile

tc ServerTomcat

Jetty

lightweight

CloudFoundryGoogle App Engine

Amazon Web ServicesHeroku

the cloud:

WebSphereJBoss ASWebLogic

(on legacy versions, too!)

traditional

The Spring framework

20

At its core, the Spring Framework...

•Provides comprehensive infrastructural support for developing enterprise Java™ applications•Spring deals with the plumbing•So you can focus on solving the domain problem

Spring 3.1 Environment Abstraction

•Bean definitions for a specific environment (Profiles)•e.g. development, testing, production

•Possibly different deployment environments

•Activate profiles by name•spring.profiles.active system property

•Other means outside deployment unit

•“default” profile activates if no other profiles specified

•Custom resolution of placeholders•Dependent on the actual environment

•Ordered property sources

21

Isolating Cloud Configuration

22

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

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

Java Configuration with Profiles

23

@Configuration @Profile(“local”)public class LocalDataSourceConfiguration {

@Bean public javax.sql.DataSource dataSource() { ... }

}

@Configuration @Profile(“cloud”)public class CloudDataSourceConfiguration {

@Bean public javax.sql.DataSource dataSource() { ... }

}

Agenda

•Why Cloud? Why PaaS?•Spring Framework for the Cloud•Developing NoSQL applications for the Cloud with Spring Data•Cloud application integration with RabbitMQ and Spring Integration•Wrap up

24

25

Data Access Challenge #1: Scale Horizontally

26

Data Access Challenge #2: Heterogeneous data access needs

27

NoSQL offers several data store categories

ColumnKey-Value Document Graph

Redis, Riak

Cassandra,HBase

MongoDB Neo4J

28

NoSQL offers several data store categories

ColumnKey-Value Document Graph

MongoDB

29

Spring Frameworkbuilt-in data access support

•Transaction abstractions•Common data access exception hierarchy•JDBC - JdbcTemplate•ORM - Hibernate, JPA support•OXM - Object to XML mapping•Serializer/Deserializer strategies (Spring 3.0)•Cache support (Spring 3.1)

30

•Spring Data Key-value•Spring Data Document•Spring Data Graph•Spring Data Column•Spring Data Blob•Spring Data JPA Repository / JDBC Extensions•Spring Gemfire / Spring Hadoop ...•Grails iNcOnSeQuentiaL

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

31

Spring Data Building Blocks

•Low level data access APIs✓MongoTemplate, RedisTemplate ...•Object Mapping (Java and GORM)•Cross Store Persistence Programming model•Generic Repository support •Productivity support in Roo and Grails

32

Spring MongoDB

33

Spring Data Document Mongo

•MongoTemplate•MongoConverter interface for mapping Mongo documents•SimpleMongoConverter for basic POJO mapping support•Leverage Spring 3.0 TypeConverters and SpEL•Exception translation•Advanced Mapping•Annotation based (@Document, @Id, @DbRef)•MongoRepository•Built on Hades support for JPA Repositories

34

Simple Domain Class

35

Mongo Template

Direct Usage of the Mongo Template:

Insert into “Person” Collection

findOne using query: { "name" : "Joe"} in db.collection: database.Person

Dropped collection [database.person]

36

Generic Repository

Interface for generic CRUD operations on a repository for a specific type

37

Paging and Sorting Repository

Extends “CrudRepository”

Usage:

Paging and Sorting Repository:

38

Custom Repository

Custom Repository:

Keyword Sample Logical result

GreaterThan findByAgeGreaterThan(int age) {"age" : {"$gt" : age}}

LessThan findByAgeLessThan(int age) {"age" : {"$lt" : age}}

Between findByAgeBetween(int from, int to) {"age" : {"$gt" : from, "$lt" : to}}

NotNull findByFirstnameNotNull() {”firstname" : {"$ne" : null}}

Null findByFirstnameNull() {”firstname" : null}

Like findByFirstnameLike(String name) "firstname" : firstname} (regex)

Keywords :

39

Cross Store

40

JPA and MongoDB

JPA “Customer” with a “SurveyInfo” Document

41

Using a Cross-Store

Saving a Customer with a SurveryInfoCreate Customer

Create SurveyInfo

Assign Survey to CustomerSave

Mongo Document:

42

DemoCross-Store Persistence with Mongo

Agenda

•Why Cloud? Why PaaS?•Spring Framework for the Cloud•Developing NoSQL applications for the Cloud with Spring Data•Cloud application integration with RabbitMQ and Spring Integration•Wrap up

43

WidgetInventoryService

AccountingService

ShippingService

@ControllerStoreFront

GadgetInventoryService

RDBMS

wgrus-monolithic.war

44

It’s simple to develop but ....

•Lack of scalability•Scale through replication

•Non-replicable component => nothing can be replicated

•Can’t scale different parts of the application differently

•Lack of deployability•Deploy it all in one go

•Increased risk of something breaking

•Applications are brittle•Store can’t accept orders unless all services are available

•Failure (e.g. memory leak) in one component can take down every other

•Monolingual•Can’t use non-JVM server-side technologies: NodeJS, Rails

45

46

Solution: Asynchronous Architecture

ShippingService

StoreFront

wgrus-store.war

AccountingService

wgrus-billing.war

wgrus-shipping.war

WidgetInventoryService

wgrus-inventory.war

GadgetInventoryService

wgrus-inventory.war

MySQL

47

Message Broker

RabbitMQ – Messaging that Just Works

RobustHigh-performance

Easy to useAMQP LEADER

Spring AMQP

•Encapsulates low-level details

•Simplifies sending and receiving of messages

Producer

Spring AMQP

AMQP

AmqpTemplate

Consumer

ListenerContainer

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

50

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

51

Order Service

Messaging Gateway

Channel Service Activator

Shipping service

52

Example of reconfigurability - local

@Servicepublic class OrderServiceImpl {

@Autowiredprivate ShippingService shippingService;

public void placeOrder() {String orderId = generateOrderId();…shippingService.shipOrder(orderId);}

}

@Servicepublic class ShippingServiceImpl {

public void shipOrder(String orderId) { System.out.println("shipped order: " + orderId);}

}

Order Service

Messaging Gateway

Channel Service Activator

Shipping service

AMQP

RabbitMQ

AMQP Channel

53

Example of reconfigurability - distributed

Code unchanged in new deployment

54

Using Spring Integration with the web store application

ShippingService

StoreFront

wgrus-store.war

Credit Service

wgrus-billing.war

wgrus-shipping.war

WidgetInventoryService

wgrus-inventory.war

GadgetInventoryService

55

Spring Integration Logic

orderChannel object to JSON amqpOut

StoreUI

Message Endpoint AMQP

Store front flow

56

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

57

AMQP shipping order

channel

Json to Object

Service Activator

shipping service

Shipping flow

58

59

DemoWGRUS in the Cloud

Agenda

•Why Cloud? Why PaaS?•Spring Framework for the Cloud•Developing NoSQL applications for the Cloud with Spring Data•Cloud application integration with RabbitMQ and Spring Integration•Wrap up

60

Summary

•Cloud? Good.•Spring? Good.•Spring and the Cloud is a match made in heaven

61

Where to Find More

•Spring Data Project: http://bit.ly/spring-data

•Spring Integration Project:http://www.springsource.org/spring-integration

•Mongo Cross Store Sample: https://github.com/jencompgeek/cloudfoundry-samples/tree/master/cross-store

•WGRUS Sample:https://github.com/markfisher/springone-wgrus

•CloudFoundry Samples: http://bit.ly/cloudfoundry-samples

• Sign up for (free) Cloud Foundryhttp://www.cloudfoundry.com

•MicroCloud Foundry for Spring Developershttp://bit.ly/mcf4spring

•Cloud Foundry and Spring YouTube Channels

62

Recommended