Windows Azure Service Bus (6) 中继(Relay On) 使用VS2013开发Service Bus Relay On
《Windows Azure Platform 系列文章目录》
注意:本文介绍的是国内由世纪互联运维的Windows Azure服务。
项目文件请在这里下载。
我们在使用Azure平台的时候,经常会遇到本地应用和云端应用进行互通互连的情况。
在这种混合云的场景下,我们可以通过以下方式解决:
一.Point-To-Site VPN
将本地的一台设备(Point),与云端的网络(Site)进行互通互联。
- 这里的Point指的就是企业内网的一台主机(VPN客户端)
- 这里的Site是指Azure Virtual Network的网络
- 这样可以实现将企业内网的一台主机与云端网络互通互联,同时通过VPN保证网络的安全性
企业内网的主机需要安装VPN客户端
支持以下客户端操作系统:
Windows 7 (32位和64位)
Windows Server 2008 R2(仅支持64位)
Windows 8(32位和64位)
Windows Server 2012(仅支持64位)
Point-to-Site VPN是使用SSTP VPN协议
有兴趣的读者可以参考笔者之前的文章:
Windows Azure Virtual Network (8) 创建Azure Point-to-Site点到站点 VPN
二.Site-To-Site VPN
将本地的网络(Site)与云端的Azure虚拟网络(Site)进行互通互联。
Site-To-Site VPN的前提要求:
- 企业本地网络需要固定的公网IPV4地址
- 需要微软认证的VPN设备 or Windows Server 2012 RRAS
设备列表请参考:https://msdn.microsoft.com/en-us/library/azure/jj156075.aspx
- VPN设备必须在NAT设备的前面
Site-to-Site VPN是使用IPSec VPN协议
这篇文章我回头再写 :)
以上的方法都可以看出,Azure Virtual Network VPN的方式,都是基于TCP/IP网络协议的。
在很多场景下,实现Point-To-Site和Site-To-Site并不是非常容易。例如以下场景:
- 企业没有固定的公网IPV4地址
- 企业的IT策略,不允许设置Azure Site-To-Site VPN
在这种情况下,就可以使用Azure Service Bus Relay On实现应用层的混合云了。
Azure Service Bus Relay On技术:
- 不依赖于固定公网IPV4或者VPN设备
- 可以穿透NAT和防火墙设备
- 只需要本地数据中心打开HTTP 80端口或HTTPS 443端口
如下图:

Azure Service Bus Relay On是通过HTTP, HTTPS实现应用的混合云。假设当需要把企业内网WCF应用服务发布到公网,被其他客户端进行调用的时候,只需要在企业内网允许HTTP需要80端口,或者HTTPS需要的443端口即可。
另外,企业内网的WCF应用服务,和客户端(外网)应用服务之间,不是直接调用的。而是首先需要将WCF服务,在Azure Service Bus Relay On进行注册,客户端(外网)应用才能调用被注册的Azure Service Bus Relay On。Azure Service Relay On概念上类似于代理服务器。

注意:Azure Service Bus在企业内网的应用目前只能支持WCF服务。
前面的介绍完了,现在进入Demo演示阶段。笔者模拟以下场景:
1.在企业内网中,有一台Web Service,上面部署了WCF服务
2.通过Azure Service Bus Relay On,将WCF注册到Service Bus上。
3.从Internet上的另外一台客户端机器,通过验证Service Bus的SAS验证方式,来调用Azure Service Bus Relay On上的服务。
以下项目分为三类:
- 使用PowerShell,创建Azure Service Bus Relay On
- 服务器端,将WCF服务注册到Azure Service Bus。因为WCF项目执行的时候,需要宿主,本项目以Windows Console为宿主。
- 客户端,Internet上的机器,调用Azure Service Bus注册的WCF服务
一.使用PowerShell,创建Azure Service Bus Relay On
请读者注意,Azure Service Bus Relay On只能通过PowerShell来创建。如何读者已经通过Management Portal,创建了Azure Service的话,是不能正常使用Relay On功能的。需要删除,然后通过PowerShell重建。
1.我们使用管理员身份,运行Azure PowerShell
2.假设读者有多个Azure订阅的情况下,切换当前订阅
Select-AzureSubscription '[YourSubscriptionName]' -current
3.执行以下代码:
New-AzureSBNamespace -Name '[YourServiceBusName]' -NamespaceType 'Messaging' -Location '[YourLocation]'
例如笔者想创建的Service Bus名称为leiservicebus,位于China East数据中心
New-AzureSBNamespace -Name 'leizhangservicebus' -NamespaceType 'Messaging' -Location 'China East'
以下为执行结果截图:
4.可以在Azure Management Portal查看到创建结果。我们还可以查看Service Bus的连接字符串,如下图:

