设为首页 收藏本站
查看: 1099|回复: 0

[经验分享] Azure service bus Topic基本用法

[复制链接]

尚未签到

发表于 2017-6-30 08:36:37 | 显示全部楼层 |阅读模式
  我们在升级一个POS系统的时候,决定使用微软公有云计算平台下的Azure ServiceBus 进行POS客户端与服务器的交互。
  本文主要时作者在学习使用 Azure SDK for .NET 操作由世纪互联运营的 中国区Azure 上的 Service Bus。

  目录
一、安装AzureServiceBus程序集
  二、在Portal创建命名空间
  三、通过代码创建Topic
  四、通过代码创建订阅
  五、创建并发送消息
  六、消费消息


  
  1.通过nuget安装程序集
  using Microsoft.ServiceBus;
using Microsoft.ServiceBus.Messaging;
DSC0000.png

  using Microsoft.Azure;
DSC0001.png

  2. 在Portal创建 service bus 命名空间:
DSC0002.png

  只有标准级别可以使用主题(Topic),因此创建命名空间时,请选择标准;
  也可以创建后,在缩放选项卡里调整为标准。
DSC0003.png

  3.创建主题,可以通过portal创建,也可以通过代码创建:
DSC0004.png

  通过代码创建主题:


DSC0005.gif DSC0006.gif


1 // Create the topic if it does not exist already.
2             string connectionString =
3                 CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString");
4
5             var namespaceManager =
6                 NamespaceManager.CreateFromConnectionString(connectionString);
7
8             if (!namespaceManager.TopicExists("TestTopic"))
9             {
10
11                 //默认的createtopic方法
12                  namespaceManager.CreateTopic("TestTopic");
13
14
15                 // 通过TopicDescription构建一个重载
16                 //TopicDescription td = new TopicDescription("TestTopicCustomer");
17                 //td.MaxSizeInMegabytes = 5120;
18                 //td.DefaultMessageTimeToLive = new TimeSpan(0, 1, 0);
19                 //if (!namespaceManager.TopicExists("TestTopicCustomer"))
20                 //{
21                 //    namespaceManager.CreateTopic(td);
22                 //}
23
24             }
View Code  4.创建订阅:
  下面的代码演示创建了三个订阅,其中Product和Customer订阅增加了一个SqlFilter,即如果发送消息时,
  message.Properties["Entity"] = "Product",则消息会由Product订阅处理;
  message.Properties["Entity"] = "Customer",则消息会由Customer订阅处理;





1  string connectionString =
2     CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString");
3
4             var namespaceManager =
5                 NamespaceManager.CreateFromConnectionString(connectionString);
6
7             if (!namespaceManager.SubscriptionExists("TestTopic", "AllMessages"))
8             {
9                 namespaceManager.CreateSubscription("TestTopic", "AllMessages");
10             }
11
12             Console.WriteLine("AllMessages done");
13
14
15             // Create a "ProductMessagesFilter" filtered subscription.
16             SqlFilter ProductMessagesFilter =
17                new SqlFilter("Entity='Product'");
18
19             namespaceManager.CreateSubscription("TestTopic",
20                "Product",
21                ProductMessagesFilter);
22
23             Console.WriteLine("Product done");
24
25             // Create a "CustomerMessagesFilter" filtered subscription.
26             SqlFilter CustomerMessagesFilter =
27                new SqlFilter("Entity='Customer'");
28
29             namespaceManager.CreateSubscription("TestTopic",
30                "Customer",
31                CustomerMessagesFilter);
32
33             Console.WriteLine("Customers done");
View Code  5.创建并发送普通消息:
  下面创建了5个product和3个customer并发送了消息,

BrokeredMessage message = new BrokeredMessage(product);

BrokeredMessage message = new BrokeredMessage(customer);

  Product有如下设置:message.Properties["Entity"] = "Product";
  Customer有如下设置:message.Properties["Entity"] = "Customer";


  则 product 订阅会有5条消息,customer订阅会有3条消息,另外,Allmessages没有任何sqlfilter,则该订阅会有5+3=8条消息。





