BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Spring Cloud 1.0 - Cloud Platform Abstraction

Spring Cloud 1.0 - Cloud Platform Abstraction

This item in japanese

Lire ce contenu en français

Pivotal has recently released Spring Cloud 1.0, an open-source library that provides a simple way to develop JVM-based applications for the cloud. Applications can connect to various cloud services and discover information about the cloud environment at runtime. Spring Cloud can be used with both Spring and non-Spring based applications. Spring Cloud 1.0 has support Cloud Foundry and Heroku, and can be extended to support other cloud platforms.

Spring Cloud introduces the concepts of a Cloud Connector and a Service Connector. A Cloud Connector is an interface that a cloud provider implements to allow the rest of the library to work with a cloud platform. Spring Cloud 1.0 comes with a Cloud Foundry Cloud Connector and a Heroku Cloud Connector. A Service Connector is an object that represents a connection to a service. Spring Cloud 1.0 comes with a Service Connector for javax.sql.DataSource and Spring Data projects. Custom Cloud Connectors and Service Connectors can be written to support other cloud platforms and services, packaged as JAR files and simply added to the classpath.

To use Spring Cloud with a Spring application, you will need to add the spring-cloud-spring-service-connector dependency. Here's the Maven dependency snippet.

	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-spring-service-connector</artifactId>
		<version>1.0.0.RELEASE</version>
	</dependency>

To use Spring Cloud with a non-Spring application, you will need to add the spring-cloud-core dependency instead.

	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-core</artifactId>
		<version>1.0.0.RELEASE</version>
	</dependency>

If you want the ability to deploy to both Cloud Foundry and Heroku, add the following dependencies.

	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-cloudfoundry-connector</artifactId>
		<version>1.0.0.RELEASE</version>
	</dependency>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-heroku-connector</artifactId>
		<version>1.0.0.RELEASE</version>
	</dependency>

Here's an example of getting a DataSource service and an ApplicationInstanceInfo. ApplicationInstanceInfo provides information about the instance, and is cloud platform specific.

	// MyController.java
	@Controller
	public class MyController {
		@Autowired(required = false) DataSource dataSource;
		
		@Autowired ApplicationInstanceInfo instanceInfo;
		
		...
	}
	// CloudConfig.java
	@Configuration
	@ServiceScan
	@Profile("cloud")
	public class CloudConfig extends AbstractCloudConfig {
		@Bean
		public ApplicationInstanceInfo applicationInfo() {
			return cloud().getApplicationInstanceInfo();
		}
	}

The @Profile("cloud") annotation is used since we only want this configuration loaded in a cloud environment. The @ServiceScan annotation scans for all services and creates a bean for autowiring. @ServiceScan is similar to @ComponentScan, but instead of looking for components and beans, it looks for bound services.

For a quick walkthrough on how to deploy to Cloud Foundry or Heroku, read the Introducing Spring Cloud blog post. This blog post uses a Spring Boot sample application. Non-Spring based applications should go over the Spring Cloud Core README usage notes.

Rate this Article

Adoption
Style

BT