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

Java 使用连接字符串通过Spring Boot连接到Azure EventHub(类似Kafka),java,spring-boot,azure-eventhub,Java,Spring Boot,Azure Eventhub,我需要使用启用了kafka的Spring Boot连接到事件中心,并且我有连接字符串和名称空间,我应该在哪里连接 我正在使用这种依赖关系 <dependency> <groupId>com.microsoft.azure</groupId> <artifactId>spring-cloud-azure-eventhubs-stream-binder</artifactId> <ve

我需要使用启用了kafka的Spring Boot连接到事件中心,并且我有连接字符串和名称空间,我应该在哪里连接

我正在使用这种依赖关系

 <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>
因为现在它正在抱怨缺少资源组

我应该如何连接到EventHub?

tl;博士

我的问题包含不正确的依赖项,我添加了两个绑定,这是不正确的。当你启动应用程序SpringCloudStream时,你不知道什么是主要的。所以你只需要选择一个

因此,由于我想使用Event Hub,但之前没有使用它的经验,但有使用Kafka的经验,并且Event Hub的工作模式符合Kafka协议,所以我开始这样看。微软的所有教程都不起作用(我很难过)。它们已经过时了

所以,我开始想,如果它是通过卡夫卡协议工作的,也许我可以通过一些配置更改将事件中心线程化为简单的卡夫卡。在谷歌搜索之后,我找到了很多关于如何做的教程

您只需创建常规卡夫卡消费者/制作人。我已经用春云溪做过了

@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配置添加到应用程序。*文件中。您需要获取事件中心的连接字符串

我的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)应该是事件中心地址,类似于
blabla.servicebus.windows.net:9093
(不要忘记端口)。对于事件\u HUB\u CONNECTION\u STRING,您应该指定将连接字符串解析为密码的模块,它应该类似于
org.apache.kafka.common.security.plain.PlainLoginModule required username=“$ConnectionString”password=“{your\u CONNECTION\u STRING}”\

请使用以下配置
spring.cloud.azure.resource group=wingtiptoysresources-spring.cloud.azure.region=West-US-spring.cloud.azure.eventhub.namespace=wingtiptoys-spring.cloud.azure.eventhub.connection-string=xxxxxxx
您有任何更新吗?@JimXu是的,我有,我找到了一个解决方案,然后提出了不同的解决方案。我将简短地发布它供参考:我使用了spring cloud stream“starter”活页夹kafka包,而不是问题中显示的内容,并使其正常工作。谢谢,这对我帮助很大。能够在输入和输出时绑定到不同的队列。
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