4. 服务实例日志记录

您可以将 Service Broker 配置为与 Service instance logs CLI 插件兼容,以便跟踪和流式传输后备应用程序的日志。spring-doc.cadn.net.cn

如果您使用 Gradle,请在应用程序的build.gradle文件:spring-doc.cadn.net.cn

dependencies {
    api 'spring-cloud-starter-app-broker-logging:2.4.0'
}

如果您使用 Maven,请在应用程序的pom.xml文件:spring-doc.cadn.net.cn

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-app-broker-logging</artifactId>
        <version>2.4.0</version>
    </dependency>
</dependencies>

然后,包括serviceInstanceLogsEndpoint在 Catalog 元数据属性中:spring-doc.cadn.net.cn

spring:
  cloud:
    openservicebroker:
      catalog:
        services:
          - id: "service-id"
            name: "service-name"
            metadata:
              properties:
                serviceInstanceLogsEndpoint: https://scg-service-broker.system.domain.com/logs/

最后,为ApplicationIdsProvider为了检索给定服务实例 ID 的后备应用程序 ID,例如:spring-doc.cadn.net.cn

/*
 * Copyright 2016-2024 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.cloud.appbroker.acceptance.logging;

import org.cloudfoundry.client.CloudFoundryClient;
import org.cloudfoundry.client.v3.applications.ApplicationResource;
import org.cloudfoundry.client.v3.applications.ListApplicationsRequest;
import org.cloudfoundry.operations.CloudFoundryOperations;
import org.cloudfoundry.operations.spaces.GetSpaceRequest;
import org.cloudfoundry.operations.spaces.SpaceDetail;
import reactor.core.publisher.Flux;

import org.springframework.cloud.appbroker.logging.ApplicationIdsProvider;
import org.springframework.stereotype.Component;

@Component
class BackingApplicationIdsProvider implements ApplicationIdsProvider {

	private final CloudFoundryClient cloudFoundryClient;

	private final CloudFoundryOperations cloudFoundryOperations;

	BackingApplicationIdsProvider(CloudFoundryClient cloudFoundryClient,
			CloudFoundryOperations cloudFoundryOperations) {
		this.cloudFoundryClient = cloudFoundryClient;
		this.cloudFoundryOperations = cloudFoundryOperations;
	}

	@Override
	public Flux<String> getApplicationIds(String serviceInstanceId) {
		return this.cloudFoundryOperations.spaces()
			.get(GetSpaceRequest.builder().name(serviceInstanceId).build())
			.map(SpaceDetail::getId)
			.flatMap((spaceId) -> this.cloudFoundryClient.applicationsV3()
				.list(ListApplicationsRequest.builder().spaceIds(spaceId).build()))
			.flatMapMany((listApplicationsResponse) -> Flux.fromIterable(listApplicationsResponse.getResources())
				.map(ApplicationResource::getId));
	}

}