zoukankan      html  css  js  c++  java
  • Azure Event Hub 技术研究系列2-发送事件到Event Hub

    上篇博文中,我们介绍了Azure Event Hub的一些基本概念和架构:

    Azure Event Hub 技术研究系列1-Event Hub入门篇

    本篇文章中,我们继续深入研究,了解Azure Event Hub的创建、编程SDK,实现将事件发送到云端的Azure Event Hub。

    一、Azure Portal中创建Event Hub

    创建一个新的Event Hub:

    将连接字符串拷贝出来,备用。

    二、通过Event Hub的SDK将事件发送到Event Hub

    新建一个Console工程:EventHubSend

    添加Nuget:

    Microsoft.Azure.EventHubs

    添加关键引用:

    using Microsoft.Azure.EventHubs;
    using System.Text;
    using System.Threading.Tasks;

    添加常量作为事件中心连接字符串和实体路径(单个事件中心名称)

     private static EventHubClient eventHubClient;
    private const string EhConnectionString = "{Event Hubs connection string}";  //第一步拷贝的连接字符串
     private const string EhEntityPath = "{Event Hub path/name}"; //MyEventHub

    新加MainAsync函数

    private static async Task MainAsync(string[] args)
            {            
                var connectionStringBuilder = new EventHubsConnectionStringBuilder(EhConnectionString)
                {
                    EntityPath = EhEntityPath
                };
    
                eventHubClient = EventHubClient.CreateFromConnectionString(connectionStringBuilder.ToString());
    
                await SendEvents(100);
    
                await eventHubClient.CloseAsync();
    
                Console.WriteLine("Press ENTER to exit.");
                Console.ReadLine();
            }

    将100个事件消息发送到EventHub方法:SendEvents

            /// <summary>
            /// 创建100个消息事件,异步发送到EventHub
            /// </summary>
            /// <param name="count">个数</param>
            /// <returns></returns>
            private static async Task SendEvents(int count)
            {
                for (var i = 0; i < count; i++)
                {
                    try
                    {
                        var eventEntity = $"Event {i}";
                        Console.WriteLine($"Sending Event: {eventEntity}");
                        await eventHubClient.SendAsync(new EventData(Encoding.UTF8.GetBytes(eventEntity)));
                    }
                    catch (Exception exception)
                    {
                        Console.WriteLine($"{DateTime.Now} > Exception: {exception.Message}");
                    }
    
                    await Task.Delay(10);
                }
    
                Console.WriteLine($"{count} messages sent.");
            }

    在Main函数中添加:

    static void Main(string[] args)
    {
         MainAsync(args).GetAwaiter().GetResult();
    }

    Run:

     

    发现错误了:The messaging entity 'sb://myeventhubtest.servicebus.chinacloudapi.cn/MyEventHub' could not be found.

    MyEventHub这个是我们在代码中指定的。

    private const string EhEntityPath = "MyEventHub"; //MyEventHub

    这个是否需要在Azure Portal中提前创建好?

    再次Run:

    这次可以了。

    周国庆

    2017/5/17

  • 相关阅读:
    JAVA小技能-之远程调试
    征集系统功能开发进度总结
    征集系统功能开发进度总结
    linux常用的监控命令
    Makefile中 =、:=和 += 的区别
    Linux驱动编译错误:implicit declaration of function “copy_form_user”,“copy_to_user“
    Android 第一个驱动之 word_count(一)
    Ubuntu16.04 默认 gcc、g++ 版本过高会导致Android2.3.4 , Android 4.0.1_r1 编译报错
    降低 make 版本教程
    Tensorflow教程分享:TensorFlow 基础详解
  • 原文地址:https://www.cnblogs.com/tianqing/p/6865046.html
Copyright © 2011-2022 走看看