4. 服务实例日志记录
您可以配置服务代理以兼容服务实例日志 CLI 插件,从而跟踪并流式传输支持应用程序的日志。
如果使用 Gradle,请在应用程序的 build.gradle 文件中包含以下内容:
dependencies {
api 'spring-cloud-starter-app-broker-logging:2.4.0'
}
如果使用 Maven,请在应用程序的 pom.xml 文件中包含以下内容:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-app-broker-logging</artifactId>
<version>2.4.0</version>
</dependency>
</dependencies>
然后,在目录元数据属性中包含 serviceInstanceLogsEndpoint:
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,例如:
/*
* 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;
@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));
}
}