利用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) ...
随机推荐
- c#摄像头编程实例 (转)
c#摄像头编程实例 摄像头编程 安装摄像头后,一般可以找到一个avicap32.dll文件 这是一个关于设想头的类 using system;using System.Runtime.Intero ...
- vi 使用入门
几种模式: Normal Mode 命令模式 Insert Mode 编辑模式 Command-line Mode Visual Mode Select M ...
- iconfont 图标字体
iconfont 技术的主要是将图标转化为字体来减少应用体积.如需在项目中使用iconfont技术,图标矢量图一开始都应合并转化为字体库. 优点: 减小体积,字体文件比图片要小 图标保真缩放,解决 ...
- Spark Streaming架构设计和运行机制总结
本期内容 : Spark Streaming中的架构设计和运行机制 Spark Streaming深度思考 Spark Streaming的本质就是在RDD基础之上加上Time ,由Time不断的运行 ...
- cat > file << EOF 的用法
cat> 文件名<<eof 用来创建文件在这之后输入任何东西 都是在 文件里的输入完成之后EOF结尾 代表结束比如cat > 1.txt <<eof12345eof ...
- git 撤销commit
如果不小心commit了一个不需要commit的文件,可以对其进行撤销. 先使用git log 查看 commit日志 commit 422bc088a7d6c5429f1d0760d008d86c5 ...
- 在win7下将CapsLock按键变成esc
我喜欢用vim来编辑,经常要按到esc,但是去按那个按键确实比较的远,而且CapsLock这个按键对我来说着实有些鸡肋,所以就想在win7上也能像ubuntu那样把capslock映射为esc,在网上 ...
- java System.getProperty()参数大全
java.version Java Runtime Environment versionjava.vendor Java Runtime Environment vendorjava.vendor. ...
- centos 7.2 网卡配置文件 及 linux bridge的静态配置
在 centos 7.2 系统内, 网卡的配置文件在: /etc/sysconfig/network-scripts/ 下. 命名规则: ifcfg-xxxx. xxx为设备名称. 通过分析 ne ...
- zabbix数据库mariadb从服务器迁移到云mysql数据库的操作
zabbix数据库mariadb从本机迁移到云mysql数据库的操作 1.将zabbix数据库导出,并导入到云数据库中 由于数据库较大,如果直接使用shell会话中断会导致数据库导出或者导入失败,使用 ...