1  string connectionString =
2     CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString");
3
4             TopicClient Client =
5                 TopicClient.CreateFromConnectionString(connectionString, "TestTopic");
6
7            // Client.Send(new BrokeredMessage());
8
9
10             //Add 5 Products
11             var product = new Model.Product();
12
13             for (int i = 0; i < 5; i++)
14             {
15
16                 product.ProductID ="ProductID"+ i.ToString();
17
18                 // Create message, passing a Product  for the body.
19                 BrokeredMessage message = new BrokeredMessage(product);
20
21                 // Set additional custom app-specific property.
22                 message.Properties["Entity"] = "Product";
23
24                 //ScheduledMessage
25                 //message.ScheduledEnqueueTimeUtc = DateTime.Now.AddMinutes(1);
26
27                 // Send message to the topic.
28                 Client.Send(message);
29             }
30
31
32             //Add 3 Customers
33             var customer = new Model.Customer();
34
35             for (int i = 0; i < 3; i++)
36             {
37
38                 customer.CustomerID = "CustomerID"+i.ToString();
39
40                 // Create message, passing a Customer message for the body.
41                 BrokeredMessage message = new BrokeredMessage(customer);
42
43                 // Set additional custom app-specific property.
44                 message.Properties["Entity"] = "Customer";
45
46                 // Send message to the topic.
47                 Client.Send(message);
48             }
View Code  发送消息后的效果:
DSC0007.png

   创建定时消息:
  设置消息的如下属性,则消息在发送成功后延时1分钟后才会出现在上图中,也才可以被订阅消费。
  message.ScheduledEnqueueTimeUtc = DateTime.Now.AddMinutes(1);
  6.消费消息:
  不同的订阅可以取不同订阅里的消息进行处理,处理完成后,标记message.Complete()即可从订阅里清除消息。





1 string connectionString =
2     CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString");
3
4             //SubscriptionClient Client =
5             //    SubscriptionClient.CreateFromConnectionString
6             //            (connectionString, "TestTopic", "Customer");
7
8             //SubscriptionClient Client =
9             //   SubscriptionClient.CreateFromConnectionString
10             //           (connectionString, "TestTopic", "Product");
11
12             SubscriptionClient Client =
13               SubscriptionClient.CreateFromConnectionString
14                       (connectionString, "TestTopic", "AllMessages");
15
16             // Configure the callback options.
17             OnMessageOptions options = new OnMessageOptions();
18             options.AutoComplete = false;
19             options.AutoRenewTimeout = TimeSpan.FromMinutes(1);
20
21             Client.OnMessage((message) =>
22             {
23                 try
24                 {
25                     // Process message from subscription.
26                     Console.WriteLine("Messages");
27               
28                     Console.WriteLine("MessageID: " + message.MessageId);
29                     Console.WriteLine("Message EntityType=: " +
30                         message.Properties["Entity"]);
31
32
33                     if(message.Properties["Entity"].ToString()=="Customer")
34                     {
35                         Console.WriteLine("CustomerID=" + message.GetBody<Customer>().CustomerID.ToString());
36                     }
37                     if (message.Properties["Entity"].ToString() == "Product")
38                     {
39                         Console.WriteLine("ProductID=" + message.GetBody<Product>().ProductID.ToString());
40                     }
41
42                     
43
44                     // Remove message from subscription.
45                     message.Complete();
46                 }
47                 catch (Exception)
48                 {
49                     // Indicates a problem, unlock message in subscription.
50                     message.Abandon();
51                 }
52             }, options);
53
54             Console.ReadLine();
View Code  特别注意:消费掉Product或Customer里的消息后,AllMessage里的消息不受影响。
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-389586-1-1.html 上篇帖子: Azure上的几个坑 下篇帖子: Azure Storage用法:使用Blob Storage
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表