3. 广告服务
Service Broker 目录提供了一组元数据,用于描述可用服务以及成本和功能等属性。
该目录通过 Service Broker 提供给平台的服务市场/v2/catalog
端点。
Service Broker 可以提供Catalog
或实施服务CatalogService
.
3.1. 提供目录 Bean
您可以通过创建 Spring Bean 并将其贡献给 Spring 来公开 Service Broker 目录
应用程序上下文。
您可以在 Spring 中执行此作@Configuration
类,如下所示:
package com.example.servicebroker;
@Configuration
public class ExampleCatalogConfiguration {
@Bean
public Catalog catalog() {
Plan plan = Plan.builder()
.id("simple-plan")
.name("standard")
.description("A simple plan")
.free(true)
.build();
ServiceDefinition serviceDefinition = ServiceDefinition.builder()
.id("example-service")
.name("example")
.description("A simple example")
.bindable(true)
.tags("example", "tags")
.plans(plan)
.build();
return Catalog.builder()
.serviceDefinitions(serviceDefinition)
.build();
}
}
3.2. Providing a Catalog by Using Properties
You can configure a catalog with Spring Boot externalized configuration within a Java properties file or a YAML file.
The catalog is parsed and made available as a Catalog
bean during autoconfiguration.
The following YAML file configures a catalog:
# Example Spring Boot YAML configuration
spring:
cloud:
openservicebroker:
catalog:
services:
- id: example-service
name: example
description: A simple example
bindable: true
tags:
- example
- tags
plans:
- id: simple-plan
name: standard
description: A simple plan
The following properties file configures a catalog:
# Example Spring Boot properties configuration
spring.cloud.openservicebroker.catalog.services[0].id=example-service
spring.cloud.openservicebroker.catalog.services[0].name=example
spring.cloud.openservicebroker.catalog.services[0].description=A simple example
spring.cloud.openservicebroker.catalog.services[0].bindable=true
spring.cloud.openservicebroker.catalog.services[0].tags[0]=example
spring.cloud.openservicebroker.catalog.services[0].tags[1]=tags
spring.cloud.openservicebroker.catalog.services[0].plans[0].id=simple-plan
spring.cloud.openservicebroker.catalog.services[0].plans[0].name=standard
spring.cloud.openservicebroker.catalog.services[0].plans[0].description=A simple plan
3.3. Implementing a Catalog Service
A service broker can take more control over the catalog by implementing the CatalogService
interface.
This might be required if some details of the catalog metadata need to be read from the environment or from an external data source.
The following example implements the CatalogService
interface:
package com.example.servicebroker;
@Service
public class ExampleCatalogService implements CatalogService {
@Override
public Mono<Catalog> getCatalog() {
return getServiceDefinition("example-service")
.map(serviceDefinition -> Catalog.builder()
.serviceDefinitions(serviceDefinition)
.build());
}
@Override
public Mono<ServiceDefinition> getServiceDefinition(String serviceId) {
return Mono.just(ServiceDefinition.builder()
.id(serviceId)
.name("example")
.description("A simple example")
.bindable(true)
.tags("example", "tags")
.plans(getPlan())
.build());
}
@Override
public Mono<ResponseEntity<Catalog>> getResponseEntityCatalog(HttpHeaders httpHeaders) {
// Use this method to handle catalog caching and ETag support.
// The example below is a basic ETag comparison and response.
if ("useful-etag-value".equals(httpHeaders.getIfNoneMatch())) {
return Mono.just(ResponseEntity
.status(304)
.eTag("useful-etag-value")
.build());
}
else {
return getCatalog()
.map(catalog -> ResponseEntity
.status(200)
.eTag("different-etag-value")
.body(catalog));
}
}
private Plan getPlan() {
return Plan.builder()
.id("simple-plan")
.name("standard")
.description("A simple plan")
.free(true)
.build();
}
}