0

0

使用nginx搭建高可用,高并发的wcf集群

PHP中文网

PHP中文网

发布时间:2017-10-23 10:48:04

|

3054人浏览过

|

来源于php中文网

原创

很多情况下基于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》 一台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代码

  
  
    
      
   
    
      
        
         

           

           
         

       

     

     

       

         

           

             

           

         

         

         

           

             

           

         

       

     

   

 

 

因为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,下载之后,就是常规的三板斧安装!!!

开源电子商务系统(网店) iWebShop
开源电子商务系统(网店) iWebShop

iWebShop基于iWebSI框架开发,在获得iWebSI技术平台库支持的条件下,iWebShop可以轻松满足用户量级百万至千万级的大型电子商务网站的性能要求。站点的集群与分布式技术(分布式计算与存储/高可用性/负载均衡)被屏蔽在SI 平台之内,基于iWebShop并且按照SI平台库扩展规范开发的新增功能模块,也将同时获得这种超级计算与处理的能力。作为开源的LAMP电子商务系统,iWebShop

下载

[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.defaultfastcgi_params          mime.types          scgi_params          win-utf
fastcgi_params.default  mime.types.default  scgi_params.default[root@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  '$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;            #设置主机头和客户端真实地址,以便服务器获取客户端真实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端的程序搭建

《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复杂均衡。

相关专题

更多
java学习网站推荐汇总
java学习网站推荐汇总

本专题整合了java学习网站相关内容,阅读专题下面的文章了解更多详细内容。

6

2026.01.08

java学习网站汇总
java学习网站汇总

本专题整合了java学习网站相关内容,阅读专题下面的文章了解更多详细内容。

0

2026.01.08

正则表达式 删除
正则表达式 删除

本专题整合了正则表达式删除教程大全,阅读专题下面的文章了解更多详细教程。

20

2026.01.08

java 元空间 永久代
java 元空间 永久代

本专题整合了java中元空间和永久代的区别,阅读专题下面的文章了解更多详细内容。

3

2026.01.08

java 永久代和元空间
java 永久代和元空间

本专题整合了java中元空间和永久代的区别,阅读专题下面的文章了解更多详细内容。

0

2026.01.08

java成品网站源码资源大全
java成品网站源码资源大全

本专题整合了java成品网站源码相关内容,阅读专题下面的文章了解更多详细内容。

9

2026.01.08

java过滤器教程大全
java过滤器教程大全

本专题整合了java过滤器相关教程,阅读专题下面的文章了解更多详细内容。

4

2026.01.08

作业帮网页版入口地址大全
作业帮网页版入口地址大全

本专题整合了作业帮网页版地址整理,阅读专题下面的文章了解更多详细内容。

4

2026.01.08

学习通网页版入口地址大全
学习通网页版入口地址大全

本专题整合了学生通网页版入口相关整理,阅读专题下面的文章了解更多详细内容。

19

2026.01.08

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
进程与SOCKET
进程与SOCKET

共6课时 | 0.3万人学习

nginx浅谈
nginx浅谈

共15课时 | 0.8万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号