consuming data services with spring apps on cloud foundry

21
Unless otherwise indicated, these slides are © 2013-2016 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Consuming Data Services with Spring Apps on Cloud Foundry Scott Frederick @scottyfred

Upload: spring-by-pivotal

Post on 16-Apr-2017

801 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Consuming Data Services with Spring Apps on Cloud Foundry

Unless o therw ise ind ica ted , these s l ides are © 2013-2016 P ivo ta l So f tware , Inc . and l i censed under a Creat ive Commons At t r ibu t ion-NonCommerc ia l l i cense: h t tp : / / c rea t ivecommons.org / l i censes /by-nc /3 .0 /

Consuming Data Services with Spring Apps on Cloud Foundry

Scott Frederick@scottyfred

Page 2: Consuming Data Services with Spring Apps on Cloud Foundry

Unless o therw ise ind ica ted , these s l ides are © 2013-2016 P ivo ta l So f tware , Inc . and l i censed under a Creat ive Commons At t r ibu t ion-NonCommerc ia l l i cense: h t tp : / / c rea t ivecommons.org / l i censes /by-nc /3 .0 /

Apps and Services

2

$ cf push my-app $ cf create p-mysql 100mb my-db

$ cf bind my-app my-db

Page 3: Consuming Data Services with Spring Apps on Cloud Foundry

Unless o therw ise ind ica ted , these s l ides are © 2013-2016 P ivo ta l So f tware , Inc . and l i censed under a Creat ive Commons At t r ibu t ion-NonCommerc ia l l i cense: h t tp : / / c rea t ivecommons.org / l i censes /by-nc /3 .0 /

Layers of Data Access

3

application

Spring Data

connection library / driverconfigure connection parameters here

Page 4: Consuming Data Services with Spring Apps on Cloud Foundry

Unless o therw ise ind ica ted , these s l ides are © 2013-2016 P ivo ta l So f tware , Inc . and l i censed under a Creat ive Commons At t r ibu t ion-NonCommerc ia l l i cense: h t tp : / / c rea t ivecommons.org / l i censes /by-nc /3 .0 /

What’s a “data service”? Resources provisioned on demand

• Managed services instances are provisioned by service brokers • User-provided services instances can be used to expose off-platform

resources to applications running on the platform

Once provisioned, service instances can be exposed to applications on the platform

Data services provide persistence and messaging to applications

4

Page 5: Consuming Data Services with Spring Apps on Cloud Foundry

Unless o therw ise ind ica ted , these s l ides are © 2013-2016 P ivo ta l So f tware , Inc . and l i censed under a Creat ive Commons At t r ibu t ion-NonCommerc ia l l i cense: h t tp : / / c rea t ivecommons.org / l i censes /by-nc /3 .0 / 5

Page 6: Consuming Data Services with Spring Apps on Cloud Foundry

Unless o therw ise ind ica ted , these s l ides are © 2013-2016 P ivo ta l So f tware , Inc . and l i censed under a Creat ive Commons At t r ibu t ion-NonCommerc ia l l i cense: h t tp : / / c rea t ivecommons.org / l i censes /by-nc /3 .0 /

