以程式設計方式使用 Spring Cloud

工程 | Ramnivas Laddad | 2014 年 7 月 29 日 | ...

上一篇部落格中,我示範了如何使用 Spring Cloud 的 Java 設定選項,以宣告方式取得服務連接器 (如果您需要,也有 XML 命名空間支援)。在本篇部落格中,我們將更仔細地探討如何以程式設計方式使用 Spring Cloud。這將有助於您無法使用 Java 或 XML 設定的情況。它也將揭開 Spring Cloud 的運作方式,並為本系列的下一篇部落格做好準備,我們將在其中討論擴展 Spring Cloud。

若要使用 Spring Cloud,我們需要存取 Cloud 類別的物件。但是,您無法直接建立 Cloud 物件 (其建構子不是 public)。相反地,您將透過 CloudFactory 取得它。

CloudFactory cloudFactory = new CloudFactory();
Cloud cloud = cloudFactory.getCloud();

以這種方式建立的雲端物件適用於應用程式運作的環境。例如,如果應用程式在 Cloud Foundry 中執行,則會配置為了解它如何將服務公開給應用程式。請注意,建立 CloudFactory 實例有點耗費資源,因此您應該盡量避免建立多個實例。如果您使用 Spring 等依賴注入框架,它將負責避免多個實例;否則,只需自己管理它。

一旦我們有了雲端物件,我們可以使用各種條件取得應用程式實例資訊、服務資訊物件,以及使用指定的條件取得服務連接器。假設您想要取得繫結到應用程式的所有服務的 ServiceInfo 物件,並列印關聯式服務的 JDBC URL,您可以使用以下程式碼片段

List<ServiceInfo> serviceInfos = cloud.getServiceInfos();
for (ServiceInfo serviceInfo : serviceInfos) {
    if (serviceInfo instanceof RelationalServiceInfo) {
        System.out.println(((RelationalServiceInfo) serviceInfo).getJdbcUrl());
    }
}

這將產生如下輸出

jdbc:postgresql://babar.elephantsql.com:5432/tbsonrjm?user=***&password=***

使用 getServiceInfos() 及其變體取得的物件包含足夠的資訊,例如 URL 和憑證,以建立服務連接器。在某些情況下,取得 ServiceInfo 物件可能就是您所需要的,因為您可以隨時根據它建立合適的連接器(例如 DataSource)。但在大多數情況下,您會讓 Spring Cloud 為服務建立合適的服務連接器。例如,如果您想直接取得 "inventory-db" 服務的 DataSource,您可以使用以下程式碼片段

DataSource inventoryDataSource = 
    cloud.getServiceConnector("inventory-db", DataSource.class, null);

此方法有一個變體:getSingletonServiceConnector(),您可以如下使用

DataSource inventoryDataSource = cloud.getSingletonServiceConnector(DataSource.class, null);

在這裡,它將傳回繫結到應用程式的唯一關聯式資料庫服務的 DataSource;如果沒有這樣的服務或有多個服務,它將拋出例外。我們將 null 作為最後一個參數傳遞給這兩種方法,以使用已建立連接器的預設配置。但是,您可以傳遞您想要覆蓋的配置。例如,在這裡我們指定池配置以及要建立的資料來源的連接配置。

PoolConfig poolConfig = new PoolConfig(20, 200);
ConnectionConfig connectionConfig = new ConnectionConfig("characterEncoding=UTF-8");
DataSourceConfig serviceConfig = new DataSourceConfig(poolConfig, connectionConfig);
DataSource invetoryDataSource = cloud.getSingletonServiceConnector(DataSource.class, serviceConfig);

最後,有一種方法可以取得包含應用程式 ID(取決於雲端,但通常是應用程式名稱)、應用程式實例 ID 和寬鬆定義的應用程式屬性的應用程式資訊。讓我們列印所有這些資訊

ApplicationInstanceInfo appInstanceInfo = cloud.getApplicationInstanceInfo();
System.out.println("Application id: " + appInstanceInfo.getAppId());
System.out.println("Application instance id: " + appInstanceInfo.getInstanceId());
for (Map.Entry<String, Object> entry: appInstanceInfo.getProperties().entrySet()) {
    System.out.println("Application property: " + entry.getKey() + "=" + entry.getValue());
}

當您在 Cloud Foundry 中運行的應用程式中執行此程式碼時,您會得到類似於以下的輸出(此處縮寫)。如果同一個應用程式在 Heroku 中運行,它將產生類似的輸出,但具有不同的一組金鑰

Application id: hello-spring-cloud
Application instance id: 8b523252a9d3478b92750ef27ad4e5b0
Application property: limits={mem=800, disk=1024, fds=16384}
Application property: application_version=b1257c57-2a5c-47aa-8ca7-5e8b6d9a7b9c
Application property: application_name=hello-spring-cloud
Application property: application_uris=[hello-spring-cloud.cfapps.io]
Application property: version=b1257c57-2a5c-47aa-8ca7-5e8b6d9a7b9c
Application property: name=hello-spring-cloud
Application property: space_name=development
Application property: space_id=5f629937-1821-4f48-9eb4-8c67c70c0df0
Application property: application_id=a345f90f-e075-4005-b003-f4ab86ad716a
Application property: instance_id=8b523252a9d3478b92750ef27ad4e5b0
Application property: instance_index=0
Application property: host=0.0.0.0
Application property: port=61023
Application property: start=2014-07-15 21:27:34 +0000
Application property: state_timestamp=1405459654

這幾乎是您需要了解的以程式設計方式使用 Spring Cloud 的所有內容。在下一篇部落格中,我們將把重點轉移到 Spring Cloud 的可擴展性方面。敬請期待。

取得 Spring 電子報

透過 Spring 電子報保持聯繫

訂閱

領先一步

VMware 提供培訓和認證,以加速您的進度。

了解更多

取得支援

Tanzu Spring 在一個簡單的訂閱中提供 OpenJDK™、Spring 和 Apache Tomcat® 的支援和二進位檔案。

了解更多

即將舉行的活動

查看 Spring 社群中所有即將舉行的活動。

檢視全部