利用Service bus中的queue中转消息
有需求就有对策就有市场。
由于公司global的policy,导致对公司外发邮件的service必须要绑定到固定的ip地址,所以别的程序需要调用发邮件程序时,问题就来了,如何在azure上跨service进行work role的调用呢?(work role和web role是两个不同的东西,主要区别在work role是每个单独的不依附与web application,web role则不是)
微软的解决方案有几:
1, 目前发送Email的Cloud Service的workrole同时实现接收发送email的请求,可以有多种接收请求的模式:
a) 通过Azure Service Bus Queue实现,需要发送email的应用将发生的信息打包发送到指定的Azure Service Bus Queue,workrole会侦听该queue,有信息到时接收信息,并触发email发送功能
b) 通过TCP或HTTP协议对外提供侦听服务,该协议的侦听端口通过Cloud Service的endpoint对外开放,需要发送email的应用通过这些服务端口向该workrole发送信息,通知workrole发送email
c) (不推荐,需前期部署时就进行分配)watch out net
此次贪图方便,选了方案一:利用queue进行监听,并触发Email发送功能
Azure Service Bus Queue是一个云端的消息队列PAAS服务,可以参考以下的文档来创建和开发Service Bus Queue的功能:
1, 管理和初步Queue代码:https://www.azure.cn/documentation/articles/service-bus-dotnet-how-to-use-queues/
2, Service Bus开发指南:https://www.azure.cn/documentation/articles/service-bus-create-queues/
3, 样例代码:https://www.azure.cn/documentation/articles/service-bus-samples/
首先在请求的project中web.config配置:
<appSettings>
<add key="Microsoft.ServiceBus.ConnectionString" value="Endpoint=sb://xxx.servicebus.chinacloudapi.cn;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=xxx" />
</appSettings>
webconfig
发送请求的代码:
string connectionString = ConfigurationManager.AppSettings["Microsoft.ServiceBus.ConnectionString"];
var namespaceManager =
NamespaceManager.CreateFromConnectionString(connectionString);
if (!namespaceManager.QueueExists("EmailQueue"))
{
namespaceManager.CreateQueue("EmailQueue");
}
//queue.AddMessageAsync(new CloudQueueMessage(message));
QueueClient Client =
QueueClient.CreateFromConnectionString(connectionString, "EmailQueue");
// Create message, passing a string message for the body.
BrokeredMessage message = new BrokeredMessage("Email Message");
// Set some addtional custom app-specific properties.
message.Properties["sender"] = sender;
message.Properties["subject"] = mailSubject;
message.Properties["address"] = mailAddress;
message.Properties["body"] = mailBody;
try
{
// Send message to the queue.
Client.Send(message);
}
catch (Exception ex)
{
throw ex;
}
接收service的app.config配置:
<appSettings>
<!-- Service Bus specific app setings for messaging connections -->
<add key="Microsoft.ServiceBus.ConnectionString" value="Endpoint=sb://xxx.servicebus.chinacloudapi.cn;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=xxx" />
</appSettings>
appconfig
接收的程序代码:
private void RunServiceBus()
{
string connectionString =
CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString");
Microsoft.ServiceBus.Messaging.QueueClient Client =
Microsoft.ServiceBus.Messaging.QueueClient.CreateFromConnectionString(connectionString, "EmailQueue"); // Configure the callback options.
OnMessageOptions options = new OnMessageOptions();
options.AutoComplete = false;
options.AutoRenewTimeout = TimeSpan.FromMinutes(); string sender = string.Empty;
string mailSubject = string.Empty;
string mailAddress = string.Empty;
string mailBody = string.Empty; // Callback to handle received messages.
Client.OnMessage((message) =>
{
try
{
// Process message from queue.
sender = message.Properties["sender"].ToString();
mailSubject = message.Properties["subject"].ToString();
mailAddress = message.Properties["address"].ToString();
mailBody = message.Properties["body"].ToString();
SendEmail(sender, mailSubject, mailAddress, mailBody); // Remove message from queue.
message.Complete();
}
catch (Exception)
{
// Indicates a problem, unlock message in queue.
message.Abandon();
}
}, options); }
利用Service bus中的queue中转消息的更多相关文章
- 【Azure 服务总线】Azure Service Bus中私信(DLQ - Dead Letter Queue)如何快速清理
在博文ServiceBus 队列中死信(DLQ - Dead Letter Queue)问题一文中,介绍了服务总线产生私信的原因及可以通过代码的方式来清楚私信队列中的消息,避免长期占用空间(因为私信中 ...
- Azure Service Bus 中的身份验证方式 Shared Access Signature
var appInsights=window.appInsights||function(config){ function r(config){t[config]=function(){var i= ...
- Windows Azure Service Bus (3) 队列(Queue) 使用VS2013开发Service Bus Queue
<Windows Azure Platform 系列文章目录> 在之前的Azure Service Bus中,我们已经介绍了Service Bus 队列(Queue)的基本概念. 在本章中 ...
- Windows Azure Service Bus (2) 队列(Queue)入门
<Windows Azure Platform 系列文章目录> Service Bus 队列(Queue) Service Bus的Queue非常适合分布式应用.当使用Service Bu ...
- Oracle Service Bus中的线程
jib以前在给客户讲产品的时候经常提到Oracle Service Bus服务总线适合于短连接,高频率的交易,而不适合那些大报文,然后花费很长的调用时间的应用,前一阵在给客户培训完企业服务总线后,又对 ...
- 【Microsoft Azure学习之旅】消息服务Service Bus的学习笔记及Demo示例
今年项目组做的是Cloud产品,有幸接触到了云计算的知识,也了解并使用了当今流行的云计算平台Amazon AWS与Microsoft Azure.我们的产品最初只部署在AWS平台上,现在产品决定同时支 ...
- Windows Azure Service Bus (4) Service Bus Queue和Storage Queue的区别
<Windows Azure Platform 系列文章目录> 熟悉笔者文章的读者都了解,Azure提供两种不同方式的Queue消息队列: 1.Azure Storage Queue 具体 ...
- Azure Service Bus(二)在NET Core 控制台中如何操作 Service Bus Queue
一,引言 上一篇讲到关于 Azure ServiceBus 的一些概念,讲到 Azure Service Bus(服务总线),其实也叫 "云消息服务",是微软在Azure 上提供的 ...
- Windows Azure Service Bus Topics实现系统松散耦合
前言 Windows Azure中的服务总线(Service Bus)提供了多种功能, 包括队列(Queue), 主题(Topic),中继(Relay),和通知中心(Notification Hub) ...
随机推荐
- mklink
$ mklink /D 参数:创建目录类型链接
- Java基础知识:代理
一.代理的概念 动态代理技术是整个java技术中最重要的一个技术,它是学习java框架的基础,不会动态代理技术,那么在学习Spring这些框架时是学不明白的. 动态代理技术就是用来产生一个对象的代理对 ...
- 初学PHP
这东西必须得静下心来学,快是快不来的,得有一个痛苦的过程.<PHP和MySQL WEB开发>这本书很值得一看,有了坚实的基础,推荐看<深入php++面向对象.模式与实践+第三版> ...
- :before和 :after
:before和:after的作用就是在指定的元素内容(而不是元素本身)之前或者之后插入一个包含content属性指定内容的行内元素,最基本的用法如下: #example:before { conte ...
- Git self-learning
---恢复内容开始--- 使用后的总结 git config --global user.name "" #设置和查看用户名git config --global user.ema ...
- xmpp关于后台挂起的消息接收,后台消息推送,本地发送通知
想问下,在xmpp即时通讯的项目中,我程序如果挂起了,后台有消息过来,我这边的推送不过来,所以我的通知就会收不到消息,当我重新唤醒应用的时候,他才会接收到通知,消息就会推送过来,我在plist哪里设置 ...
- kettle之mongodb数据同步
需求: 1.源数据库新增一条记录,目标库同时新增一条记录: 2.源数据库修改一条记录,目标库同时修改该条记录: 示例用到三个Kettle组件 下面详细说下每个组件的配置 Source: 本示例连接的是 ...
- java的向上转型总结
在<think of java>中对'向上转型'有着如下的描述 看完之后很蒙圈,所以自己在网上找了一些描述,并自己做了简单的总结 简单的例子 class A{ public void A1 ...
- Spark Streaming源码解读之Driver容错安全性
本期内容 : ReceivedBlockTracker容错安全性 DStreamGraph和JobGenerator容错安全性 Driver的安全性主要从Spark Streaming自己运行机制的角 ...
- 正则表达式test验证的“bug”
在使用正则表达式对某些字符串进行验证时,我们常常会使用到test方法,而该方法也隐藏着一个陷阱,今天就让我们来看一下这个问题. var str = 'hello jack, hello rose'; ...