领先一步
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 等项目的推出,我们希望能够减少样板代码量,并使您能够更快地构建自己的服务代理。