Build a high-availability and high-concurrency wcf cluster using nginx, nginxwcf

Source: Internet
Author: User

Build a high-availability and high-concurrency wcf cluster using nginx, nginxwcf

In many cases, zookeeper is the preferred choice for Complex Balance Based on wcf, which can have better control granularity. However, zk is not very friendly to C # And is relatively difficult to implement. In actual situations, if

If your load mechanism is coarse-grained, nginx can be used first to achieve complex balancing and dual-machine hot backup, so as to implement our business with the minimum amount of code, I will share the following details.

 

I. prepared materials

 

1. Let's not talk much about it. The servers shown in Figure 1 are all virtualized by vmware, for example:

Three windows machines in 1, two windows servers in WCF (192.168.23.187, 192.168.23.188), and one Client server (192.168.23.1)

2 is a Centos machine used to host the web complex balanced nginx (192.168.23.190 ).

3. Add host ing to the Hosts file of all clients: [192.168.23.190 cluster.com] to access the IP address of the server where nginx is located through the domain name.

 

II. Environment Construction

1. WCF Program

Since it is a test, it must be a simple program, and the code is not completely provided.

 

The HomeService implementation Class Code of 1 is as follows (output the IP address of the current server for ease of viewing ):

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 ("current request returned by server = {0}", ip); 9} 10}

 

2 App. Config code

 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         

 

Because the IP addresses of the two windows machines are 192.168.23.187 and 192.168.23.188, pay attention to the baseAddress in config during deployment.

 

2. Build nginx on centos

Nginx I want to home or more, go to the official website to download the latest "nginx-1.13.6": http://nginx.org/en/download.html, download the following, the regular three Board ax installation !!!

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

Find the conf file under the nginx installation directory and modify the nginx. conf configuration.

[root@localhost nginx]# cd conf[root@localhost conf]# lsfastcgi.conf            koi-utf             nginx.conf           uwsgi_paramsfastcgi.conf.default    koi-win             nginx.conf.default   uwsgi_params.defaultfastcgi_params          mime.types          scgi_params          win-utffastcgi_params.default  mime.types.default  scgi_params.default[root@localhost conf]# vim nginx.conf

 

The detailed configuration is as follows. Pay attention to the following "marked red" and the weight is called at. For other configurations, you can search for them online.

# 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 $ response "$ 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 ; # Set the Host header and the real client address so that the Server can obtain the real client 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/50 x.html; location =/50x.html {root html;} # proxy the PHP scr Ipts 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. 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_se Ssion_cache shared: SSL: 1 m; # ssl_session_timeout 5 m; # ssl_ciphers HIGH :! ANULL :! MD5; # ssl_prefer_server_ciphers on; # location/{# root html; # index index.html index.htm ;#}#}}

 

3. Build a client program

The first thing in "1" is to map 192.168.23.190 to the host of the Local Machine. Because the service is not provided to third parties, it is easy to add the host.

192.168.23.190 cluster.com

 

2 is then the client program to add service reference, because the host ing is added, so the service reference address is "http://cluster.com ". The Code is as follows:

 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     }

 

Finally, run the following program to check whether the back-end wcf is called in the method with a weight of in the 1000 cycle ???

 

See no, Isn't it awesome? I only need to access services through cluster.com, nginx will automatically give me complex balancing, which is a very simple wcf complex balancing in our development.

 

I hope this article will help you ~~~~ Complete source code: cluster.zip

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.