|
对于最新稳定版本,请使用 spring-cloud-stream 5.0.0! |
绑定可视化与控制
Spring Cloud Stream 支持通过执行器端点以及编程方式对绑定进行可视化和控制。
程序化方式
自3.1版本起,我们暴露了org.springframework.cloud.stream.binding.BindingsLifecycleController该标记为豆子,一旦注入,可用于控制单个绑定的生命周期
例如,查看其中一个测试用例中的片段。如你所见,我们检索到BindingsLifecycleController从Spring应用上下文中执行个别方法以控制 的生命周期回声-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(..)作定义是为了确保你获得配置属性类的正确类型。
| 根据你更改的属性类型,可能需要重新启动绑定才能生效(前面提到的)。 |
驱动器
由于执行器和Web是可选的,你必须先添加一个Web依赖,并手动添加执行器依赖。以下示例展示了如何为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>
你可以按以下方式添加执行器依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
要在Cloud Foundry中运行Spring Cloud Stream 2.0应用,您必须添加Spring Boot启动网和Spring-启动-执行器迁移到类路径。否则,应用程序因健康检查失败而无法启动。 |
你还必须启用绑定执行器端点通过设置以下属性实现:--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>/执行器/bindings
另外,要查看单个绑定,可以访问以下类似的URL之一:<host>:<port>/actuator/bindings/<bindingName>;
你也可以通过发布到相同网址并提供州参数为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
暂停和恢复只有当对应的活页夹及其底层技术支持时才有效。否则,日志中会显示警告信息。目前,只有 Kafka 和 [Solace](github.com/SolaceProducts/solace-spring-cloud/tree/master/solace-spring-cloud-starters/solace-spring-cloud-stream-starter#consumer-bindings-pauseresume) 活页夹支持暂停和恢复国家。 |
净化敏感数据
在使用绑定执行器端点时,有时必须对敏感数据进行净化,如用户凭证、SSL密钥信息等。为此,终端用户应用程序可以提供消毒功能在应用程序中以豆子形式从Spring Boot中获取数据。以下是在为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;
}
};
}