如何使用Nginx搭建高可用高并發(fā)的Wcf集群

本篇內(nèi)容主要講解“如何使用Nginx搭建高可用高并發(fā)的Wcf集群”,感興趣的朋友不妨來看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“如何使用Nginx搭建高可用高并發(fā)的Wcf集群”吧!

成都創(chuàng)新互聯(lián)公司服務(wù)項(xiàng)目包括皮山網(wǎng)站建設(shè)、皮山網(wǎng)站制作、皮山網(wǎng)頁制作以及皮山網(wǎng)絡(luò)營(yíng)銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢(shì)、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,皮山網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到皮山省份的部分城市,未來相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!

很多情況下基于wcf的復(fù)雜均衡都首選zookeeper,這樣可以擁有更好的控制粒度,但zk對(duì) C#  不大友好,實(shí)現(xiàn)起來相對(duì)來說比較麻煩,實(shí)際情況下,如果你的負(fù)載機(jī)制粒度很粗糙的話,用nginx就可以搞定啦,既可以實(shí)現(xiàn)復(fù)雜均衡,又可以實(shí)現(xiàn)雙機(jī)熱備,以最小的代碼量實(shí)現(xiàn)我們的業(yè)務(wù)。

一:準(zhǔn)備的材料

1. 話不多說,一圖勝千言,圖中的服務(wù)器都是采用vmware虛擬化,如下圖:

如何使用Nginx搭建高可用高并發(fā)的Wcf集群

  • 三臺(tái)windows機(jī)器  ,兩個(gè)WCF的windows服務(wù)器承載(192.168.23.187,192.168.23.188),一臺(tái)Client的服務(wù)器(192.168.23.1)

  • 一臺(tái)Centos機(jī)器,用來承載web復(fù)雜均衡nginx(192.168.23.190)。

  • 在所有的Client的Hosts文件中增加host映射:【192.168.23.190  cluster.com】,方便通過域名的形式訪問nginx所在服務(wù)器的ip地址。

二:環(huán)境搭建

1. WCF程序

既然是測(cè)試,肯定就是簡(jiǎn)單的程序,代碼就不完全給出了。

  • HomeService實(shí)現(xiàn)類代碼如下(輸出當(dāng)前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("當(dāng)前 request 由 server={0} 返回", ip);         }     }
  • 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>

因?yàn)閣indows的兩臺(tái)機(jī)器的ip地址是192.168.23.187,192.168.23.188,所以部署的時(shí)候注意一下config中的baseAddress地址。

2. centos上的nginx搭建

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

[root@localhost nginx-1.13.6]# yum install -y gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel openssl openssl-devel [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             nginx.conf           uwsgi_params fastcgi.conf.default    koi-win             nginx.conf.default   uwsgi_params.default fastcgi_params          mime.types          scgi_params          win-utf fastcgi_params.default  mime.types.default  scgi_params.default [root@localhost conf]# vim nginx.conf

詳細(xì)配置如下,注意下面“標(biāo)紅”的地方,權(quán)重按照1:5的方式進(jìn)行調(diào)用,關(guān)于其他的配置,大家可以在網(wǎng)上搜一下就可以了。

#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  '$remote_addr - $remote_user [$time_local] "$request" '     #                  '$status $body_bytes_sent "$http_referer" '     #                  '"$http_user_agent" "$http_x_forwarded_for"';      #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;              #設(shè)置主機(jī)頭和客戶端真實(shí)地址,以便服務(wù)器獲取客戶端真實(shí)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's document root         # concurs with nginx'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端的程序搭建

  • 第一件事就是將 192.168.23.190 映射到本機(jī)的host中去,因?yàn)榉?wù)不提供給第三方使用,所以加host還是很輕松的。

  • 如何使用Nginx搭建高可用高并發(fā)的Wcf集群

  • 然后就是client端程序添加服務(wù)引用,因?yàn)樘砑恿薶ost映射,所以服務(wù)引用地址就是"http://cluster.com"。代碼如下:

如何使用Nginx搭建高可用高并發(fā)的Wcf集群

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);             }              Console.Read();         }     }

最后來執(zhí)行以下程序,看看1000次循環(huán)中,是不是按照權(quán)重1:5 的方式對(duì)后端的wcf進(jìn)行調(diào)用的???

如何使用Nginx搭建高可用高并發(fā)的Wcf集群

如何使用Nginx搭建高可用高并發(fā)的Wcf集群

看到?jīng)]有,是不是很????,我只需要通過cluster.com進(jìn)行服務(wù)訪問,nginx會(huì)自動(dòng)給我負(fù)載均衡,這就是我們開發(fā)中非常簡(jiǎn)單化的wcf復(fù)雜均衡,希望本篇對(duì)你有幫助~~~~

到此,相信大家對(duì)“如何使用Nginx搭建高可用高并發(fā)的Wcf集群”有了更深的了解,不妨來實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

網(wǎng)頁題目:如何使用Nginx搭建高可用高并發(fā)的Wcf集群
瀏覽路徑:http://muchs.cn/article2/gphdic.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開發(fā)、微信小程序、全網(wǎng)營(yíng)銷推廣、定制開發(fā)、企業(yè)建站服務(wù)器托管

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化