目錄html
1 大概思路... 1nginx
2 Nginx集羣WCF分佈式局域網結構圖... 1windows
3 關於WCF的BasicHttpBinding. 1安全
4 編寫WCF服務、客戶端程序... 2ruby
5 URL保留項... 6服務器
6 部署WCF服務程序到局域網內3臺PC機... 7app
7 Nginx集羣配置搭建... 8負載均衡
8 啓動WCF客戶端程序... 10框架
9 總結... 11分佈式
l Nginx集羣WCF分佈式局域網結構圖
l 關於WCF的BasicHttpBinding
l 編寫WCF服務、客戶端程序
l URL保留項
l 部署WCF服務程序到局域網內3臺PC機
l Nginx集羣配置搭建
l 啓動WCF客戶端程序
l 總結
關於WCF便可以寄宿於IIS,也能夠自我寄宿,本文采用的是自我寄宿方式。之因此採用自我寄宿方式,很大程度上,在一些特殊的場景,例以下載大文件(如幾百MB、1G等)、圖片、文檔等,若是以IIS爲宿主,可能會產生內存不夠用。因此這裏採用自我寄宿的方式爲例子。
Nginx集羣除了在反向代理能夠應用到,在WCF的處理上,也有很好的表現。如下是Nginx集羣在WCF分佈式的設計結構圖:
首先了解系統預約義綁定對不一樣安全模式的支持,具體參照如下該文:
[WCF安全系列]綁定、安全模式與客戶端憑證類型:總結篇
https://www.cnblogs.com/artech/archive/2011/05/28/Authentication_034.html
這裏WCF的Ningx集羣,主要用的是BasicHttpBinding。表示一個綁定,Windows Communication Foundation (WCF) 服務能夠使用此綁定配置和公開這樣的終結點:這些終結點可以與基於 ASMX 的 Web 服務和客戶端以及符合 WS-I Basic Profile 1.1 標準的其餘服務進行通訊。
l 解決方案的主要目錄以下:
l WCF服務程序
IOutputSomething.cs
using System.ServiceModel; namespace Service.Interface { [ServiceContract] public interface IOutputSomething { [OperationContract] string GetContentData(int i); [OperationContract] string GetIpAddress(); } }
OutputSomething.cs
using Service.Interface; using System.Net; using System.ServiceModel; namespace Service { [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)] public class OutputSomething : IOutputSomething { /// <summary> /// 名稱 /// </summary> string threadNumber; readonly object thisLocak = new object(); public string GetContentData(int i) { lock (thisLocak) { threadNumber = i.ToString() + " - " + "我是主機:" + GetIpAddress(); } return string.Format("序列號{0},線程號{1}", i, threadNumber); } public string GetIpAddress() { string AddressIP = string.Empty; foreach (IPAddress _IPAddress in Dns.GetHostEntry(Dns.GetHostName()).AddressList) { if (_IPAddress.AddressFamily.ToString() == "InterNetwork") { AddressIP = _IPAddress.ToString(); } } return AddressIP; } } }
Program.cs
using Service; using System; using System.ServiceModel; namespace HighlyConcurrentHosting { class Program { static void Main(string[] args) { using (ServiceHost host = new ServiceHost(typeof(OutputSomething))) { host.Opened += delegate { Console.WriteLine(host.Description.Endpoints[0].Address.Uri + "已經啓動,按任意鍵終止服務!"); }; host.Open(); Console.Read(); } } } }
l 客戶端程序
Program.cs
using HighlyConcurrentClient.HighlyConcurrentService; using System; using System.Net; namespace HighlyConcurrentClient { class Program { static void Main(string[] args) { string AddressIP = string.Empty; foreach (IPAddress _IPAddress in Dns.GetHostEntry(Dns.GetHostName()).AddressList) { if (_IPAddress.AddressFamily.ToString() == "InterNetwork") { AddressIP = _IPAddress.ToString(); } } Console.WriteLine("本機IP是:" + AddressIP); using (OutputSomethingClient proxy = new OutputSomethingClient()) { for (int i = 0; i < 20; i++) { Console.WriteLine(proxy.GetContentData(i)); } } Console.Read(); } } }
客戶端添加服務引用後,Address多是某一臺PC機的IP地址(例如:address="http:// 10.92.202.56:5600/OutputSomething")這是須要修改成如下Nginx的地址
address="http://zhyongfeng.com/OutputSomething",配置以下:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> <system.serviceModel> <bindings> <basicHttpBinding> <binding name="BasicHttpBinding_IOutputSomething" /> </basicHttpBinding> </bindings> <client> <endpoint address="http://zhyongfeng.com/OutputSomething" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IOutputSomething" contract="HighlyConcurrentService.IOutputSomething" name="BasicHttpBinding_IOutputSomething" /> </client> </system.serviceModel> </configuration>
即如圖所示:
在默認的操做系統配置中,Windows Communication Foundation (WCF) 爲端口 80 建立可全局訪問的保留項,使全部用戶都可以運行應用程序,在該應用程序中使用雙向 HTTP 綁定來進行雙工通訊。
返回了:
「System.ServiceModel.AddressAccessDeniedException」類型的未經處理的異常在 System.ServiceModel.dll 中發生
其餘信息: HTTP 沒法註冊 URL http://+:5600/OutputSomething/。進程不具備此命名空間的訪問權限(有關詳細信息,請參見 http://go.microsoft.com/fwlink/?LinkId=70353)。
以管理員方式運行C:\windows\System32\cmd.exe:
netsh http add urlacl url=http://+:5600/ user="\Everyone" netsh http add iplisten ipaddress=0.0.0.0:5600 防火牆的入站規則: netsh advfirewall firewall add rule name="5600端口" dir=in action=allow protocol=TCP localport=5600 url保留項刪除 netsh http delete urlacl url=http://+:5600/ url保留項顯示 netsh http show urlacl
這裏主要使用以下添加URL保留項便可:
netsh http add urlacl url=http://+:5600/ user="\Everyone"
遠程進行部署WCF服務程序時,須要將它的config配置文件,從新定位address,將默認的IP地址127.0.0.1:5600修改成遠程計算機的IP地址:
10.92.202.56:5600、10.92.202.57:5700、10.92.202.58:5800
而後啓動遠程計算機的WCF服務程序,運行效果以下:
本機IE上訪問WCF服務端的運行效果:
經過自主義域名zhyongfeng.com:80端口進行負載均衡集羣訪問,則訪問C:\Windows\System32\drivers\etc\hosts,添加下列「本機IP 自定義的域名」:
10.93.85.66 zhyongfeng.com
針對WCF部署的多臺PC機配置(設置了proxy_connect_timeout爲10s,若是其中一臺機down掉了,能夠轉發到另外一臺機器)以下:
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; upstream zhyongfeng.com { server 10.92.202.56:5600; server 10.92.202.57:5700; server 10.92.202.58:5800; } server { listen 80; server_name zhyongfeng.com; location / { proxy_pass http://zhyongfeng.com; proxy_connect_timeout 10s; } } }
運行CMD:
D:\DTLDownLoads\nginx-1.10.2>start nginx
D:\DTLDownLoads\nginx-1.10.2>nginx -s reload
訪問WCF服務端:http://zhyongfeng.com/OutputSomething/metadata,運行結果:
啓動WCF客戶端程序,運行效果圖以下:
遠程桌面關掉其中一臺10.92.202.56:5600的PC機:
從新啓動WCF客戶端程序,由於Nginx配置文件設置了proxy_connect_timeout爲10s,則關閉的PC機10.92.202.56:5600在10s後會將它的消息轉發給10.92.202.57:5700,繼續由其它2臺PC機執行:
WCF是由微軟開發的一系列支持數據通訊的應用程序框架,經過開源框架Nginx的結合,可以有更多的擴展性。Nginx結合WCF對局域網內的佈局有很大關係,經過WCF整合報表服務器、郵件服務器、文檔服務器等,WCF原來就整合了原有的windows通信的 .net Remoting,WebService,Socket的機制,Nginx讓具有分佈式功能的WCF更增強大了。
源代碼下載:
http://download.csdn.net/download/ruby_matlab/10122670
PDF下載: