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

by admin on 2019年4月11日

    

    

       
很多情形下基于wcf的扑朔迷离均衡都首要选用zookeeper,那样能够享有更好的主宰粒度,但zk对C#
相当的小和谐,落成起来相对来说相比麻烦,实况下,借使

       
很多情况下基于wcf的繁杂均衡都首要选用zookeeper,那样能够具有更加好的主宰粒度,但zk对C#
非常的小和谐,达成起来相对来说比较麻烦,真实境况下,即便

你的载荷机制粒度极粗劣的话,优先选择nginx就能够化解,既能够达成复杂均衡,又足以兑现双机热备,以细小的代码量完结大家的作业,上边具体享受下。

你的载重型机器制粒度相当粗劣的话,优先利用nginx就能够消除,既可以完结复杂均衡,又能够兑现双机热备,以细小的代码量完毕大家的业务,下边具体享受下。

 

 

一:准备的材质

一:准备的资料

 

 

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

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

     图片 1

     图片 2

《一》 三台windows机器
,多少个WCF的windows服务器承载(1玖二.168.二三.1八七,1九贰.16捌.2三.18捌),1台Client的服务器(1九二.16八.2三.1)

《一》 三台windows机器
,七个WCF的windows服务器承载(1九二.16捌.二叁.1捌7,1玖二.168.2三.188),壹台Client的服务器(1九二.168.2三.一)

《二》 一台Centos机器,用来承载web复杂均衡nginx(1玖贰.168.②叁.190)。

《二》 1台Centos机器,用来承载web复杂均衡nginx(1九2.16八.二3.190)。

《三》在具有的Client的Hosts文件中扩充host映射:【1九贰.168.2叁.190
cluster.com】,方便通过域名的花样拜访nginx所在服务器的ip地址。

《3》在颇具的Client的Hosts文件中扩展host映射:【1玖二.16八.二三.190
cluster.com】,方便通过域名的款式拜访nginx所在服务器的ip地址。

 

 

贰:环境搭建

二:环境搭建

  1. WCF程序
  1. WCF程序

    既然是测试,肯定就是简不难单的主次,代码就不完全交给了。

    既然是测试,肯定正是回顾的顺序,代码就不完全交给了。

 

 

《一》
Home瑟维斯达成类代码如下(输出当前server的ip地址,方便查看):

《1》
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     }
 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     }

 

 

《2》 App.Config代码

《2》 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>
 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地址是1玖二.16八.23.1八七,1九2.16八.2叁.18八,所以安插的时候注意一下config中的baseAddress地址。

因为windows的两台机器的ip地址是1玖二.168.二叁.1八七,1九二.16八.二三.18八,所以布置的时候注意一下config中的baseAddress地址。

 

 

  1. centos上的nginx搭建
  1. centos上的nginx搭建

   
nginx作者想我们用的照旧比较多的,去官网下载最新的就好【nginx-1.13.6】:http://nginx.org/en/download.html,下载之后,正是平日的3板斧安装!!!

   
nginx小编想大家用的如故相比多的,去官网下载最新的就好【nginx-1.13.6】:http://nginx.org/en/download.html,下载之后,即是正规的三板斧安装!!!

[root@localhost nginx-1.13.6]# ./configure --prefix=/usr/myapp/nginx
[root@localhost nginx-1.13.6]# make && make install
[root@localhost nginx-1.13.6]# ./configure --prefix=/usr/myapp/nginx
[root@localhost nginx-1.13.6]# make && make install

   

   

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

    然后在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
[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的艺术进行调用,关于任何的布局,我们能够在网上搜一下就能够了。

   
详细铺排如下,注意上边“标红”的地点,权重遵照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;
    #    }
    #}

}
#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;
    #    }
    #}

}

 

 

 三. client端的次序搭建

 叁. client端的次第搭建

《一》 第贰件事正是将 1九二.16八.贰3.190
映射到本机的host中去,因为劳动不提须求第一方使用,所以加host照旧很自在的。

《一》 第一件事正是将 192.16八.二三.190
映射到本机的host中去,因为劳动不提须要第三方应用,所以加host依然很自在的。

192.168.23.190 cluster.com
192.168.23.190 cluster.com

图片 3

图片 4

 

 

《二》
然后正是client端程序添加服务引用,因为添加了host映射,所以服务引用地址就是”http://cluster.com"。
代码如下:

《二》
然后正是client端程序添加服务引用,因为添加了host映射,所以服务引用地址正是”http://cluster.com"。
代码如下:

图片 5

图片 6

 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     }
 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     }

 

 

最终来进行以下顺序,看看一千次巡回中,是或不是依照权重①:5的措施对后端的wcf实行调用的???

末尾来推行以下顺序,看看一千次巡回中,是否依照权重一:伍的办法对后端的wcf实行调用的???

图片 7

图片 8

图片 9

图片 10

 

 

总的来看未有,是否很牛逼,小编只须要经过cluster.com进行服务走访,nginx会自动给本身复杂均衡,那便是大家付出中杰出简单化的wcf复杂均衡。

见到未有,是还是不是很牛逼,笔者只须要通过cluster.com举办劳动走访,nginx会自动给本人复杂均衡,那正是我们开发中十一分不难化的wcf复杂均衡。

 

 

梦想本篇对您有帮带~~~~
  附完整源代码:cluster.zip   
 

意在本篇对你有接济~~~~
  附完整源代码:cluster.zip   
 

 

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图