Top Banner
Spring into the Cloud Jennifer Hickey @jencompgeek [email protected]
62

Spring Into the Cloud

May 11, 2015

Download

Technology

Jennifer Hickey

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.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Spring Into the Cloud

Spring into the Cloud

Jennifer [email protected]@springsource.com

Page 2: Spring Into the Cloud

About this session

Spring and the Cloud: a match

made in heaven

Page 3: Spring Into the Cloud

About Jennifer

3

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

Page 4: Spring Into 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

4

Page 5: Spring Into the Cloud

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

Page 6: Spring Into the Cloud

6

Smart phones overtake PCs in Q4 2010

New kinds of clients

Page 7: Spring Into the Cloud

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

Page 8: Spring Into the Cloud

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

Page 9: Spring Into the Cloud

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

Page 10: Spring Into the Cloud

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

Page 11: Spring Into the Cloud

Data Explosion: Data Volumes increasing at 60% per year

1120Horizontally scalable, distributed NoSQL Databases

Eventual consistency rather than ACID

Page 12: Spring Into the Cloud

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

Page 13: Spring Into the Cloud

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

Page 14: Spring Into the Cloud

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

Page 15: Spring Into the Cloud

SaaS

PaaS

IaaS

Three layers of cloud computing

15

Need to be here

Page 16: Spring Into the Cloud

16

What you need is PaaS =

Easy deployment

Application management

Easy scaling up and down

Services:DatabaseBlob storage Messaging...

+

Page 17: Spring Into the Cloud

Cloud Foundry: Services, Frameworks and Clouds

17

Page 18: Spring Into 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

18

Page 19: Spring Into the Cloud

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

Page 20: Spring Into the Cloud

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

Page 21: Spring Into the Cloud

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

Page 22: Spring Into the Cloud

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>

Page 23: Spring Into the Cloud

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() { ... }

}

Page 24: Spring Into 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

24

Page 25: Spring Into the Cloud

25

Data Access Challenge #1: Scale Horizontally

Page 26: Spring Into the Cloud

26

Data Access Challenge #2: Heterogeneous data access needs

Page 27: Spring Into the Cloud

27

NoSQL offers several data store categories

ColumnKey-Value Document Graph

Redis, Riak

Cassandra,HBase

MongoDB Neo4J

Page 28: Spring Into the Cloud

28

NoSQL offers several data store categories

ColumnKey-Value Document Graph

MongoDB

Page 29: Spring Into the Cloud

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)

Page 30: Spring Into the Cloud

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

Page 31: Spring Into the Cloud

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

Page 32: Spring Into the Cloud

32

Spring MongoDB

Page 33: Spring Into the Cloud

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

Page 34: Spring Into the Cloud

34

Simple Domain Class

Page 35: Spring Into the Cloud

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]

Page 36: Spring Into the Cloud

36

Generic Repository

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

Page 37: Spring Into the Cloud

37

Paging and Sorting Repository

Extends “CrudRepository”

Usage:

Paging and Sorting Repository:

Page 38: Spring Into the Cloud

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 :

Page 39: Spring Into the Cloud

39

Cross Store

Page 40: Spring Into the Cloud

40

JPA and MongoDB

JPA “Customer” with a “SurveyInfo” Document

Page 41: Spring Into the Cloud

41

Using a Cross-Store

Saving a Customer with a SurveryInfoCreate Customer

Create SurveyInfo

Assign Survey to CustomerSave

Mongo Document:

Page 42: Spring Into the Cloud

42

DemoCross-Store Persistence with Mongo

Page 43: Spring Into 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

43

Page 44: Spring Into the Cloud

WidgetInventoryService

AccountingService

ShippingService

@ControllerStoreFront

GadgetInventoryService

RDBMS

wgrus-monolithic.war

44

Page 45: Spring Into the Cloud

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

Page 46: Spring Into the Cloud

46

Solution: Asynchronous Architecture

Page 47: Spring Into the Cloud

ShippingService

StoreFront

wgrus-store.war

AccountingService

wgrus-billing.war

wgrus-shipping.war

WidgetInventoryService

wgrus-inventory.war

GadgetInventoryService

wgrus-inventory.war

MySQL

47

Message Broker

Page 48: Spring Into the Cloud

RabbitMQ – Messaging that Just Works

RobustHigh-performance

Easy to useAMQP LEADER

Page 49: Spring Into the Cloud

Spring AMQP

•Encapsulates low-level details

•Simplifies sending and receiving of messages

Producer

Spring AMQP

AMQP

AmqpTemplate

Consumer

ListenerContainer

Page 50: Spring Into the Cloud

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

Page 51: Spring Into the Cloud

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

Page 52: Spring Into the Cloud

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);}

}

Page 53: Spring Into the Cloud

Order Service

Messaging Gateway

Channel Service Activator

Shipping service

AMQP

RabbitMQ

AMQP Channel

53

Example of reconfigurability - distributed

Code unchanged in new deployment

Page 54: Spring Into the Cloud

54

Using Spring Integration with the web store application

Page 55: Spring Into the Cloud

ShippingService

StoreFront

wgrus-store.war

Credit Service

wgrus-billing.war

wgrus-shipping.war

WidgetInventoryService

wgrus-inventory.war

GadgetInventoryService

55

Spring Integration Logic

Page 56: Spring Into the Cloud

orderChannel object to JSON amqpOut

StoreUI

Message Endpoint AMQP

Store front flow

56

Page 57: Spring Into the Cloud

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

Page 58: Spring Into the Cloud

AMQP shipping order

channel

Json to Object

Service Activator

shipping service

Shipping flow

58

Page 59: Spring Into the Cloud

59

DemoWGRUS in the Cloud

Page 60: Spring Into 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

Page 61: Spring Into the Cloud

Summary

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

61

Page 62: Spring Into the Cloud

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