Cloud Foundry for Spring Developers

Engineering
Mark Fisher
April 12, 2011

By now, many of you have probably seen the Cloud Foundry webinar and Rod’s blog from earlier today. I’d like to provide a quick follow-up that features a “hello-spring” sample application deployed in the cloud. Thanks to Cloud Foundry, there’s practically no learning curve at all.

Before we get started, let’s consider three goals that have driven Spring from day one:

focus on simplicity and productivity to make developers lives easier

support innovative technologies in a consistent way

ensure portability of applications across deployment environments

Then, consider those same three goals in relation to Cloud Foundry:

Simplicity and Productivity: Deploying a Spring application to the cloud is as simple as dragging and dropping within SpringSource Tool Suite, and even when building an application to run in the cloud, developers can take advantage of the productivity gains enabled by Roo and Grails exactly as they normally would.

Consistent Innovation: Projects like Spring Social and Spring Data embrace innovative technologies such as Twitter and non-relational data stores that are increasingly popular for cloud-based applications, and they do so in ways that are consistent with the existing Spring platform. Cloud Foundry provides services to support such applications. RabbitMQ will be available as a cloud service soon, so the same applies to applications that rely on RabbitMQ for messaging via Spring AMQP and Spring Integration.

Portability: The cloud is first and foremost a new deployment environment, and yet it’s easy to create an application that can run in and out of the cloud without even swapping configuration files.

With those goals in mind, we’ve designed a sample application that provides an introduction to Cloud Foundry for Spring developers. This is the first of many sample applications that will be featured on this blog over the following days and weeks. It demonstrates how a single configuration can support both local testing and cloud deployment. It also demonstrates services available in Cloud Foundry: MySQL, Redis, and MongoDB. Let’s quickly walk through the actual deployment and then work backwards to the configuration.

With a Cloud Foundry account, you can deploy (or “push”) applications to the cloud. Those applications can use services by binding to them. When you use the vmc command line tool or the STS plugin, you can either bind to an existing service or create a new one. Let’s look at how to do this with the command line tool, vmc. Here’s the output of the ‘vmc push’ command:

Would you like to deploy from the current directory? [Yn]: y
Application Deployed URL: ‘hello-spring.cloudfoundry.com’?
Detected a Java SpringSource Spring Application, is this correct? [Yn]: y
Memory Reservation [Default:512M] (64M, 128M, 256M, 512M, 1G or 2G) 256
Creating Application: OK
Would you like to bind any services to ‘hello-spring’? [yN]: y
Would you like to use an existing provisioned service [yN]? n
The following system services are available::

  1. redis
  2. mongodb
  3. mysql
    Please select one you wish to provision: 3
    Specify the name of the service [mysql-63854]: hello-db
    Creating Service: OK
    Binding Service: OK
    Uploading Application:
    Checking for available resources: OK
    Processing resources: OK
    Packing application: OK
    Uploading (42K): OK
    Push Status: OK

The vmc output shows the individual steps, but the same task can be accomplished by dragging and dropping to a Cloud Foundry Server instance within SpringSource Tool Suite as shown here:

Cloud Foundry Server in STS

Now that we’ve seen the deployment, let’s take a look at the configuration. It’s surprisingly simple, since you don’t even need to configure explicit credentials and connection strings. Instead, you can retrieve a reference to this DataSource from the cloud itself using the CloudFoundry “cloud” namespace. Let’s look at how we might configure a bean, DataSourceCustomerService, to reference the CloudFoundry provisioned database service:

<cloud:data-source id=“db”/>

The first element will create a bean of type java.sql.DataSource, and the second will be injected with its that reference to satisfy its dependency.
That’s pretty much all you need to know in order to deploy a new application into the cloud and provision services. However, we also want to test the application locally. Thankfully, the forthcoming Spring 3.1 release offers some assistance here via Spring profiles. The “profiles” feature in Spring 3.1 – explained very clearly in this recent blog by Chris Beams - enables you to “switch on” certain beans at runtime.

