使用连接字符串 Spring 引导连接到 Azure EventHub(类似 Kafka)
Connect to Azure EventHub(Kafka like) with Spring Boot using connection string
我需要通过 Spring 引导连接到启用了 kafka 的事件中心,我有连接字符串和名称 space 我应该在哪里连接。
我正在使用此类依赖项
<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 文件,但是我得到了我应该使用的连接字符串,所以有什么方法可以指定
只有名称为 space 的连接字符串如下:
spring.cloud.azure.eventhub.connection-string
spring.cloud.azure.eventhub.namespace
因为现在抱怨缺少资源组。
我应该如何连接到 EventHub?
tl;博士
我的问题包含不正确的依赖项,我添加了两个不正确的活页夹。当你启动应用程序时 spring 云流不知道什么是主要的。所以你只需要选择一个。
因此,由于我想使用 Event Hub,但之前没有使用它的经验,但有使用 Kafka 的经验,并且 Event Hub 具有通过 Kafka 协议工作的模式,所以我开始以这种方式寻找。 Microsoft 的所有教程都不起作用(我很伤心)。它们已经过时了。
所以,我开始考虑如果它通过 Kafka 协议工作,也许我可以通过一些配置更改将事件中心作为简单的 Kafka 线程化。谷歌搜索后,我发现了很多教程如何去做。
您只需要创建常规的 Kafka consumer/producer。我用 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}"\
我需要通过 Spring 引导连接到启用了 kafka 的事件中心,我有连接字符串和名称 space 我应该在哪里连接。
我正在使用此类依赖项
<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 文件,但是我得到了我应该使用的连接字符串,所以有什么方法可以指定 只有名称为 space 的连接字符串如下:
spring.cloud.azure.eventhub.connection-string
spring.cloud.azure.eventhub.namespace
因为现在抱怨缺少资源组。
我应该如何连接到 EventHub?
tl;博士
我的问题包含不正确的依赖项,我添加了两个不正确的活页夹。当你启动应用程序时 spring 云流不知道什么是主要的。所以你只需要选择一个。
因此,由于我想使用 Event Hub,但之前没有使用它的经验,但有使用 Kafka 的经验,并且 Event Hub 具有通过 Kafka 协议工作的模式,所以我开始以这种方式寻找。 Microsoft 的所有教程都不起作用(我很伤心)。它们已经过时了。
所以,我开始考虑如果它通过 Kafka 协议工作,也许我可以通过一些配置更改将事件中心作为简单的 Kafka 线程化。谷歌搜索后,我发现了很多教程如何去做。
您只需要创建常规的 Kafka consumer/producer。我用 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}"\