很多时候基于WCF的复杂均衡首选zookeeper,可以有更好的控制粒度,但是zk对C#不是很友好,相对实施起来麻烦。实际上,如果你的负载机制非常粗粒度,你可以使用nginx来处理它。既可以实现复杂的平衡,又可以实现双机热备,以最小的代码量实现我们的业务。一:准备材料1.话不多说,一图抵千言,图中的服务器都是vmware虚拟出来的,如下图:三台windows机器,两台WCFwindows服务器(192.168.23.187,192.168.23.188),一台Client服务器(192.168.23.1)和一台Centos机器,用于托管web综合体balancenginx(192.168.23.190)。在所有Client的Hosts文件中添加主机映射:[192.168.23.190cluster.com],方便以域名的形式访问到nginx所在服务器的ip地址。二:环境搭建1.WCF程序既然是测试,肯定是简单的程序,代码就不完整给出了。HomeService实现类代码如下(输出当前服务器的ip地址,方便查看):.AddressFamily==AddressFamily.InterNetwork).ToString();returnstring.Format("当前请求返回server={0}",ip);}}App.Configcode<配置>因为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@localhostnginx-1.13.6]#yuminstall-ygccgcc-c++automakepcrepcre-develzlibzlib-developensslopenssl-devel[root@localhostnginx-1.13.6]#./configure--prefix=/usr/myapp/nginx[root@localhostnginx-1.13.6]#make&&makeinstall然后nginx安装目录下找到conf文件,修改里面的nginx.conf配置。[root@localhostnginx]#cdconf[root@localhostconf]#lsfastcgi.confkoi-utfnginx.confuwsgi_paramsfastcgi.conf.defaultkoi-winnginx.conf.defaultuwsgi_params.defaultfastcgi_paramsmime.typesscgi_paramswin-utffastcgi_params.defaultmime.types.defaultscgi_paramshost.default]#root@localhost.confvimnginx.conf的详细配置如下,注意下面“红色”的地方,权重按照1:5的方法调用,其他配置可以上网搜索。#usernobody;worker_processes1;#error_loglogs/error.log;#error_loglogs/error.lognotice;#error_loglogs/error.loginfo;#pidlogs/nginx.pid;events{worker_connections1024;}http{includemime.types;default_typeapplication/octet-stream;#log_formatmain'$remote_addr-$remote_user[$time_local]"$request"'#'$status$body_bytes_sent"$http_referer"'#'"$http_user_agent""$http_x_forwarded_for"';#access_loglogs/access.logmain;sendfileon;#tcp_nopushon;#keepalive_timeout0;keepalive_timeout65;#gzipon;upstreamcluster.com{server192.168.23.187:8733weight=1;server192.168.23.188:8733weight=5;}服务器{listen80;server_namelocalhost;#charsetkoi8-r;#access_loglogs/host.access.logmain;location/{roothtml;indexindex.htmlindex.htm;proxy_passhttp://cluster.com;#设置主机头和客户端真实地址,以方便服务器获取客户端真实IPproxy_set_headerX-Forwarded-Host$host;proxy_set_headerXForwarded-Server$host;proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;proxy_set_headerX-真实IP$remote_addr;}#error_page404/404.html;#redirectservererrorpagestothestaticpage/50x.html#error_page500502503504/50x.html;location=/50x.html{roothtml;}#proxythePHPscriptstoApachelisteningon127.0.0.1:80##location~\.php${#proxy_passhttp://127.0.0.1;#}#passthePHPscriptstoFastCGIserverlisteningon127.0.0.1:9000##location~\.php${#roothtml;#fastcgi_pass127.0.0.1:9000;#fastcgi_indexindex.php;#fastcgi_paramSCRIPT_FILENAME/scripts$fastcgi_script_name;#includefastcgi_params;#}#denyaccessto.htaccessfiles,ifApache'sdocumentroot#concurswithnginx'sone##location~/\.ht{#denyall;#}}#anothervirtualhostusingmixofIP-,name-,andport-basedconfiguration##server{#listen8000;#listensomename:8080;#server_namesomenamealiasanother.alias;#location/{#roothtml;#indexindex.htmlindex.htm;#}#}#HTTPSserver##server{#listen443ssl;#server_namelocalhost;#ssl_certificatecert.pem;#ssl_certificate_keycert。键;#ssl_session_cacheshared:SSL:1m;#ssl_session_timeout5m;#ssl_ciphersHIGH:!aNULL:!MD5;#ssl_prefer_server_cipherson;#location/{#roothtml;#indexindex.htmlindex.htm;#}#}}3.客户端首先要做的是将192.168.23.190映射到本地主机,因为服务不是它是提供给第三方使用的,所以添加主机还是很容易的。然后客户端程序添加一个服务引用。因为添加了主机映射,服务引用地址为“http://cluster.com”。代码如下:classProgram{staticvoidMain(string[]args){for(inti=0;i<1000;i++){HomeServiceClientclient=newHomeServiceClient();variinfo=client.DoWork("helloworld!");Console.WriteLine(info);System.Threading.Thread.Sleep(1000);}Console.Read();}}最后执行下面的程序,看后端wcf是否被调用,权重为1:51000次循环???看到没有,是不是很????,我只需要通过cluster.com访问服务,nginx会自动帮我负载均衡。这就是我们开发中非常简单的wcf复杂平衡。希望本文对您有所帮助~~~~本文转载自微信公众号《线线码农聊技术》,可通过以下二维码关注。转载本文,请联系一线码农,聊聊技术公众号。