領先一步
VMware 提供培訓和認證,以加速您的進展。
了解更多到目前為止,你們許多人可能已經看過 Cloud Foundry 線上研討會 和 Rod 今天稍早發表的 部落格。我想提供一個快速的後續報導,重點介紹部署在雲端的 "hello-spring" 範例應用程式。 感謝 Cloud Foundry,幾乎完全沒有學習曲線。
在我們開始之前,讓我們先考慮從一開始就驅動 Spring 的三個目標
接著,考慮與 Cloud Foundry 相關的這三個相同目標
考慮到這些目標,我們設計了一個範例應用程式,為 Spring 開發人員介紹 Cloud Foundry。 這是未來幾天和幾週內將在本部落格上推出的眾多範例應用程式中的第一個。 它示範了單一組態如何支援本機測試和雲端部署。 它還示範了 Cloud Foundry 中可用的服務:MySQL、Redis 和 MongoDB。 讓我們快速瀏覽一下實際部署,然後回頭研究組態。
透過 Cloud Foundry 帳戶,您可以將應用程式部署(或“推送”)到雲端。 這些應用程式可以透過綁定到服務來使用服務。 當您使用 vmc 命令列工具或 STS 外掛程式時,您可以綁定到現有服務或建立新服務。 讓我們看看如何使用命令列工具 vmc 來執行此操作。 以下是 'vmc push' 命令的輸出
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
vmc 輸出顯示了各個步驟,但是相同的任務可以透過拖放到 SpringSource Tool Suite 中的 Cloud Foundry Server 實例來完成,如下所示
既然我們已經看過部署,讓我們看一下組態。 它出乎意料地簡單,因為您甚至不需要配置明確的憑證和連線字串。 相反地,您可以使用 CloudFoundry “cloud” 命名空間從雲端本身檢索對此 DataSource 的參考。 讓我們看看我們如何配置一個 bean,DataSourceCustomerService,以參考 CloudFoundry 佈建的資料庫服務
<cloud:data-source id="db"/>
<bean class="example.AccountRepository">
<property name="dataSource" ref="db"/>
</bean>
第一個元素將建立一個 java.sql.DataSource 類型的 bean,第二個元素將注入其參考以滿足其依賴性。 這幾乎是您需要知道的全部內容,才能將新應用程式部署到雲端並佈建服務。 但是,我們也希望在本機測試應用程式。 幸運的是,即將發佈的 Spring 3.1 版本透過 Spring profiles 在此處提供了一些協助。 Spring 3.1 中的 “profiles” 功能 – 在 Chris Beams 的這篇 近期部落格 中有非常清楚的解釋 – 使您能夠在執行時 “開啟” 某些 bean。
當您想要將 bean 與特定環境關聯時,這可能非常有用。 使用 Spring 的屬性佔位符解析機制來根據外部屬性更改物件的定義,可以實現類似的功能。 但這有所不同:例如,假設您想要在雲端中使用 <cloud:data-source/> 元素,在 localhost 上定義為 <bean> 的常規 DriverManagerDataSource,以及在透過 <jee:jndi-lookup/> 進行整合測試時透過 JNDI 檢索的 DataSource。 Spring profiles 可以輕鬆地將這些 bean 中的每一個與一個 profile 關聯起來,這樣 – 雖然所有三個都在組態中定義 – 但在任何時候只有一個是基於環境而啟用的。
Spring 提供了一些預先封裝的策略來啟用 profiles。 一種是使用命令列系統屬性。 但是,如果您需要根據僅在執行時才知曉的資訊動態設定活動 profiles,那麼請考慮使用 ApplicationContextInitializer 實作,如 Chris Beams 的這篇 其他文章 中所述。
讓我們看看 hello-spring 應用程式的組態以及它如何考慮 profiles
<beans>
<beans profile="default">
<jdbc:embedded-database id="dataSource"/>
</beans>
<beans profile="cloud">
<cloud:data-source id="dataSource"/>
</beans>
</beans>
巢狀 <beans> 元素及其 "profile" 屬性是此處使用的唯一新的 Spring 3.1 功能。 巢狀 beans 元素讓我們可以指定,只有在指定的 profile 處於活動狀態時,才應啟用其中包含的任何 bean。
透過這些簡單的步驟,我們現在可以在本機建置和測試我們的邏輯,然後將完全相同的應用程式部署到雲端,而無需進行任何更改。 雲端命名空間也支援其他服務。 如果我們添加 MongoDB 和 Redis 支援,組態將如下所示
<beans profile="default">
<jdbc:embedded-database id="dataSource"/>
<bean id="mongo" class="com.mongodb.Mongo"/>
<bean id="redis" class="org.springframework.data.keyvalue.redis.connection.jedis.JedisConnectionFactory"/>
</beans>
<beans profile="cloud">
<cloud:data-source id="dataSource"/>
<cloud:mongo id="mongo"/>
<cloud:redis-connection-factory id="redis"/>
</beans>
hello-spring 範例展示了所有這些以及更多內容,並且可以從此 儲存庫 克隆。 該應用程式最重要的特點是它可以放在本機伺服器(例如 STS 中可用的 tc Server 實例)或新的 Cloud Foundry 伺服器上。
這是本機部署的 hello-spring 應用程式的螢幕截圖
這是完全相同的應用程式在雲端中運行的情況。 請注意 URL 和連線字串的不同之處
目前就這樣,但請關注此處。 我們在未來幾天和幾週內計劃推出更多範例應用程式(關注 此儲存庫)和部落格!