Nginx集群之WCF分布式局域網應用


目錄

1       大概思路... 1

2       Nginx集群WCF分布式局域網結構圖... 1

3       關於WCF的BasicHttpBinding. 1

4       編寫WCF服務、客戶端程序... 2

5       URL保留項... 6

6       部署WCF服務程序到局域網內3台PC機... 7

7       Nginx集群配置搭建... 8

8       啟動WCF客戶端程序... 10

9       總結... 11

1       大概思路

l  Nginx集群WCF分布式局域網結構圖

l  關於WCF的BasicHttpBinding

l  編寫WCF服務、客戶端程序

l  URL保留項

l  部署WCF服務程序到局域網內3台PC機

l  Nginx集群配置搭建

l  啟動WCF客戶端程序

l  總結

2       Nginx集群WCF分布式局域網結構圖

關於WCF即可以寄宿於IIS,也可以自我寄宿,本文采用的是自我寄宿方式。之所以采用自我寄宿方式,很大程度上,在一些特殊的場景,例如下載大文件(如幾百MB、1G等)、圖片、文檔等,如果以IIS為宿主,可能會產生內存不夠用。所以這里采用自我寄宿的方式為例子。

Nginx集群除了在反向代理可以應用到,在WCF的處理上,也有很好的表現。以下是Nginx集群在WCF分布式的設計結構圖:

3       關於WCF的BasicHttpBinding

首先了解系統預定義綁定對不同安全模式的支持,具體參照以下該文:

[WCF安全系列]綁定、安全模式與客戶端憑證類型:總結篇

https://www.cnblogs.com/artech/archive/2011/05/28/Authentication_034.html

  •   所有的綁定都可以不采用任何的安全傳輸機制,即支持None安全模式;
  •   BasicHttpBinding的默認模式為None,WS相關的綁定默認模式為Message,而局域網相關綁定的模式模式為Transport;
  •   除了NetNamedPipeBinding,所有的綁定都支持Message安全模式;
  •   對於所有支持Message模式的綁定,除了NetMsmqBinding都支持Mixed模式;
  •   除了WSDualHttpBinding,所有的綁定都支持Transport模式;
  •   只有BasicHttpBinding支持TransportCredentialOnly模式;
  •   只有NetMsmqBinding支持Both安全模式。

 這里WCF的Ningx集群,主要用的是BasicHttpBinding。表示一個綁定,Windows Communication Foundation (WCF) 服務可以使用此綁定配置和公開這樣的終結點:這些終結點能夠與基於 ASMX 的 Web 服務和客戶端以及符合 WS-I Basic Profile 1.1 標准的其他服務進行通信。

4       編寫WCF服務、客戶端程序

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>

即如圖所示:

5       URL保留項

在默認的操作系統配置中,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"

 

6       部署WCF服務程序到局域網內3台PC機

遠程進行部署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服務端的運行效果:

7       Nginx集群配置搭建

通過自主義域名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,運行結果:

8       啟動WCF客戶端程序

啟動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機執行:

9       總結

WCF是由微軟開發的一系列支持數據通信的應用程序框架,通過開源框架Nginx的結合,能夠有更多的擴展性。Nginx結合WCF對局域網內的布局有很大關系,通過WCF整合報表服務器、郵件服務器、文檔服務器等,WCF原來就整合了原有的windows通訊的 .net Remoting,WebService,Socket的機制,Nginx讓具備分布式功能的WCF更加強大了。

源代碼下載:

http://download.csdn.net/download/ruby_matlab/10122670

PDF下載:

Nginx集群之WCF分布式局域網應用.pdf


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM