简体   繁体   English

使用连接字符串通过 Spring 引导连接到 Azure EventHub(Kafka like)

[英]Connect to Azure EventHub(Kafka like) with Spring Boot using connection string

I need to connect to event hub with enabled kafka with Spring Boot, and I have connection string and name space where should I connect.我需要使用 Spring 引导连接到启用了 kafka 的事件中心,并且我有连接字符串和名称空间应该在哪里连接。

I'm using such dependencies我正在使用这样的依赖项

 <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>

I found a tutorial where I need to login into azure from my local machine with az login and create auth file, BUT I was provided with connection string which should I use, so is there any way to specify ONLY connection string with namespace like this:我找到了一个教程,我需要使用 az login 从我的本地计算机登录 azure 并创建身份验证文件,但是我提供了我应该使用的连接字符串,所以有什么方法可以只指定具有如下命名空间的连接字符串:

spring.cloud.azure.eventhub.connection-string
spring.cloud.azure.eventhub.namespace

Because now it is complaining that is is missing resource-group.因为现在它抱怨缺少资源组。

How should I connect to EventHub?我应该如何连接到 EventHub?

tl;dr tl;博士

My question contains incorrect dependencies, I've added two binders, which incorrect.我的问题包含不正确的依赖项,我添加了两个不正确的活页夹。 When you start app spring cloud stream don't know what is primary.当您启动应用程序 spring 云 stream 不知道什么是主要的。 So you need to choose only one.所以你只需要选择一个。

So as I want to work with Event Hub, but had not previous experience with it, but had experience with Kafka and Event Hub has mode to work by Kafka protocol I started to look in that way.因此,由于我想使用 Event Hub,但以前没有使用它的经验,但有使用 Kafka 的经验,并且 Event Hub 具有通过 Kafka 协议工作的模式,我开始以这种方式看待。 All tutorial from Microsoft are not working (sad for me).微软的所有教程都不起作用(对我来说很难过)。 They are outdated.它们已经过时了。

So, I started to think if it is working by Kafka protocol, maybe I can thread Event Hub as simple Kafka with some configuration changes.所以,我开始考虑如果它是通过 Kafka 协议工作的,也许我可以通过一些配置更改将 Event Hub 线程化为简单的 Kafka。 After googling I found a lot of tutorial how to do it.谷歌搜索后,我发现了很多教程如何做到这一点。

All you need is to create regular Kafka consumer/producer.您所需要的只是创建常规的 Kafka 消费者/生产者。 I've done it with Spring Cloud Stream我已经用 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());
    }
}

And then just add proper jaas configuration into application.* file.然后只需将正确的 jaas 配置添加到 application.* 文件中。 You need to get connection string for your Event Hub您需要获取事件中心的连接字符串

My yaml file:我的 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

One important thing EVENT_HUB_KAFKA_BROKER should be Event Hub address, something like blablabla.servicebus.windows.net:9093 (don't forget port).一件重要的事情 EVENT_HUB_KAFKA_BROKER 应该是事件中心地址,例如blablabla.servicebus.windows.net:9093 (不要忘记端口)。 For EVENT_HUB_CONNECTION_STRING you hould specify module which will be parsing connection string as password and it should be something like org.apache.kafka.common.security.plain.PlainLoginModule required username="$ConnectionString" password="{your_connection_string}"\对于 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM