Nginx集群之WCF分布式消息隊列


目錄

1       大概思路... 1

2       Nginx集群之WCF分布式消息隊列... 1

3       MSMQ消息隊列... 2

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

5       服務器安裝MSMQ.. 5

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

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

8       運行結果... 8

9       總結... 11

1       大概思路

l  Nginx集群之WCF分布式消息隊列

l  MSMQ消息隊列

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

l  服務器安裝MSMQ

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

l  Nginx集群配置搭建

l  運行結果

l  總結

2       Nginx集群之WCF分布式消息隊列

針對WCF分布式消息隊列MSMQ大大提高了處理能力,無論是發送方還是接收方都不用等待對方返回成功消息,但是不適合Client與Server端的實時交互。WCF分布式消息隊列,在處理日志方面,效果還是很顯著的。

當然,針對消息隊列的處理技術,有很多種,例如:ActiveMQ、RabbitMQ、ZeroMQ、Kafka、MetaMQ、RocketMQ。本文使用的是微軟自帶的消息隊列MSMQ,結合WCF在Ningx集群的環境下,創建一個類似日志型或郵件型的WCF服務。

 

以下是WCF分布式消息隊列的架構:

 

3       MSMQ消息隊列

消息隊列MSMQ提供更高程序的非連接通信支持,因為它支持離線通信方式。消息的發送者在離線方式下進行,離線通信方式讓進行消息交換的參與者變成完全獨立的兩個應用,它們之間唯一的紐帶就是消息隊列。

如果借助MSMQ提供的通信方式,無論出現多高的負載,只要抵達的消息累積量不超過設定的限額,MSMQ就完全可以按照自己的節奏來進行處理。將高負載的請求延后到低負載的時候進行處理,很好地解決了負載上面的問題。

MSMQ中主要有兩個概念。

一個是消息Message:Message是通信雙方需要傳遞的消息,它可以是文本、圖片、視頻等。消息包含發送和接收者的標識,只有指定的用戶才能取得消息。

隊列

描述

公共隊列

在整個消息隊列網絡中復制,有可能由網絡連接的所有站點訪問

專用隊列

不在整個網絡中發布,它們僅在所駐留的本地計算機上可用,專用隊列只能由知道隊列的完整路徑名稱或標簽的應用程序訪問

日志隊列

包含確認在給定“消息隊列中發送的消息回執消息”

響應隊列

包含目標應用程序接收到消息時返回給發送應用程序的響應消息,包括機器日志隊列、機器死信隊列和機器事務死信隊列

 

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

l  WCF服務程序

Program.cs

using Service;
using System;
using System.ServiceModel;

namespace MessageDealingHosting
{
    class Program
        {
            static void Main(string[] args)
            {                
                using (ServiceHost host = new ServiceHost(typeof(PublicMessageQueue)))
                {
                    host.Opened += delegate
                    {
                        Console.WriteLine(host.Description.Endpoints[0].Address.Uri + "已經啟動,按任意鍵終止服務!");
                    };

                    host.Open();
                    Console.Read();
                }
            }
        }
}

PublicMessageQueue.cs

using System;
using Service.Interface;

namespace Service
{
    public partial class PublicMessageQueue : IPublicMessageQueue
    {
        public void SendMessage(string msg)
        {
            Console.WriteLine(string.Format("消息隊列開始處理,輸入{0}成功", msg));
        }
    }
}

IPublicMessageQueue.cs

using System;
using Service.Interface;

namespace Service
{
    public partial class PublicMessageQueue : IPublicMessageQueue
    {
        public void SendMessage(string msg)
        {
            Console.WriteLine(string.Format("消息隊列開始處理,輸入{0}成功", msg));
        }
    }
}

服務端配置文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="MessageQueueBehavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings>
      <netMsmqBinding>
        <binding name="MessageQueueBinding" exactlyOnce="false">
          <security mode="None"></security>
        </binding>
      </netMsmqBinding>
    </bindings>
    <services>
      <service behaviorConfiguration="MessageQueueBehavior" name="Service.PublicMessageQueue">
        <endpoint address="net.msmq://10.92.202.56/private/wcf_message" binding="netMsmqBinding" bindingConfiguration="MessageQueueBinding"
          contract="Service.Interface.IPublicMessageQueue" />
        <host>
          <baseAddresses>
            <add baseAddress="http:// 10.92.202.56:5600/wcf_message" />
          </baseAddresses>
        </host>
      </service>
    </services>
  </system.serviceModel>
</configuration>

l  客戶端程序

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MessageDealingClient.MessageDealingService;
using System.ServiceModel;

namespace MessageDealingClient
{
    class Program
    {
        static void Main(string[] args)
        {
            using (ChannelFactory<IPublicMessageQueue> channelFactory = new ChannelFactory<IPublicMessageQueue>("NetMsmqBinding_IPublicMessageQueue"))
            {
                IPublicMessageQueue proxyServer = channelFactory.CreateChannel();
                proxyServer.SendMessage("hello world");
                Console.WriteLine("客戶端先啟動,將消息傳到消息隊列,若WCF服務啟動,則會輸出hello world");
            }
            Console.Read();
        }
    }
}

客戶端配置文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <bindings>
            <netMsmqBinding>
                <binding name="NetMsmqBinding_IPublicMessageQueue" exactlyOnce="false">
                    <security mode="None" />
                </binding>
            </netMsmqBinding>
        </bindings>
        <client>
            <endpoint address="net.msmq://10.92.202.56/private/wcf_message"
                binding="netMsmqBinding" bindingConfiguration="NetMsmqBinding_IPublicMessageQueue"
                contract="MessageDealingService.IPublicMessageQueue" name="NetMsmqBinding_IPublicMessageQueue" />
        </client>
    </system.serviceModel>
</configuration>

 

5       服務器安裝MSMQ

l  打開“控制面板”

l  單擊“程序”,然后在“程序和功能”下單擊“打開或關閉 Windows 功能

l  展開“Microsoft Message Queue (MSMQ) 服務器”,展開“Microsoft Message Queue (MSMQ) 服務器核心”,然后選中對應於以下要安裝的“消息隊列”功能的復選框

n  MSMQ Active Directory 域服務集成(用於加入域的計算機)。

n  MSMQ HTTP 支持。

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

遠程部署WCF服務端程序到PC機

7       Nginx集群配置搭建

通過自主義域名zhyongfeng.com:80端口進行負載均衡集群訪問,則訪問C:\Windows\System32\drivers\etc\hosts,添加下列“本機IP 自定義的域名”:

10.93.85.66     zhyongfeng.com

使用Nginx匹配原則針對WCF部署的1台PC機配置如下:

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;
        } 
    location /wcf_message{
            proxy_pass   http://10.92.202.56:5600/wcf_message;
            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/wcf_message,運行結果:

8        運行結果

啟動遠程WCF服務端,同時啟動WCF客戶端,結果如下:

關閉遠程WCF服務端,啟動WCF客戶端,結果如下:

再啟動WCF服務端,結果如下:

 

9       總結

基於WCF分布式消息隊列,可以在一些客戶端並不需要服務端響應的場景上應用。消息隊列在異步處理上有巨大優勢,是一項可選擇性的進程間的通信,能夠保持進程之間通信的穩定性。

 

源代碼下載:

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

 

PDF下載:

Nginx集群之WCF分布式消息隊列.pdf


免責聲明!

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



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