Spring Cloud Stream 支持通过 Actuator 端点以及编程方式对绑定进行可视化和控制。Spring中文文档

程序化方式

从 3.1 版开始,我们公开了注册为 bean 和一次 注入可用于控制单个结合的生命周期org.springframework.cloud.stream.binding.BindingsLifecycleControllerSpring中文文档

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

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

驱动器

由于执行器和 Web 是可选的,因此您必须首先添加其中一个 Web 依赖项,并手动添加执行器依赖项。 下面的示例演示如何为 Web 框架添加依赖项:Spring中文文档

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

以下示例演示如何为 WebFlux 框架添加依赖项:Spring中文文档

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

您可以按如下方式添加 Actuator 依赖项:Spring中文文档

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

您还必须通过设置以下属性来启用执行器端点:。bindings--management.endpoints.web.exposure.include=bindingsSpring中文文档

一旦满足这些先决条件。应用程序启动时,应在日志中看到以下内容:Spring中文文档

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

若要可视化当前绑定,请访问以下 URL:<host>:<port>/actuator/bindingsSpring中文文档

或者,若要查看单个绑定,请访问类似于以下内容的 URL 之一:<host>:<port>/actuator/bindings/<bindingName>;Spring中文文档

还可以通过发布到同一 URL 来停止、启动、暂停和恢复单个绑定,同时提供 JSON 格式的参数,如以下示例所示:stateSpring中文文档

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

清理敏感数据

使用绑定执行器端点时,有时必须清理任何敏感数据,例如用户凭据、有关 SSL 密钥的信息等。 为了实现这一点,最终用户应用程序可以在应用程序中提供 from Spring Boot 作为 bean。 下面是一个示例,用于在为 Apache Kafka 的属性提供值时对数据进行加扰。SanitizingFunctionsasl.jaas.configSpring中文文档

@Bean
public SanitizingFunction sanitizingFunction() {
	return sanitizableData -> {
		if (sanitizableData.getKey().equals("sasl.jaas.config")) {
			return sanitizableData.withValue("data-scrambled!!");
		}
		else {
			return sanitizableData;
		}
	};
}
要在 Cloud Foundry 中运行 Spring Cloud Stream 2.0 应用,必须将 和 添加到类路径中。否则, 由于运行状况检查失败,应用程序将无法启动。spring-boot-starter-webspring-boot-starter-actuator
PAUSED并且只有在相应的粘合剂及其底层技术支持时才起作用。否则,您将在日志中看到警告消息。 目前,只有 Kafka 和 [Solace](github.com/SolaceProducts/solace-spring-cloud/tree/master/solace-spring-cloud-starters/solace-spring-cloud-stream-starter#consumer-bindings-pauseresume) 活页夹支持 and 状态。RESUMEDPAUSEDRESUMED