gpt4 book ai didi

java - 使用连接字符串通过 Spring Boot 连接到 Azure EventHub(类似 Kafka)

转载 作者:行者123 更新时间:2023-12-04 12:33:58 25 4
gpt4 key购买 nike

我需要使用 Spring Boot 连接到启用了 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 并创建 auth 文件,但是我得到了我应该使用的连接字符串,所以有什么方法可以指定
只有这样的命名空间的连接字符串:
spring.cloud.azure.eventhub.connection-string
spring.cloud.azure.eventhub.namespace
因为现在提示缺少资源组。
我应该如何连接到 EventHub?

最佳答案

tl;博士
我的问题包含不正确的依赖项,我添加了两个不正确的活页夹。当您启动应用程序时,spring cloud stream 不知道什么是主要的。所以你只需要选择一个。
因此,因为我想使用 Event Hub,但之前没有使用它的经验,但有 Kafka 的经验,并且 Event Hub 具有通过 Kafka 协议(protocol)工作的模式,所以我开始以这种方式看待。 Microsoft 的所有教程都不起作用(让我感到难过)。它们已经过时了。
所以,我开始考虑如果它是通过 Kafka 协议(protocol)工作的,也许我可以通过一些配置更改将 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}"\

关于java - 使用连接字符串通过 Spring Boot 连接到 Azure EventHub(类似 Kafka),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63226387/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com