搶先一步
VMware 提供培訓和認證,以加速您的進展。
瞭解更多我們最近宣布了 Spring Cloud Services 3.0 的正式版本,其中涉及對該專案先前使用的架構進行重大重新設計。如相關部落格文章中所詳述,Spring Cloud Services 已移至最新版本的 Spring Framework 和 Spring Boot,並且現在建立在反應式編程模型和 Spring WebFlux 之上。此重新設計的兩個關鍵元件以開放原始碼 Spring Cloud 專案的形式提供。
第一個專案是 Spring Cloud Open Service Broker。此專案已經存在一段時間;但是,最近的 3.0.0 版本本身也經過重新設計,以整合反應式編程模型並更新以支援 Spring WebFlux。
今天推出的第二個專案是 Spring Cloud App Broker。Spring Cloud App Broker 代表 Spring Cloud Services 中底層架構的重要部分的抽象,我們很高興地宣布 Spring Cloud App Broker 1.0.1 現在可用。
Open Service Broker API 專案允許開發人員將服務交付到在雲端原生平台(如 Cloud Foundry、Kubernetes 和 OpenShift)中運行的應用程式。到目前為止,建置基於 Spring Boot 的服務代理應用程式的唯一選項是將 Spring Cloud Open Service Broker 啟動器新增到您的專案、包含所需的組態並實作所需的介面。Spring Cloud Open Service Broker 對於服務代理實作的意見較少,並將許多決策留給開發人員,需要開發人員自己實作所有服務代理應用程式邏輯。
Spring Cloud App Broker 也是一個用於建置實作 Open Service Broker API 的 Spring Boot 應用程式的框架。它提供一個基於 Spring Boot 的框架,使開發人員能夠快速建立一個服務代理,該代理在佈建受管理服務時將應用程式和服務部署到平台。它基於 Spring Cloud Open Service Broker,提供 Spring Cloud Open Service Broker 介面的意見型實作。
這在實際意義上意味著什麼?假設由服務代理部署的受管理服務依賴於其他服務或應用程式。透過使用 App Broker 組態屬性,可以宣告這些服務和應用程式並定義依賴關係。當服務代理收到佈建新服務實例的請求時,App Broker 將管理相關應用程式和服務的部署或佈建,並在適當情況下繫結這些服務和應用程式。相反地,當收到刪除服務實例的請求時,App Broker 將取消繫結並刪除先前佈建的任何相關服務和應用程式。
建立一個 Spring Boot 應用程式,並在應用程式的建置檔中包含 Spring Cloud App Broker 依賴項。
在應用程式的 pom.xml
檔案中包含以下內容
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-app-broker-cloudfoundry</artifactId>
<version>1.0.1.RELEASE</version>
</dependency>
</dependencies>
在應用程式的 build.gradle
檔案中包含以下內容
dependencies {
compile 'org.springframework.cloud:spring-cloud-starter-app-broker-cloudfoundry:1.0.1.RELEASE'
}
服務代理使用 Spring Boot 外部化組態進行設定,並使用 YAML 或 Java Properties 檔案提供(例如,您可以在 application.yml
檔案中提供組態)。由於 Spring Cloud App Broker 建基於 Spring Cloud Open Service Broker,因此您必須提供 Spring Cloud Open Service Broker 組態才能使用 Spring Cloud App Broker。
使用 spring.cloud.openservicebroker
下的屬性包含 Spring Cloud Open Service Broker 組態,如下列範例所示
spring:
cloud:
openservicebroker:
catalog:
services:
- name: example
id: ebca66fd-461d-415b-bba3-5e379d671c88
description: A useful service
bindable: true
tags:
- example
plans:
- name: standard
id: e19e6bc3-37c1-4478-b70f-c7157ebbb28c
description: A standard plan
free: true
使用 spring.cloud.appbroker
下的屬性包含 Spring Cloud App Broker 組態,如下列範例所示
spring:
cloud:
appbroker:
services:
- service-name: example
plan-name: standard
apps:
- name: example-service-app1
path: classpath:app1.jar
- name: example-service-app2
path: classpath:app2.jar
deployer:
cloudfoundry:
api-host: api.sys.example.com
api-port: 443
username: admin
password: adminpass
default-org: test
default-space: development
可以使用以下屬性為服務代理管理的一個或多個服務設定後端應用程式和相關服務。
後端應用程式的屬性可以設定為所有應用程式部署的預設值,或者可以單獨設定每個服務的後端應用程式部署。如需更完整的可用屬性清單,請參閱 參考文件。
spring:
cloud:
appbroker:
deployer:
cloudfoundry:
properties:
memory: 1G
health-check: http
health-check-http-endpoint: /health
health-check-timeout: 180
在服務的組態中,在 spring.cloud.appbroker.services.*
下設定特定服務的覆寫值,如下列範例所示
spring:
cloud:
appbroker:
services:
- service-name: example
plan-name: standard
apps:
- name: example-service-app1
path: classpath:app1.jar
properties:
memory: 2G
count: 2
no-route: true
後端應用程式可能需要一或多個相依的服務。例如,應用程式可能需要使用 MySQL 資料庫。App Broker 會建立任何已配置的服務,並將它們繫結到相關的應用程式。服務是使用已部署應用程式的 services
下的屬性來配置,如下列範例所示:
spring:
cloud:
appbroker:
services:
- service-name: example
plan-name: standard
apps:
- name: example-service-app1
path: classpath:app1.jar
services:
- service-instance-name: example-db
name: mysql
plan: small
parameters:
param-key: param-value
除了上述支援的配置之外,Spring Cloud App Broker 還提供了更多自訂佈建服務實例流程的方法。例如,App Broker 可以透過使用自訂的憑證提供者,為每個後端應用程式部署產生並分配唯一憑證。
也提供了工作流程,用於建立、更新和刪除服務實例的各個階段,以及用於建立和刪除服務實例繫結。例如,可以實作 CreateServiceInstanceWorkflow
並將其配置為應用程式中的 Spring Bean,以便將額外的功能掛鉤到建立服務實例的請求中。這可能像處理特定的日誌記錄需求一樣簡單。
建置功能性服務代理可能具有挑戰性,但藉由 Spring Cloud Open Service Broker 和 Spring Cloud App Broker 等專案的可用性,我們希望能減少樣板程式碼的數量,並使您能夠更快地建置自己的服務代理。