[英]Spring cloud stream custom binder not registered. Disables the kafka binder if used @Configuration
[英]Spring Cloud Stream Kafka Binder Configuration update at runtime
我正在使用 Spring 云 stream 以及 Kafka 绑定器使用 SASL 连接到 Kafka 集群。 SASL 配置如下所示:
spring.cloud.stream.kafka.binder.configuration.sasl.mechanism=SCRAM-SHA-512
spring.cloud.stream.kafka.binder.configuration.sasl.jaas.config= .... required username="..." password="..."
spring.cloud.stream.kafka.binder.configuration.security.protocol=SASL_SSL
我想以编程方式/在运行时更新用户名和密码,如何在 Spring Cloud Stream 中使用 Spring Kafka 活页夹来更新用户名和密码?
旁注:使用BinderFactory
我可以获得对具有KafkaBinderConfigurationProperties
的KafkaMessageChannelBinder
的引用,在其configuration
hashmap 我可以看到这些配置,但我想知道如何在运行时更新配置,以便这些更改也反映在连接中?
@Autowired
BinderFactory binderFactory
....
public void foo()
{
KafkaMessageChannelBinder k = (KafkaMessageChannelBinder)binderFactory.getBinder(null, MessageChannel.class);
// Using debugger I inspected k.configurationProperties.configuration which has the SASL properties I need to update
}
可以使用配置提供 jaas 用户名和密码,这也意味着可以在运行时使用相同的属性覆盖它们。
在运行时,您可以覆盖application.properties
中设置的值。 例如,如果您正在使用java -jar
运行应用程序,则可以简单地将属性与其一起传递: spring.cloud.stream.kafka.binder.jaas.options.username
. 然后这个新值将在应用程序运行期间生效。
I came across this problem yesterday and spent about 3-4 hours in order to figure out how to programmatically update the username and password in Spring Cloud Stream using Spring Kafka binders as one cannot/should not store passwords inside Git.(Spring Boot Version 2.5 .2)覆盖bean KafkaBinderConfigurationProperties 的作品。
@Bean
@Primary
public KafkaBinderConfigurationProperties kafkaBinderConfigurationProperties(KafkaBinderConfigurationProperties properties) {
String saslJaasConfigString = "org.apache.kafka.common.security.scram.ScramLoginModule required username=${USERNAME_FROM_EXTERNAL_SYSTEM_LIKE_VAULT} password=${PASSWORD_FROM_EXTERNAL_SYSTEM_LIKE_VAULT}"
Map<String, String> configMap = properties.getConfiguration();
configMap.put(SaslConfigs.SASL_JAAS_CONFIG, saslJaasConfigString);
return properties;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.