[英]Connect to Azure EventHub(Kafka like) with Spring Boot using connection string
我需要使用 Spring 引导连接到启用了 kafka 的事件中心,并且我有连接字符串和名称空间应该在哪里连接。
我正在使用这样的依赖项
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>spring-cloud-azure-eventhubs-stream-binder</artifactId>
<version>1.2.7</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-binder-kafka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream</artifactId>
</dependency>
我找到了一个教程,我需要使用 az login 从我的本地计算机登录 azure 并创建身份验证文件,但是我提供了我应该使用的连接字符串,所以有什么方法可以只指定具有如下命名空间的连接字符串:
spring.cloud.azure.eventhub.connection-string
spring.cloud.azure.eventhub.namespace
因为现在它抱怨缺少资源组。
我应该如何连接到 EventHub?
tl;博士
我的问题包含不正确的依赖项,我添加了两个不正确的活页夹。 当您启动应用程序 spring 云 stream 不知道什么是主要的。 所以你只需要选择一个。
因此,由于我想使用 Event Hub,但以前没有使用它的经验,但有使用 Kafka 的经验,并且 Event Hub 具有通过 Kafka 协议工作的模式,我开始以这种方式看待。 微软的所有教程都不起作用(对我来说很难过)。 它们已经过时了。
所以,我开始考虑如果它是通过 Kafka 协议工作的,也许我可以通过一些配置更改将 Event Hub 线程化为简单的 Kafka。 谷歌搜索后,我发现了很多教程如何做到这一点。
您所需要的只是创建常规的 Kafka 消费者/生产者。 我已经用 Spring Cloud Stream 完成了
@Slf4j
@EnableBinding(Sink.class)
public class KafkaSink {
@StreamListener(Sink.INPUT)
public void consumerMessage(TestMessage testMessage) {
log.info("{}", testMessage);
}
}
@Component
@EnableBinding(Source.class)
public class KafkaSource {
private final MessageChannel output;
@Autowired
public KafkaSource(MessageChannel output) {
this.output = output;
}
public void send(TestMessage testMessage) {
output.send(MessageBuilder.withPayload(testMessage).build());
}
}
然后只需将正确的 jaas 配置添加到 application.* 文件中。 您需要获取事件中心的连接字符串
我的 yaml 文件:
spring:
cloud:
stream:
bindings:
input:
destination: my-topic
output:
destination: my-topic
kafka:
binder:
auto-create-topics: true
brokers: ${EVENT_HUB_KAFKA_BROKER}
configuration:
sasl:
jaas:
config: ${EVENT_HUB_CONNECTION_STRING}
mechanism: PLAIN
security:
protocol: SASL_SSL
一件重要的事情 EVENT_HUB_KAFKA_BROKER 应该是事件中心地址,例如blablabla.servicebus.windows.net:9093
(不要忘记端口)。 对于 EVENT_HUB_CONNECTION_STRING,您应该指定将连接字符串解析为密码的模块,它应该类似于org.apache.kafka.common.security.plain.PlainLoginModule required username="$ConnectionString" password="{your_connection_string}"\
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.