Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Nginx to build Wcf clusters with high availability and high concurrency

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article focuses on "how to use Nginx to build a high-availability and high-concurrency Wcf cluster". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use Nginx to build a high availability and high concurrency Wcf cluster.

In many cases, zookeeper is preferred for complex balancers based on wcf, so it can have better control granularity, but zk is not very friendly to C#, so it is relatively troublesome to implement. In fact, if the granularity of your load mechanism is very rough, you can use nginx to achieve not only complex balancers, but also dual-server hot backup to achieve our business with the minimum amount of code.

One: prepared materials

1. Needless to say, a picture is worth a thousand words. The servers in the figure are all virtualized by vmware, as shown in the following figure:

Three windows machines, two windows servers for WCF (192.168.23.187192.168.23.188), and one server for Client (192.168.23.1)

A Centos machine that hosts the web complex equalization nginx (192.168.23.190).

Add host mapping to all Client Hosts files: [192.168.23.190 cluster.com] to facilitate access to the ip address of the server where nginx resides in the form of a domain name.

Second, environmental construction

1. WCF program

Since it is a test, it must be a simple program, and the code is not fully given.

The HomeService implementation class code is as follows (output the ip address of the current server for easy viewing):

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

App.Config code

Because the ip addresses of the two machines on windows are 192.168.23.187192.168.23.188, pay attention to the baseAddress address in config when you deploy.

2. Nginx building on centos

Nginx I think you still use more, go to the official website to download the latest [nginx-1.13.6]: http://nginx.org/en/download.html, download, is the regular three axe installation!

[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

Then find the conf file under the nginx installation directory and modify the nginx.conf configuration inside.

[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

The detailed configuration is as follows, pay attention to the following "standard red" place, the weight is called according to 1:5, about other configurations, you can search on the Internet.

# 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 server 8733 weight=1; server 192.168.23.188 keepalive_timeout 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; # sets the host header and the real address of the client so that the server can obtain the real IP proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Server $host of the client 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 500502 503 504 / 50x.html Location = / 50x.html {root html;} # proxy the PHP scripts to Apache listening on 127.0.0.1 50x.html 80 # # location ~\ .php$ {# proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1 fastcgi_pass 9000 # # location ~\. Php$ {# root html; # fastcgi_pass 127.0.0.1 root html; 9000; # fastcgi_index index.php; # fastcgi_pass / 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 # ssl_prefer_server_ciphers on; # location / {# root html; # index index.html index.htm; #} #}}

3. Program construction on the client side

The first thing to do is to map 192.168.23.190 to the native host, because the service is not available to third parties, so adding host is easy.

Then the client side program adds the service reference, and because the host mapping is added, the service reference address is "http://cluster.com"." The code is as follows:

Class Program {static void Main (string [] args) {for (int I = 0; I < 1000; iTunes +) {HomeServiceClient client = new HomeServiceClient (); var info = client.DoWork ("hello world!"); Console.WriteLine (info); System.Threading.Thread.Sleep (1000) } Console.Read ();}}

Finally, let's execute the following procedure to see if the back-end wcf is called according to the weight 1:5 in the 1000 loops.

See? I just need to access the service through cluster.com, and nginx will automatically give me load balancing. this is the very simplified wcf complex balance in our development. I hope this article will be helpful to you.

At this point, I believe you have a deeper understanding of "how to use Nginx to build a high availability and high concurrency Wcf cluster". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report