此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 spring-cloud-stream 4.1.4! |
绑定可视化和控制
Spring Cloud Stream 支持通过 Actuator 端点以及编程方式对 Bindings 进行可视化和控制。
编程方式
从 3.1 版本开始,我们公开了org.springframework.cloud.stream.binding.BindingsLifecycleController
它被注册为 Bean 并且
injected 可用于控制单个绑定的生命周期
例如,查看其中一个测试用例中的片段。如您所见,我们检索BindingsLifecycleController
从 Spring 应用程序上下文并执行单个方法来控制echo-in-0
捆绑。。
BindingsLifecycleController bindingsController = context.getBean(BindingsLifecycleController.class);
Binding binding = bindingsController.queryState("echo-in-0");
assertThat(binding.isRunning()).isTrue();
bindingsController.changeState("echo-in-0", State.STOPPED);
//Alternative way of changing state. For convenience we expose start/stop and pause/resume operations.
//bindingsController.stop("echo-in-0")
assertThat(binding.isRunning()).isFalse();
驱动器
由于 actuator 和 web 是可选的,因此您必须首先添加其中一个 web 依赖项,然后手动添加 actuator 依赖项。 下面的示例演示如何为 Web 框架添加依赖项:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
以下示例演示如何为 WebFlux 框架添加依赖项:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
您可以按如下方式添加 Actuator 依赖项:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
要在 Cloud Foundry 中运行 Spring Cloud Stream 2.0 应用程序,您必须添加spring-boot-starter-web 和spring-boot-starter-actuator 添加到 Classpath 中。否则,
由于运行状况检查失败,应用程序将无法启动。 |
您还必须启用bindings
actuator endpoints 的 Endpoints 的调用:--management.endpoints.web.exposure.include=bindings
.
满足这些先决条件后。当应用程序启动时,您应该会在日志中看到以下内容:
: Mapped "{[/actuator/bindings/{name}],methods=[POST]. . . : Mapped "{[/actuator/bindings],methods=[GET]. . . : Mapped "{[/actuator/bindings/{name}],methods=[GET]. . .
要可视化当前绑定,请访问以下 URL:<host>:<port>/actuator/bindings
或者,要查看单个绑定,请访问类似于以下内容的 URL 之一:<host>:<port>/actuator/bindings/<bindingName>;
您还可以通过发布到同一 URL 来停止、启动、暂停和恢复单个绑定,同时提供state
参数为 JSON,如以下示例所示:
curl -d '{"state":"STOPPED"}' -H "Content-Type: application/json" -X POST http://<host>:<port>/actuator/bindings/myBindingName curl -d '{"state":"STARTED"}' -H "Content-Type: application/json" -X POST http://<host>:<port>/actuator/bindings/myBindingName curl -d '{"state":"PAUSED"}' -H "Content-Type: application/json" -X POST http://<host>:<port>/actuator/bindings/myBindingName curl -d '{"state":"RESUMED"}' -H "Content-Type: application/json" -X POST http://<host>:<port>/actuator/bindings/myBindingName
PAUSED 和RESUMED 仅当相应的 Binder 及其底层技术支持时才有效。否则,您会在日志中看到警告消息。
目前,只有 Kafka 和 [Solace](github.com/SolaceProducts/solace-spring-cloud/tree/master/solace-spring-cloud-starters/solace-spring-cloud-stream-starter#consumer-bindings-pauseresume) Binders 支持PAUSED 和RESUMED 国家。 |
清理敏感数据
使用 binding actuator 端点时,有时清理任何敏感数据(例如用户凭证、有关 SSL 密钥的信息等)至关重要。
为了实现这一点,最终用户应用程序可以提供SanitizingFunction
从 Spring Boot 作为应用程序中的 Bean 进行。
下面是一个示例,用于在为 Apache Kafka 的sasl.jaas.config
财产。
@Bean
public SanitizingFunction sanitizingFunction() {
return sanitizableData -> {
if (sanitizableData.getKey().equals("sasl.jaas.config")) {
return sanitizableData.withValue("data-scrambled!!");
}
else {
return sanitizableData;
}
};
}