This can be quite useful when you want to associate beans with particular environments. Similar functionality is possible using Spring’s property placeholder resolution mechanism to vary the definition of objects based on external properties. But this is different: suppose for example that you want to use the cloud:data-source/ element in the cloud, a regular DriverManagerDataSource defined as a on localhost and a DataSource retrieved through JNDI in integration testing via jee:jndi-lookup/. Spring profiles makes it easy to associate each of these beans with a profile, so that – while all three are defined in the configuration – only one is active at any time based on the environment.

Spring provides a few pre-packaged strategies for enabling profiles. One is to use command line system properties. However, if you need to dynamically set the active profiles based on information that is known only at runtime, then consider using an ApplicationContextInitializer implementation, as described in this other post by Chris Beams.

Let’s look at the hello-spring application’s configuration and how it takes profiles into account:

<beans profile="cloud">
    <cloud:data-source id="dataSource"/>
</beans>

The nested elements and their “profile” attributes are the only new Spring 3.1 features in use here. The nested beans element lets us specify that any beans contained therein should be enabled only if the designated profile is active.

With those simple steps, we can now build and test our logic locally and then deploy the exact same application to the cloud without changing anything. The cloud namespace has support for other services, as well. If we add MongoDB and Redis support, the configuration would look like this:

The hello-spring example shows all of this and more, and it can be cloned from this repository. The most important characteristic of the application is that it can be dropped onto either a local server, such as the tc Server instance available within STS, or to the new Cloud Foundry server.

Here’s a screenshot of the hello-spring application deployed locally:

Hello Spring deployed locally

And here’s the exact same application running in the cloud. Notice how the URLs and connection strings are different:

Hello Spring deployed in the cloud

That’s all for now, but watch this space. We have many more sample applications (watch this repo) and blogs planned in the days and weeks ahead!
comments powered by Disqus

translate:
翻译:

简单性和生产力:将Spring应用程序部署到云上就像在SpringSource工具套件中拖放一样简单,即使在构建要在云上运行的应用程序时,开发人员也可以像往常一样利用Roo和Grails带来的生产力提高。
一致性创新:Spring Social和Spring Data等项目采用了创新技术,如Twitter和非关系数据存储,这些技术在基于云的应用程序中越来越受欢迎,而且它们采用了与现有Spring平台一致的方式。CloudFoundry提供了支持此类应用程序的服务。RabbitMQ很快将作为云服务提供,因此这同样适用于依赖RabbitMQ通过Spring AMQP和Spring集成进行消息传递的应用程序。
可移植性:云首先是一个新的部署环境,但是很容易创建一个应用程序,它可以在云中运行,甚至不需要交换配置文件。
考虑到这些目标,我们设计了一个示例应用程序,为Spring开发人员介绍Cloud Foundry。这是许多示例应用程序中的第一个,这些应用程序将在接下来的几天和几周内出现在本博客上。它演示了单个配置如何同时支持本地测试和云部署。它还演示了Cloud Foundry中可用的服务:MySQL、Redis和MongoDB。让我们快速浏览实际的部署,然后返回到配置。
使用CloudFoundry帐户,您可以将(或“推送”)应用程序部署到云中。这些应用程序可以通过绑定来使用服务。使用vmc命令行工具或STS插件时,可以绑定到现有服务或创建新服务。让我们看看如何使用命令行工具vmc来实现这一点。下面是“vmc push”命令的输出:
是否要从当前目录部署?[Yn]:是的
应用程序部署的URL:‘hello spring.cloudfoundry.com’?
检测到Java SpringSource Spring应用程序,是否正确?[Yn]:是的
内存保留[默认:512M](64M、128M、256M、512M、1G或2G)256
创建应用程序:确定
是否要将任何服务绑定到“hello spring”?[yN]:是的
是否要使用现有的已设置服务[yN]?n个
以下系统服务可用:
一。redis公司
2。数据库
三。mysql数据库
请选择您希望提供的一个:3
指定服务的名称[mysql-63854]:hello db
创建服务:确定
绑定服务:确定
正在上载应用程序:
检查可用资源:确定
处理资源:确定
包装申请:可以
上传(42K):确定
推送状态:OK

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值