使用Nginx搭建高可用高并发的Wcf集群

服务器
很多情况下基于wcf的复杂均衡都首选zookeeper,这样可以拥有更好的控制粒度,但zk对 C# 不大友好,实现起来相对来说比较麻烦,实际情况下,如果你的负载机制粒度很粗糙的话,用nginx就可以搞定啦,既可以实现复杂均衡,又可以实现双机热备,以最小的代码量实现我们的业务。

[[348879]]

很多情况下基于wcf的复杂均衡都首选zookeeper,这样可以拥有更好的控制粒度,但zk对 C# 不大友好,实现起来相对来说比较麻烦,实际情况下,如果你的负载机制粒度很粗糙的话,用nginx就可以搞定啦,既可以实现复杂均衡,又可以实现双机热备,以最小的代码量实现我们的业务。

一:准备的材料

1. 话不多说,一图胜千言,图中的服务器都是采用vmware虚拟化,如下图:

  • 三台windows机器 ,两个WCF的windows服务器承载(192.168.23.187,192.168.23.188),一台Client的服务器(192.168.23.1)
  • 一台Centos机器,用来承载web复杂均衡nginx(192.168.23.190)。
  • 在所有的Client的Hosts文件中增加host映射:【192.168.23.190 cluster.com】,方便通过域名的形式访问nginx所在服务器的ip地址。

二:环境搭建

1. WCF程序

既然是测试,肯定就是简单的程序,代码就不完全给出了。

  • HomeService实现类代码如下(输出当前server的ip地址,方便查看):
  1. public class HomeService : IHomeService 
  2.     { 
  3.         public string DoWork(string msg) 
  4.         { 
  5.             var ip = Dns.GetHostAddresses(Dns.GetHostName()).FirstOrDefault(i => i.AddressFamily == 
  6.                                                                AddressFamily.InterNetwork).ToString(); 
  7.  
  8.             return string.Format("当前 request 由 server={0} 返回", ip); 
  9.         } 
  10.     } 
  • App.Config代码
  1. <?xml version="1.0" encoding="utf-8" ?> 
  2. <configuration> 
  3.   <startup> 
  4.     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" /> 
  5.   </startup> 
  6.   <system.serviceModel> 
  7.     <behaviors> 
  8.       <serviceBehaviors> 
  9.         <behavior name=""
  10.           <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" /> 
  11.           <serviceDebug includeExceptionDetailInFaults="false" /> 
  12.         </behavior> 
  13.       </serviceBehaviors> 
  14.     </behaviors> 
  15.     <services> 
  16.       <service name="WcfService.HomeService"
  17.         <endpoint address="/HomeService" binding="basicHttpBinding" contract="WcfService.IHomeService"
  18.           <identity> 
  19.             <dns value="localhost" /> 
  20.           </identity> 
  21.         </endpoint> 
  22.         <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> 
  23.         <host> 
  24.           <baseAddresses> 
  25.             <add baseAddress="http://192.168.23.187:8733" /> 
  26.           </baseAddresses> 
  27.         </host> 
  28.       </service> 
  29.     </services> 
  30.   </system.serviceModel> 
  31. </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,下载之后,就是常规的三板斧安装!!!

  1. [root@localhost nginx-1.13.6]# yum install -y gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel openssl openssl-devel 
  2. [root@localhost nginx-1.13.6]# ./configure --prefix=/usr/myapp/nginx 
  3. [root@localhost nginx-1.13.6]# make && make install 

然后在nginx的安装目录下面找到conf文件,修改里面的nginx.conf 配置。

  1. [root@localhost nginx]# cd conf 
  2. [root@localhost conf]# ls 
  3. fastcgi.conf            koi-utf             nginx.conf           uwsgi_params 
  4. fastcgi.conf.default    koi-win             nginx.conf.default   uwsgi_params.default 
  5. fastcgi_params          mime.types          scgi_params          win-utf 
  6. fastcgi_params.default  mime.types.default  scgi_params.default 
  7. [root@localhost conf]# vim nginx.conf 

