很多情况下基于wcf的复杂均衡都首选zookeeper,这样可以拥有更好的控制粒度,但zk对 C# 不大友好,实现起来相对来说比较麻烦,实际情况下,如果你的负载机制粒度很粗糙的话,用nginx就可以搞定啦,既可以实现复杂均衡,又可以实现双机热备,以最小的代码量实现我们的业务。
一:准备的材料
1. 话不多说,一图胜千言,图中的服务器都是采用vmware虚拟化,如下图:
二:环境搭建
1. WCF程序
既然是测试,肯定就是简单的程序,代码就不完全给出了。
- 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);
- }
- }
- <?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]# 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
详细配置如下,注意下面“标红”的地方,权重按照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端的程序搭建
- 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();
- }
- }
最后来执行以下程序,看看1000次循环中,是不是按照权重1:5 的方式对后端的wcf进行调用的???
看到没有,是不是很????,我只需要通过cluster.com进行服务访问,nginx会自动给我负载均衡,这就是我们开发中非常简单化的wcf复杂均衡,希望本篇对你有帮助~~~~
本文转载自微信公众号「一线码农聊技术」,可以通过以下二维码关注。转载本文请联系一线码农聊技术公众号。
在VMworld 2020,VMware宣布与NVIDIA进行全面合作,共同推出新一代的混合云架构...
根据TrendForce的最新调查,自2020年初以来,COVID-19流感大流行加速了世界各地...
在疫情的影响下,人们的工作和生活方式在过去的一年发生了前所未有的变化。为了...
人头马君度(Rmy Cointreau)的历史非常重要,这家酒业公司以将最好的酒陈化100年...
本文中的五个步骤有助于您掌握转型的总体需求,并有助于您处理一些真正重要的事...
根据调查,随着用户对计算能力、存储和网络容量的需求增长,服务器需求比经济不...
时间真快呀!转眼又至周一。让我们卯足干劲继续前行,先来看看上周有哪些不容错...
2020年11月26日深圳潮数科技于石家庄成功召开数据安全 新时代新基建信息应用之基...
【51CTO.com快译】数字化转型使应用程序领导人必须找到有效的方法来更新改造遗留...
2020年以来,由疫情停工减产所导致的缺芯困局影响着全球汽车发展,而本以为2021...