注意:客户端是否可以连接Azure Service Bus Relay是通过上面的连接字符串来验证的。
以上,创建Azure Service Bus Relay On的工作就完成了。
二.服务器端,将WCF服务注册到Azure Service Bus
接下来,我们使用管理员身份,运行Visual Studio 2013。创建一个新的项目,重名为SBRelay。
1.增加Windows Console Library,命名为SBRelayServer
2.在Program.cs增加以下代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
using Foundation;
using Microsoft.ServiceBus;
using System.ServiceModel.Description; namespace SBRelayServer
{
class Program
{
static void Main(string[] args)
{
var tokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider("RootManageSharedAccessKey", "+LSd5njWikUZEi+RY/NpkkR/GL3PlpVRGCv23gwKrM0="); var serviceHost = new ServiceHost(typeof(EchoService)); var serviceEndpoint = serviceHost.AddServiceEndpoint(
typeof(IEcho),
new NetTcpRelayBinding(),
"sb://leizhangservicebus.servicebus.chinacloudapi.cn/echo"); serviceEndpoint.Behaviors.Add(new TransportClientEndpointBehavior(tokenProvider)); serviceHost.Open();
Console.WriteLine("Press ENTER to close");
Console.ReadLine();
serviceHost.Close();
}
}
}
注意:上图的TokenProvider为我们的SAS密钥。
上面的应用程序中,会将本地的WCF注册到云端的Azure Service Bus,地址为sb://leizhangservicebus.servicebus.chinacloudapi.cn/echo
3.记得在项目文件中,使用NuGet,下载Azure Service Bus Package,如下图所示

4.在SBRelayServer项目中,增加EchoService.cs,输入以下代码:
using Foundation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace SBRelayServer
{
class EchoService : IEcho
{
public string GetValue()
{
return "Hello World!";
}
}
}
服务器端会返回给客户端字符串,内容为Hello World!
三.客户端,从客户端调用
1.增加Windows Console Library,命名为SBRelayClient
2.在Program.cs中,增加如下代码:
using Microsoft.ServiceBus;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
using Foundation;
using System.Diagnostics; namespace SBRelayClient
{
class Program
{
static void Main(string[] args)
{
var binding = new NetTcpRelayBinding(); var channelFactory = new ChannelFactory<IEchoChannel>
(
binding,
new EndpointAddress("sb://leizhangservicebus.servicebus.chinacloudapi.cn/echo")
); channelFactory.Endpoint.EndpointBehaviors.Add(new TransportClientEndpointBehavior { TokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider("RootManageSharedAccessKey", "+LSd5njWikUZEi+RY/NpkkR/GL3PlpVRGCv23gwKrM0=") }); using (var ch = channelFactory.CreateChannel())
{
Stopwatch sw = new Stopwatch();
sw.Start();
string returnValue = ch.GetValue();
sw.Stop();
Console.WriteLine("Get {0} From WCF Server", returnValue);
}
Console.ReadLine();
}
}
}
上面的代码中,会从服务器读取GetValue方法,返回给客户端字符串。
如何执行该代码?
1.编译项目文件
2.执行服务器端项目,SBRelayServer项目下的SBRelayServer.exe。
3.等到服务器端项目,SBRelayServer.exe命令行显示Press ENTER to close,请不要关闭SBRelayServer.exe
4.执行客户端项目,SBRelayClient项目下的SBRelayClient.exe
5.下图中,左侧为服务器端命令行界面,右侧为客户端命令行结果,其中的Hello World即为服务器的返回值。

