simplify cloud applications using spring cloud

61
© 2014 SpringOne 2GX. All rights reserved. Do not distribute without permission. Simplify Cloud Applications using Spring Cloud Ramnivas Laddad and Scott Frederick

Upload: ramnivas-laddad

Post on 25-Jan-2015

1.089 views

Category:

Technology


3 download

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

Page 1: Simplify Cloud Applications using Spring Cloud

© 2014 SpringOne 2GX. All rights reserved. Do not distribute without permission.

Simplify Cloud Applications using Spring Cloud

Ramnivas Laddad and Scott Frederick

Page 2: Simplify Cloud Applications using Spring Cloud

Agenda

• Spring Cloud Basics • Application Config Options

• Java config • XML config

• Extensibility mechanism • Cloud Platforms • Cloud Services • Frameworks

• Spring Boot Integration

2

Page 3: Simplify Cloud Applications using Spring Cloud

Cloud Apps Awareness

3

Page 4: Simplify Cloud Applications using Spring Cloud

Cloud Apps Awareness

3

Application

Page 5: Simplify Cloud Applications using Spring Cloud

Container

Cloud Apps Awareness

3

Application

Page 6: Simplify Cloud Applications using Spring Cloud

Container

Cloud Apps Awareness

3

Application

App Instance

Information

Page 7: Simplify Cloud Applications using Spring Cloud

Container

Cloud Apps Awareness

3

Application

Service

App Instance

Information

Page 8: Simplify Cloud Applications using Spring Cloud

Container

Cloud Apps Awareness

3

Application

Service

Services

Information

App Instance

Information

Page 9: Simplify Cloud Applications using Spring Cloud

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

Page 10: Simplify Cloud Applications using Spring Cloud

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

Page 11: Simplify Cloud Applications using Spring Cloud

Cloud Apps Awareness: Heroku Instance Information• Individual env variables !

PORT 12345! DYNO web.1!

6

Page 12: Simplify Cloud Applications using Spring Cloud

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

Page 13: Simplify Cloud Applications using Spring Cloud

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

Page 14: Simplify Cloud Applications using Spring Cloud

Using Java Config

9

Page 15: Simplify Cloud Applications using Spring Cloud

Scanning for services

!!

@Configuration@CloudScanpublic class CloudConfig {!}

10

Page 16: Simplify Cloud Applications using Spring Cloud

Scanning for services

!!

@Configuration@CloudScanpublic class CloudConfig {!}

10

Page 17: Simplify Cloud Applications using Spring Cloud

Taking over control

@Configurationpublic class CloudConfig extends AbstractCloudConfig {

@Beanpublic DataSource dataSource() { return connectionFactory().dataSource();}!

@Bean public MongoDbFactory mongoDb() { return connectionFactory().mongoDbFactory(); }}

11

Page 18: Simplify Cloud Applications using Spring Cloud

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

Page 19: Simplify Cloud Applications using Spring Cloud

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

Page 20: Simplify Cloud Applications using Spring Cloud

Acquiring generic services

@Configurationpublic class CloudConfig extends AbstractCloudConfig { @Bean public Search search() { return connectionFactory().service( "search-service", Search.class); }}

14

Page 21: Simplify Cloud Applications using Spring Cloud

Demo Spring Cloud on Cloud Foundry

15

Page 22: Simplify Cloud Applications using Spring Cloud

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/

Page 23: Simplify Cloud Applications using Spring Cloud

Demo Spring Cloud on Heroku

17

Page 24: Simplify Cloud Applications using Spring Cloud

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/

Page 25: Simplify Cloud Applications using Spring Cloud

Using XML Config

19

Page 26: Simplify Cloud Applications using Spring Cloud

Scanning for services

20

<cloud:service-scan/>

Page 27: Simplify Cloud Applications using Spring Cloud

Taking over control

21

<cloud:data-source/>!<cloud:mongo-db-factory/>!...

Page 28: Simplify Cloud Applications using Spring Cloud

Taking over control: Specifying services

22

<cloud:data-source service-name="inventory-service"/>!<cloud:data-source service-name="customers-service"/>

Page 29: Simplify Cloud Applications using Spring Cloud

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>

Page 30: Simplify Cloud Applications using Spring Cloud

