首頁 運維 windows維 使用nginx搭建高可用,高並發的wcf集群

使用nginx搭建高可用,高並發的wcf集群

Oct 23, 2017 am 10:48 AM
nginx 可用 並行

很多情況下基於wcf的複雜均衡都首選zookeeper,這樣可以擁有更好的控製粒度,但zk對C# 不大友好,實現起來相對來說比較麻煩,實際情況下,如果

你的負載機製粒度很粗糙的話,優先使用nginx就可以搞定,既可以實現複雜均衡,又可以實現雙機熱備,以最小的代碼量實現我們的業務,下面具體分享下。

一:準備的材料

  1. 話不多說,一圖勝千言,圖中的伺服器都是採用vmware虛擬化,如下圖:

《 1》 三台windows機器,兩台WCF的windows伺服器承載(192.168.23.187,192.168.23.188),一台Client的伺服器(192.168.23.1)

《2》 一台的伺服器(192.168.23.1)

《2》 一台Centos機器,用來承載web複雜均衡nginx(192.168.23.190)。

《3》在所有的Client的Hosts檔案中增加host映射:【192.168.23.190 cluster.com】,方便透過網域名稱的形式存取nginx所在伺服器的ip位址。

二:環境搭建

1. WCF程序

    既然是測試,肯定就是簡單的程序,程式碼就不完全給了。

 

《1》 HomeService實作類別程式碼如下(輸出目前server的ip位址,方便查看):

 public class HomeService : IHomeService 
     { 
         public string DoWork(string msg) 
         { 
             var ip = Dns.GetHostAddresses(Dns.GetHostName()).FirstOrDefault(i => i.AddressFamily == 
                                                         AddressFamily.InterNetwork).ToString(); 
 
             return string.Format("当前 request 由 server={0} 返回", ip); 
         }
       
     }
登入後複製

 

《2》 App.Config代碼

 <?xml version="1.0" encoding="utf-8" ?> 
 <configuration> 
   <startup> 
     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" /> 
  </startup> 
   <system.serviceModel> 
     <behaviors> 
       <serviceBehaviors> 
         <behavior name="">

           <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />

           <serviceDebug includeExceptionDetailInFaults="false" />
         </behavior>

       </serviceBehaviors>

     </behaviors>

     <services>

       <service name="WcfService.HomeService">

         <endpoint address="/HomeService" binding="basicHttpBinding" contract="WcfService.IHomeService">

           <identity>

             <dns value="localhost" />

           </identity>

         </endpoint>

         <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />

         <host>

           <baseAddresses>

             <add baseAddress="http://192.168.23.187:8733" />

           </baseAddresses>

         </host>

       </service>

     </services>

   </system.serviceModel>

 </configuration>
登入後複製

 

因為windows的兩台機器的ip位址是192.168.23.187,192.168.23.188 ,所以部署的時候要注意一下config中的baseAddress位址。

 

2. centos上的nginx搭建

    nginx我想大家用的還是比較多的,去官網下載最新的就好【nginx-1.13.6】 :http://nginx.org/en/download.html,下載之後,就是常規的三板斧安裝! ! !

[root@localhost nginx-1.13.6]# ./configure --prefix=/usr/myapp/nginx

[root@localhost nginx-1.13.6]# make && make install

#   

    然後在nginx的安裝目錄下方找到conf文件,修改裡面的nginx.conf 設定。

[root@localhost nginx]# cd conf
[root@localhost conf]# ls
fastcgi.conf            koi-utf        koi-win nginx.conf.default   uwsgi_params.defaultfastcgi_params          mime.types          scgi_params    localhost conf]# vim nginx.conf


    詳細配置如下,注意下面「標紅」的地方,權重按照1:5的方式進行調用,關於其他的配置,大家可以在網上搜一下就可以了。

