BT

New Early adopter or innovator? InfoQ has been working on some new features for you. Learn more

Spring Cloud 1.0 - Cloud Platform Abstraction

| by Bienvenido David on Jun 11, 2014. Estimated reading time: 2 minutes |

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 Stage
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread
Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Discuss

Login to InfoQ to interact with what matters most to you.


Recover your password...

Follow

Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.

Like

More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.

Notifications

Stay up-to-date

Set up your notifications and don't miss out on content that matters to you

BT