发送消息到Azure事件中心的简单方法 – 第二种方法

huangapple go评论85阅读模式
英文:

Simple way to send message to Azure Event Hub - second approach

问题

我的第一个尝试是使用 com.microsoft.azure:azure-eventhubs:3.2.0 依赖项,但我遇到了以下问题:https://stackoverflow.com/questions/63039023/client-hangs-when-calling-azure-event-hub-and-facing-connection-error

所以我的第二种方法可以是:

  • 要么使用另一个依赖项(我也需要使用OAuth2) -> 不幸的是,我还没有找到任何一个
  • 要么不使用依赖项,而是自己编写发送消息的代码 -> 这可能是一个大任务

您能否推荐一个支持OAuth2的库?
或者一个支持通过OAuth2发送消息到Event Hub的示例代码?
或者第三种方法...?

谢谢,
V.

英文:

My first approach was to use the com.microsoft.azure:azure-eventhubs:3.2.0 dependency but I had the following problem: https://stackoverflow.com/questions/63039023/client-hangs-when-calling-azure-event-hub-and-facing-connection-error

So my 2nd approach can be

  • either to use another dependency (I need to use OAuth2 too) -> unfortunately I haven't found any
  • or don't use a dependency but code the message sending myself -> it might be a big task

Could you please recommend me library that supports OAuth2?
Or a sample code which supports message sending to Event Hub over AMQP with OAuth2?
Or a 3rd approach ...?

Thanks,
V.

答案1

得分: 1

关于您的挂起问题,我在原始线程Client hangs when calling Azure Event Hub and facing connection error中发布了一个新的答案。那应该解决您的问题

现在回到这个问题,主要您应该遵循最新的教程(使用生产者/消费者模式使用Event Hub v5 SDK)。

import com.azure.messaging.eventhubs.*;

public class Sender {
    public static void main(String[] args) {
        final String connectionString = "EVENT HUBS NAMESPACE CONNECTION STRING";
        final String eventHubName = "EVENT HUB NAME";

        // 使用命名空间连接字符串和事件中心名称创建生产者
        EventHubProducerClient producer = new EventHubClientBuilder()
            .connectionString(connectionString, eventHubName)
            .buildProducerClient();

        // 准备要发送到事件中心的事件批次    
        EventDataBatch batch = producer.createBatch();
        batch.tryAdd(new EventData("First event"));
        batch.tryAdd(new EventData("Second event"));
        batch.tryAdd(new EventData("Third event"));
        batch.tryAdd(new EventData("Fourth event"));
        batch.tryAdd(new EventData("Fifth event"));

        // 将事件批次发送到事件中心
        producer.send(batch);

        // 关闭生产者
        producer.close();
    }
}

关于OAuth2的问题,完全可以利用AAD身份验证方法,而不是共享访问签名(默认的连接字符串方法)。您可以按照此教程进行托管身份验证方案,或者按照此教程进行基于自定义AAD应用程序的身份验证。

关于OAuth2库的建议,Azure世界中的第一方选择将是MSAL,以实现无缝集成和支持。

关于通过自定义代码编写整个内容的问题,虽然在技术上是可能的,但老实说,由于以下原因,投资于这个庞大的任务实际上是非常不切实际的:

  1. 为什么要重新发明轮子?
  2. 这将增加您的维护工作负担,因为这个自定义代码几乎只涉及技术连接,几乎不为业务增加任何价值。
  3. 您几乎将自己解决任何问题或未来兼容性的问题。
  4. 在您的代码中添加所有低级别的AMQP和OAuth处理会为您的团队增加不必要的负担。
  5. ..最重要的是,这将花费金钱和时间 发送消息到Azure事件中心的简单方法 – 第二种方法

使用标准库不仅可以节省大量工作和金钱,还可以确保可重用性,并得到创建者和社区的支持。

英文:

Regarding your hang issue, I posted a new answer in the original thread Client hangs when calling Azure Event Hub and facing connection error. That should fix your issue.

Now coming back to this question, primarily you should follow the latest tutorial (uses Event Hub v5 sdk using Producer/Consumer pattern).

import com.azure.messaging.eventhubs.*;

public class Sender {
    public static void main(String[] args) {
        final String connectionString = "EVENT HUBS NAMESPACE CONNECTION STRING";
        final String eventHubName = "EVENT HUB NAME";

        // create a producer using the namespace connection string and event hub name
        EventHubProducerClient producer = new EventHubClientBuilder()
            .connectionString(connectionString, eventHubName)
            .buildProducerClient();

        // prepare a batch of events to send to the event hub    
        EventDataBatch batch = producer.createBatch();
        batch.tryAdd(new EventData("First event"));
        batch.tryAdd(new EventData("Second event"));
        batch.tryAdd(new EventData("Third event"));
        batch.tryAdd(new EventData("Fourth event"));
        batch.tryAdd(new EventData("Fifth event"));

        // send the batch of events to the event hub
        producer.send(batch);

        // close the producer
        producer.close();
    }
}

Regarding the question on OAuth2, it is very much possible to leverage AAD authentication approach instead of Shared Access Signature (the default connection string approach). You can follow this tutorial for Managed Identity scenario OR this tutorial for custom AAD application based auth.

Regarding the recommendation on OAuth2 library, the first party choice in the Azure world would be MSAL for seamless integration and support.

Regarding the question on writing the whole thing by custom code, while it's technically possible, but honestly it's super impractical to invest on this mammoth task due to following reasons:

  1. Why to reinvent the wheel?
  2. It will increase your maintenance headache for a hell lot of custom code which does only technical wiring, but hardly adds any value to the business.
  3. You would be pretty much on your own to figure out any issue or future compatibility.
  4. Adding all the low level AMQP and OAuth handling in your code would add unnecessary burden to your team to master those.
  5. ..and most importantly it would cost Money and Time 发送消息到Azure事件中心的简单方法 – 第二种方法

Using standard libraries not only saves you from lot of efforts and money, but ensures reusability, and support from both the creator and communities.

huangapple
  • 本文由 发表于 2020年8月6日 20:32:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63283712.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定