"VCAP_SERVICES": { "p-mysql": [{ "label": "p-mysql", "name": "customer-db", "plan": "100mb", "tags": ["mysql","relational"] "credentials": { "jdbcUrl": "jdbc:mysql://192.168.1.149:3306/

cf_231f410b_235f_461e_a7f6_bddf3469fa93?user=khuTGcRGBsvIWUjB\u0026password=aFwkW2UTMhHPcUsR",

"uri": "mysql://khuTGcRGBsvIWUjB:[email protected]:3306/cf_231f410b_235f_461e_a7f6_bddf3469fa93?reconnect=true"

} }]

Application Environment

6

Page 7: Consuming Data Services with Spring Apps on Cloud Foundry

Unless o therw ise ind ica ted , these s l ides are © 2013-2016 P ivo ta l So f tware , Inc . and l i censed under a Creat ive Commons At t r ibu t ion-NonCommerc ia l l i cense: h t tp : / / c rea t ivecommons.org / l i censes /by-nc /3 .0 / 7

http://cloud.spring.io/spring-cloud-connectors

Page 8: Consuming Data Services with Spring Apps on Cloud Foundry

Unless o therw ise ind ica ted , these s l ides are © 2013-2016 P ivo ta l So f tware , Inc . and l i censed under a Creat ive Commons At t r ibu t ion-NonCommerc ia l l i cense: h t tp : / / c rea t ivecommons.org / l i censes /by-nc /3 .0 /

Supported Services• Relational Databases

• MySQL• PostgreSQL• Oracle• DB2• SQLServer

• MongoDB• Redis• RabbitMQ• SMTP e-mail

8

http://cloud.spring.io/spring-cloud-connectors

Page 9: Consuming Data Services with Spring Apps on Cloud Foundry

Unless o therw ise ind ica ted , these s l ides are © 2013-2016 P ivo ta l So f tware , Inc . and l i censed under a Creat ive Commons At t r ibu t ion-NonCommerc ia l l i cense: h t tp : / / c rea t ivecommons.org / l i censes /by-nc /3 .0 /

Demo

9

Page 10: Consuming Data Services with Spring Apps on Cloud Foundry

Unless o therw ise ind ica ted , these s l ides are © 2013-2016 P ivo ta l So f tware , Inc . and l i censed under a Creat ive Commons At t r ibu t ion-NonCommerc ia l l i cense: h t tp : / / c rea t ivecommons.org / l i censes /by-nc /3 .0 /

Java Buildpack Auto Re-configurationAdds an “auto-reconfiguration” jar to the application’s classpath on deployment

• Implements a BeanPostProcessor that inspects AppContext • Detects beans of known types supported by Connectors• Replaces existing beans in the AppContext with Connectors-created beans

Automatically enabled when core Spring jars are detected in the deployed app archive

10

Spring Cloud Connectors

Java Buildpack auto-reconfigurationJava Buildpack

application

Page 11: Consuming Data Services with Spring Apps on Cloud Foundry

Unless o therw ise ind ica ted , these s l ides are © 2013-2016 P ivo ta l So f tware , Inc . and l i censed under a Creat ive Commons At t r ibu t ion-NonCommerc ia l l i cense: h t tp : / / c rea t ivecommons.org / l i censes /by-nc /3 .0 /

Java Buildpack Auto Re-configurationLimitations

• Supports only one bean of a service type (e.g. relational database)• Supports RDBMD, MongoDB, Redis, and RabbitMQ only• Connection details can’t be customized

Automatically disabled when Connectors classes are detected in the appCan be manually disabled via an environment variable

11

$ cf set-env customer-app JBP_CONFIG_SPRING_AUTO_RECONFIGURATION '{enabled: false}'

Page 12: Consuming Data Services with Spring Apps on Cloud Foundry

Unless o therw ise ind ica ted , these s l ides are © 2013-2016 P ivo ta l So f tware , Inc . and l i censed under a Creat ive Commons At t r ibu t ion-NonCommerc ia l l i cense: h t tp : / / c rea t ivecommons.org / l i censes /by-nc /3 .0 /

Demo

12

Page 13: Consuming Data Services with Spring Apps on Cloud Foundry

Unless o therw ise ind ica ted , these s l ides are © 2013-2016 P ivo ta l So f tware , Inc . and l i censed under a Creat ive Commons At t r ibu t ion-NonCommerc ia l l i cense: h t tp : / / c rea t ivecommons.org / l i censes /by-nc /3 .0 /

Spring Boot Auto ConfigurationSpring Boot provides auto configuration via Connectors

• Creates connection beans for any service type known to Connectors (core or extensions)

• Takes precedence over other Spring Data auto configuration

Cloud Connectors starter includes classes necessary to enable this auto configuration

• Disables Java Buildpack auto re-configuration

13

Spring Cloud Connectors

Spring Boot auto configuration

application

Page 14: Consuming Data Services with Spring Apps on Cloud Foundry

Unless o therw ise ind ica ted , these s l ides are © 2013-2016 P ivo ta l So f tware , Inc . and l i censed under a Creat ive Commons At t r ibu t ion-NonCommerc ia l l i cense: h t tp : / / c rea t ivecommons.org / l i censes /by-nc /3 .0 /

spring.cloud.enabled=false

Limitations• Supports only one bean of a service type (e.g. relational database) • Connection details can’t be customized

Automatically disabled when the Connectors “Cloud” class is present in the app contextCan be manually disabled via a Spring Boot property

Spring Boot Auto Configuration

14

Page 15: Consuming Data Services with Spring Apps on Cloud Foundry

Unless o therw ise ind ica ted , these s l ides are © 2013-2016 P ivo ta l So f tware , Inc . and l i censed under a Creat ive Commons At t r ibu t ion-NonCommerc ia l l i cense: h t tp : / / c rea t ivecommons.org / l i censes /by-nc /3 .0 /

Demo

15

Page 16: Consuming Data Services with Spring Apps on Cloud Foundry

Unless o therw ise ind ica ted , these s l ides are © 2013-2016 P ivo ta l So f tware , Inc . and l i censed under a Creat ive Commons At t r ibu t ion-NonCommerc ia l l i cense: h t tp : / / c rea t ivecommons.org / l i censes /by-nc /3 .0 /

Connectors Manual ConfigurationGives full control over the connection configuration with a small amount of code

Allows customization of connection parameters and connection pool configuration

Supports configuration of multiple services of the same type

16

Spring Cloud Connectors

application

Page 17: Consuming Data Services with Spring Apps on Cloud Foundry

Unless o therw ise ind ica ted , these s l ides are © 2013-2016 P ivo ta l So f tware , Inc . and l i censed under a Creat ive Commons At t r ibu t ion-NonCommerc ia l l i cense: h t tp : / / c rea t ivecommons.org / l i censes /by-nc /3 .0 /

Configuration with Multiple Services

17

@Configuration public class DatabaseConfig extends AbstractCloudConfig { @Bean public DataSource customerDataSource() { return connectionFactory().dataSource(“customer-db”); }

@Bean public DataSource orderDataSource() { return connectionFactory().dataSource(“order-db”); } }

Page 18: Consuming Data Services with Spring Apps on Cloud Foundry

Unless o therw ise ind ica ted , these s l ides are © 2013-2016 P ivo ta l So f tware , Inc . and l i censed under a Creat ive Commons At t r ibu t ion-NonCommerc ia l l i cense: h t tp : / / c rea t ivecommons.org / l i censes /by-nc /3 .0 /

@Configuration @Profile("default") public class LocalDatabaseConfig { @Bean public DataSource dataSource() { return DataSourceBuilder.create().build(); } }

@Configuration @Profiles(“cloud”) public class CloudDatabaseConfig extends AbstractCloudConfig { @Bean public DataSource dataSource() { return connectionFactory().dataSource(); } }

Profiles for local testing

18

Page 19: Consuming Data Services with Spring Apps on Cloud Foundry

Unless o therw ise ind ica ted , these s l ides are © 2013-2016 P ivo ta l So f tware , Inc . and l i censed under a Creat ive Commons At t r ibu t ion-NonCommerc ia l l i cense: h t tp : / / c rea t ivecommons.org / l i censes /by-nc /3 .0 /

A “cloud” property source is added to Spring Boot propertiesThese properties can be used to explicitly configure other Spring Boot properties

Limitations• Supports only one bean of a service type (e.g. relational database)• Properties vary by service offering • Either the service type (e.g. “mysql”) or name (e.g. “customer-db”)

must be used in property placeholder

spring.datasource.url: ${cloud.services.mysql.connection.jdbcurl} spring.datasource.username: ${cloud.services.mysql.connection.username} spring.datasource.password: ${cloud.services.mysql.connection.password}

One More Option

19

Page 20: Consuming Data Services with Spring Apps on Cloud Foundry

Unless o therw ise ind ica ted , these s l ides are © 2013-2016 P ivo ta l So f tware , Inc . and l i censed under a Creat ive Commons At t r ibu t ion-NonCommerc ia l l i cense: h t tp : / / c rea t ivecommons.org / l i censes /by-nc /3 .0 /

Binding to Data Services with Spring Boot in Cloud Foundryhttps://spring.io/blog/2015/04/27/binding-to-data-services-with-spring-boot-in-cloud-foundry

Demo apphttps://github.com/scottfrederick/data-binding

Consuming other types of data serviceshttps://github.com/cloudfoundry-samples/spring-music

Further Reading

20

Page 21: Consuming Data Services with Spring Apps on Cloud Foundry

Unless o therw ise ind ica ted , these s l ides are © 2013-2016 P ivo ta l So f tware , Inc . and l i censed under a Creat ive Commons At t r ibu t ion-NonCommerc ia l l i cense: h t tp : / / c rea t ivecommons.org / l i censes /by-nc /3 .0 /

Learn More. Stay Connected.

Introduction to Spring Data - Greg TurnquistWhat's New in Spring Data? - Mark Paluch, Christoph Strobl

Advanced Spring Data REST - Oliver Gierke

@springcentralspring.io/blog

@pivotalpivotal.io/blog

@pivotalcfhttp://engineering.pivotal.io