此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 spring-cloud-stream 4.1.4spring-doc.cn

绑定可视化和控制

Spring Cloud Stream 支持通过 Actuator 端点以及编程方式对 Bindings 进行可视化和控制。spring-doc.cn

编程方式

从 3.1 版本开始,我们公开了 which 被注册为 bean 和一次 injected 可用于控制单个绑定的生命周期org.springframework.cloud.stream.binding.BindingsLifecycleControllerspring-doc.cn

例如,查看其中一个测试用例中的片段。正如你所看到的,我们从 Spring 应用程序上下文中检索并执行单独的方法来控制绑定的生命周期。BindingsLifecycleControllerecho-in-0spring-doc.cn

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 框架添加依赖项:spring-doc.cn

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-web</artifactId>
</dependency>

以下示例演示如何为 WebFlux 框架添加依赖项:spring-doc.cn

<dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-webflux</artifactId>
</dependency>

您可以按如下方式添加 Actuator 依赖项:spring-doc.cn

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
要在 Cloud Foundry 中运行 Spring Cloud Stream 2.0 应用程序,必须将 and 添加到 Classpath。否则, 由于运行状况检查失败,应用程序将无法启动。spring-boot-starter-webspring-boot-starter-actuator

您还必须通过设置以下属性来启用 Actuator 端点: 。bindings--management.endpoints.web.exposure.include=bindingsspring-doc.cn

满足这些先决条件后。当应用程序启动时,您应该会在日志中看到以下内容:spring-doc.cn

: Mapped "{[/actuator/bindings/{name}],methods=[POST]. . .
: Mapped "{[/actuator/bindings],methods=[GET]. . .
: Mapped "{[/actuator/bindings/{name}],methods=[GET]. . .

要可视化当前绑定,请访问以下 URL:<host>:<port>/actuator/bindingsspring-doc.cn

或者,要查看单个绑定,请访问类似于以下内容的 URL 之一:<host>:<port>/actuator/bindings/<bindingName>;spring-doc.cn

您还可以通过发布到同一 URL 来停止、启动、暂停和恢复单个绑定,同时以 JSON 形式提供参数,如以下示例所示:statespring-doc.cn

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并且仅在相应的 Binder 及其底层技术支持时工作。否则,您会在日志中看到警告消息。 目前,只有 Kafka 和 [Solace](github.com/SolaceProducts/solace-spring-cloud/tree/master/solace-spring-cloud-starters/solace-spring-cloud-stream-starter#consumer-bindings-pauseresume) 绑定器支持 and 状态。RESUMEDPAUSEDRESUMED

清理敏感数据

使用 binding actuator 端点时,有时清理任何敏感数据(例如用户凭证、有关 SSL 密钥的信息等)至关重要。 为了实现这一点,最终用户应用程序可以在应用程序中提供 from Spring Boot 作为 Bean。 下面是一个在为 Apache Kafka 的属性提供值时对数据进行加扰的示例。SanitizingFunctionsasl.jaas.configspring-doc.cn

@Bean
public SanitizingFunction sanitizingFunction() {
	return sanitizableData -> {
		if (sanitizableData.getKey().equals("sasl.jaas.config")) {
			return sanitizableData.withValue("data-scrambled!!");
		}
		else {
			return sanitizableData;
		}
	};
}