simplify cloud applications using spring cloud
DESCRIPTION
Developing an application to a cloud platform involves working with deployed application's environment and connecting to services. Spring Cloud, a new project, simplifies these tasks in a variety of cloud platforms including Cloud Foundry and Heroku. Spring Cloud makes it possible to deploy the same artifact (a war or a jar) to multiple cloud environments. It supports multiple clouds through the concept of Cloud Connector and provides out of the box implementation for Cloud Foundry and Heroku. Spring Cloud is designed for extension, making it simple to create a cloud connector for other cloud platforms. Spring Cloud also supports connecting to multiple services through the concept of service connectors. Out of the box, it provides support for many common services, but also makes it easy to extend it to other services. While Spring Cloud can be used by applications using any JVM language and framework, it further simplifies Spring applications through Java and XML-based configuration. In this talk, we will introduce the Spring Cloud project, show how you can simplify configuring applications for cloud deployment, discuss its extensibility mechanism, and put it to good use by showing practical examples from the field.TRANSCRIPT
© 2014 SpringOne 2GX. All rights reserved. Do not distribute without permission.
Simplify Cloud Applications using Spring Cloud
Ramnivas Laddad and Scott Frederick
Agenda
• Spring Cloud Basics • Application Config Options
• Java config • XML config
• Extensibility mechanism • Cloud Platforms • Cloud Services • Frameworks
• Spring Boot Integration
2
Cloud Apps Awareness
3
Cloud Apps Awareness
3
Application
Container
Cloud Apps Awareness
3
Application
Container
Cloud Apps Awareness
3
Application
App Instance
Information
Container
Cloud Apps Awareness
3
Application
Service
App Instance
Information
Container
Cloud Apps Awareness
3
Application
Service
Services
Information
App Instance
Information
Cloud Apps Awareness: Cloud Foundry Instance Information
{ "limits": { "mem" : 512, "disk" : 1024, "fds" : 16384 }, "application_version":"5e6fe3f7-6900-4af8-8376-bf3223ce886b", "application_name":"hello-spring-cloud", "application_uris":["myapp.cfapps.io"], "version":"5e6fe3f7-6900-4af8-8376-bf3223ce886b", "name":"hello-spring-cloud", "space_name":"development", "space_id":"5f629937-1821-4f48-9eb4-8c67c70c0df0", "instance_id":"b84fa4cd1c75431486dec1609828ae36", "instance_index":0, "host":"0.0.0.0", "port":63202, "started_at_timestamp":1401394307, "state_timestamp":1401394307}
4
VCAP_APPLICATION env variable
Cloud Apps Awareness: Cloud Foundry Services
{ "elephantsql": [ { "name" : "inventory-db", "label" : "elephantsql", "tags" : ["relational","Data Store","postgresql"], "plan" : "turtle", "credentials" : { "uri" : "postgres://user:[email protected]:5432/db", "max_conns" : "5" } } ], "rediscloud": [ { "name" : "rediscloud-service", "label" : "rediscloud", "tags" : ["key-value","redis","Data Store"], "plan" : "25mb", "credentials" : { "hostname" : "pub-redis.garantiadata.com", "port" : "11853", "password" : "pass" } } ]}
5
VCAP_SERVICES env variable
Cloud Apps Awareness: Heroku Instance Information• Individual env variables !
PORT 12345! DYNO web.1!
6
Cloud Apps Awareness: Heroku Services
• One environment variable per service !
HEROKU_POSTGRESQL_PURPLE_URL postgres://user:[email protected]:5432/db!
REDISCLOUD_URL redis://rediscloud:[email protected]:19038
REDISTOGO_URL redis://redistogo:[email protected]:9139
7
Spring Cloud Goals
• Abstraction over cloud services and application environment • Implementation for multiple cloud platforms
• Cloud Foundry • Heroku • Local testing simulating a cloud-like environment
• Extensibility without modifying the core code • Cloud Connector • Service Creator
8
Using Java Config
9
Scanning for services
!!
@Configuration@CloudScanpublic class CloudConfig {!}
10
Scanning for services
!!
@Configuration@CloudScanpublic class CloudConfig {!}
10
Taking over control
@Configurationpublic class CloudConfig extends AbstractCloudConfig {
@Beanpublic DataSource dataSource() { return connectionFactory().dataSource();}!
@Bean public MongoDbFactory mongoDb() { return connectionFactory().mongoDbFactory(); }}
11
Taking over control: Working with specific services
@Configurationpublic class CloudConfig extends AbstractCloudConfig { @Bean
public DataSource inventoryDataSource() { return connectionFactory().dataSource("inventory-service");}!
@Bean public DataSource customerDataSource() { return connectionFactory().dataSource("customers-service"); }}
12
Taking over control: Configuring Services
@Configurationpublic class CloudConfig extends AbstractCloudConfig { @Bean public DataSource inventoryDataSource() { PoolConfig poolConfig = new PoolConfig(20, 200); ConnectionConfig connectionConfig = new ConnectionConfig("characterEncoding=UTF-8"); DataSourceConfig serviceConfig = new DataSourceConfig(poolConfig, connectionConfig); return connectionFactory().dataSource( "inventory-service", serviceConfig); }}
13
Acquiring generic services
@Configurationpublic class CloudConfig extends AbstractCloudConfig { @Bean public Search search() { return connectionFactory().service( "search-service", Search.class); }}
14
Demo Spring Cloud on Cloud Foundry
15
Deploy to Cloud Foundry
$ gradle assemble $ cf create-service cleardb spark cities-db $ cf push
16
---applications:- name: cities host: cities-s12gx path: build/libs/spring-boot-cities.jar services: - cities-db env: SPRING_PROFILES_ACTIVE: cloud
http://cities-s12gx.cfapps.io/
Demo Spring Cloud on Heroku
17
Deploy to Heroku
$ heroku create cities-s12gx --stack cedar --buildpack http://github.com/heroku/heroku-buildpack-java.git $ heroku config:set SPRING_CLOUD_APP_NAME=spring-boot-cities $ heroku config:set SPRING_PROFILES_ACTIVE=cloud $ heroku addons:add heroku-postgresql:hobby-dev $ git push heroku master
18
http://cities-s12gx.herokuapp.com/
Using XML Config
19
Scanning for services
20
<cloud:service-scan/>
Taking over control
21
<cloud:data-source/>!<cloud:mongo-db-factory/>!...
Taking over control: Specifying services
22
<cloud:data-source service-name="inventory-service"/>!<cloud:data-source service-name="customers-service"/>
Taking over control: Configuring Services
23
<cloud:data-source service-name="inventory-service"> <cloud:pool pool-size="20" max-wait-time="200"/> <cloud:connection properties="characterEncoding=UTF-8"/></cloud:data-source>
Acquiring generic services
24
<cloud:service service-name="search-service"/>
Acquiring generic services
24
<cloud:service service-name="search-service"/>
<cloud:service service-name="search-service" connector-type="com.example.Search"/>
Services Supported
• Cloud Foundry, Heroku, and Local connectors with Spring • MySQL, Postgres, Oracle • Redis • MongoDB • AMQP / RabbitMQ • SMTP
• Pivotal CF connectors with Spring • Pivotal HD
25
Demo Spring Cloud with Hadoop
26
Explicit Cloud Config
27
@org.springframework.context.annotation.Configurationpublic class CloudConfig extends AbstractCloudConfig { @Bean public ConnectionFactory rabbitConnectionFactory() { return connectionFactory().rabbitConnectionFactory(); } @Bean public DataSource hawqDataSource() { return connectionFactory().dataSource("phd-service/hawq"); }! @Bean public DataSource gemfirexdDataSource() { return connectionFactory().dataSource("phd-service/gemfirexd"); } @Bean public Configuration hadoopConfiguration() { return connectionFactory().service(Configuration.class); }}
Consuming Service Beans: Explicit Config
28
@Autowired @Qualifier("hawqDataSource") DataSource hawqDataSource; @Autowired @Qualifier(“gemfirexdDataSource")DataSource gemfirexdDataSource;!@Autowired ConnectionFactory rabbitConnectionFactory;!@Autowired Configuration hadoopConfiguration;!!
Cloud Config with Scanning
29
@Configuration@CloudScanpublic class CloudConfig {}
Consuming Service Beans: Explicit Config
30
@Autowired @Qualifier("phd-service/hawq") DataSource hawqDataSource; @Autowired @Qualifier("phd-service/gemfirexd")DataSource gemfirexdDataSource;!@Autowired ConnectionFactory rabbitConnectionFactory;!@Autowired Configuration hadoopConfiguration;
Spring Cloud Extensibility
31
Axes of Extensibility
32
Axes of Extensibility
32
Cloud Platform
Axes of Extensibility
32
Cloud Platform
Cloud Services
Axes of Extensibility
32
Cloud Platform
Cloud Services
Framework Support
Service Connector
ServiceInfo
Extensibility Overview
33
CloudConnector ServiceConnectorCreator
Service ConnectorService Connector
Cloud Platform Extensibility
34
public interface CloudConnector {! boolean isInMatchingCloud(); ApplicationInstanceInfo getApplicationInstanceInfo();! List<ServiceInfo> getServiceInfos();}
Registering Cloud Connector
!• Add a line with the class name to /META-INF/services/org.springframework.cloud.CloudConnector!!!!
• Cloud Foundry example:! org.springframework.cloud.cloudfoundry.CloudFoundryConnector!
•Heroku example:!org.springframework.cloud.heroku.HerokuConnector
35
Cloud Service Extensibility
36
Cloud Service Extensibility
36
public interface ServiceInfo { public String getId();}
Cloud Service Extensibility
36
public interface ServiceInfoCreator<SI extends ServiceInfo, SD> { public boolean accept(SD serviceData);! public SI createServiceInfo(SD serviceData);}
public interface ServiceInfo { public String getId();}
Registering Service Info Creators
• Each Cloud Connector can choose any scheme it prefers !• Cloud Foundry Cloud Connector example
/META-INF/services/org.springframework.cloud.cloudfoundry.CloudFoundryServiceInfoCreator
37
Registering Service Info Creators
• Each Cloud Connector can choose any scheme it prefers !• Cloud Foundry Cloud Connector example
/META-INF/services/org.springframework.cloud.cloudfoundry.CloudFoundryServiceInfoCreator
37
org.springframework.cloud.cloudfoundry.MysqlServiceInfoCreatororg.springframework.cloud.cloudfoundry.PostgresqlServiceInfoCreatororg.springframework.cloud.cloudfoundry.RedisServiceInfoCreatororg.springframework.cloud.cloudfoundry.MongoServiceInfoCreatororg.springframework.cloud.cloudfoundry.AmqpServiceInfoCreatororg.springframework.cloud.cloudfoundry.MonitoringServiceInfoCreatororg.springframework.cloud.cloudfoundry.SmtpServiceInfoCreatororg.springframework.cloud.cloudfoundry.OracleServiceInfoCreator
Framework extensibility
• Mapping ServiceInfo to framework-specific service connector • RelationalServiceInfo -> DataSource • MongoServiceInfo -> MongoDbFactory • ...
38
public interface ServiceConnectorCreator<SC, SI extends ServiceInfo> { ! SC create(SI serviceInfo, ServiceConnectorConfig serviceConnectorConfig); Class<SC> getServiceConnectorType();! Class<?> getServiceInfoType();}
Framework extensibility registration
• Each framework may choose any mechanism!!
• Spring Connection Creator example!/META-INF/services/org.springframework.cloud.service.ServiceConnectorCreator
39
Framework extensibility registration
• Each framework may choose any mechanism!!
• Spring Connection Creator example!/META-INF/services/org.springframework.cloud.service.ServiceConnectorCreator
39
org.springframework.cloud.service.relational.MysqlDataSourceCreatororg.springframework.cloud.service.relational.PostgresqlDataSourceCreatororg.springframework.cloud.service.relational.OracleDataSourceCreatororg.springframework.cloud.service.keyval.RedisConnectionFactoryCreatororg.springframework.cloud.service.document.MongoDbFactoryCreatororg.springframework.cloud.service.messaging.RabbitConnectionFactoryCreatororg.springframework.cloud.service.smtp.MailSenderCreator
Demo Spring Cloud to Consume a Microservice
40
Deploy to Cloud Foundry
$ gradle assemble $ cf create-user-provided-service cities-ws -p '{"url": "http://cities-s12gx.cfapps.io/", "tag": "cities"}' $ cf push
41
---applications:- name: cities-ui host: cities-ui-s12gx path: build/libs/spring-boot-cities-ui.war services: [ cities-ws ] env: SPRING_PROFILES_ACTIVE: cloud
http://cities-ui-s12gx.cfapps.io
Spring Boot Integration
42
Auto-configuration
• Add spring-cloud dependencies in project • … that’s it !!
• The same effect as adding @CloudScan
43
Available in Spring Boot 1.2.x
Spring Cloud starter
!<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cloud</artifactId></dependency>
44
Available in Spring Boot 1.2.x
What’s next
• Support for many more services • Elasticsearch • Memcache • Riak • Cassandra • Neo4j • ...
• Support for other cloud platforms • Support for other frameworks?
45
What’s next
• Support for many more services • Elasticsearch • Memcache • Riak • Cassandra • Neo4j • ...
• Support for other cloud platforms • Support for other frameworks?
45
Community Contributions Welcome!
© 2014 SpringOne 2GX. All rights reserved. Do not distribute without permission.
Simplify Cloud Applications using Spring Cloud
Ramnivas Laddad and Scott Frederick