绑定可视化与控制

Spring Cloud Stream 支持通过 Actuator 端点和编程式方式进行绑定可视化与控制。

编程式方式

自版本 3.1 起,我们暴露了 org.springframework.cloud.stream.binding.BindingsLifecycleController 类,它被注册为一个 bean,注入后可用于控制各个绑定的生命周期。

例如,请看测试用例中的片段。如你所见,我们从 Spring 应用上下文获取 BindingsLifecycleController,并执行各个方法来控制 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();

此外,自版本 4.2 起,你还可以访问消费者和生产者的配置属性,以便更动态地管理其值。你可以根据绑定名称从 BindingsLifecycleController 请求这些属性。例如:

KafkaConsumerProperties properties = controller.getExtensionProperties("log-in-0”);
RabbitProducerProperties properties = controller.getExtensionProperties(“log-out-0”);

定义 getExtensionProperties(..) 操作是为了确保你获取到正确类型的配置属性类。

根据你更改的属性类型,你可能需要重新启动绑定才能使其生效(如前所述)

Actuator

由于 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-webspring-boot-starter-actuator 添加到 classpath 中。否则,由于健康检查失败,应用将无法启动。

你还必须通过设置以下属性来启用 bindings Actuator 端点:--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 发送 POST 请求并提供 JSON 格式的 state 参数来停止、启动、暂停和恢复单个绑定,如下例所示:

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
PAUSEDRESUMED 仅在相应的 binder 及其底层技术支持时才起作用。否则,你会在日志中看到警告消息。目前,只有 Kafka 和 [Solace](github.com/SolaceProducts/solace-spring-cloud/tree/master/solace-spring-cloud-stream-starter#consumer-bindings-pauseresume) binder 支持 PAUSEDRESUMED 状态。

敏感数据脱敏

在使用绑定 actuator 端点时,对用户凭据、SSL 密钥信息等敏感数据进行脱敏有时至关重要。为此,最终用户应用可以在应用中将 Spring Boot 提供的 SanitizingFunction 作为 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;
		}
	};
}