Spring Into the Cloud


Published on

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.

Published in: Technology, Business
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Spring Into the Cloud

  1. 1. Spring into the CloudJennifer Hickey@jencompgeekjennifer.hickey@springsource.com
  2. 2. About this session Spring and the Cloud: a match made in heaven
  3. 3. About Jennifer•Long time SpringSource-er•Contributed to many Spring and SpringSource projects•Cloud Foundry Engineer •Focused on framework support 3
  4. 4. Agenda•Why Cloud? Why PaaS?•Spring Framework for the Cloud•Data Developing NoSQL applications for the Cloud with Spring•Integration Cloud application integration with RabbitMQ and Spring•Wrap up 4
  5. 5. Traditional web application architecture• Browser client• Monolithic WAR• RDBMS Desktop MySQL Apache WAR Browser Database Tomcat Simple to develop Simple to test Simple to deploy Simple to scale: just add Apache + more Tomcats 5
  6. 6. New kinds of clients Smart phones overtake PCs in Q4 2010 6
  7. 7. Users expect a rich, dynamic and interactive experience on mobile devices and desktop gh ou d en oo ’t g HTTP Request isn Desktop ct ure Java Web e Browser hit HTML/Javascript Application I arc ty le U Olds 7
  8. 8. Users expect a rich, dynamic and interactive experience on mobile devices and desktop HTML5 WS Request Javascript Application XML/JSON response Web ServicesMobile and Desktop Browser Web Socket/Eventing 8
  9. 9. 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
  10. 10. Need scalable architectures to handle massive loads•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 10
  11. 11. Data Explosion: Data Volumes increasing at 60% per year Horizontally scalable, distributed NoSQL Databases 20 11 Eventual consistency rather than ACID
  12. 12. Scaling development WAR Front End Scalable User Management != Search development Ordering• Forces multiple developers/teams to efforts synchronize development• Obstacle to frequent, independent deployments• Increases risk of failure - need to redeploy everything to change one thing 12
  13. 13. Scaling development•Need “SOA” approach •Partition application into set of services •Partition by noun or by verb•manages their own release schedule. and Each team is responsible for a service •New code updates frequently •Mature services upgrade infrequently User User Management Management Front-end Search Search Front-End Ordering Ordering Front-End 13
  14. 14. Modern application architecture User Management MySQL Tomcat Desktop Browser Native NodeJS Search NodeJS Mobile front-end front-end Application application application RabbitMQ Tomcat HTML5 Mongo mobile application Redis Ordering Tomcat 14
  15. 15. Three layers of cloud computing SaaS PaaSNeed to be here IaaS 15
  16. 16. What you need is PaaS = Easy deployment Services: Application management Database + Blob storage Easy scaling up and down Messaging ... 16
  17. 17. Cloud Foundry: Services, Frameworks and Clouds 17
  18. 18. Agenda•Why Cloud? Why PaaS?•Spring Framework for the Cloud•Data Developing NoSQL applications for the Cloud with Spring•Integration Cloud application integration with RabbitMQ and Spring•Wrap up 18
  19. 19. Spring’s aim: bring simplicity to java development data web tier batch integration & access & service tier mobile processing messaging / NoSQL / RIA Big Data The Spring frameworkthe cloud: lightweight traditional CloudFoundry WebSphere tc Server Google App Engine JBoss AS Tomcat Amazon Web Services WebLogic Jetty Heroku (on legacy versions, too!) 19
  20. 20. At its core, the Spring Framework...•developing enterprise Java™ applications Provides comprehensive infrastructural support for •Spring deals with the plumbing •So you can focus on solving the domain problem 20
  21. 21. 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
  22. 22. Isolating Cloud Configuration<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> 22
  23. 23. Java Configuration with Profiles @Configuration @Profile(“local”) public class LocalDataSourceConfiguration { @Bean public javax.sql.DataSource dataSource() { ... } } @Configuration @Profile(“cloud”) public class CloudDataSourceConfiguration { @Bean public javax.sql.DataSource dataSource() { ... } } 23
  24. 24. Agenda•Why Cloud? Why PaaS?•Spring Framework for the Cloud•Data Developing NoSQL applications for the Cloud with Spring•Integration Cloud application integration with RabbitMQ and Spring•Wrap up 24
  25. 25. Data Access Challenge #1: Scale Horizontally 25
  26. 26. Data Access Challenge #2: Heterogeneous data access needs 26
  27. 27. NoSQL offers several data store categoriesKey-Value Column Document Graph Redis, Cassandra, MongoDB Neo4J Riak HBase 27
  28. 28. NoSQL offers several data store categoriesKey-Value Column Document Graph MongoDB 28
  29. 29. Spring Framework built-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) 29
  30. 30. http://www.springsource.org/spring-data•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 30
  31. 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 31
  32. 32. Spring MongoDB 32
  33. 33. Spring Data Document Mongo•MongoTemplate interface for mapping Mongo documents • MongoConverter •SimpleMongoConverter for basic POJO mapping support •Leverage Spring 3.0 TypeConverters and SpEL •Exception translation•Advanced Mapping •Annotation based (@Document, @Id, @DbRef)•MongoRepository for JPA Repositories •Built on Hades support 33
  34. 34. Simple Domain Class 34
  35. 35. Mongo TemplateDirect Usage of the Mongo Template: Insert into “Person” Collection findOne using query: { "name" : "Joe"} in db.collection: database.Person Dropped collection [database.person] 35
  36. 36. Generic RepositoryInterface for generic CRUD operations on a repository for a specific type 36
  37. 37. Paging and Sorting RepositoryPaging and Sorting Repository: Extends “CrudRepository” Usage: 37
  38. 38. Custom RepositoryCustom Repository:Keywords :Keyword Sample Logical resultGreaterThan 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) 38
  39. 39. Cross Store 39
  40. 40. JPA and MongoDB JPA “Customer” with a “SurveyInfo” Document 40
  41. 41. Using a Cross-Store Saving a Customer with a SurveryInfo Create Customer Create SurveyInfo Assign Survey to Customer Save Mongo Document: 41
  42. 42. DemoCross-Store Persistence with Mongo 42
  43. 43. Agenda•Why Cloud? Why PaaS?•Spring Framework for the Cloud•Data Developing NoSQL applications for the Cloud with Spring•Integration Cloud application integration with RabbitMQ and Spring•Wrap up 43
  44. 44. wgrus-monolithic.war @Controller StoreFront Widget Gadget Accounting ShippingInventoryService InventoryService Service Service RDBMS 44
  45. 45. 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. 46. Solution:Asynchronous Architecture 46
  47. 47. wgrus-billing.war Accounting Service wgrus-inventory.warwgrus-store.war Widget InventoryService Message StoreFront Broker MySQL wgrus-inventory.war Gadget InventoryService wgrus-shipping.war Shipping Service 47
  48. 48. RabbitMQ – Messaging that Just Works Robust High-performance Easy to use AMQP LEADER
  49. 49. Spring AMQP• Encapsulates low-level details Producer Consumer• Simplifies sending and receiving of messages Amqp Listener Template Container Spring AMQP AMQP
  50. 50. Spring Integration• Builds on Spring framework• High-level of abstraction for building message based applications• Implements EAI patterns• Provides applicationfor exchanging messages between plumbing components• Promotes loosely coupled components• Integrates with JMS, AMQP, HTTP, Email, File infrastructure: external messaging transfer 50
  51. 51. 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 •destinationchannel adapter - read from channel write to external Outbound 51
  52. 52. Example of reconfigurability - local @Service public class OrderServiceImpl { @Service public class ShippingServiceImpl { @Autowired private ShippingService shippingService; public void shipOrder(String orderId) { System.out.println("shipped order: " + public void placeOrder() { orderId); String orderId = generateOrderId(); } … shippingService.shipOrder(orderId); } } } Order Shipping Service service Messaging Gateway Channel Service Activator 52
  53. 53. Example of reconfigurability - distributed Code unchanged in new deployment Order Shipping Service serviceMessaging Gateway RabbitMQ Channel AMQP AMQP Channel Service Activator 53
  54. 54. Using Spring Integration with the web store application 54
  55. 55. wgrus-store.war wgrus-shipping.war StoreFront Shipping Servicewgrus-billing.war wgrus-inventory.war Credit Service Spring Integration Logic Widget InventoryService Gadget InventoryService 55
  56. 56. Store front flowStoreUI object to Message Endpoint orderChannel amqpOut AMQP JSON 56
  57. 57. 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 57
  58. 58. Shipping flow shipping service Json to AMQP shipping Service Object order Activator channel 58
  59. 59. DemoWGRUS in the Cloud 59
  60. 60. Agenda•Why Cloud? Why PaaS?•Spring Framework for the Cloud•Data Developing NoSQL applications for the Cloud with Spring•Integration Cloud application integration with RabbitMQ and Spring•Wrap up 60
  61. 61. Summary•Cloud? Good.•Spring? Good.•Spring and the Cloud is a match made in heaven 61
  62. 62. Where to Find More•http://bit.ly/spring-data Spring Data Project:•http://www.springsource.org/spring-integration Spring Integration Project:•https://github.com/jencompgeek/cloudfoundry-samples/tree/master/cross- Mongo Cross Store Sample: store•https://github.com/markfisher/springone-wgrus WGRUS Sample:•http://bit.ly/cloudfoundry-samples CloudFoundry Samples:• Sign up for (free) Cloud Foundry http://www.cloudfoundry.com•http://bit.ly/mcf4spring for Spring Developers MicroCloud Foundry•Cloud Foundry and Spring YouTube Channels 62