Nginx集群之WCF分布式局域网应用

目录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分布式

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

相关文章
相关标签/搜索