Acquiring generic services

24

<cloud:service service-name="search-service"/>

Page 31: Simplify Cloud Applications using Spring Cloud

Acquiring generic services

24

<cloud:service service-name="search-service"/>

<cloud:service service-name="search-service" connector-type="com.example.Search"/>

Page 32: Simplify Cloud Applications using Spring Cloud

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

Page 33: Simplify Cloud Applications using Spring Cloud

Demo Spring Cloud with Hadoop

26

Page 34: Simplify Cloud Applications using Spring Cloud

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

Page 35: Simplify Cloud Applications using Spring Cloud

Consuming Service Beans: Explicit Config

28

@Autowired @Qualifier("hawqDataSource") DataSource hawqDataSource; @Autowired @Qualifier(“gemfirexdDataSource")DataSource gemfirexdDataSource;!@Autowired ConnectionFactory rabbitConnectionFactory;!@Autowired Configuration hadoopConfiguration;!!

Page 36: Simplify Cloud Applications using Spring Cloud

Cloud Config with Scanning

29

@Configuration@CloudScanpublic class CloudConfig {}

Page 37: Simplify Cloud Applications using Spring Cloud

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;

Page 38: Simplify Cloud Applications using Spring Cloud

Spring Cloud Extensibility

31

Page 39: Simplify Cloud Applications using Spring Cloud

Axes of Extensibility

32

Page 40: Simplify Cloud Applications using Spring Cloud

Axes of Extensibility

32

Cloud Platform

Page 41: Simplify Cloud Applications using Spring Cloud

Axes of Extensibility

32

Cloud Platform

Cloud Services

Page 42: Simplify Cloud Applications using Spring Cloud

Axes of Extensibility

32

Cloud Platform

Cloud Services

Framework Support

Page 43: Simplify Cloud Applications using Spring Cloud

Service Connector

ServiceInfo

Extensibility Overview

33

CloudConnector ServiceConnectorCreator

Service ConnectorService Connector

Page 44: Simplify Cloud Applications using Spring Cloud

Cloud Platform Extensibility

34

public interface CloudConnector {! boolean isInMatchingCloud(); ApplicationInstanceInfo getApplicationInstanceInfo();! List<ServiceInfo> getServiceInfos();}

Page 45: Simplify Cloud Applications using Spring Cloud

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

Page 46: Simplify Cloud Applications using Spring Cloud

Cloud Service Extensibility

36

Page 47: Simplify Cloud Applications using Spring Cloud

Cloud Service Extensibility

36

public interface ServiceInfo { public String getId();}

Page 48: Simplify Cloud Applications using Spring Cloud

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

Page 49: Simplify Cloud Applications using Spring Cloud

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

Page 50: Simplify Cloud Applications using Spring Cloud

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

Page 51: Simplify Cloud Applications using Spring Cloud

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

Page 52: Simplify Cloud Applications using Spring Cloud

Framework extensibility registration

• Each framework may choose any mechanism!!

• Spring Connection Creator example!/META-INF/services/org.springframework.cloud.service.ServiceConnectorCreator

39

Page 53: Simplify Cloud Applications using Spring Cloud

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

Page 54: Simplify Cloud Applications using Spring Cloud

Demo Spring Cloud to Consume a Microservice

40

Page 55: Simplify Cloud Applications using Spring Cloud

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

Page 56: Simplify Cloud Applications using Spring Cloud

Spring Boot Integration

42

Page 57: Simplify Cloud Applications using Spring Cloud

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

Page 58: Simplify Cloud Applications using Spring Cloud

Spring Cloud starter

!<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cloud</artifactId></dependency>

44

Available in Spring Boot 1.2.x

Page 59: Simplify Cloud Applications using Spring Cloud

What’s next

• Support for many more services • Elasticsearch • Memcache • Riak • Cassandra • Neo4j • ...

• Support for other cloud platforms • Support for other frameworks?

45

Page 60: Simplify Cloud Applications using Spring Cloud

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!

Page 61: Simplify Cloud Applications using Spring Cloud

© 2014 SpringOne 2GX. All rights reserved. Do not distribute without permission.

Simplify Cloud Applications using Spring Cloud

Ramnivas Laddad and Scott Frederick