详细配置如下,注意下面“标红”的地方,权重按照1:5的方式进行调用,关于其他的配置,大家可以在网上搜一下就可以了。

  1. #user  nobody; 
  2. worker_processes  1; 
  3.  
  4. #error_log  logs/error.log; 
  5. #error_log  logs/error.log  notice; 
  6. #error_log  logs/error.log  info; 
  7.  
  8. #pid        logs/nginx.pid; 
  9.  
  10.  
  11. events { 
  12.     worker_connections  1024; 
  13.  
  14.  
  15. http { 
  16.     include       mime.types; 
  17.     default_type  application/octet-stream; 
  18.  
  19.     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" ' 
  20.     #                  '$status $body_bytes_sent "$http_referer" ' 
  21.     #                  '"$http_user_agent" "$http_x_forwarded_for"'
  22.  
  23.     #access_log  logs/access.log  main; 
  24.  
  25.     sendfile        on
  26.     #tcp_nopush     on
  27.  
  28.     #keepalive_timeout  0; 
  29.     keepalive_timeout  65; 
  30.  
  31.     #gzip  on
  32.  
  33.     upstream  cluster.com{ 
  34.                  server 192.168.23.187:8733 weight=1; 
  35.                  server 192.168.23.188:8733 weight=5; 
  36.                  } 
  37.  
  38.     server { 
  39.         listen       80; 
  40.         server_name  localhost; 
  41.  
  42.         #charset koi8-r; 
  43.  
  44.         #access_log  logs/host.access.log  main; 
  45.  
  46.         location / { 
  47.             root   html; 
  48.             index  index.html index.htm; 
  49.             proxy_pass http://cluster.com;  
  50.             #设置主机头和客户端真实地址,以便服务器获取客户端真实IP 
  51.             proxy_set_header X-Forwarded-Host $host; 
  52.             proxy_set_header X-Forwarded-Server $host; 
  53.             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
  54.             proxy_set_header  X-Real-IP  $remote_addr;  
  55.         } 
  56.  
  57.         #error_page  404              /404.html; 
  58.  
  59.         # redirect server error pages to the static page /50x.html 
  60.         # 
  61.         error_page   500 502 503 504  /50x.html; 
  62.         location = /50x.html { 
  63.             root   html; 
  64.         } 
  65.  
  66.         # proxy the PHP scripts to Apache listening on 127.0.0.1:80 
  67.         # 
  68.         #location ~ \.php$ { 
  69.         #    proxy_pass   http://127.0.0.1; 
  70.         #} 
  71.  
  72.         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 
  73.         # 
  74.         #location ~ \.php$ { 
  75.         #    root           html; 
  76.         #    fastcgi_pass   127.0.0.1:9000; 
  77.         #    fastcgi_index  index.php; 
  78.         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name; 
  79.         #    include        fastcgi_params; 
  80.         #} 
  81.  
  82.         # deny access to .htaccess files, if Apache's document root 
  83.         # concurs with nginx's one 
  84.         # 
  85.         #location ~ /\.ht { 
  86.         #    deny  all
  87.         #} 
  88.     } 
  89.  
  90.  
  91.     # another virtual host using mix of IP-, name-, and port-based configuration 
  92.     # 
  93.     #server { 
  94.     #    listen       8000; 
  95.     #    listen       somename:8080; 
  96.     #    server_name  somename  alias  another.alias; 
  97.  
  98.     #    location / { 
  99.     #        root   html; 
  100.     #        index  index.html index.htm; 
  101.     #    } 
  102.     #} 
  103.  
  104.  
  105.     # HTTPS server 
  106.     # 
  107.     #server { 
  108.     #    listen       443 ssl; 
  109.     #    server_name  localhost; 
  110.  
  111.     #    ssl_certificate      cert.pem; 
  112.     #    ssl_certificate_key  cert.key
  113.  
  114.     #    ssl_session_cache    shared:SSL:1m; 
  115.     #    ssl_session_timeout  5m; 
  116.  
  117.     #    ssl_ciphers  HIGH:!aNULL:!MD5; 
  118.     #    ssl_prefer_server_ciphers  on
  119.  
  120.     #    location / { 
  121.     #        root   html; 
  122.     #        index  index.html index.htm; 
  123.     #    } 
  124.     #} 
  125.  

3. client端的程序搭建

  • 第一件事就是将 192.168.23.190 映射到本机的host中去,因为服务不提供给第三方使用,所以加host还是很轻松的。
  • 然后就是client端程序添加服务引用,因为添加了host映射,所以服务引用地址就是"http://cluster.com"。代码如下:

  1. class Program 
  2.     { 
  3.         static void Main(string[] args) 
  4.         { 
  5.             for (int i = 0; i < 1000; i++) 
  6.             { 
  7.                 HomeServiceClient client = new HomeServiceClient(); 
  8.  
  9.                 var info = client.DoWork("hello world!"); 
  10.  
  11.                 Console.WriteLine(info); 
  12.  
  13.                 System.Threading.Thread.Sleep(1000); 
  14.             } 
  15.  
  16.             Console.Read(); 
  17.         } 
  18.     } 

最后来执行以下程序,看看1000次循环中,是不是按照权重1:5 的方式对后端的wcf进行调用的???

看到没有,是不是很????,我只需要通过cluster.com进行服务访问,nginx会自动给我负载均衡,这就是我们开发中非常简单化的wcf复杂均衡,希望本篇对你有帮助~~~~

本文转载自微信公众号「一线码农聊技术」,可以通过以下二维码关注。转载本文请联系一线码农聊技术公众号。

 

责任编辑:武晓燕 来源: 一线码农聊技术
相关推荐

2017-11-13 11:07:32

Nginx搭建高可用

2019-10-09 16:02:16

NginxKeepalivedLvs

2023-11-13 09:03:10

2021-06-17 06:29:16

kube-vip Kubernetes开源项目

2014-10-09 10:04:23

CentOS集群

2019-07-02 08:38:45

NginxTomcatKeepalived

2021-09-09 07:45:25

kube-vip Kuberneteshostname

2014-08-08 13:30:44

Nginx

2013-01-30 10:12:24

NginxNginx优化高并发

2012-02-15 22:40:23

heartbeat高可用

2023-05-15 08:20:56

2023-11-07 07:30:18

Hadoop高可用

2019-12-24 14:28:00

KeepalivedNginxTomcat

2021-05-24 09:28:41

软件开发 技术

2020-10-28 11:20:18

RabbitMQHAProxy运维

2024-02-27 09:48:25

Redis集群数据库

2018-05-15 10:54:33

NginxRedisEhcache

2019-12-24 09:30:59

苏宁高可用高并发

2018-01-12 14:20:37

数据库MySQL高可用架构

2017-02-19 19:57:05

ZooKeeper集群
点赞
收藏

51CTO技术栈公众号