#user  nobody; worker_processes 
 1; #error_log  logs/error.log;
  #error_log  logs/error.log  notice; 
  #error_log  logs/error.log  info; 
  #pid logs/nginx.pid; events {  
    worker_connections  1024; } 
    http {   
     include       mime.types;   
      default_type  application/octet-stream;  
        #log_format  main  &#39;$remote_addr - $remote_user [$time_local] "$request" &#39;    #                 
         &#39;$status $body_bytes_sent "$http_referer" &#39;    #                  &#39;
         "$http_user_agent" "$http_x_forwarded_for"&#39;;   
          #access_log  logs/access.log  
          main;    sendfile        
          on;    #tcp_nopush     
          on;    #keepalive_timeout  0;    keepalive_timeout  65;    #gzip  
          on;    
   upstream  cluster.com{        
    server 192.168.23.187:8733 weight=1;        
     server 192.168.23.188:8733 weight=5;           
  }    
  server {        
  listen       80;       
   server_name  localhost;        
   #charset koi8-r;       
    #access_log  logs/host.access.log  main;       
       location / {            
       root   html;            index  index.html index.htm;            
       proxy_pass http://cluster.com;            #设置主机头和客户端真实地址,以便服务器获取客户端真实IP           
         proxy_set_header X-Forwarded-Host $host;        
             proxy_set_header X-Forwarded-Server $host;           
              proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;           
               proxy_set_header  X-Real-IP  $remote_addr;        }       
                #error_page  404              /404.html;       
                 # redirect server error pages to the static page /50x.html     
                    #        error_page   500 502 503 504  /50x.html;
                            location = /50x.html {          
                              root   html;        }       
                  # proxy the PHP scripts to Apache listening on 127.0.0.1:80    
                  #        #location ~ \.php$ {   
					#    proxy_pass   http://127.0.0.1;    
				#}       
				# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000       
				#        #location ~ \.php$ {        #    root           html;  
				#    fastcgi_pass   127.0.0.1:9000;        #    fastcgi_index  index.php;
				#    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name; 
				#    include        fastcgi_params;       
				#}        # deny access to .htaccess files,
				if Apache&#39;s document root        # concurs
				with nginx&#39;s one       

 #        #location ~ /\.ht {    

#    deny  all;        #} 
 }    # another virtual host using mix of IP-, name-, and port-based configuration   
  #    #server {   
   #    listen       8000;   
    #    listen       somename:8080;    
    #    server_name  somename  alias  another.alias;    
    #    location / {    #        root   html;   
     #        index  index.html index.htm;    #    }    
     #}    # HTTPS server    #    #server {   
	       #    listen       443 ssl;   
	        #    server_name  localhost;   
	         #    ssl_certificate      cert.pem;   
	          #    ssl_certificate_key  cert.key;    
	          #    ssl_session_cache    shared:SSL:1m;   
	           #    ssl_session_timeout  5m;    
	           #    ssl_ciphers  HIGH:!aNULL:!MD5;   
	            #    ssl_prefer_server_ciphers  on;    
	            #    location / {    #        root   html;   
	             #        index  index.html index.htm;   
             #    }    #} }
登入後複製


 

 3. client端的程式建構

《1》 第一件事就是將 192.168.23.190 對應到本機的host中去,因為服務不提供給第三方使用,所以加host還是很輕鬆的。

192.168.23.190 cluster.com


 

《2》 然後就是client端程式加入服務引用,程式碼如下:


  class Program

    { 
         static void Main(string[] args) 
        { 
             for (int i = 0; i < 1000; i++) 
            { 
                 HomeServiceClient client = new HomeServiceClient(); 

                 var info = client.DoWork("hello world!");


               Console.WriteLine(info);
               

                System.Threading.Thread.Sleep(1000);14            }
           


           Console.Read();
           
        }
       
  }
登入後複製

 

最後來執行以下程序,看看1000次循環中,是不是按照權重1:5 的方式對後端的wcf進行調用的? ? ?



 

#看到沒有,是不是很屌,我只需要透過cluster.com進行服務訪問,nginx會自動給我複雜均衡,這就是我們開發中非常簡化的wcf複雜均衡。


以上是使用nginx搭建高可用,高並發的wcf集群的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.聊天命令以及如何使用它們
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

怎麼查看nginx是否啟動 怎麼查看nginx是否啟動 Apr 14, 2025 pm 01:03 PM

確認 Nginx 是否啟動的方法:1. 使用命令行:systemctl status nginx(Linux/Unix)、netstat -ano | findstr 80(Windows);2. 檢查端口 80 是否開放;3. 查看系統日誌中 Nginx 啟動消息;4. 使用第三方工具,如 Nagios、Zabbix、Icinga。

linux怎麼查看nginx是否啟動 linux怎麼查看nginx是否啟動 Apr 14, 2025 pm 12:48 PM

在 Linux 中,使用以下命令檢查 Nginx 是否已啟動:systemctl status nginx根據命令輸出進行判斷:如果顯示 "Active: active (running)",則 Nginx 已啟動。如果顯示 "Active: inactive (dead)",則 Nginx 已停止。

nginx在windows中怎麼配置 nginx在windows中怎麼配置 Apr 14, 2025 pm 12:57 PM

如何在 Windows 中配置 Nginx?安裝 Nginx 並創建虛擬主機配置。修改主配置文件並包含虛擬主機配置。啟動或重新加載 Nginx。測試配置並查看網站。選擇性啟用 SSL 並配置 SSL 證書。選擇性設置防火牆允許 80 和 443 端口流量。

linux怎麼啟動nginx linux怎麼啟動nginx Apr 14, 2025 pm 12:51 PM

在 Linux 中啟動 Nginx 的步驟:檢查 Nginx 是否已安裝。使用 systemctl start nginx 啟動 Nginx 服務。使用 systemctl enable nginx 啟用在系統啟動時自動啟動 Nginx。使用 systemctl status nginx 驗證啟動是否成功。在 Web 瀏覽器中訪問 http://localhost 查看默認歡迎頁面。

怎麼解決nginx跨域問題 怎麼解決nginx跨域問題 Apr 14, 2025 am 10:15 AM

解決 Nginx 跨域問題有兩種方法:修改跨域響應頭:添加指令以允許跨域請求,指定允許的方法和頭,以及設置緩存時間。使用 CORS 模塊:啟用模塊並配置 CORS 規則,允許跨域請求、方法、頭和設置緩存時間。

nginx怎麼查看運行狀態 nginx怎麼查看運行狀態 Apr 14, 2025 am 11:48 AM

查看 Nginx 運行狀態的方法有:使用 ps 命令查看進程狀態;查看 Nginx 配置文件 /etc/nginx/nginx.conf;使用 Nginx 狀態模塊啟用狀態端點;使用 Prometheus、Zabbix 或 Nagios 等監控工具。

怎麼啟動nginx服務器 怎麼啟動nginx服務器 Apr 14, 2025 pm 12:27 PM

啟動 Nginx 服務器需要按照不同操作系統採取不同的步驟:Linux/Unix 系統:安裝 Nginx 軟件包(例如使用 apt-get 或 yum)。使用 systemctl 啟動 Nginx 服務(例如 sudo systemctl start nginx)。 Windows 系統:下載並安裝 Windows 二進製文件。使用 nginx.exe 可執行文件啟動 Nginx(例如 nginx.exe -c conf\nginx.conf)。無論使用哪種操作系統,您都可以通過訪問服務器 IP

nginx304錯誤怎麼解決 nginx304錯誤怎麼解決 Apr 14, 2025 pm 12:45 PM

問題的答案:304 Not Modified 錯誤表示瀏覽器已緩存客戶端請求的最新資源版本。解決方案:1. 清除瀏覽器緩存;2. 禁用瀏覽器緩存;3. 配置 Nginx 允許客戶端緩存;4. 檢查文件權限;5. 檢查文件哈希;6. 禁用 CDN 或反向代理緩存;7. 重啟 Nginx。

See all articles