3. 广告服务

Service Broker 目录提供了一组元数据,用于描述可用服务以及成本和功能等属性。 该目录通过 Service Broker 终端节点提供给平台的服务市场。/v2/catalogspring-doc.cn

服务代理可以提供 Catalog 类型的 Spring Bean,也可以实现服务 CatalogServicespring-doc.cn

3.1. 提供目录 Bean

您可以通过创建 Spring Bean 并将其贡献给 Spring 来公开 Service Broker 目录 应用程序上下文。 你可以在 Spring 类中执行此操作,如下所示:@Configurationspring-doc.cn

package com.example.servicebroker;

import org.springframework.cloud.servicebroker.model.catalog.Catalog;
import org.springframework.cloud.servicebroker.model.catalog.Plan;
import org.springframework.cloud.servicebroker.model.catalog.ServiceDefinition;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@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. 使用属性提供目录

您可以在 Java 属性文件或 YAML 文件中使用 Spring Boot 外部化配置配置目录。 在自动配置期间,将解析该目录并将其作为 Bean 提供。Catalogspring-doc.cn

以下 YAML 文件配置目录:spring-doc.cn

# 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

以下属性文件配置目录:spring-doc.cn

# 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. 实施 Catalog 服务

Service Broker 可以通过实现接口来对目录进行更多控制。 如果需要从环境或外部数据源读取目录元数据的某些详细信息,则可能需要这样做。CatalogServicespring-doc.cn

以下示例实现该接口:CatalogServicespring-doc.cn

package com.example.servicebroker;

import reactor.core.publisher.Mono;

import org.springframework.cloud.servicebroker.model.catalog.Catalog;
import org.springframework.cloud.servicebroker.model.catalog.Plan;
import org.springframework.cloud.servicebroker.model.catalog.ServiceDefinition;
import org.springframework.cloud.servicebroker.service.CatalogService;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

@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();
	}

}