本博-三石Blog(下文简称本博),在本博客文章结尾处右下脚未注明转载、来源、出处的作品(内容)均为本博原创,本站对于原创作品内容对其保留版权,请勿随意转载,如若真有需要的朋友可以发Mail联系我;转载本博原创作品(内容)也必须遵循“署名-非商业用途-保持一致”的创作共用协议,请务必以文字链接的形式标明或保留文章原始出处和博客作者(Lei Zhang)的信息,关于本博摄影作品请务必注意保留(www.cnblog.com/threestone)等相关水印版权信息,否则视为侵犯原创版权行为;本博谢绝商业网站转载。版权所有,禁止一切有违中华人民共和国著作权保护法及相关法律和本博(法律)声明的非法及恶意抄袭。
Windows Azure Service Bus (6) 中继(Relay On) 使用VS2013开发Service Bus Relay On的更多相关文章
- Windows Azure Service Bus (3) 队列(Queue) 使用VS2013开发Service Bus Queue
<Windows Azure Platform 系列文章目录> 在之前的Azure Service Bus中,我们已经介绍了Service Bus 队列(Queue)的基本概念. 在本章中 ...
- Windows Azure Service Bus (5) 主题(Topic) 使用VS2013开发Service Bus Topic
<Windows Azure Platform 系列文章目录> 项目文件,请在这里下载 在笔者之前的文章中Windows Azure Service Bus (1) 基础 介绍了Servi ...
- Windows Azure HandBook (5) Azure混合云解决方案
<Windows Azure Platform 系列文章目录> 在很多情况下,我们都会遇到本地私有云和公有云做互通互联的混合云场景.对于这种混合云的场景,微软的Windows Azure会 ...
- Windows Azure Service Bus (4) Service Bus Queue和Storage Queue的区别
<Windows Azure Platform 系列文章目录> 熟悉笔者文章的读者都了解,Azure提供两种不同方式的Queue消息队列: 1.Azure Storage Queue 具体 ...
- Windows Azure Cloud Service (11) PaaS之Web Role, Worker Role(上)
<Windows Azure Platform 系列文章目录> 本文是对Windows Azure Platform (六) Windows Azure应用程序运行环境内容的补充. 我们知 ...
- [Windows Azure] Building worker role B (email sender) for the Windows Azure Email Service application - 5 of 5.
Building worker role B (email sender) for the Windows Azure Email Service application - 5 of 5. This ...
- [Windows Azure] Building worker role A (email scheduler) for the Windows Azure Email Service application - 4 of 5.
Building worker role A (email scheduler) for the Windows Azure Email Service application - 4 of 5. T ...
- [Windows Azure] Windows Azure Web Sites, Cloud Services, and VMs: When to use which?
This document provides guidance on how to make an informed decision in choosing between Windows Azur ...
- [Windows Azure] Windows Azure Execution Models
Windows Azure Execution Models Windows Azure provides different execution models for running applica ...
随机推荐
- 淘宝上倒卖新浪微盘事件来龙去脉——谈谈巧用IMEI
这是一个老黄历的事件,曾记得淘宝上的卖家卖10元卖50g网络硬盘,并且卖的相当的火,一个月就卖了500个账号.由于我也是那个事件的亲身经历者之一,这里就看到了IMEI号在项目中防止作弊是何其的重要. ...
- 用JS识别各版本浏览器
自昨天发了各浏览器内核介绍的随笔,就闲不住了,想直接写个JS来识别用户所用浏览器版本. 写着写着却发现很多坑爹的地方,比如IE10-的版本是依循常规支持attachEvent,但到了IE11,却只支持 ...
- httpclient瓶颈
问题现象: 1.系统异常,应用拒绝访问. 2.web容器线程爆满 问题分析: 1.数据库正常 2.日志信息没有异常 问题思考: 1.应用访问量突破顶峰. 2.应用在某处存在瓶颈 发现问题: 需要了解线 ...
- 【css3】--四种气泡
在聊天的场景中,聊天内容需要用到气泡修饰,如下图.下面一一讲解. 图片式: 第一个样式是京东客服,气泡的圆角和钩子都是用了图片.使用了一个table组合成了一个圆角的框框.lm样式拼出了钩子. < ...
- [.net 面向对象编程基础] (14) 重构
[.net 面向对象编程基础] (14) 重构 通过面向对象三大特性:封装.继承.多态的学习,可以说我们已经掌握了面向对象的核心.接下来的学习就是如何让我们的代码更优雅.更高效.更易读.更易维护.当然 ...
- c++实现冒泡排序
# include<iostream> #include<stdio.h> using namespace std; void maopao(int *list){ int i ...
- IOS 多线程02-pthread 、 NSThread 、GCD 、NSOperationQueue、NSRunLoop
注:本人是翻译过来,并且加上本人的一点见解. 要点: 1.前言 2.pthread 3.NSThread 4.Grand Central Dispatch(GCD) 5.Operation Queue ...
- SVN 使用
我是一个前端,svn 的服务器配置也是后端弄好的,到底怎么弄的不清楚. 最开始是想和xcode关联起来,每次提交代码也方便,但是在Xcode里的偏好设置Accounts 模块 添加了SVN 服务端地址 ...
- ios NSFileManager和NSFileHandle(附:获取文件大小 )
转自 http://blog.csdn.net/zhibudefeng/article/details/7795946 //file 文件操作 NSFileManager 常见的NSFileMana ...
- 说说设计模式~门面模式(Facade)
返回目录 门面模式(Facade)属于结构型模式的一种,它符合面向对象的封装原则,但又不符合开闭原则,呵呵,今天我们主要说它的优点,不谈缺点. 定义 门面模式,是指提供一个统一的接口去访问多个子系统的 ...