<!-- To use Spring Cloud Connectors for development -->
-<dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-localconfig-connector</artifactId>
- <version>${VERSION}</version>
-</dependency>
+
<!-- To use Spring Cloud Connectors for development -->
+<dependency>
+ <groupId>org.springframework.cloud</groupId>
+ <artifactId>spring-cloud-localconfig-connector</artifactId>
+ <version>${VERSION}</version>
+</dependency>
-<!-- If you intend to deploy the app to Cloud Foundry -->
-<dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-cloudfoundry-connector</artifactId>
- <version>${VERSION}</version>
-</dependency>
+<!-- If you intend to deploy the app to Cloud Foundry -->
+<dependency>
+ <groupId>org.springframework.cloud</groupId>
+ <artifactId>spring-cloud-cloudfoundry-connector</artifactId>
+ <version>${VERSION}</version>
+</dependency>
-<!-- If you intend to deploy the app to Heroku -->
-<dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-heroku-connector</artifactId>
- <version>${VERSION}</version>
-</dependency>
+<!-- If you intend to deploy the app to Heroku -->
+<dependency>
+ <groupId>org.springframework.cloud</groupId>
+ <artifactId>spring-cloud-heroku-connector</artifactId>
+ <version>${VERSION}</version>
+</dependency>
dependencies {
- // To use Spring Cloud Connectors for development
- compile 'org.springframework.cloud:spring-cloud-localconfig-connector:${VERSION}'
+ // To use Spring Cloud Connectors for development
+ compile 'org.springframework.cloud:spring-cloud-localconfig-connector:${VERSION}'
- // If you intend to deploy the app to Cloud Foundry
- compile 'org.springframework.cloud:spring-cloud-cloudfoundry-connector:${VERSION}'
+ // If you intend to deploy the app to Cloud Foundry
+ compile 'org.springframework.cloud:spring-cloud-cloudfoundry-connector:${VERSION}'
- // If you intend to deploy the app to Heroku
- compile 'org.springframework.cloud:spring-cloud-heroku-connector:${VERSION}'
+ // If you intend to deploy the app to Heroku
+ compile 'org.springframework.cloud:spring-cloud-heroku-connector:${VERSION}'
}
Create a CloudFactory instance. Creation of a CloudFactory instance is a bit expensive, so we recommend using a singleton instance. If you are using a dependency injection framework such as Spring, create a bean for the CloudFactory.
Use the Cloud instance to access application and service information and to create service connectors.
-
// ServiceInfo has all the information necessary to connect to the underlying service
-List<ServiceInfo> serviceInfos = cloud.getServiceInfos();
+
// ServiceInfo has all the information necessary to connect to the underlying service
+List<ServiceInfo> serviceInfos = cloud.getServiceInfos();
-
// Find the `ServiceInfo` definitions suitable for connecting to a particular service type
-List<ServiceInfo> databaseInfos = cloud.getServiceInfos(DataSource.class);
+
// Find the `ServiceInfo` definitions suitable for connecting to a particular service type
+List<ServiceInfo> databaseInfos = cloud.getServiceInfos(DataSource.class);
-
// Alternatively, let Spring Cloud Connectors create a service connector for you
-String serviceId = "inventory-db";
-DataSource ds = cloud.getServiceConnector(serviceId, DataSource.class,
- null/* default config */);
+
// Alternatively, let Spring Cloud Connectors create a service connector for you
+String serviceId = "inventory-db";
+DataSource ds = cloud.getServiceConnector(serviceId, DataSource.class,
+ null /* default config */);
The Spring Service Connector creates service connection objects for Spring applications. It allows for Java or XML configuration of connection beans and currently supports relational databases such as MySQL and PostgreSQL via javax.sql.DataSource, SMTP via org.springframework.mail.MailSender, RabbitMQ via Spring AMQP, and MongoDB and Redis via Spring Data projects. It also provides support for connecting to generic (e.g., private) services.
+
The Spring Service Connector creates service connection objects for Spring applications. It allows for Java or XML configuration of connection beans and currently supports relational databases such as MySQL and PostgreSQL via javax.sql.DataSource; SMTP via org.springframework.mail.MailSender; RabbitMQ via Spring AMQP; and MongoDB, Redis, and Cassandra via Spring Data projects. It also provides support for connecting to generic (e.g., private) services.
The Cloud Foundry Connector discovers services bound to an application running in a Cloud Foundry environment. As it consumes bound service information in Cloud Foundry’s standard format, it is provider-agnostic; it currently is aware of application monitoring, DB2, MongoDB, MySQL, Oracle, PostgreSQL, RabbitMQ, Redis, SMTP, and SQL Server services.
+
The Cloud Foundry Connector discovers services bound to an application running in a Cloud Foundry environment. As it consumes bound service information in Cloud Foundry’s standard format, it is provider-agnostic; it currently is aware of application monitoring, Cassandra, DB2, MongoDB, MySQL, Oracle, PostgreSQL, RabbitMQ, Redis, SMTP, and SQL Server services.
The Heroku Connector discovers services bound to an application running in Heroku. It currently is aware of PostgreSQL (provided by Heroku), MySQL (provided by ClearDB), Redis (provided by Redis To Go, Redis Cloud, RedisGreen, openredis, and Heroku), MongoDB (provided by mLab, MongoHQ, and MongoSoup), and RabbitMQ (provided by CloudAMQP). It can be extended to support additional providers for these services.
+
The Heroku Connector discovers services bound to an application running in Heroku. It currently is aware of PostgreSQL (provided by Heroku), MySQL (provided by ClearDB), Redis (provided by Redis To Go, Redis Cloud, RedisGreen, openredis, and Heroku), MongoDB (provided by MongoLab, MongoHQ, and MongoSoup), and RabbitMQ (provided by CloudAMQP). It can be extended to support additional providers for these services.
public class HelloWorldServiceInfo extends UriBasedServiceInfo {
+ public static final String URI_SCHEME = "helloworld";
- // Needed to support structured service definitions such as Cloud Foundry's
- public HelloWorldServiceInfo(String id, String host, int port, String username, String password, String greeting) {
- super(id, URI_SCHEME, host, port, username, password, greeting);
+ // Needed to support structured service definitions such as Cloud Foundry's
+ public HelloWorldServiceInfo(String id, String host, int port, String username, String password, String greeting) {
+ super(id, URI_SCHEME, host, port, username, password, greeting);
}
- // Needed to support URI-based service definitions such as Heroku's
- public HelloWorldServiceInfo(String id, String uri) {
- super(id, uri);
+ // Needed to support URI-based service definitions such as Heroku's
+ public HelloWorldServiceInfo(String id, String uri) {
+ super(id, uri);
}
}
//Connect to the only available RabbitMQ service
-@Bean
-public RabbitConnectionFactory rabbitFactory() {
- return connectionFactory().rabbitConnectionFactory();
+
//Connect to the only available RabbitMQ service
+@Bean
+public RabbitConnectionFactory rabbitFactory() {
+ return connectionFactory().rabbitConnectionFactory();
}
//Connect to the 'bunnymq' RabbitMQ service
-@Bean
-public RabbitConnectionFactory rabbitFactory() {
- return connectionFactory().rabbitConnectionFactory("bunnymq");
+
//Connect to the 'bunnymq' RabbitMQ service
+@Bean
+public RabbitConnectionFactory rabbitFactory() {
+ return connectionFactory().rabbitConnectionFactory("bunnymq");
}
//Connect to the 'my-own-personal-sql' relational database service
-@Bean
-publicDataSource dataSource() {
- return connectionFactory().dataSource("my-own-personal-sql");
+
//Connect to the 'my-own-personal-sql' relational database service
+@Bean
+public DataSource dataSource() {
+ return connectionFactory().dataSource("my-own-personal-sql");
}
//Set order of DataSource implementations, using a List of full class names
-@Bean
-publicDataSource dataSource() {
- List<String> dataSourceNames = Arrays.asList("TomcatJdbcPooledDataSourceCreator", "HikariCpPooledDataSourceCreator", "BasicDbcpPooledDataSourceCreator");
- DataSourceConfig dbConfig = new DataSourceConfig(dataSourceNames);
- return connectionFactory().dataSource("my-own-personal-sql", dbConfig);
+
//Set order of DataSource implementations, using a List of full class names
+@Bean
+public DataSource dataSource() {
+ List<String> dataSourceNames = Arrays.asList("TomcatJdbcPooledDataSourceCreator", "HikariCpPooledDataSourceCreator", "BasicDbcpPooledDataSourceCreator");
+ DataSourceConfig dbConfig = new DataSourceConfig(dataSourceNames);
+ return connectionFactory().dataSource("my-own-personal-sql", dbConfig);
}
//Set order of DataSource implementations, using a List of strings contained in class names
-@Bean
-publicDataSource dataSource() {
- List<String> dataSourceNames = Arrays.asList("TomcatJdbc", "HikariCp", "BasicDbcp");
- DataSourceConfig dbConfig = new DataSourceConfig(dataSourceNames);
- return connectionFactory().dataSource("my-own-personal-sql", dbConfig);
+
//Set order of DataSource implementations, using a List of strings contained in class names
+@Bean
+public DataSource dataSource() {
+ List<String> dataSourceNames = Arrays.asList("TomcatJdbc", "HikariCp", "BasicDbcp");
+ DataSourceConfig dbConfig = new DataSourceConfig(dataSourceNames);
+ return connectionFactory().dataSource("my-own-personal-sql", dbConfig);
}
//Connect to the 'mongo-service' MongoDB service
-@Bean
-public MongoDbFactory mongoFactory() {
- return connectionFactory().mongoDbFactory("mongo-service");
+
//Connect to the 'mongo-service' MongoDB service
+@Bean
+public MongoDbFactory mongoFactory() {
+ return connectionFactory().mongoDbFactory("mongo-service");
}
//Connect to the only available Redis service
-@Bean
-public RedisConnectionFactory redisFactory() {
- return connectionFactory().redisConnectionFactory();
+
//Connect to the only available Redis service
+@Bean
+public RedisConnectionFactory redisFactory() {
+ return connectionFactory().redisConnectionFactory();
}
//Connect to the 'redis-service' Redis service
-@Bean
-public RedisConnectionFactory redisFactory() {
- return connectionFactory().redisConnectionFactory("redis-service");
+
//Connect to the 'redis-service' Redis service
+@Bean
+public RedisConnectionFactory redisFactory() {
+ return connectionFactory().redisConnectionFactory("redis-service");
}
//Connect to the 'redis-service' Redis service, providing configuration and setting a property
-@Bean
-public RedisConnectionFactory redisFactory() {
- Map<String, Object> properties = newHashMap<String, Object>();
- properties.put("timeout", 10);
- PoolConfig poolConfig = new PoolConfig(5, 30, 3000);
- RedisConnectionFactoryConfig redisConfig = new RedisConnectionFactoryConfig(poolConfig, properties);
- return connectionFactory().redisConnectionFactory("redis-service", redisConfig);
+
//Connect to the 'redis-service' Redis service, providing configuration and setting a property
+@Bean
+public RedisConnectionFactory redisFactory() {
+ Map<String, Object> properties = new HashMap<String, Object>();
+ properties.put("timeout", 10);
+ PoolConfig poolConfig = new PoolConfig(5, 30, 3000);
+ RedisConnectionFactoryConfig redisConfig = new RedisConnectionFactoryConfig(poolConfig, properties);
+ return connectionFactory().redisConnectionFactory("redis-service", redisConfig);
+}
+
+
+
+
+
Cassandra
+
+
To connect to a unique Cassandra service, you can create a service bean using cluster(). The following example connects to the only Cassandra service bound to the application.
+
+
+
+
//Connect to the only available Cassandra service
+@Bean
+public Cluster cluster() {
+ return connectionFactory().cluster();
+}
+
+
+
+
To provide configuration for a unique Cassandra service, you can use an overloaded cluster() variant. The following example connects to the only Cassandra service bound to the application and supplies configuration using a CassandraClusterConfig with metrics and JMX reporting disabled.
+
+
+
+
//Connect to the only available Cassandra service, supplying configuration
+@Bean
+public Cluster cluster() {
+ CassandraClusterConfig config = new CassandraClusterConfig();
+ config.setMetricsEnabled(false);
+ config.setJmxReportingEnabled(false);
+ return connectionFactory().cluster(config);
+}
+
+
+
+
To connect to a specific Cassandra service, you can use an overloaded variant of cluster(). The following example connects specifically to the cassandra-service Cassandra service.
+
+
+
+
//Connect to the 'cassandra-service' Cassandra service
+@Bean
+public Cluster cluster() {
+ return connectionFactory().cluster("cassandra-service");
+}
+
+
+
+
To connect to a specific Cassandra service and provide configuration, you can use an overloaded cluster() variant. The following example connects to the cassandra-service Cassandra service and supplies configuration using a CassandraClusterConfig with metrics and JMX reporting disabled.
+
+
+
+
//Connect to the 'cassandra-service' Cassandra service, supplying configuration
+@Bean
+public Cluster cluster() {
+ CassandraClusterConfig config = new CassandraClusterConfig();
+ config.setMetricsEnabled(false);
+ config.setJmxReportingEnabled(false);
+ return connectionFactory().cluster("cassandra-service", config);
}
<!-- Connect to the 'bunnymq' RabbitMQ service with a bean of id 'rabbitmq' -->
-<cloud:rabbit-connection-factoryid="rabbitmq"service-name="bunnymq"/>
+
<!-- Connect to the 'bunnymq' RabbitMQ service with a bean of id 'rabbitmq' -->
+<cloud:rabbit-connection-factory id="rabbitmq" service-name="bunnymq" />
<!-- Connect to the 'bunnymq' RabbitMQ service with a bean of id 'rabbitmq', setting channel cache size -->
-<cloud:rabbit-connection-factoryid="rabbitmq"service-name="bunnymq">
- <cloud:rabbit-optionschannel-cache-size="200"/>
-</cloud:rabbit-connection-factory>
+
<!-- Connect to the 'bunnymq' RabbitMQ service with a bean of id 'rabbitmq', setting channel cache size -->
+<cloud:rabbit-connection-factory id="rabbitmq" service-name="bunnymq">
+ <cloud:rabbit-options channel-cache-size="200"/>
+</cloud:rabbit-connection-factory>
<!-- Connect to the 'bunnymq' RabbitMQ service with a bean of id 'rabbitmq', setting channel cache size and connection properties -->
-<cloud:rabbit-connection-factoryid="rabbitmq"service-name="bunnymq">
- <cloud:rabbit-optionschannel-cache-size="200"/>
- <cloud:connection-properties>
- <entrykey="requestedHeartbeat"value="5"/>
- <entrykey="connectionTimeout"value="10"/>
- </cloud:connection-properties>
-</cloud:rabbit-connection-factory>
+
<!-- Connect to the 'bunnymq' RabbitMQ service with a bean of id 'rabbitmq', setting channel cache size and connection properties -->
+<cloud:rabbit-connection-factory id="rabbitmq" service-name="bunnymq">
+ <cloud:rabbit-options channel-cache-size="200"/>
+ <cloud:connection-properties>
+ <entry key="requestedHeartbeat" value="5"/>
+ <entry key="connectionTimeout" value="10"/>
+ </cloud:connection-properties>
+</cloud:rabbit-connection-factory>
<!-- Connect to the 'my-own-personal-sql' relational database service, with a bean of id 'mysql' -->
-<cloud:data-sourceid="mysql"service-name="my-own-personal-sql"/>
+
<!-- Connect to the 'my-own-personal-sql' relational database service, with a bean of id 'mysql' -->
+<cloud:data-source id="mysql" service-name="my-own-personal-sql" />
<!-- Connect to the 'my-own-personal-sql' relational database service with a bean of id 'mysql', setting connection properties -->
-<cloud:data-sourceid="mysql"service-name="my-own-personal-sql">
- <cloud:connectionproperties="useUnicode=yes;characterEncoding=UTF-8"/>
-</cloud:data-source>
+
<!-- Connect to the 'my-own-personal-sql' relational database service with a bean of id 'mysql', setting connection properties -->
+<cloud:data-source id="mysql" service-name="my-own-personal-sql">
+ <cloud:connection properties="useUnicode=yes;characterEncoding=UTF-8"/>
+</cloud:data-source>
<!-- Connect to the 'my-own-personal-sql' relational database service with a bean of id 'mysql', configuring pool settings -->
-<cloud:data-sourceid="mysql"service-name="my-own-personal-sql">
- <cloud:poolpool-size="5-30"max-wait-time="3000"/>
-</cloud:data-source>
+
<!-- Connect to the 'my-own-personal-sql' relational database service with a bean of id 'mysql', configuring pool settings -->
+<cloud:data-source id="mysql" service-name="my-own-personal-sql">
+ <cloud:pool pool-size="5-30" max-wait-time="3000"/>
+</cloud:data-source>
<!-- Set order of DataSource implementations, using strings contained in class names -->
-<cloud:data-sourceid="mysql"service-name="my-own-personal-sql"/>
- <cloud:pool-data-sources>
- <value>TomcatJdbc</value>
- <value>TomcatDbcp</value>
- <value>BasicDbcp</value>
- </cloud:pool-data-sources>
-</cloud:data-source>
+
<!-- Set order of DataSource implementations, using strings contained in class names -->
+<cloud:data-source id="mysql" service-name="my-own-personal-sql" />
+ <cloud:pool-data-sources>
+ <value>TomcatJdbc</value>
+ <value>TomcatDbcp</value>
+ <value>BasicDbcp</value>
+ </cloud:pool-data-sources>
+</cloud:data-source>
<!-- Connect to the 'mongo-service' MongoDB service with a bean of id 'mongo', setting connections per host -->
-<cloud:mongo-db-factoryid="mongo"service-name="mongo-service">
- <cloud:mongo-optionsconnections-per-host="50"/>
-</cloud:mongo-db-factory>
+
<!-- Connect to the 'mongo-service' MongoDB service with a bean of id 'mongo', setting connections per host -->
+<cloud:mongo-db-factory id="mongo" service-name="mongo-service">
+ <cloud:mongo-options connections-per-host="50"/>
+</cloud:mongo-db-factory>
<!-- Connect to the 'redis-service' Redis service with a bean of id 'redis' -->
-<cloud:redis-connection-factoryid="redis"service-name="redis-service"/>
+
<!-- Connect to the 'redis-service' Redis service with a bean of id 'redis' -->
+<cloud:redis-connection-factory id="redis" service-name="redis-service"/>
<!-- Connect to the 'redis-service' Redis service with a bean of id 'redis', setting a connection property -->
-<cloud:redis-connection-factoryid="redis"service-name="redis-service">
- <cloud:connection-properties>
- <entrykey="timeout"value="10"/>
- </cloud:connection-properties>
-</cloud:redis-connection-factory>
+
<!-- Connect to the 'redis-service' Redis service with a bean of id 'redis', setting a connection property -->
+<cloud:redis-connection-factory id="redis" service-name="redis-service">
+ <cloud:connection-properties>
+ <entry key="timeout" value="10"/>
+ </cloud:connection-properties>
+</cloud:redis-connection-factory>
<!-- Connect to the 'redis-service' Redis service with a bean of id 'redis', configuring pool settings -->
-<cloud:redis-connection-factoryid="redis"service-name="redis-service">
- <cloud:poolpool-size="5-30"max-wait-time="3000"/>
-</cloud:redis-connection-factory>
+
<!-- Connect to the 'redis-service' Redis service with a bean of id 'redis', configuring pool settings -->
+<cloud:redis-connection-factory id="redis" service-name="redis-service">
+ <cloud:pool pool-size="5-30" max-wait-time="3000"/>
+</cloud:redis-connection-factory>
+
+
+
+
+
Cassandra
+
+
To connect to a Cassandra service, you can use the <cloud:cassandra-session-factory/> element. The following example connects to the only Cassandra service bound to the application.
+
+
+
+
<!-- Connect to the only available Cassandra service -->
+<cloud:cassandra-session-factory/>
+
+
+
+
To connect to a specific Cassandra service, you can use the service-name attribute. The following example connects specifically to the cassandra-service Cassandra service.
+
+
+
+
<!-- Connect to the 'cassandra-service' Cassandra service -->
+<cloud:cassandra-session-factory service-name="cassandra-service"/>
+
+
+
+
To specify an id for the Cassandra connection bean, you can use the id attribute. The following example connects specifically to the cassandra-service Cassandra service with a bean given the id cassandra.
+
+
+
+
<!-- Connect to the 'cassandra-service' Cassandra service with a bean of id 'cassandra' -->
+<cloud:cassandra-session-factory id="cassandra" service-name="cassandra-service"/>
+
+
+
+
To set properties on a Cassandra service, you can use the <cloud:cassandra-options> nested element. The following example connects specifically to the cassandra-service Cassandra service with a bean given the id cassandra and uses the <cloud:cassandra-options> element.
+
+
+
+
<!-- Connect to the 'cassandra-service' Cassandra service with a bean of id 'cassandra', setting connections options -->
+<bean id="retryPolicy" class="com.datastax.driver.core.policies.DefaultRetryPolicy"/>
+
+<cloud:cassandra-session-factory id="cassandra" service-name="cassandra-service" >
+ <cloud:cassandra-options compression="NONE" retry-policy="retryPolicy"/>
+</cloud:cassandra-session-factory>